2024-01-09 17:53:33 +00:00
|
|
|
'use strict';
|
|
|
|
|
2024-01-03 18:36:00 +00:00
|
|
|
const choices = ["rock", "paper", "scissors"];
|
|
|
|
|
|
|
|
function getComputerChoice() {
|
|
|
|
return choices[Math.floor(Math.random() * 3)];
|
|
|
|
}
|
|
|
|
|
2024-01-09 18:13:16 +00:00
|
|
|
function playRoundResult(playerChoice, computerChoice) {
|
2024-01-03 18:56:13 +00:00
|
|
|
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-09 18:13:16 +00:00
|
|
|
points.playerAddPoints();
|
|
|
|
points.computerAddPoints();
|
2024-01-03 20:48:17 +00:00
|
|
|
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;
|
2024-01-09 18:13:16 +00:00
|
|
|
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};
|
|
|
|
})()
|
|
|
|
|
2024-01-09 17:53:33 +00:00
|
|
|
const buttons = document.querySelectorAll("button.choose");
|
|
|
|
const resultDiv = document.querySelector("div#result");
|
2024-01-03 18:56:13 +00:00
|
|
|
|
2024-01-09 18:13:16 +00:00
|
|
|
function playGameRound(value) {
|
|
|
|
let result = playRoundResult(value, getComputerChoice());
|
|
|
|
resultDiv.textContent = `Result: ${result}`;
|
|
|
|
let point = [];
|
|
|
|
|
|
|
|
if ((points.player >= 5 && (point = ["Player", points.player, "Computer", points.computer]))
|
|
|
|
||(points.computer >= 5 && (point = ["Computer",points.computer, "Player", points.player]))) {
|
|
|
|
resultDiv.innerText += `\r\n${point[0]} won against ${point[2]} ${point[1]}:${point[3]}`;
|
|
|
|
points.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 17:53:33 +00:00
|
|
|
buttons.forEach(button => {
|
2024-01-09 18:13:16 +00:00
|
|
|
button.addEventListener("click", () => playGameRound(button.value));
|
2024-01-09 17:53:33 +00:00
|
|
|
});
|