-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
58 lines (51 loc) · 1.91 KB
/
scripts.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
const hands = ["Rock", "Paper", "Scissors"];
let playerScore = 0;
let computerScore = 0;
let gameOver = false;
function getComputerChoice() {
const choice = Math.floor(Math.random() * hands.length);
return(hands[choice]);
}
function playRound(playerSelection,computerSelection) {
let result = document.getElementById('result');
let playerCounter = document. getElementById('playerCounter');
let computerCounter = document. getElementById('computerCounter');
let winnerDeclaration = document.getElementById('winnerDeclaration');
computerSelection = getComputerChoice();
if (
(playerSelection === "Rock" && computerSelection === "Scissors") ||
(playerSelection === "Paper" && computerSelection === "Rock") ||
(playerSelection === "Scissors" && computerSelection === "Paper")
) {
result.textContent = "You win! " + playerSelection + " beats " + computerSelection + "!";
playerScore++;
playerCounter.textContent = playerScore;
} else if (
(playerSelection === "Rock" && computerSelection === "Rock") ||
(playerSelection === "Paper" && computerSelection === "Paper") ||
(playerSelection === "Scissors" && computerSelection === "Scissors")
) {
result.textContent = "Tie game! You both played " + playerSelection;
} else {
result.textContent = "You lose! " + playerSelection + " loses to " + computerSelection + "!";
computerScore++;
computerCounter.textContent = computerScore;
}
if (playerScore === 5 && gameOver === false) {
winnerDeclaration.textContent = "Game Over! You win!";
gameOver = true;
}
if (computerScore === 5 && gameOver === false) {
winnerDeclaration.textContent = "Game Over! You lost!";
gameOver = true;
}
}
function resetBoard() {
playerScore = 0;
computerScore = 0;
result.textContent = "Make your selection:\nFirst to five wins!";
winnerDeclaration.textContent = "";
playerCounter.textContent = "/";
computerCounter.textContent = "/";
gameOver = false;
}