-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
225 lines (210 loc) · 5.43 KB
/
main.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
class TicTacToe {
constructor() {
this.state = {
board: [
[null, null, null],
[null, null, null],
[null, null, null],
],
currentPlayer: {
wins: 0,
symbol: "o",
},
playerO: {
wins: 0,
symbol: "o",
},
playerX: {
wins: 0,
symbol: "x",
},
ties: 0,
mostRecentMove: {
row: 0,
col: 0,
},
gameCondition: "running", // tie || running || win
};
}
isHorizontalWin() {
let currentRow = this.state.mostRecentMove.row;
let symbol = this.state.currentPlayer.symbol;
for (let col = 0; col < 3; col++) {
if (this.state.board[currentRow][col] !== symbol) return false;
}
return true;
}
isVerticalWin() {
let currentCol = this.state.mostRecentMove.col;
let symbol = this.state.currentPlayer.symbol;
for (let row = 0; row < 3; row++) {
if (this.state.board[row][currentCol] !== symbol) return false;
}
return true;
}
isFirstDiagonalWin() {
let symbol = this.state.currentPlayer.symbol;
let row = 0;
let col = 0;
while (row < 3 && col < 3) {
if (this.state.board[row][col] !== symbol) {
return false;
}
row++;
col++;
}
return true;
}
isSecondDiagonalWin() {
let symbol = this.state.currentPlayer.symbol;
let row = 0;
let col = 2;
while (row < 3 && col >= 0) {
if (this.state.board[row][col] !== symbol) {
return false;
}
row++;
col--;
}
return true;
}
isWin() {
let isWin =
this.isHorizontalWin() ||
this.isVerticalWin() ||
this.isFirstDiagonalWin() ||
this.isSecondDiagonalWin();
return isWin;
}
isTie() {
let isWin = this.isWin();
if (isWin) return false;
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 3; col++) {
if (this.state.board[row][col] === null) {
return false;
}
}
}
return true;
}
makeWin() {
this.state.currentPlayer.wins += 1;
this.state.gameCondition = "win";
}
makeTie() {
this.state.ties += 1;
this.state.gameCondition = "tie";
}
resetBoard() {
this.state.board = [
[null, null, null],
[null, null, null],
[null, null, null],
];
}
switchPlayer() {
if (this.state.currentPlayer.symbol === "o") {
this.state.currentPlayer = this.state.playerX;
} else {
this.state.currentPlayer = this.state.playerO;
}
}
updateState() {
let isWin = this.isWin();
let isTie = this.isTie();
if (isWin) this.makeWin();
else if (isTie) this.makeTie();
else this.switchPlayer();
}
updateBoard(row, col) {
this.state.mostRecentMove.row = row;
this.state.mostRecentMove.col = col;
this.state.board[row][col] = this.state.currentPlayer.symbol;
}
getClonedState() {
let clonedState = JSON.parse(JSON.stringify(this.state));
let frozenState = Object.freeze(clonedState);
return frozenState;
}
isValidMove(row, col) {
return this.state.board[row][col] === null;
}
makeMove(row, col) {
if (
this.state.gameCondition === "tie" ||
this.state.gameCondition === "win"
) {
this.resetBoard();
this.state.gameCondition = "running";
}
if (this.isValidMove(row, col)) {
this.updateBoard(row, col);
this.updateState();
}
let clonedState = this.getClonedState();
return clonedState;
}
}
let board = new TicTacToe();
let cells = document.querySelectorAll(".cell");
let playerOWins = document.querySelector("#player-o-wins");
let playerXWins = document.querySelector("#player-x-wins");
let ties = document.querySelector("#ties");
let container = document.querySelector(".container");
let boardDiv = document.querySelector(".board");
let h1 = document.querySelector("#game-condition");
for (let cell of cells) {
cell.addEventListener("click", clickHandler);
}
function clickHandler(e) {
const row = e.currentTarget.dataset.row;
const col = e.currentTarget.dataset.col;
state = board.makeMove(row, col);
renderGameBoard(state);
renderScoreBoard(state);
}
function renderGameBoard(state) {
const { row: mostRecentRow, col: mostRecentCol } = state.mostRecentMove;
for (let cell of cells) {
const row = cell.dataset.row;
const col = cell.dataset.col;
const symbol = state.board[row][col];
console.log(mostRecentRow);
console.log(mostRecentCol);
switch (symbol) {
case null:
cell.innerHTML = "<p></p>";
break;
default:
if (row === mostRecentRow && col === mostRecentCol) {
cell.innerHTML = `<p class="animateScaling">${symbol}</p>`;
} else {
cell.innerHTML = `<p>${symbol}</p>`;
}
break;
}
}
changeTextColor(state.gameCondition);
if (state.gameCondition === "win") {
h1.textContent = `Player ${state.currentPlayer.symbol} won!`;
} else if (state.gameCondition === "tie") {
h1.textContent = "Tie!";
} else {
h1.textContent = "";
}
}
function changeTextColor(gameCondition) {
if (gameCondition === "win" || gameCondition === "tie") {
boardDiv.classList.add("gray");
boardDiv.classList.remove("white");
} else {
boardDiv.classList.add("white");
boardDiv.classList.remove("gray");
}
}
function renderScoreBoard(state) {
playerOWins.textContent = state.playerO.wins;
playerXWins.textContent = state.playerX.wins;
ties.textContent = state.ties;
}