This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
394 lines (327 loc) · 9.77 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
'use strict'
const playerType = { human: 0, computer: 1 }
const boardSize = 3
const players = []
const stdin = process.stdin
const consoleOptions = {
blackFg: '\x1b[30m',
whiteBg: '\x1b[47m',
reset: '\x1b[0m',
cleanScreen: '\x1Bc'
}
const keys = {
enter: '\u000D',
arrowUp: '\u001B\u005B\u0041',
arrowRight: '\u001B\u005B\u0043',
arrowDown: '\u001B\u005B\u0042',
arrowLeft: '\u001B\u005B\u0044',
ctrlC: '\u0003'
}
stdin.setEncoding('utf8')
// Print a selection list
function printSelectionList (header, list, selectedOption) {
console.log(consoleOptions.cleanScreen)
console.log(header)
console.log('-'.repeat(process.stdout.columns))
for (let i = 0; i < list.length; i++) {
if (i === selectedOption) {
console.log(consoleOptions.blackFg +
consoleOptions.whiteBg +
list[selectedOption] +
consoleOptions.reset)
} else {
console.log(list[i])
}
}
}
// Get the selected option from a list
function selectionList (header, list) {
return new Promise((resolve) => {
let selectedOption = 0
stdin.setRawMode(true)
stdin.resume()
stdin.on('data', (data) => {
switch (data) {
// Enter
case keys.enter:
stdin.setRawMode(false)
stdin.pause()
stdin.removeAllListeners('data')
resolve(selectedOption)
break
// Up
case keys.arrowUp:
selectedOption--
break
// Down
case keys.arrowDown:
selectedOption++
break
// Ctrl + C
case keys.ctrlC:
process.exit()
}
if (selectedOption >= list.length) selectedOption = 0
else if (selectedOption < 0) selectedOption = list.length - 1
printSelectionList(header, list, selectedOption)
})
printSelectionList(header, list, selectedOption)
})
}
// Print a question and return the answer
function ask (question, errorMessage = null) {
return new Promise((resolve) => {
if (errorMessage !== null) {
console.log(errorMessage)
}
console.log(question)
stdin.resume()
stdin.on('data', (data) => {
stdin.removeAllListeners('data')
stdin.pause()
resolve(data.replace(/(\r)?\n$/, ''))
})
})
}
// Print a board and highlight a cell if indicated
function printBoard (board, { x, y } = { x: null, y: null }) {
console.log(consoleOptions.cleanScreen)
console.log('-------------')
for (let i = 0; i < boardSize; i++) {
let row = ''
for (let j = 0; j < boardSize; j++) {
row += '|'
// Selected cell
if (x === j && y === i) {
row += consoleOptions.whiteBg // Set white background
row += consoleOptions.blackFg // Set black font
row += ` ${(board[i][j] !== null ? players[board[i][j]].symbol : '_')} `
row += consoleOptions.reset // Reset colors
} else {
row += ` ${(board[i][j] !== null ? players[board[i][j]].symbol : ' ')} `
}
}
row += '|'
console.log(row)
console.log('-------------')
}
}
// Return an empty board
function generateBoard () {
const board = []
for (let i = 0; i < boardSize; i++) {
const row = []
for (let j = 0; j < boardSize; j++) {
row.push(null)
}
board.push(row)
}
return board
}
// Return an array with the valid moves coordinates
function getValidMoves (board) {
const validMoves = []
for (let i = 0; i < boardSize; i++) {
for (let j = 0; j < boardSize; j++) {
if (board[i][j] === null) validMoves.push({ x: j, y: i })
}
}
return validMoves
}
// Get the winner if there is one or return -1
function getWinner (board) {
const regex = new RegExp('^(.)\\1{' + (boardSize - 1) + '}$')
// Check rows
for (let i = 0; i < boardSize; i++) {
if (board[i].join('').match(regex)) return board[i][0]
}
// Check columns
for (let i = 0; i < boardSize; i++) {
let column = []
for (let j = 0; j < boardSize; j++) {
column.push(board[j][i])
}
if (column.join('').match(regex)) return column[0]
}
// Check diagonal and anti-diagonal
let diagonal = []
let antiDiagonal = []
for (let i = 0; i < boardSize; i++) {
for (let j = 0; j < boardSize; j++) {
if (i === j) diagonal.push(board[i][j])
if (i === boardSize - j - 1) antiDiagonal.push(board[i][j])
}
}
if (regex.test(diagonal.join(''))) return diagonal[0]
else if (regex.test(antiDiagonal.join(''))) return antiDiagonal[0]
return -1
}
// Minimax algorithm to get the best move
function minimax (board, player, maximizing = true, depth = 0) {
const winner = getWinner(board)
const availableMoves = getValidMoves(board)
if (winner !== -1) {
if ((maximizing && winner === player) || (!maximizing && winner !== player)) {
return { value: 100 - depth, move: null }
} else {
return { value: -100 + depth, move: null }
}
} else if (winner === -1 && availableMoves.length === 0) {
return { value: 0, move: null }
}
let bestValue = null
let bestMove = null
for (let i = 0; i < availableMoves.length; i++) {
const move = availableMoves[i]
let nextPlayer = player === 0 ? 1 : 0
let childBoard = board.map((row) => [...row])
childBoard[move.y][move.x] = player
let { value } = minimax(childBoard, nextPlayer, !maximizing, depth + 1)
if (bestValue === null || (bestValue < value && maximizing) || (bestValue > value && !maximizing)) {
bestValue = value
bestMove = move
}
}
return { value: bestValue, move: bestMove }
}
// Request to select a free for the player
function humanTurn (board, player) {
return new Promise((resolve) => {
const position = {
x: 0,
y: 0
}
stdin.setRawMode(true)
stdin.resume()
stdin.on('data', (key) => {
let showErrorMessage = false
console.log(consoleOptions.cleanScreen)
switch (key) {
// Enter
case keys.enter:
if (board[position.y][position.x] === null) {
stdin.setRawMode(false)
stdin.pause()
stdin.removeAllListeners('data')
resolve(position)
} else {
showErrorMessage = true
}
break
// Up
case keys.arrowUp:
position.y--
break
// Right
case keys.arrowRight:
position.x++
break
// Down
case keys.arrowDown:
position.y++
break
// Left
case keys.arrowLeft:
position.x--
break
// Ctrl + C
case keys.ctrlC:
process.exit()
}
if (position.y >= boardSize) position.y = 0
else if (position.y < 0) position.y = boardSize - 1
if (position.x >= boardSize) position.x = 0
else if (position.x < 0) position.x = boardSize - 1
printBoard(board, position)
console.log(`${players[player].name} (${players[player].symbol}) turn`)
if (showErrorMessage) console.log('That space is not free!')
})
printBoard(board, position)
console.log(`${players[player].name} (${players[player].symbol}) turn`)
})
}
// Initialize minimax function
function computerTurn (board, player) {
printBoard(board)
console.log(`${players[player].name} (${players[player].symbol}) turn`)
return new Promise((resolve, reject) => {
let result = minimax(board, player)
setTimeout(() => { // Visual timeout
resolve(result.move)
}, 500)
})
}
// Main function, start the game
async function game () {
const board = generateBoard()
// Main menu
const menuOptions = [
'Human vs human',
'Human vs computer',
'Computer vs computer'
]
const menuHeader = 'Welcome to TIC TAC TOE! You can use the arrow keys and [ENTER] to choose the desired option. Good luck and have fun!'
let gameMode = await selectionList(menuHeader, menuOptions)
switch (gameMode) {
// Human vs human
case 0:
players.push({ name: 'Player 1', symbol: null, type: playerType.human })
players.push({ name: 'Player 2', symbol: null, type: playerType.human })
break
// Human vs computer
case 1:
players.push({ name: 'Player', symbol: null, type: playerType.human })
players.push({ name: 'Computer', symbol: null, type: playerType.computer })
break
// Computer vs computer
case 2:
players.push({ name: 'Computer 1', symbol: null, type: playerType.computer })
players.push({ name: 'Computer 2', symbol: null, type: playerType.computer })
break
}
console.log(consoleOptions.cleanScreen)
// Ask players symbol
for (let i = 0; i < players.length; i++) {
let question = `${players[i].name} symbol?`
let symbol = null
do {
symbol = await ask(question)
console.log(consoleOptions.cleanScreen)
if (symbol.length !== 1) {
console.log('The symbol must have a single character')
}
if (players.findIndex((player) => player.symbol === symbol) !== -1) {
console.log('That symbol is already in use')
}
} while (symbol.length !== 1 || players.findIndex((player) => player.symbol === symbol) !== -1)
players[i].symbol = symbol
}
// Ask who starts
const header = 'Which player will start the game?'
let turn = await selectionList(header, players.map((player) =>
`${player.name} (${player.symbol})`))
// Game
let winner
while ((winner = getWinner(board)) === -1 && getValidMoves(board).length > 0) {
let position
if (players[turn].type === playerType.human) {
position = await humanTurn(board, turn)
} else {
position = await computerTurn(board, turn)
}
board[position.y][position.x] = turn
turn++
printBoard(board)
if (turn >= players.length) {
turn = 0
}
}
printBoard(board)
// Print game result
if (winner === -1) {
console.log('Draw')
} else {
console.log(`${players[winner].name} wins`)
}
}
game()