-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathticktactoe.html
296 lines (274 loc) · 9.51 KB
/
ticktactoe.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
display: flex;
justify-content: center;
align-items: center;
}
.message:empty {
padding-top: 1.125em;
}
.main {
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: center;
}
.board {
width: min(70vmin, 35rem);
height: min(70vmin, 35rem);
display: flex;
flex-direction: column;
}
.row {
width: 100%;
height: 100%;
display: flex;
}
.cell {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
border-bottom: solid black 4px;
border-right: solid black 4px;
}
.row:last-child .cell{
border-bottom: none;
}
.row .cell:last-child{
border-right: none;
}
button {
margin-top: 10px;
}
</style>
<title>Tic-Tac-Toe</title>
</head>
<body>
<div class="main">
<div class="board"></div>
<div class="size-btns">
<button class="shrink" onclick="shrinkBoard()" disabled>-</button>
<button class="grow" onclick="growBoard()">+</button>
</div>
<button class="randomBtn" onclick="randomTurn()">Random turn</button>
<button class="reset" onclick="reset()">Reset</button>
<h4 class="message">​</h4>
</div>
</body>
<script>
const board = document.querySelector('.board')
const message = document.querySelector('.message')
const shrinkBtn = document.querySelector('.size-btns .shrink')
const growBtn = document.querySelector('.size-btns .grow')
const randomBtn = document.querySelector('.randomBtn')
const INITIAL_BOARD_SIZE = 3
const MIN_BOARD_SIZE = 3
const MAX_BOARD_SIZE = 7
let boardSize = INITIAL_BOARD_SIZE
let isPlayerOnesTurn = true
let gameStarted
let gameOver
const reset = () => {
board.childNodes.forEach(row => {
row.childNodes.forEach(cell => {
cell.isUsed = false
cell.innerHTML = ''
})
})
message.innerHTML = ''
randomBtn.disabled = false
gameStarted = false
gameOver = false
isPlayerOnesTurn = true
if (boardSize > 3) shrinkBtn.disabled = false
if (boardSize < 7) growBtn.disabled = false
}
const startGame = () => {
shrinkBtn.disabled = true
growBtn.disabled = true
gameStarted = true
}
const currentPlayer = () => {
const isCurrentPlayerX = isPlayerOnesTurn
isPlayerOnesTurn = !isPlayerOnesTurn
return isCurrentPlayerX ? 'X': 'O'
}
const createRow = () => {
const row = document.createElement('div')
row.classList.add('row')
return row
}
const createCell = () => {
const cell = document.createElement('div')
cell.classList.add('cell')
cell.isUsed = false
cell.addEventListener('click', () => {
if (!gameStarted)
startGame()
if (!cell.isUsed && !gameOver){
cell.innerHTML = currentPlayer()
cell.isUsed = true
checkForWinner()
}
})
return cell
}
const updatePlayerSizes = () => {
const cell = document.querySelector('.cell')
const size = cell.offsetWidth * 0.7
board.style.fontSize = `${size}px`
}
const growBoard = () => {
boardSize++
board.childNodes.forEach(row => {
const cell = createCell()
row.appendChild(cell)
})
const row = createRow()
for (let j = 0; j < boardSize; j++){
const cell = createCell()
row.appendChild(cell)
}
board.appendChild(row)
if (boardSize === 7) growBtn.disabled = true
if (boardSize > 3) shrinkBtn.disabled = false
updatePlayerSizes()
}
const shrinkBoard = () => {
boardSize--
board.removeChild(board.lastChild)
board.childNodes.forEach(row => {
row.removeChild(row.lastChild)
})
if (boardSize === 3) shrinkBtn.disabled = true
if (boardSize < 7) growBtn.disabled = false
updatePlayerSizes()
}
const getResult = () => {
let noEmptyCellsExist = true
let checkHorizontalWinner = true
let prevHorizontal = null
let checkDiagonalWinner = [ true, true ]
let prevDiagonal = [ null, null ]
let checkVerticalWinner = []
let prevVertical = []
for (let i = 0; i < boardSize; i++){
checkVerticalWinner.push(true)
prevVertical.push(null)
}
for (let i = 0; i < boardSize; i++) {
checkHorizontalWinner = true
prevHorizontal = null
const row = board.children[i]
for (let j = 0; j < boardSize; j++) {
const cell = row.children[j]
if (!cell.isUsed)
noEmptyCellsExist = false
const cellPlayer = cell.innerHTML
if (j > 0) {
// Check for horizontal winner
if (checkHorizontalWinner){
if (cellPlayer && cellPlayer === prevHorizontal){
if (j === (boardSize - 1)){
return { status: 'GAME_OVER', winner: cellPlayer}
}
} else {
checkHorizontalWinner = false
}
}
}
if (i > 0){
// Check for diagonal winner
if (checkDiagonalWinner[0] && (j === i)) {
if (cellPlayer && cellPlayer === prevDiagonal[0]){
if (i === (boardSize - 1)){
return { status: 'GAME_OVER', winner: cellPlayer}
}
} else {
checkDiagonalWinner[0] = false
}
}
if (checkDiagonalWinner[1] && (j === (boardSize - i - 1))) {
if (cellPlayer && cellPlayer === prevDiagonal[1]){
if (i === (boardSize - 1)){
return { status: 'GAME_OVER', winner: cellPlayer}
}
} else {
checkDiagonalWinner[1] = false
}
}
// Check for Vertical winner
for (let vi = 0; vi < boardSize; vi++){
if (checkVerticalWinner[vi] && vi === j){
if (cellPlayer && cellPlayer === prevVertical[vi]){
if (i === (boardSize - 1)){
return { status: 'GAME_OVER', winner: cellPlayer}
}
} else {
checkVerticalWinner[vi] = false
}
}
}
}
// Update previous cellPlayer values
for (let vi = 0; vi < boardSize; vi++){
if (checkVerticalWinner[vi] && vi === j)
prevVertical[vi] = cellPlayer
}
if (checkDiagonalWinner[0] && (j === i)) prevDiagonal[0] = cellPlayer
if (checkDiagonalWinner[1] && (j === (boardSize - i - 1))) prevDiagonal[1] = cellPlayer
prevHorizontal = cellPlayer
}
}
if (noEmptyCellsExist) return { status: 'DRAW'}
}
const endGame = (endingMessage) => {
message.innerHTML = endingMessage
randomBtn.disabled = true
gameOver = true
}
const checkForWinner = () => {
const result = getResult()
if (result?.status === 'GAME_OVER')
endGame(result.winner + ' has won the game!')
else if (result?.status === 'DRAW')
endGame('It\'s a tie!')
}
const randomInt = (min, max) => {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}
const randomTurn = () => {
let availableCells = []
board.childNodes.forEach((row, i) => {
row.childNodes.forEach((cell, j) => {
if (!cell.isUsed)
availableCells.push([i, j])
})
})
const randomCell = randomInt(0, availableCells.length)
const randomCellRow = availableCells[randomCell][0]
const randomCellColumn = availableCells[randomCell][1]
board.childNodes[randomCellRow].childNodes[randomCellColumn].click()
}
for (let i = 0; i < INITIAL_BOARD_SIZE; i++){
const row = createRow()
for (let j = 0; j < INITIAL_BOARD_SIZE; j++){
const cell = createCell()
row.appendChild(cell)
}
board.appendChild(row)
}
updatePlayerSizes()
window.addEventListener('resize', updatePlayerSizes)
</script>
</html>