-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.js
More file actions
61 lines (55 loc) · 1.59 KB
/
tictactoe.js
File metadata and controls
61 lines (55 loc) · 1.59 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
class TicTacToe {
constructor() {
this.board = Array(9).fill(null);
this.currentPlayer = 'X';
this.winner = null;
}
printBoard() {
const b = this.board;
console.log(
`${b[0] || ' '} | ${b[1] || ' '} | ${b[2] || ' '}\n` +
`---------\n` +
`${b[3] || ' '} | ${b[4] || ' '} | ${b[5] || ' '}\n` +
`---------\n` +
`${b[6] || ' '} | ${b[7] || ' '} | ${b[8] || ' '}\n`
);
}
makeMove(position) {
if (this.winner || this.board[position] !== null) {
return false;
}
this.board[position] = this.currentPlayer;
this.winner = this.checkWinner();
if (!this.winner) {
this.currentPlayer = this.currentPlayer === 'X' ? 'O' : 'X';
}
return true;
}
checkWinner() {
const b = this.board;
const lines = [
[0,1,2],[3,4,5],[6,7,8], // rows
[0,3,6],[1,4,7],[2,5,8], // cols
[0,4,8],[2,4,6] // diags
];
for (const [a, bIdx, c] of lines) {
if (b[a] && b[a] === b[bIdx] && b[a] === b[c]) {
return b[a];
}
}
if (b.every(cell => cell)) return 'Draw';
return null;
}
}
// Example usage:
const game = new TicTacToe();
game.printBoard();
// To make a move: game.makeMove(position); // position: 0-8
// Example moves:
game.makeMove(0); // X
game.makeMove(4); // O
game.makeMove(1); // X
game.makeMove(5); // O
game.makeMove(2); // X wins
game.printBoard();
console.log('Winner:', game.winner);