-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
231 lines (190 loc) · 5.51 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// getting img srcs
const rockImg = "./resources/rock.png";
const paperImg = "./resources/paper.png";
const scissorsImg = "./resources/scissors.png";
// QUERYING IN NECESSARY DOM ELEMENTS
// choice tab
const choiceTab = document.querySelector("#user-choices");
// choices
const rockChoices = Array.from(document.querySelectorAll(".rock"));
const paperChoices = Array.from(document.querySelectorAll(".paper"));
const scissorChoices = Array.from(document.querySelectorAll(".scissors"));
// user choices from choice tab
const rock = rockChoices[0];
const paper = paperChoices[0];
const scissors = scissorChoices[0];
// final choices
const userChoice = document.querySelector("#user-choice img");
const compChoice = document.querySelector("#computer-choice img");
// user scores
const userScoreSpan = document.querySelector("#user-score");
const compScoreSpan = document.querySelector("#computer-score");
// commentary & reset button
const comm = document.querySelector("#commentary");
const resetBtn = document.querySelector("button");
// setting the choice imgs
rockChoices.forEach((rock) => (rock.src = rockImg));
paperChoices.forEach((paper) => (paper.src = paperImg));
scissorChoices.forEach((scissors) => (scissors.src = scissorsImg));
// GAME FUNCTONALITY STARS UNDER THIS LINE
// player decisions
let userDec = "";
let compDec = "";
// players making moves
function userMove(decision) {
userDec = decision.className;
userChoice.src = decision.src;
}
function compMove() {
let rand = Math.floor(Math.random() * 3);
switch (rand) {
case 0:
compChoice.src = rockImg;
compDec = "rock";
break;
case 1:
compChoice.src = paperImg;
compDec = "paper";
break;
case 2:
compChoice.src = scissorsImg;
compDec = "scissors";
break;
}
enlargeChoice();
}
function enlargeChoice() {
document.querySelectorAll("#computer-choices img").forEach((choice) => {
if (compChoice.src == choice.src) {
choice.style.transform = "scale(1.4)";
} else {
choice.style.transform = "";
}
});
}
// determining the winner
let winner = "";
let gameTied = false;
function determineWinner() {
if (userDec === compDec) {
gameTied = true;
} else {
gameTied = false;
}
if (userDec == "rock") {
if (compDec == "paper") {
winner = "computer";
} else {
winner = "user";
}
} else if (userDec == "paper") {
if (compDec == "scissors") {
winner = "computer";
} else {
winner = "user";
}
} else if (userDec == "scissors") {
if (compDec == "rock") {
winner = "computer";
} else {
winner = "user";
}
}
}
// updating scores
let userScore = 0;
let compScore = 0;
function updateScores() {
if (!gameTied) winner == "user" ? userScore++ : compScore++;
userScoreSpan.textContent = userScore;
compScoreSpan.textContent = compScore;
}
// commentating function
const commentate = () =>
!gameTied
? (comm.textContent = "the " + winner + " won!")
: (comm.textContent = "the game was tied!");
// decorating components upon results
function userVic(size, winCol, loseCol) {
userChoice.parentElement.style.border = `${size}px solid ${winCol}`;
compChoice.parentElement.style.border = `${size}px solid ${loseCol}`;
comm.style.borderBottom = `${size}px solid ${winCol}`;
}
function userLoss(size, winCol, loseCol) {
userChoice.parentElement.style.border = `${size}px solid ${loseCol}`;
compChoice.parentElement.style.border = `${size}px solid ${winCol}`;
comm.style.borderBottom = `${size}px solid ${loseCol}`;
}
function tie(size, tieCol) {
userChoice.parentElement.style.border = `${size}px solid ${tieCol}`;
compChoice.parentElement.style.border = `${size}px solid ${tieCol}`;
comm.style.borderBottom = `${size}px solid ${tieCol}`;
}
function decorate() {
let borderSize = 5;
let winBorderCol = "#44ff44";
let loseBorderCol = "red";
let tieBorderCol = "#ffff08";
if (winner == "user" && !gameTied) {
userVic(borderSize, winBorderCol, loseBorderCol);
} else if (winner == "computer" && !gameTied) {
userLoss(borderSize, winBorderCol, loseBorderCol);
} else {
tie(borderSize, tieBorderCol);
}
}
// RESETTER FUNCTIONS
function scoreReset() {
userScore = 0;
compScore = 0;
userScoreSpan.textContent = userScore;
compScoreSpan.textContent = compScore;
}
function resetChoices() {
userChoice.src = "";
compChoice.src = "";
}
function resetDecorations() {
userChoice.parentElement.style.border = "";
compChoice.parentElement.style.border = "";
comm.style.borderBottom = "";
}
const resetText = () => (comm.textContent = "make your move");
function resetChoiceSize() {
Array.from(document.querySelectorAll("#computer-choices img")).forEach(
(img) => (img.style.transform = "")
);
}
// EVENT LISTENERS
// popup effect
[rock, paper, scissors].forEach((choice) => {
choice.addEventListener("mousedown", (e) => {
e.target.style.transform = "scale(1.4)";
// semi r esetting on mouse down
resetChoices();
resetChoiceSize();
resetDecorations();
resetText();
});
choice.addEventListener("mouseup", (e) => {
e.target.style.transform = "";
});
});
choiceTab.addEventListener("click", (e) => {
let targetElement = e.target;
if (targetElement.parentElement.className == "choice") {
userMove(targetElement);
compMove();
determineWinner();
commentate();
decorate();
updateScores();
}
});
resetBtn.addEventListener("click", (e) => {
scoreReset();
resetChoices();
resetChoiceSize();
resetDecorations();
resetText();
});