2024-01-03 18:36:00 +00:00
|
|
|
const choices = ["rock", "paper", "scissors"];
|
|
|
|
|
|
|
|
function getComputerChoice() {
|
|
|
|
return choices[Math.floor(Math.random() * 3)];
|
|
|
|
}
|
|
|
|
|
2024-01-03 18:56:13 +00:00
|
|
|
function playRound(playerChoice, computerChoice) {
|
|
|
|
let playerChoiceCapital = playerChoice.slice(0,1).toUpperCase() + playerChoice.slice(1,playerChoice.length)
|
|
|
|
let computerChoiceCapital = computerChoice.slice(0,1).toUpperCase() + computerChoice.slice(1,computerChoice.length)
|
|
|
|
|
|
|
|
if (playerChoice == computerChoice) {
|
2024-01-03 20:48:17 +00:00
|
|
|
points.playerAddPoints();
|
|
|
|
points.computerAddPoints();
|
|
|
|
return `It's a tie! ${playerChoiceCapital} vs ${computerChoiceCapital}.`;
|
2024-01-03 18:56:13 +00:00
|
|
|
} else if ((playerChoice == "rock" && computerChoice == "paper")
|
|
|
|
|| (playerChoice == "paper" && computerChoice == "scissors")
|
|
|
|
|| (playerChoice == "scissors" && computerChoice == "rock")) {
|
2024-01-03 20:48:17 +00:00
|
|
|
points.computerAddPoints();
|
|
|
|
return `You lost! ${computerChoiceCapital} beats ${playerChoiceCapital}.`;
|
2024-01-03 18:56:13 +00:00
|
|
|
} else if ((playerChoice == "rock" && computerChoice == "scissors")
|
|
|
|
|| (playerChoice == "paper" && computerChoice == "rock")
|
|
|
|
|| (playerChoice == "scissors" && computerChoice == "paper")) {
|
2024-01-03 20:48:17 +00:00
|
|
|
points.playerAddPoints();
|
|
|
|
return `You won! ${playerChoiceCapital} beats ${computerChoiceCapital}.`;
|
2024-01-03 18:56:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-03 20:48:17 +00:00
|
|
|
const points = (() => {
|
|
|
|
this.player = 0;
|
|
|
|
this.computer = 0;
|
|
|
|
function playerAddPoints() {
|
|
|
|
player++;
|
|
|
|
}
|
|
|
|
function computerAddPoints() {
|
|
|
|
computer++;
|
|
|
|
}
|
|
|
|
function reset() {
|
|
|
|
player = 0;
|
|
|
|
computer = 0
|
2024-01-03 19:06:15 +00:00
|
|
|
}
|
2024-01-03 20:48:17 +00:00
|
|
|
return {get player() {return player},
|
|
|
|
get computer() {return computer},
|
|
|
|
playerAddPoints, computerAddPoints, reset};
|
|
|
|
})()
|
|
|
|
|
|
|
|
function game() {
|
|
|
|
points.reset();
|
2024-01-03 18:56:13 +00:00
|
|
|
|
2024-01-03 19:06:15 +00:00
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
|
let correctOption = false
|
|
|
|
let playerChoice;
|
|
|
|
|
|
|
|
while (correctOption == false) {
|
2024-01-03 20:48:17 +00:00
|
|
|
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");
|
|
|
|
}
|
2024-01-03 19:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let round = playRound(playerChoice, getComputerChoice());
|
|
|
|
let pointsString = `Points: ${points.player}:${points.computer}\n`;
|
2024-01-03 20:48:17 +00:00
|
|
|
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();
|
2024-01-03 19:06:15 +00:00
|
|
|
}
|
2024-01-03 18:56:13 +00:00
|
|
|
}
|
|
|
|
|
2024-01-03 19:06:15 +00:00
|
|
|
game();
|