Points moved to an IIFE object, added game exit option
This commit is contained in:
		
							parent
							
								
									8781da040e
								
							
						
					
					
						commit
						99a5968fbd
					
				
							
								
								
									
										66
									
								
								index.js
								
								
								
								
							
							
						
						
									
										66
									
								
								index.js
								
								
								
								
							| 
						 | 
					@ -9,52 +9,76 @@ function playRound(playerChoice, computerChoice) {
 | 
				
			||||||
    let computerChoiceCapital = computerChoice.slice(0,1).toUpperCase() + computerChoice.slice(1,computerChoice.length)
 | 
					    let computerChoiceCapital = computerChoice.slice(0,1).toUpperCase() + computerChoice.slice(1,computerChoice.length)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (playerChoice == computerChoice) {
 | 
					    if (playerChoice == computerChoice) {
 | 
				
			||||||
        return {string: `It's a tie! ${playerChoiceCapital} vs ${computerChoiceCapital}.`,
 | 
					        points.playerAddPoints();
 | 
				
			||||||
                points: {player: true, computer: true}};
 | 
					        points.computerAddPoints();
 | 
				
			||||||
 | 
					        return `It's a tie! ${playerChoiceCapital} vs ${computerChoiceCapital}.`;
 | 
				
			||||||
    } else if ((playerChoice == "rock" && computerChoice == "paper")
 | 
					    } else if ((playerChoice == "rock" && computerChoice == "paper")
 | 
				
			||||||
            || (playerChoice == "paper" && computerChoice == "scissors")
 | 
					            || (playerChoice == "paper" && computerChoice == "scissors")
 | 
				
			||||||
            || (playerChoice == "scissors" && computerChoice == "rock")) {
 | 
					            || (playerChoice == "scissors" && computerChoice == "rock")) {
 | 
				
			||||||
        return {string: `You lost! ${computerChoiceCapital} beats ${playerChoiceCapital}.`,
 | 
					        points.computerAddPoints();
 | 
				
			||||||
                points: {player: false, computer: true}};
 | 
					        return `You lost! ${computerChoiceCapital} beats ${playerChoiceCapital}.`;
 | 
				
			||||||
    } else if ((playerChoice == "rock" && computerChoice == "scissors")
 | 
					    } else if ((playerChoice == "rock" && computerChoice == "scissors")
 | 
				
			||||||
            || (playerChoice == "paper" && computerChoice == "rock")
 | 
					            || (playerChoice == "paper" && computerChoice == "rock")
 | 
				
			||||||
            || (playerChoice == "scissors" && computerChoice == "paper")) {
 | 
					            || (playerChoice == "scissors" && computerChoice == "paper")) {
 | 
				
			||||||
        return {string: `You won! ${playerChoiceCapital} beats ${computerChoiceCapital}.`,
 | 
					        points.playerAddPoints();
 | 
				
			||||||
                points: {player: true, computer: false}};
 | 
					        return `You won! ${playerChoiceCapital} beats ${computerChoiceCapital}.`;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function game() {
 | 
					const points = (() => {
 | 
				
			||||||
    let points = {
 | 
					    this.player = 0;
 | 
				
			||||||
        player: 0,
 | 
					    this.computer = 0;
 | 
				
			||||||
        computer: 0,
 | 
					    function playerAddPoints() {
 | 
				
			||||||
 | 
					        player++;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    function computerAddPoints() {
 | 
				
			||||||
 | 
					        computer++;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    function reset() {
 | 
				
			||||||
 | 
					        player = 0;
 | 
				
			||||||
 | 
					        computer = 0
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return {get player() {return player},
 | 
				
			||||||
 | 
					            get computer() {return computer},
 | 
				
			||||||
 | 
					            playerAddPoints, computerAddPoints, reset};
 | 
				
			||||||
 | 
					})()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function game() {
 | 
				
			||||||
 | 
					    points.reset();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (let i = 0; i < 5; i++) {
 | 
					    for (let i = 0; i < 5; i++) {
 | 
				
			||||||
        let correctOption = false
 | 
					        let correctOption = false
 | 
				
			||||||
        let playerChoice;
 | 
					        let playerChoice;
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        while (correctOption == false) {
 | 
					        while (correctOption == false) {
 | 
				
			||||||
            playerChoice = prompt("What do you choose, 'rock', 'paper' or 'scissors'?").toLowerCase().trim();
 | 
					            playerChoice = prompt("What do you choose, 'rock', 'paper' or 'scissors'?");
 | 
				
			||||||
        
 | 
					            if (playerChoice) {
 | 
				
			||||||
                choices.forEach(element => {
 | 
					                choices.forEach(element => {
 | 
				
			||||||
                if (playerChoice == element) {
 | 
					                    if (playerChoice.toLowerCase().trim() == element) {
 | 
				
			||||||
                        correctOption = true
 | 
					                        correctOption = true
 | 
				
			||||||
                        return;
 | 
					                        return;
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                })
 | 
					                })
 | 
				
			||||||
 | 
					            } else if (confirm(`Do you wanna quit the game?`)) {
 | 
				
			||||||
 | 
					                throw new Error("Exited the game on user's request");
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        let round = playRound(playerChoice, getComputerChoice());
 | 
					        let round = playRound(playerChoice, getComputerChoice());
 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (round.points.player) {
 | 
					 | 
				
			||||||
            points.player++;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        if (round.points.computer) {
 | 
					 | 
				
			||||||
            points.computer++;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        let pointsString = `Points: ${points.player}:${points.computer}\n`;
 | 
					        let pointsString = `Points: ${points.player}:${points.computer}\n`;
 | 
				
			||||||
        alert(pointsString + round.string);
 | 
					        alert(pointsString + round);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let confirmString;
 | 
				
			||||||
 | 
					    if (points.player > points.computer) {
 | 
				
			||||||
 | 
					        confirmString = `You won ${points.player}:${points.computer}`
 | 
				
			||||||
 | 
					    } else if (points.player == points.computer) {
 | 
				
			||||||
 | 
					        confirmString = `That was a ${points.player}:${points.computer} tie`
 | 
				
			||||||
 | 
					    } else if (points.player < points.computer) {
 | 
				
			||||||
 | 
					        confirmString = `You lost ${points.player}:${points.computer}`
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (confirm(`${confirmString}, do you wanna restart the game?`)) {
 | 
				
			||||||
 | 
					        game();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue