-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
81 lines (66 loc) · 2.32 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
let c_score = 0;
let h_score = 0;
// get HTML elements
const cpuPoints = document.querySelector("cpuPoints");
const humPoints = document.querySelector("humPoints");
const result = document.getElementById('result');
const playerScore = document.getElementById("h_points");
const computerScore = document.getElementById("c_points");
const choices = ['Rock', 'Paper', 'Scissors'];
document.getElementById("rock").addEventListener('click', () => playRound(0));
document.getElementById("paper").addEventListener('click', () => playRound(1));
document.getElementById("scissors").addEventListener('click', () => playRound(2));
function playConfetti() {
confetti();
}
function getComputerchoice() {
let c_choice = Math.floor(Math.random() * 3);
return c_choice;
}
function resetGame() {
result.textContent = 'Press the button to play!';
computerScore.textContent = '0';
playerScore.textContent = '0';
}
function disableButtons() {
document.getElementById('rock').disabled = true;
document.getElementById('paper').disabled = true;
document.getElementById('scissors').disabled = true;
}
function enableButtons() {
document.getElementById('rock').disabled = false;
document.getElementById('paper').disabled = false;
document.getElementById('scissors').disabled = false;
}
function playRound(h_choice) {
let c_choice = getComputerchoice();
result.classList.add("fadeIn");
setTimeout(function () {
result.classList.remove("fadeIn");
}, 2000);
// see who wins
if (c_choice == h_choice) {
result.textContent = `Tie! Both sides win!`
}
else if ((h_choice == 0 && c_choice == 2) || (h_choice == 1 && c_choice == 0) || (h_choice == 2 && c_choice == 1)) {
h_score++;
result.textContent = `You win! ${choices[h_choice]} beats ${choices[c_choice]}.`;
}
else {
c_score++;
result.textContent = `You lose! ${choices[c_choice]} beats ${choices[h_choice]}.`;
}
// update scores
computerScore.textContent = c_score;
playerScore.textContent = h_score;
// check if game has been won
if (c_score == 5) {
result.textContent = "You lose! Better luck next time!";
disableButtons();
}
if (h_score == 5) {
result.textContent = "You win! lucky!";
playConfetti();
disableButtons();
}
}