Points moved to an IIFE object, added game exit option

This commit is contained in:
NetMan 2024-01-03 21:48:17 +01:00
parent 8781da040e
commit 99a5968fbd
1 changed files with 50 additions and 26 deletions

View File

@ -9,52 +9,76 @@ function playRound(playerChoice, computerChoice) {
let computerChoiceCapital = computerChoice.slice(0,1).toUpperCase() + computerChoice.slice(1,computerChoice.length)
if (playerChoice == computerChoice) {
return {string: `It's a tie! ${playerChoiceCapital} vs ${computerChoiceCapital}.`,
points: {player: true, computer: true}};
points.playerAddPoints();
points.computerAddPoints();
return `It's a tie! ${playerChoiceCapital} vs ${computerChoiceCapital}.`;
} else if ((playerChoice == "rock" && computerChoice == "paper")
|| (playerChoice == "paper" && computerChoice == "scissors")
|| (playerChoice == "scissors" && computerChoice == "rock")) {
return {string: `You lost! ${computerChoiceCapital} beats ${playerChoiceCapital}.`,
points: {player: false, computer: true}};
points.computerAddPoints();
return `You lost! ${computerChoiceCapital} beats ${playerChoiceCapital}.`;
} else if ((playerChoice == "rock" && computerChoice == "scissors")
|| (playerChoice == "paper" && computerChoice == "rock")
|| (playerChoice == "scissors" && computerChoice == "paper")) {
return {string: `You won! ${playerChoiceCapital} beats ${computerChoiceCapital}.`,
points: {player: true, computer: false}};
points.playerAddPoints();
return `You won! ${playerChoiceCapital} beats ${computerChoiceCapital}.`;
}
}
function game() {
let points = {
player: 0,
computer: 0,
const points = (() => {
this.player = 0;
this.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++) {
let correctOption = false
let playerChoice;
while (correctOption == false) {
playerChoice = prompt("What do you choose, 'rock', 'paper' or 'scissors'?").toLowerCase().trim();
choices.forEach(element => {
if (playerChoice == element) {
correctOption = true
return;
}
})
playerChoice = prompt("What do you choose, 'rock', 'paper' or 'scissors'?");
if (playerChoice) {
choices.forEach(element => {
if (playerChoice.toLowerCase().trim() == element) {
correctOption = true
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());
if (round.points.player) {
points.player++;
}
if (round.points.computer) {
points.computer++;
}
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();
}
}