Repository navigation
Expand file tree
/
Copy pathgame.js
More file actions
66 lines (60 loc) · 1.79 KB
/
Copy pathgame.js
File metadata and controls
66 lines (60 loc) · 1.79 KB
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
'use strict';
const WINNING_COMBOS = [
[0, 1, 2], [3, 4, 5], [6, 7, 8], // rows
[0, 3, 6], [1, 4, 7], [2, 5, 8], // columns
[0, 4, 8], [2, 4, 6], // diagonals
];
/**
* Returns the initial game state.
*/
function createInitialState() {
return {
board: Array(9).fill(''),
current: 'X',
gameOver: false,
};
}
/**
* Returns the next player given the current one.
* @param {'X'|'O'} current
* @returns {'X'|'O'}
*/
function getNextPlayer(current) {
return current === 'X' ? 'O' : 'X';
}
/**
* Returns a new board with the move applied, or null if the move is invalid.
* @param {string[]} board
* @param {number} index 0-8
* @param {'X'|'O'} player
* @returns {string[]|null}
*/
function applyMove(board, index, player) {
if (index < 0 || index > 8) return null;
if (board[index] !== '') return null;
const next = board.slice();
next[index] = player;
return next;
}
/**
* Checks the board for a winner or draw.
* @param {string[]} board
* @returns {{ winner: string, combo: number[] }|{ winner: null, combo: [] }|null}
* - Object with winner ('X'|'O') and winning combo indices if someone won.
* - Object with winner null and empty combo if the board is full (draw).
* - null if the game is still in progress.
*/
function check(board) {
for (const combo of WINNING_COMBOS) {
const [a, b, c] = combo;
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
return { winner: board[a], combo };
}
}
if (board.every(cell => cell !== '')) return { winner: null, combo: [] };
return null;
}
// Allow require() in Node.js (Jest) while remaining a plain script in the browser.
if (typeof module !== 'undefined' && module.exports) {
module.exports = { WINNING_COMBOS, createInitialState, getNextPlayer, applyMove, check };
}