-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
114 lines (98 loc) · 3.81 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
symbols = ["ROCK", "PAPER", "SCISSORS"];
let rock = document.querySelector("#rock");
let paper = document.querySelector("#paper");
let scissors = document.querySelector("#scissors");
let container = document.querySelector("#container");
let winner = document.querySelector("#winner");
let announcer = document.querySelector("#announcer");
let score = document.querySelector("#score");
let reset = document.querySelector("#reset");
let restart = document.querySelector("#restart");
rock.addEventListener("click", function (e) {
playRound(e.target.value)
});
paper.addEventListener("click", function (e) {
playRound(e.target.value)
});
scissors.addEventListener("click", function (e) {
playRound(e.target.value)
});
restart.addEventListener("click", resetGame);
let playerScore = 0;
let computerScore = 0;
function resetGame() {
playerScore = 0;
computerScore = 0;
score.innerText = `Score (Player - PC): ${playerScore} - ${computerScore}`
announcer.innerText = "Rock, paper, scissors!";
winner.innerText = "";
reset.classList.toggle("hidden");
rock.classList.toggle("disabled");
paper.classList.toggle("disabled");
scissors.classList.toggle("disabled");
}
function gameEnd() {
reset.classList.toggle("hidden");
rock.classList.toggle("disabled");
paper.classList.toggle("disabled");
scissors.classList.toggle("disabled");
}
function computerPlay() {
return symbols[Math.floor(Math.random() * 3)];
}
function checkScore() {
if (playerScore === 5) {
winner.innerText = "Player wins round of 5!";
gameEnd();
} else if (computerScore === 5) {
winner.innerText = "Computer wins round of 5!";
gameEnd();
}
}
function playRound(playerSelection) {
let computerSelection = computerPlay();
switch (playerSelection) {
case "ROCK":
if (computerSelection === "ROCK") {
return announcer.innerText = "Rock vs Rock. Tie.";
} else if (computerSelection === "PAPER") {
computerScore++
score.innerText = `Score (Player - PC): ${playerScore} - ${computerScore}`
checkScore();
return announcer.innerText = "Rock vs Paper. You lose!"
} else {
playerScore++
score.innerText = `Score (Player - PC): ${playerScore} - ${computerScore}`
checkScore();
return announcer.innerText = "Rock vs Scissors. You win!";
}
case "PAPER":
if (computerSelection === "ROCK") {
playerScore++
score.innerText = `Score (Player - PC): ${playerScore} - ${computerScore}`
checkScore();
return announcer.innerText = "Paper vs Rock. You win!";
} else if (computerSelection === "PAPER") {
return announcer.innerText = "Paper vs Paper. Tie.";
} else {
computerScore++
score.innerText = `Score (Player - PC): ${playerScore} - ${computerScore}`
checkScore();
return announcer.innerText = "Paper vs Scissors. You lose!";
}
case "SCISSORS":
if (computerSelection === "ROCK") {
computerScore++
score.innerText = `Score (Player - PC): ${playerScore} - ${computerScore}`;
checkScore();
return announcer.innerText = "Scissors vs Rock. You lose!";
} else if (computerSelection === "PAPER") {
playerScore++
score.innerText = `Score (Player - PC): ${playerScore} - ${computerScore}`
checkScore();
return announcer.innerText = "Scissors vs Paper. You win!";
} else {
return announcer.innerText = "Scissors vs Scissors. Tie.";
}
}
}