-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_page.js
240 lines (202 loc) · 6.79 KB
/
game_page.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
const box = document.querySelectorAll('.game-container__box');
const turn = document.querySelectorAll('.player-container__player-turn');
const names = document.querySelectorAll('.player-container__player-name');
const new_game = document.querySelector('.new-game-button');
const score = document.querySelectorAll('.player-container__score');
//Getting players' names from session storage
let player_2_name = sessionStorage.getItem('player_2_name');
let player_1_name = sessionStorage.getItem('player_1_name');
//Variables which holds IDs of all the boxes clicked by a specific player
let player_1_boxes = [];
let player_2_boxes = [];
//Number of rounds won by a specific player
let player_1_wins = 0;
let player_2_wins = 0;
//Auxiliary variable which indicates which player has a move
let player = 1;
//Auxiliary variable which indicates how many moves has been already made by players
let i = 0;
//Load player's names to .player-container__player-name div
window.onload = () => {
names[0].innerHTML = player_1_name;
names[1].innerHTML = player_2_name;
};
box.forEach((box) =>
box.addEventListener('click', (e) => {
e.preventDefault;
if (player === 1 && !e.target.innerHTML) {
e.target.innerHTML = 'X';
turn[0].style.opacity = 0;
turn[1].style.opacity = 1;
player_1_boxes.push(e.target.id);
i++;
end_game(player_1_boxes);
player = 2;
}
if (player === 2 && !e.target.innerHTML) {
e.target.innerHTML = 'O';
turn[0].style.opacity = 1;
turn[1].style.opacity = 0;
player_2_boxes.push(e.target.id);
i++;
end_game(player_2_boxes);
player = 1;
}
})
);
new_game.addEventListener('click', () => {
i = 0;
player = 1;
turn[0].style.opacity = 1;
turn[1].style.opacity = 0;
player_1_boxes = [];
player_2_boxes = [];
turn[0].innerHTML = 'Your turn! You play X';
turn[1].innerHTML = 'Your turn! You play O';
box.forEach((box) => {
box.innerHTML = '';
box.style = '';
});
});
//p_score is a parameter which can be represented by player_1_boxes or player_2_boxes
//It holds IDs of all the boxes clicked by a specific player
function end_game(p_score) {
if (
//Checking if any of 'winning combination' has been clicked by a player
(p_score.includes('0') && p_score.includes('1') && p_score.includes('2')) ||
(p_score.includes('3') && p_score.includes('4') && p_score.includes('5')) ||
(p_score.includes('6') && p_score.includes('7') && p_score.includes('8')) ||
(p_score.includes('0') && p_score.includes('3') && p_score.includes('6')) ||
(p_score.includes('1') && p_score.includes('4') && p_score.includes('7')) ||
(p_score.includes('2') && p_score.includes('5') && p_score.includes('8')) ||
(p_score.includes('0') && p_score.includes('4') && p_score.includes('8')) ||
(p_score.includes('2') && p_score.includes('4') && p_score.includes('6'))
) {
//Coloring the 'winning combination'
if (
p_score.includes('0') &&
p_score.includes('1') &&
p_score.includes('2')
) {
box[0].style.background = '#2c68b5';
box[0].style.color = '#f0eeee';
box[1].style.background = '#2c68b5';
box[1].style.color = '#f0eeee';
box[2].style.background = '#2c68b5';
box[2].style.color = '#f0eeee';
}
if (
p_score.includes('3') &&
p_score.includes('4') &&
p_score.includes('5')
) {
box[3].style.background = '#2c68b5';
box[3].style.color = '#f0eeee';
box[4].style.background = '#2c68b5';
box[4].style.color = '#f0eeee';
box[5].style.background = '#2c68b5';
box[5].style.color = '#f0eeee';
}
if (
p_score.includes('6') &&
p_score.includes('7') &&
p_score.includes('8')
) {
box[6].style.background = '#2c68b5';
box[6].style.color = '#f0eeee';
box[7].style.background = '#2c68b5';
box[7].style.color = '#f0eeee';
box[8].style.background = '#2c68b5';
box[8].style.color = '#f0eeee';
}
if (
p_score.includes('0') &&
p_score.includes('3') &&
p_score.includes('6')
) {
box[0].style.background = '#2c68b5';
box[0].style.color = '#f0eeee';
box[3].style.background = '#2c68b5';
box[3].style.color = '#f0eeee';
box[6].style.background = '#2c68b5';
box[6].style.color = '#f0eeee';
}
if (
p_score.includes('1') &&
p_score.includes('4') &&
p_score.includes('7')
) {
box[1].style.background = '#2c68b5';
box[1].style.color = '#f0eeee';
box[4].style.background = '#2c68b5';
box[4].style.color = '#f0eeee';
box[7].style.background = '#2c68b5';
box[7].style.color = '#f0eeee';
}
if (
p_score.includes('2') &&
p_score.includes('5') &&
p_score.includes('8')
) {
box[2].style.background = '#2c68b5';
box[2].style.color = '#f0eeee';
box[5].style.background = '#2c68b5';
box[5].style.color = '#f0eeee';
box[8].style.background = '#2c68b5';
box[8].style.color = '#f0eeee';
}
if (
p_score.includes('0') &&
p_score.includes('4') &&
p_score.includes('8')
) {
box[0].style.background = '#2c68b5';
box[0].style.color = '#f0eeee';
box[4].style.background = '#2c68b5';
box[4].style.color = '#f0eeee';
box[8].style.background = '#2c68b5';
box[8].style.color = '#f0eeee';
}
if (
p_score.includes('2') &&
p_score.includes('4') &&
p_score.includes('6')
) {
box[2].style.background = '#2c68b5';
box[2].style.color = '#f0eeee';
box[4].style.background = '#2c68b5';
box[4].style.color = '#f0eeee';
box[6].style.background = '#2c68b5';
box[6].style.color = '#f0eeee';
}
//Blocking all the boxes which hasn't been clicked during the round by using space character
box.forEach((box) => {
if (!box.innerHTML) {
box.innerHTML = ' ';
}
});
//The last move leads to winning in tic tac toe so the player who made it is the one who won
//Number of player's moves is represented by player_1_boxes.length and player_1_boxes.length
if (player_1_boxes.length > player_2_boxes.length) {
player_1_wins++;
//Updating number of wins of player 1
score[0].innerHTML = player_1_wins;
} else {
player_2_wins++;
//Updating number of wins of player 2
score[1].innerHTML = player_2_wins;
}
//Announcing the end of the round
turn[0].style.opacity = 1;
turn[0].innerHTML = 'End of the game';
turn[1].style.opacity = 1;
turn[1].innerHTML = 'End of the game';
}
//If there is no 'winning combination' and the 9th move has been made annouce a draw
else if (i === 9) {
turn[0].style.opacity = 1;
turn[0].innerHTML = 'Draw';
turn[1].style.opacity = 1;
turn[1].innerHTML = 'Draw';
}
}