-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
198 lines (169 loc) · 5.56 KB
/
game.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
/*
My approach was to first create the html layout for the tic-tac-toe game, add
click events and then verify/check for the results horizontally, vertically and
diagonally
1. Once the table was created, I focused on adding the click event to the cells.
2. Then check for horizontal verification
3. Then check for vertical verification
4. Then for the diagonal verification
Features:
1. Start button
2. Reset button
3. Winner's names will displayed in the ordered list
*/
let gameScreen = document.querySelector('.main')
let table = gameScreen.querySelector('table')
let rows = table.querySelectorAll('tr');
let cells = table.querySelectorAll('td');
let gamesList = gameScreen.querySelector('.matches-played')
let itsX = false;
let coiuntCells = 0;
let x = prompt("enter player 1's name");
let o = prompt("enter player 2's name");
// adding click events to the cells for X and O to appear
for (let i = 0; i < cells.length; i++) {
cells[i].addEventListener('click', function (event) {
let parent = this.parentNode;
if (!this.innerText) {
if (itsX) {
this.innerText = 'O';
itsX = false;
coiuntCells++;
if (horizontalMatch(this)) {
addScoreCard(o)
return clearTable();
}
if (verticalMatch(i, this)) {
addScoreCard(o)
return clearTable()
}
if (checkDiagonal(i, this)) {
addScoreCard(o)
return clearTable()
}
}
else {
this.innerText = 'X';
itsX = true
coiuntCells++;
if (horizontalMatch(this)) {
addScoreCard(x)
return clearTable()
}
if (verticalMatch(i, this)) {
addScoreCard(x)
return clearTable()
}
if (checkDiagonal(i, this)) {
addScoreCard(x)
return clearTable()
}
}
} else if (coiuntCells >= 8) {
addScoreCard()
clearTable()
}
})
}
// to clear the table
function clearTable() {
for (let i = 0; i < cells.length; i++) {
cells[i].innerText = '';
}
coiuntCells = 0;
}
// for horizontal check fron left to right or right to left
function horizontalMatch(cell) {
let parent = cell.parentNode;
let value = cell.innerText;
let children = parent.childNodes;
for (let i = 0; i < children.length; i++) {
if (children[i].innerText !== value) return false;
}
return true;
}
// for vertical check fron top to bottom or bottom to top
function verticalMatch(idx, cell) {
let correctIndex = { 3: 0, 4: 1, 5: 2, 6: 0, 7: 1, 8: 2 };
let checkIndex = idx > 2 ? correctIndex[idx] : idx;
let value = cell.innerText;
for (let i = 0; i < rows.length; i++) {
if (rows[i].childNodes[checkIndex].innerText !== value) return false;
}
return true;
}
// to check for diagonal matches
function checkDiagonal(idx, cell) {
let correctIndex = { 0: 0, 4: 1, 6: 0, 8: 2 };
let checkIdx = correctIndex[idx];
let value = cell.innerText;
if(topLeftDiag(idx, value)) return true;
if(topRightDiag(idx, value)) return true;
if(bottomLeftDiag(idx, value)) return true;
if(bottomRightDiag(idx, value)) return true;
if(centerDiag(idx, value)) return true;
}
function topLeftDiag(idx, value){
if (idx === 0) {
if (rows[0].childNodes[0].innerText !== value) return false;
if (rows[1].childNodes[1].innerText !== value) return false;
if (rows[2].childNodes[2].innerText !== value) return false;
return true;
}
}
function topRightDiag(idx, value){
if (idx === 2) {
if (rows[0].childNodes[2].innerText !== value) return false;
if (rows[1].childNodes[1].innerText !== value) return false;
if (rows[2].childNodes[0].innerText !== value) return false;
return true;
}
}
function bottomLeftDiag(idx, value){
if (idx === 6) {
if (rows[2].childNodes[0].innerText !== value) return false;
if (rows[1].childNodes[1].innerText !== value) return false;
if (rows[0].childNodes[2].innerText !== value) return false;
return true;
}
}
function bottomRightDiag(idx, value){
if (idx === 8) {
if (rows[2].childNodes[2].innerText !== value) return false;
if (rows[1].childNodes[1].innerText !== value) return false;
if (rows[0].childNodes[0].innerText !== value) return false;
return true;
}
}
function centerDiag(idx, value){
if(idx === 4){
if(topLeftDiag(0, value)) return true;
if(topRightDiag(2, value)) return true;
if(bottomLeftDiag(6, value)) return true;
if(bottomRightDiag(8, value)) return true;
}
}
// start and reset btns
let start = gameScreen.querySelector('.st-btn');
let reset = gameScreen.querySelector('.rst-btn');
reset.addEventListener('click', function(){
clearTable()
})
start.addEventListener('click', function(){
x = prompt("enter player 1's name");
o = prompt("enter player 2's name")
})
// add wins to the list
function addScoreCard(player){
let list = document.createElement('li');
if(!player){
list.innerHTML = `
<p>Match draw</p>
`
} else {
list.innerHTML = `
<p>${player} won</p>
`
}
gamesList.append(list);
}