-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
305 lines (201 loc) · 7.69 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
function Gameboard (){
let board = [];
let moves = 0;
for (let i = 0; i < 3; i++) {
board[i] = [];
for (let j = 0; j < 3; j++) {
board[i].push(Cell());
}
}
function setSymbol (row, column, playerSymbol){
if(board[row][column].getSymbol() === '_'){
board[row][column].addSymbol(playerSymbol)
return true;
}
else{
console.log('Invalid Move, Try Again!');
return false;
}
}
return {
board,
setSymbol,
moves
}
}
function Cell (){
let symbol = '_';
function addSymbol (playerSymbol) {
symbol = playerSymbol;
}
function getSymbol (){
return symbol;
}
return {
addSymbol,
getSymbol,
}
}
function GamePlay (){
boardObj = Gameboard();
const Players = [
{
name : 'One',
symbol : 'X',
color : 'red'
},
{
name : 'Two',
symbol : 'O',
color : 'green'
}
];
let activePlayer = Players[0];
function setActivePlayer () {
activePlayer = activePlayer === Players[0] ? Players[1] : Players[0];
}
function getActivePlayer (){
return activePlayer;
}
function checkWin(moves){
let current = boardObj.board;
let winStatus = {
WIN : false,
TIE : false,
WINSYMBOL : '_'
}
switch (true) {
case ((current[0][0].getSymbol() === current[0][1].getSymbol()) && (current[0][2].getSymbol() === current[0][0].getSymbol()) && (current[0][0].getSymbol() !== '_')):
winStatus.WIN = true;
winStatus.WINSYMBOL = current[0][0].getSymbol();
break;
case ((current[1][0].getSymbol() === current[1][1].getSymbol()) && (current[1][0].getSymbol()=== current[1][2].getSymbol()) && (current[1][0].getSymbol() !== '_')):
winStatus.WIN = true;
winStatus.WINSYMBOL = current[1][0].getSymbol();
break;
case ((current[2][0].getSymbol() === current[2][1].getSymbol()) && (current[2][2].getSymbol() === current[2][0].getSymbol()) && (current[2][0].getSymbol() !== '_')):
winStatus.WIN = true;
winStatus.WINSYMBOL = current[2][0].getSymbol();
break;
case ((current[0][0].getSymbol() === current[1][0].getSymbol()) && (current[2][0].getSymbol() === current[0][0].getSymbol()) && (current[0][0].getSymbol() !== '_')):
winStatus.WIN = true;
winStatus.WINSYMBOL = current[0][0].getSymbol();
break;
case ((current[0][1].getSymbol() === current[1][1].getSymbol()) && (current[2][1].getSymbol() === current[0][1].getSymbol()) && (current[0][1].getSymbol() !== '_')):
winStatus.WIN = true;
winStatus.WINSYMBOL = current[0][1].getSymbol();
break;
case ((current[0][2].getSymbol() === current[1][2].getSymbol()) && (current[2][2].getSymbol() === current[0][2].getSymbol()) && (current[0][2].getSymbol() !== '_')):
winStatus.WIN = true;
winStatus.WINSYMBOL = current[0][2].getSymbol();
break;
case ((current[0][0].getSymbol() === current[1][1].getSymbol()) && (current[2][2].getSymbol() === current[0][0].getSymbol()) && (current[0][0].getSymbol() !== '_')):
winStatus.WIN = true;
winStatus.WINSYMBOL = current[0][0].getSymbol();
break;
case ((current[2][0].getSymbol() === current[1][1].getSymbol()) && (current[0][2].getSymbol() === current[1][1].getSymbol()) && (current[2][0].getSymbol() !== '_')):
winStatus.WIN = true;
winStatus.WINSYMBOL = current[2][0].getSymbol();
break;
case (moves == 9):
winStatus.TIE = true;
break;
default:
break;
}
return winStatus
}
function printBoard (row, column){
for (let i = 0; i < 3; i++) {
let r = ' ';
for (let j = 0; j < 3; j++) {
r += boardObj.board[i][j].getSymbol() + ' ';
}
console.log(r);
}
console.log('nextMove');
return boardObj.board[row][column].getSymbol();
}
const playRound = function (row, column){
//Do 3 things, 1)change the state of the cell board[row][coulmn], 2)change the playerTurn, 3)print the updated board
let status;
boardObj.moves += 1;
let moveSuccessfull = boardObj.setSymbol(row, column, activePlayer.symbol);
if(moveSuccessfull){
setActivePlayer();
status = checkWin(boardObj.moves);
let move = printBoard(row, column);
return { moveSuccessfull , move , status};
}
else return {moveSuccessfull } ;
}
//Initial Setup message
//printBoard(status);
return {
playRound,
getActivePlayer,
}
}
// ----------------------------DOM code below-----------------------------------------------------
function ScreenController () {
let game = GamePlay();
let clickedBtn = null;
//Populate boardDiv
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
const cellBtn = document.createElement('button');
cellBtn.id = '' + i + j;
cellBtn.classList.add('cell');
cellBtn.addEventListener('click', handleBoardClick)
boardDiv.appendChild(cellBtn);
}
}
function resetGameBoard (){
game = GamePlay();
let cellsArray = boardDiv.getElementsByClassName('cell');
toogleButtons(cellsArray, false);
// Set the text content of each button to an empty string
setTimeout(() => {
for (var i = 0; i < cellsArray.length; i++) {
cellsArray[i].textContent = '';
toogleButtons(cellsArray, true);
winStatusDiv.textContent = '';
clickedBtn.style.border = ` 2px rgb(137, 137, 137) solid`;
}
}, 2000);
function toogleButtons(buttons, enable) {
for (var i = 0; i < buttons.length; i++) {
buttons[i].disabled = !enable;
}
}
}
function updateScreen( move, status){
clickedBtn.textContent = move;
clickedBtn.style.color = `${game.getActivePlayer().color}`;
clickedBtn.style.border = `2px ${game.getActivePlayer().color} solid`;
if (status.WIN) {
winStatusDiv.textContent = `${status.WINSYMBOL} wins!`;
resetGameBoard();
}
else if (status.TIE){
winStatusDiv.textContent = 'It\'s a tie!';
resetGameBoard();
}
else{
winStatusDiv.textContent = `Player ${game.getActivePlayer().name}'s Turn...`;
}
}
function handleBoardClick (event) {
if (clickedBtn) {
clickedBtn.style.border = ` 2px rgb(137, 137, 137) solid`;
}
clickedBtn = event.target;
let content = game.playRound(parseInt(clickedBtn.id[0]) , parseInt(clickedBtn.id[1])) //Extract row and column form id and pass as arg
if (content.moveSuccessfull){
updateScreen( content.move , content.status);
}
}
}
const boardDiv = document.querySelector('.board');
const winStatusDiv = document.querySelector('.winner');
ScreenController ();