-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
94 lines (83 loc) · 2.91 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Get elements
const rockButton = document.querySelector(".rock");
const paperButton = document.querySelector(".paper");
const scissorsButton = document.querySelector(".scissors");
const resetButton = document.querySelector(".reset");
const resultMessage = document.querySelector(".result-message");
const mainHeader = document.querySelector("h1");
const scoreBoard = document.querySelector("h3");
const allButtons = document.querySelectorAll("button");
// Set default values
let computerScore = 0;
let playerScore = 0;
let playerSelection;
let computerSelection;
// Get computer selection
function getComputerSelection() {
const options = ["Rock", "Paper", "Scissors"];
return options[Math.floor(Math.random() * options.length)];
}
// Play a round of the game
function gameRound() {
computerSelection = getComputerSelection();
if (computerSelection == playerSelection) {
resultMessage.textContent = `Tie! Both players chose ${computerSelection}`;
} else if (
(computerSelection === "Rock" && playerSelection === "Scissors") ||
(computerSelection === "Paper" && playerSelection === "Rock") ||
(computerSelection === "Scissors" && playerSelection === "Paper")
) {
computerScore++;
resultMessage.textContent = `Computer wins! Computer's ${computerSelection} beat your ${playerSelection}`;
} else if (
(playerSelection === "Rock" && computerSelection === "Scissors") ||
(playerSelection === "Paper" && computerSelection === "Rock") ||
(playerSelection === "Scissors" && computerSelection === "Paper")
) {
playerScore++;
resultMessage.textContent = `You win! Your ${playerSelection} beat computer's ${computerSelection}`;
}
scoreBoard.textContent = `You: ${playerScore} | Computer: ${computerScore}`;
}
// End the game
function endGame() {
for (let button of allButtons) {
button.classList.toggle("hidden");
}
if (computerScore === 5) {
resultMessage.textContent = `Game Over! Computer Wins with ${computerSelection}!`;
} else if (playerScore === 5) {
resultMessage.textContent = `Game Over! You Win with ${playerSelection}!`;
}
}
//Reset the scores and display
function resetScores() {
playerScore = 0;
computerScore = 0;
scoreBoard.textContent = `Score: 0`;
}
// Check for a winner (first to 5 points)
function checkForWinner() {
if (playerScore === 5 || computerScore === 5) {
endGame();
}
}
//Player selection events
function playerChoice(button, choice) {
button.addEventListener("click", () => {
playerSelection = choice;
gameRound();
checkForWinner();
mainHeader.textContent = `${playerSelection} vs. ${computerSelection}`;
});
}
playerChoice(rockButton, "Rock");
playerChoice(paperButton, "Paper");
playerChoice(scissorsButton, "Scissors");
// Reset Game event
resetButton.addEventListener("click", () => {
resetScores();
endGame();
resultMessage.textContent = `First to 5 points wins!`;
mainHeader.textContent = "Rock Paper Scissors";
});