-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
272 lines (235 loc) · 9.47 KB
/
index.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
localStorage.clear();
// Holds currentPlays
let currentPlays = {
elementIDOfOpponentPlay: null,
elementIDOfPlayerPlay: null
};
//Holds wins/draws/losses
let results = {
wins: 0,
draws: 0,
losses: 0
};
//gets the width of the body
function getWidth() {
return Math.max(
document.body.scrollWidth,
document.documentElement.scrollWidth,
document.body.offsetWidth,
document.documentElement.offsetWidth,
document.documentElement.clientWidth
);
}
// function that handles initial landing page transitions
function landingPageTransitions(element) {
const widthPage = getWidth();
document.getElementById("landingPageContainer").style.transform =
"translateX(" + -1 * widthPage + "px)";
setTimeout(function() {
document.getElementById("landingPageContainer").style.visibility = "hidden";
}, 800);
}
// method that calcluates the result of the game based on the played elements
function calculateResult(playerPlayElement, opponentPlayElement) {
if (
(playerPlayElement.id === "playerRock" &&
opponentPlayElement.id === "opponentRock") ||
(playerPlayElement.id === "playerPaper" &&
opponentPlayElement.id === "opponentPaper") ||
(playerPlayElement.id === "playerScissors" &&
opponentPlayElement.id === "opponentScissors")
) {
results["draws"] += 1;
document.getElementById("resultMessage").innerHTML = "It's a DRAW!";
} else if (
(playerPlayElement.id === "playerRock" &&
opponentPlayElement.id === "opponentScissors") ||
(playerPlayElement.id === "playerPaper" &&
opponentPlayElement.id === "opponentRock") ||
(playerPlayElement.id === "playerScissors" &&
opponentPlayElement.id === "opponentPaper")
) {
results["wins"] += 1;
document.getElementById("resultMessage").innerHTML = "Player1 WINS!";
document.getElementById("playerScoreContainer").innerHTML =
"" + results["wins"] + "";
} else {
results["losses"] += 1;
document.getElementById("resultMessage").innerHTML = "Opponent WINS!";
document.getElementById("opponentScoreContainer").innerHTML =
"" + results["losses"] + "";
}
localStorage.setItem("results", JSON.stringify(results));
}
// Function that randomly selects a play for the opponent
// It returns the element that was chosen
function opponentPlays() {
let playsArray = [
{
playName: "rock",
elementID: "opponentRock"
},
{
playName: "paper",
elementID: "opponentPaper"
},
{
playName: "scissors",
elementID: "opponentScissors"
}
];
let rand = Math.floor(Math.random() * 3) + 0;
// Stores ID of currently Played element of OPPONENT in local storage
currentPlays["elementIDOfOpponentPlay"] = playsArray[rand]["elementID"];
localStorage.setItem("currentPlays", JSON.stringify(currentPlays));
return document.getElementById(playsArray[rand]["elementID"]);
}
// function that works with displaying only the played elements and hiding all the others
function showSelectedPlays(playerPlayedElement, opponentPlayedElement) {
//gets position of the middle elements of both containers
let paperPlayerElement = playerPlayedElement.parentElement.childNodes[3].getBoundingClientRect();
let paperOpponentElement = opponentPlayedElement.parentElement.childNodes[3].getBoundingClientRect();
// Get the sibling elements of the clicked play
let playerPlayElementSiblings = playerPlayedElement.parentElement.childNodes;
let opponentPlayElementSiblings =
opponentPlayedElement.parentElement.childNodes;
// hide all other elements and move the clicked element to the middle
for (let index = 1; index <= 5; index += 2) {
//Handles the player container elements
if (playerPlayElementSiblings[index].id != playerPlayedElement.id) {
playerPlayElementSiblings[index].classList.remove("showPlayContainer");
playerPlayElementSiblings[index].classList.add("hidePlayContainer");
} else {
let currentPlayerElement = playerPlayedElement.parentElement.childNodes[
index
].getBoundingClientRect();
playerPlayElementSiblings[index].style.transform =
"translateY(" +
(paperPlayerElement.top - currentPlayerElement.top) +
"px)";
}
//Handles the opponent container elements
if (opponentPlayElementSiblings[index].id != opponentPlayedElement.id) {
opponentPlayElementSiblings[index].classList.remove("showPlayContainer");
opponentPlayElementSiblings[index].classList.add("hidePlayContainer");
} else {
let currentOpponentElement = opponentPlayedElement.parentElement.childNodes[
index
].getBoundingClientRect();
opponentPlayElementSiblings[index].style.transform =
"translateY(" +
(paperOpponentElement.top - currentOpponentElement.top) +
"px)";
}
}
}
// function that stores the current played elements from both players
// adds the currenPlay class to them
// displays the result and the play again button
function playerPlays(currentElement, currentElementID) {
//disable player Container
document.getElementById("playerContainer").disabled = true;
//Remove any previous plays/selections
let prevPlays = JSON.parse(localStorage.getItem("currentPlays"));
if (prevPlays) {
document
.getElementById(prevPlays["elementIDOfOpponentPlay"])
.classList.remove("currentPlay");
document
.getElementById(prevPlays["elementIDOfPlayerPlay"])
.classList.remove("currentPlay");
}
// Stores current Play of player in local storage
currentPlays["elementIDOfPlayerPlay"] = currentElementID;
localStorage.setItem("currentPlays", currentPlays);
//adds the currentPlay class to both the opponent's played element and to the player's played element
currentElement.classList.add("currentPlay");
let opponentPlayElement = opponentPlays();
opponentPlayElement.classList.add("currentPlay");
//deals with hiding non-played and showing by moving to the middle the played elements
showSelectedPlays(currentElement, opponentPlayElement);
//calculates the result
calculateResult(currentElement, opponentPlayElement);
// the message container and the play again button container appears after player plays
document.getElementById("messageContainer").classList.add("showResults");
document.getElementById("messageContainer").style.opacity = 1;
document.getElementById("btnContainer").classList.add("showResults");
document.getElementById("btnContainer").style.opacity = 1;
document.getElementById("btnContainer").style.zIndex = 1;
}
// function Resets Game to start another round
function resetGame() {
// Get the sibling elements of the clicked play
let playerPlayElementSiblings = document.getElementById("playerContainer")
.childNodes;
let opponentPlayElementSiblings = document.getElementById("opponentContainer")
.childNodes;
//show all elements and move them back to their original position
for (let index = 1; index <= 5; index += 2) {
playerPlayElementSiblings[index].classList.remove("hidePlayContainer");
playerPlayElementSiblings[index].classList.add("showPlayContainer");
playerPlayElementSiblings[index].style.transform = "translateY(0)";
opponentPlayElementSiblings[index].classList.remove("hidePlayContainer");
opponentPlayElementSiblings[index].classList.add("showPlayContainer");
opponentPlayElementSiblings[index].style.transform = "translateY(0)";
}
// Re-enable the players container
document.getElementById(
currentPlays["elementIDOfPlayerPlay"]
).parentElement.disabled = false;
// remove the currentPlay class from both the player and the opponent
document
.getElementById(currentPlays["elementIDOfPlayerPlay"])
.classList.remove("currentPlay");
document
.getElementById(currentPlays["elementIDOfOpponentPlay"])
.classList.remove("currentPlay");
//Hide the message of the result and the play again button
document.getElementById("messageContainer").classList.remove("showResults");
document.getElementById("btnContainer").classList.remove("showResults");
document.getElementById("messageContainer").style.opacity = 0;
document.getElementById("btnContainer").style.opacity = 0;
document.getElementById("btnContainer").style.zIndex = -1;
}
function returnHome() {
const widthPage = getWidth();
document.getElementById("landingPageContainer").style.visibility = "visible";
document.getElementById("landingPageContainer").style.transform =
"translateX(" + 0 + "px)";
document.getElementById("playerScoreContainer").innerHTML = "0";
document.getElementById("opponentScoreContainer").innerHTML = "0";
}
//Play button on the lanfing page is clicked
document.getElementById("playBtn").onclick = function() {
landingPageTransitions(this);
};
//player's Rock element is clicked
document.getElementById("playerRock").onclick = function() {
if (!this.parentElement.disabled) {
playerPlays(this, "playerRock");
}
};
//player's Paper element is clicked
document.getElementById("playerPaper").onclick = function() {
if (!this.parentElement.disabled) {
playerPlays(this, "playerPaper");
}
};
//player's Scissors element is clicked
document.getElementById("playerScissors").onclick = function() {
if (!this.parentElement.disabled) {
playerPlays(this, "playerScissors");
}
};
//Again button is clicked
document.getElementById("resetBtn").onclick = function() {
resetGame();
};
//Again button is clicked
document.getElementById("closeBtn").onclick = function() {
if (document.getElementById("btnContainer").style.opacity != 0) {
resetGame();
}
returnHome();
localStorage.clear();
};