-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.js
124 lines (106 loc) · 3.07 KB
/
app.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
115
116
117
118
119
120
121
122
123
124
// Prevent animation on load
setTimeout(() => {
document.body.classList.remove("preload");
}, 500);
// DOM
const btnRules = document.querySelector(".rules-btn");
const btnClose = document.querySelector(".close-btn");
const modalRules = document.querySelector(".modal");
const CHOICES = [
{
name: "paper",
beats: "rock",
},
{
name: "scissors",
beats: "paper",
},
{
name: "rock",
beats: "scissors",
},
];
const choiceButtons = document.querySelectorAll(".choice-btn");
const gameDiv = document.querySelector(".game");
const resultsDiv = document.querySelector(".results");
const resultDivs = document.querySelectorAll(".results__result");
const resultWinner = document.querySelector(".results__winner");
const resultText = document.querySelector(".results__text");
const playAgainBtn = document.querySelector(".play-again");
const scoreNumber = document.querySelector(".score__number");
let score = 0;
// Game Logic
choiceButtons.forEach((button) => {
button.addEventListener("click", () => {
const choiceName = button.dataset.choice;
const choice = CHOICES.find((choice) => choice.name === choiceName);
choose(choice);
});
});
function choose(choice) {
const aichoice = aiChoose();
displayResults([choice, aichoice]);
displayWinner([choice, aichoice]);
}
function aiChoose() {
const rand = Math.floor(Math.random() * CHOICES.length);
return CHOICES[rand];
}
function displayResults(results) {
resultDivs.forEach((resultDiv, idx) => {
setTimeout(() => {
resultDiv.innerHTML = `
<div class="choice ${results[idx].name}">
<img src="images/icon-${results[idx].name}.svg" alt="${results[idx].name}" />
</div>
`;
}, idx * 1000);
});
gameDiv.classList.toggle("hidden");
resultsDiv.classList.toggle("hidden");
}
function displayWinner(results) {
setTimeout(() => {
const userWins = isWinner(results);
const aiWins = isWinner(results.reverse());
if (userWins) {
resultText.innerText = "you win";
resultDivs[0].classList.toggle("winner");
keepScore(1);
} else if (aiWins) {
resultText.innerText = "you lose";
resultDivs[1].classList.toggle("winner");
keepScore(-1);
} else {
resultText.innerText = "draw";
}
resultWinner.classList.toggle("hidden");
resultsDiv.classList.toggle("show-winner");
}, 1000);
}
function isWinner(results) {
return results[0].beats === results[1].name;
}
function keepScore(point) {
score += point;
scoreNumber.innerText = score;
}
// Play Again
playAgainBtn.addEventListener("click", () => {
gameDiv.classList.toggle("hidden");
resultsDiv.classList.toggle("hidden");
resultDivs.forEach((resultDiv) => {
resultDiv.innerHTML = "";
resultDiv.classList.remove("winner");
});
resultText.innerText = "";
resultWinner.classList.toggle("hidden");
resultsDiv.classList.toggle("show-winner");
});
// Show/Hide Rules
btnRules.addEventListener("click", () => {
modalRules.classList.toggle("show-modal");
});
btnClose.addEventListener("click", () => {
modalRules.classList.toggle("show-modal");
});