forked from underhack004/sample-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
87 lines (78 loc) · 2.23 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
// Function to create the game board
function createBoard() {
return [
['', '', ''],
['', '', ''],
['', '', '']
];
}
// Function to print the game board
function printBoard(board) {
console.log(' 0 1 2');
for (let i = 0; i < 3; i++) {
let row = i + ' ';
for (let j = 0; j < 3; j++) {
row += board[i][j] + ' ';
}
console.log(row);
}
}
// Function to check if there's a winner
function checkWinner(board, player) {
// Check rows and columns
for (let i = 0; i < 3; i++) {
if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
return true;
}
if (board[0][i] === player && board[1][i] === player && board[2][i] === player) {
return true;
}
}
// Check diagonals
if ((board[0][0] === player && board[1][1] === player && board[2][2] === player) ||
(board[0][2] === player && board[1][1] === player && board[2][0] === player)) {
return true;
}
return false;
}
// Function to check if the board is full (tie)
function checkTie(board) {
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] === '') {
return false;
}
}
}
return true;
}
// Function to play the game
function playGame() {
let board = createBoard();
let currentPlayer = 'X';
let winner = null;
while (!winner && !checkTie(board)) {
printBoard(board);
console.log(`Player ${currentPlayer}'s turn`);
let row = prompt('Enter row (0, 1, or 2): ');
let col = prompt('Enter column (0, 1, or 2): ');
if (board[row][col] === '') {
board[row][col] = currentPlayer;
if (checkWinner(board, currentPlayer)) {
winner = currentPlayer;
} else {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
}
} else {
console.log('That cell is already taken. Try again.');
}
}
printBoard(board);
if (winner) {
console.log(`Congratulations! Player ${winner} wins!`);
} else {
console.log('It\'s a tie!');
}
}
// Start the game
playGame();