-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
191 lines (167 loc) · 4.9 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
let cells = Array.from(document.querySelectorAll('.cell'))
let heading = document.querySelector('#heading');
let restartBtn = document.getElementById('restart-btn')
let player = 'X'
let gameEnd = false
const displayController = (e) => {
if(gameEnd === true){
return;
}
let cellId = e.target.id;
let id = cellId[9]
let row = cellId.slice(0,4);
board.boardUpdate(row, id, player)
let cell = document.getElementById(`${cellId}`);
if(cell.innerHTML !== ''){
return;
}
cell.innerHTML = `
<div>
<p id= '${cellId}'>
${player}
</p>
</div>
`;
if(player === 'X'){
player = 'O'
} else if(player === 'O'){
player = 'X'
}
heading.innerHTML = `<h2 id="heading">Player ${player}'s turn</h2>`
let winner = board.checkForWin()
if(winner === 'X' || winner === 'O'){
heading.innerHTML = `<h2 id="heading">Player ${winner} has won!`;
gameEnd = true
} else if(winner === 'noone'){
heading.innerHTML = '<h2 id="heading">Its a Tie!</h2>';
gameEnd = true;
}
}
cells.forEach((cell) => {
cell.addEventListener('click', displayController)
})
const gameBoard = () => {
let boardRow1 = ['', '', ''];
let boardRow2 = ['', '', ''];
let boardRow3 = ['', '', ''];
let RowsArr = [
boardRow1,
boardRow2,
boardRow3
]
let winner = ''
const boardUpdate = (row, id, player) => {
if(row == 'row1'){
boardRow1[id] = player;
} else if(row === 'row2'){
boardRow2[id] = player;
} else if(row == 'row3'){
boardRow3[id] = player;
}
}
const checkForWin = () => {
// Check rows for win
let result = ''
let rowUnderCheck = []
winner = ''
for(let i = 0; i < RowsArr.length ; i++){
if(result === 'XXX' || result === 'OOO'){
winner = rowUnderCheck[0];
break;
}
result = '';
rowUnderCheck = RowsArr[i]
for(let k = 0; k < rowUnderCheck.length; k++){
if (rowUnderCheck[k] !== ''){
result += rowUnderCheck[k]
}
if(result === 'XXX' || result === 'OOO'){
winner = rowUnderCheck[0];
break;
}
}
}
//Check columns for win
for(let i = 0; i < RowsArr.length; i++){
if(result === 'XXX' || result === 'OOO'){
winner = result.slice(0,1);
break;
}
result = ''
for(let k = 0; k < RowsArr.length; k++){
if(RowsArr[k][i] !== ''){
result += RowsArr[k][i];
}
if(result === 'XXX' || result === 'OOO'){
winner = RowsArr[k][i] ;
break;
}
}
}
//check diagnol
result = ''
for(let i = 0; i < 3; i++){
if(RowsArr[i][i] !== ''){
result += RowsArr[i][i]
}
if(result === 'XXX' || result === 'OOO'){
winner = RowsArr[i][i] ;
}
}
// check anti-diagnol
result = ''
let k = 2
for(let i = 0; i < 3; i++){
if(RowsArr[i][k] !== ''){
result += RowsArr[i][k]
}
if(result === 'XXX' || result === 'OOO'){
winner = RowsArr[i][k] ;
}
k--
}
//check if its a tie
if( winner === ''){
let arraysPassed = 0
let pass = false
for(let i = 0; i < RowsArr.length; i++){
for(let k = 0; k < 3; k++){
if(RowsArr[i][k] === ''){
pass = true;
break;
}
}
if(pass === true){
break;
}
arraysPassed++ ;
}
if(arraysPassed === 3){
winner = 'noone'
}
}
return winner
}
const gameRestart = () => {
boardRow1 = ['', '', ''];
boardRow2 = ['', '', ''];
boardRow3 = ['', '', ''];
RowsArr = [
boardRow1,
boardRow2,
boardRow3
]
}
return {boardUpdate, checkForWin, gameRestart}
}
const board = gameBoard()
const restartGame = () => {
cells.forEach((cell) => {
cell.innerHTML = ''
})
player = 'X'
heading.innerHTML = `<h2 id="heading">Player ${player}'s turn</h2>`;
gameEnd = false;
board.gameRestart()
}
restartBtn.addEventListener('click', restartGame)