-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic-tac-toe-bot.js
84 lines (70 loc) · 2.33 KB
/
tic-tac-toe-bot.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
const board = document.getElementById('board');
const statusDisplay = document.getElementById('status');
const playAgainButton = document.getElementById('play-again');
let boardState = ["", "", "", "", "", "", "", "", ""];
let currentPlayer = "X";
let gameActive = true;
const winningConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
function handleCellClick(event) {
const clickedCell = event.target;
const clickedCellIndex = parseInt(clickedCell.getAttribute('data-index'));
if (boardState[clickedCellIndex] !== "" || !gameActive) {
return;
}
boardState[clickedCellIndex] = currentPlayer;
clickedCell.textContent = currentPlayer;
if (checkWin()) {
statusDisplay.textContent = `Player ${currentPlayer} wins!`;
gameActive = false;
return;
}
if (boardState.every(cell => cell !== "")) {
statusDisplay.textContent = "It's a draw!";
gameActive = false;
return;
}
currentPlayer = currentPlayer === "X" ? "O" : "X";
if (currentPlayer === "O") {
botMove();
}
}
function checkWin() {
return winningConditions.some(condition => {
return condition.every(index => boardState[index] === currentPlayer);
});
}
function botMove() {
let emptyCells = boardState.map((cell, index) => cell === "" ? index : null).filter(index => index !== null);
let randomIndex = emptyCells[Math.floor(Math.random() * emptyCells.length)];
boardState[randomIndex] = currentPlayer;
document.querySelector(`.cell[data-index="${randomIndex}"]`).textContent = currentPlayer;
if (checkWin()) {
statusDisplay.textContent = `Player ${currentPlayer} wins!`;
gameActive = false;
return;
}
if (boardState.every(cell => cell !== "")) {
statusDisplay.textContent = "It's a draw!";
gameActive = false;
return;
}
currentPlayer = currentPlayer === "X" ? "O" : "X";
}
function resetGame() {
boardState = ["", "", "", "", "", "", "", "", ""];
currentPlayer = "X";
gameActive = true;
statusDisplay.textContent = "";
document.querySelectorAll('.cell').forEach(cell => cell.textContent = "");
}
board.addEventListener('click', handleCellClick);
playAgainButton.addEventListener('click', resetGame);