-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
221 lines (191 loc) · 5.32 KB
/
app.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
/////////////////
/* BOARD SETUP */
/////////////////
function createEmptyBoard() {
return new Array(9).fill("");
}
///////////
/* STATE */
///////////
function createInitialState() {
const initialState = {
board: createEmptyBoard(),
winner: null,
player1: "",
player2: "",
numMoves: 0,
isPlaying: false,
};
return initialState;
}
let state = createInitialState();
const restartBtn = document.getElementById("gameRestart");
restartBtn.addEventListener("click", restartGame);
function restartGame() {
state = createInitialState();
renderState();
const player1 = document.getElementsByName("player1")[0];
const player2 = document.getElementsByName("player2")[0];
const gameStatus = document.getElementById("gameStatus");
player1.value = "";
player2.value = "";
gameStatus.innerHTML = "";
}
// do I still need this section below
const player1 = document.getElementsByName("player1")[0];
player1.addEventListener("change", (e) => {
const node = e.target;
state.player1 = node.value;
});
const player2 = document.getElementsByName("player2")[0];
player2.addEventListener("change", (e) => {
const node = e.target;
state.player2 = node.value;
});
// down to this line
const gameStatus = document.getElementById("gameStatus");
const registerPlayersButton = document.getElementById("registerPlayers");
registerPlayersButton.addEventListener("click", (e) => {
state.isPlaying = true;
gameStatus.innerHTML = `It's ${state.player1}'s turn!`;
});
//////////////////////
/* BUILD GAME BOARD */
//////////////////////
function buildDOMBoard() {
const DOMBoard = document.getElementById("board");
const { board } = state;
for (let i = 0; i < board.length; i++) {
const cell = document.createElement("div");
cell.id = `cell:${i}`;
cell.className = "cell";
cell.dataset.cellId = i;
DOMBoard.appendChild(cell);
}
}
buildDOMBoard();
////////////////////
/* GAME MECHANICS */
////////////////////
const clickHandler = (e) => {
if (!state.isPlaying) return;
if (e.target.className !== "cell") return;
handleMove(e.target);
renderState();
checkWin();
};
document.getElementById("board").addEventListener("click", clickHandler);
function handleMove(node) {
const nodeId = node.dataset.cellId;
const move = state.numMoves % 2 === 0 ? "X" : "O";
if (state.board[nodeId]) {
return;
}
state.board[nodeId] = move;
state.numMoves++;
const playerName = state.numMoves % 2 === 0 ? state.player1 : state.player2;
gameStatus.innerHTML = `It's ${playerName}'s turn!`;
console.log({ playerName, move, numMoves: state.numMoves });
}
function renderState() {
const boardSquares = document.querySelectorAll(".cell");
for (let i = 0; i < state.board.length; i++) {
boardSquares[i].innerHTML = `${state.board[i]}`;
}
}
function checkWin() {
const board = state.board;
const numMoves = state.numMoves;
const playerName = state.numMoves % 2 === 0 ? state.player2 : state.player1;
if (numMoves === 9) {
gameStatus.innerHTML = "Game Over! It's a draw";
state.isPlaying = false;
return;
}
function getRows(board) {
let rows = {
row1: [board[0], board[1], board[2]],
row2: [board[3], board[4], board[5]],
row3: [board[6], board[7], board[8]],
};
return rows;
}
function getCols(board) {
let cols = {
col1: [board[0], board[3], board[6]],
col2: [board[1], board[4], board[7]],
col3: [board[2], board[5], board[8]],
};
return cols;
}
function getDiags(board) {
let diagonals = {
diag1: [board[0], board[4], board[8]],
diag2: [board[3], board[4], board[6]],
};
return diagonals;
}
let rows = getRows(board);
for (const key in rows) {
const array = rows[key];
let isRowWinX = array.every(function (elem) {
return elem === "X";
});
if (isRowWinX) {
gameStatus.innerHTML = `${playerName} wins!`;
state.isPlaying = false;
return;
}
let isRowWinO = array.every(function (elem) {
return elem === "O";
});
if (isRowWinO) {
gameStatus.innerHTML = `${playerName} wins!`;
state.isPlaying = false;
return;
}
}
let cols = getCols(board);
for (const key in cols) {
const array = cols[key];
let isColWinX = array.every(function (elem) {
return elem === "X";
});
if (isColWinX) {
gameStatus.innerHTML = `${playerName} wins!`;
state.isPlaying = false;
return;
}
let isColWinO = array.every(function (elem) {
return elem === "O";
});
if (isColWinO) {
gameStatus.innerHTML = `${playerName} wins!`;
state.isPlaying = false;
return;
}
let diags = getDiags(board);
for (const key in diags) {
const array = diags[key];
let isDiagWinX = array.every(function (elem) {
return elem === "X";
});
if (isDiagWinX) {
gameStatus.innerHTML = `${playerName} wins!`;
state.isPlaying = false;
return;
}
let isDiagWinO = array.every(function (elem) {
return elem === "O";
});
if (isDiagWinO) {
gameStatus.innerHTML = `${playerName} wins!`;
state.isPlaying = false;
return;
}
}
}
}
// live-server injected its markup and it's being rendered into page
// this masks that script injection
document.body.querySelector("script").style.display = "none";