-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
186 lines (158 loc) · 4.59 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
/**
* Reference: https://codesandbox.io/p/sandbox/tic-tac-toedynamic-yirkd
*/
const turns = ['X', 'O'];
let turn = 0;
let reset = 0;
let dimension = 3; // boardSize
let winLine = 3; // hoanggbao: line to win
let board;
function handleReset(isPlaying, opacity, val) {
reset = val;
document.getElementById('play_again').disabled = isPlaying;
document.getElementById('rst').disabled = isPlaying;
document.getElementById('play_again').style.opacity = String(opacity);
document.getElementById('rst').style.opacity = String(opacity);
}
handleReset(true, 0.5, 0);
function rst() {
window.location.reload();
}
function play_again() {
board = new Array(dimension).fill('').map(() => new Array(dimension).fill(''));
initGame();
turn = 0;
handleReset(true, 0.5, 0);
let rm = document.getElementById('field');
while (rm.firstChild) {
rm.removeChild(rm.firstChild);
}
initGame();
}
function start() {
//alert(player1+" "+player2);
document.getElementById('bottom').classList.remove('hide');
document.getElementById('st').setAttribute('disabled', true);
document.getElementById('st').style.opacity = String(0.5);
board = new Array(dimension).fill('').map(() => new Array(dimension).fill(''));
initGame();
}
/**
* Check win
* @description dynamically check win with dynamic boardSize and line to win
* @param {*} row position row of this move
* @param {*} col position column of this move
* @param {*} board board array with 2x2 matrix
* @returns { true } if win
* @author: hoanggbao
* Date Modified: 12:04 - 12.06.24
* TODO: Need Improve
*/
function rowChecked(row) {
let cnt = 0;
let ch = 'X';
for (let i = 0; i < dimension; ++i) {
if (board[row][i] === '') {
cnt = 0;
} else if (ch === board[row][i]) {
cnt++;
if (cnt === winLine) {
return true;
}
} else {
cnt = 1;
ch = board[row][i];
}
}
return false;
}
function colChecked(col) {
let cnt = 0;
let ch = 'X';
for (let i = 0; i < dimension; ++i) {
if (board[i][col] === '') {
cnt = 0;
} else if (ch === board[i][col]) {
cnt++;
if (cnt === winLine) {
return true;
}
} else {
cnt = 1;
ch = board[i][col];
}
}
return false;
}
function diagonal1Checked(row, col) {
let cnt = 0;
let ch = 'X';
for (let k = Math.max(-row, -col); k <= Math.min(dimension - row - 1, dimension - col - 1); ++k) {
if (board[row + k][col + k] === '') {
cnt = 0;
} else if (ch === board[row + k][col + k]) {
cnt++;
if (cnt === winLine) {
return true;
}
} else {
cnt = 1;
ch = board[row + k][col + k];
}
}
return false;
}
function diagonal2Checked(row, col) {
let cnt = 0;
let ch = 'X';
for (let k = Math.max(-row, -dimension + col + 1); k <= Math.min(dimension - row - 1, col); ++k) {
if (board[row + k][col - k] === '') {
cnt = 0;
} else if (ch === board[row + k][col - k]) {
cnt++;
if (cnt === winLine) {
return true;
}
} else {
cnt = 1;
ch = board[row + k][col - k];
}
}
return false;
}
function checkWin(row, col) {
return rowChecked(row) || colChecked(col) || diagonal1Checked(row, col) || diagonal2Checked(row, col);
}
const handleClick = (cell, i, j) => {
const el = cell;
if (reset === 1 || el.innerHTML !== '') {
return;
}
board[i][j] = turns[turn % 2];
el.innerHTML = board[i][j];
if (checkWin(i, j)) { // hoanggbao: new condition
alert(turns[turn % 2] + " is winner");
handleReset(false, 1, 1);
return;
}
turn++;
document.getElementById('info').innerHTML = turns[turn % 2] + " turn";
if (turn === dimension * dimension && reset === 0) {
alert('Game is drawn');
handleReset();
}
};
function initGame() {
let gameContainer = document.getElementById('field');
for (let i = 0; i < dimension; i++) {
let row = document.createElement('div');
row.className = 'row';
for (let j = 0; j < dimension; j++) {
let cell = document.createElement('div');
cell.addEventListener('click', (event) => handleClick(cell, i, j));
cell.className = 'cell';
row.appendChild(cell);
}
gameContainer.appendChild(row);
}
}