-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
175 lines (149 loc) · 4.64 KB
/
scripts.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
// Global variables
let board, cells, status, resetBtn, playerScoreElement, aiScoreElement;
let currentPlayer = 'X';
let gameState = ['', '', '', '', '', '', '', '', ''];
let gameActive = true;
let playerScore = 0;
let aiScore = 0;
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]
];
// Functions
function handleCellClick(clickedCellEvent) {
const clickedCell = clickedCellEvent.target;
const clickedCellIndex = parseInt(clickedCell.getAttribute('data-index'));
if (gameState[clickedCellIndex] !== '' || !gameActive) {
return;
}
updateCell(clickedCell, clickedCellIndex);
checkWin();
}
function updateCell(clickedCell, clickedCellIndex) {
gameState[clickedCellIndex] = currentPlayer;
clickedCell.classList.add(currentPlayer.toLowerCase());
}
function checkWin() {
let roundWon = false;
for (let i = 0; i < winningConditions.length; i++) {
const winCondition = winningConditions[i];
let a = gameState[winCondition[0]];
let b = gameState[winCondition[1]];
let c = gameState[winCondition[2]];
if (a === '' || b === '' || c === '') {
continue;
}
if (a === b && b === c) {
roundWon = true;
break;
}
}
if (roundWon) {
status.textContent = `Player ${currentPlayer} has won!`;
gameActive = false;
updateScore();
return;
}
let roundDraw = !gameState.includes('');
if (roundDraw) {
status.textContent = 'Game ended in a draw!';
gameActive = false;
return;
}
changePlayer();
}
function changePlayer() {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
status.textContent = `Player ${currentPlayer}'s turn`;
if (currentPlayer === 'O') {
setTimeout(aiMove, 500);
}
}
function updateScore() {
if (currentPlayer === 'X') {
playerScore++;
playerScoreElement.textContent = playerScore;
} else {
aiScore++;
aiScoreElement.textContent = aiScore;
}
}
function aiMove() {
const bestMove = findBestMove();
const cell = cells[bestMove];
updateCell(cell, bestMove);
checkWin();
}
function findBestMove() {
// Check for winning move
for (let i = 0; i < 9; i++) {
if (gameState[i] === '') {
gameState[i] = 'O';
if (checkWinningMove('O')) {
gameState[i] = '';
return i;
}
gameState[i] = '';
}
}
// Check for blocking player's winning move
for (let i = 0; i < 9; i++) {
if (gameState[i] === '') {
gameState[i] = 'X';
if (checkWinningMove('X')) {
gameState[i] = '';
return i;
}
gameState[i] = '';
}
}
// Take center if available
if (gameState[4] === '') {
return 4;
}
// Take corners if available
const corners = [0, 2, 6, 8];
const availableCorners = corners.filter(i => gameState[i] === '');
if (availableCorners.length > 0) {
return availableCorners[Math.floor(Math.random() * availableCorners.length)];
}
// Take any available edge
const edges = [1, 3, 5, 7];
const availableEdges = edges.filter(i => gameState[i] === '');
if (availableEdges.length > 0) {
return availableEdges[Math.floor(Math.random() * availableEdges.length)];
}
// If we get here, there are no moves available (shouldn't happen)
return -1;
}
function checkWinningMove(player) {
for (let i = 0; i < winningConditions.length; i++) {
const [a, b, c] = winningConditions[i];
if (gameState[a] === player && gameState[b] === player && gameState[c] === player) {
return true;
}
}
return false;
}
function resetGame() {
gameState = ['', '', '', '', '', '', '', '', ''];
gameActive = true;
currentPlayer = 'X';
cells.forEach(cell => {
cell.classList.remove('x', 'o');
});
status.textContent = `Player ${currentPlayer}'s turn`;
}
// Initialization
document.addEventListener('DOMContentLoaded', () => {
board = document.getElementById('board');
cells = document.querySelectorAll('.cell');
status = document.getElementById('status');
resetBtn = document.getElementById('resetBtn');
playerScoreElement = document.getElementById('playerScore');
aiScoreElement = document.getElementById('aiScore');
cells.forEach(cell => cell.addEventListener('click', handleCellClick));
resetBtn.addEventListener('click', resetGame);
status.textContent = `Player ${currentPlayer}'s turn`;
});