-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
170 lines (149 loc) · 4.95 KB
/
script.js
File metadata and controls
170 lines (149 loc) · 4.95 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
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
const cells = document.querySelectorAll('.cell');
const restartBtn = document.getElementById('restart-btn');
const homeBtn = document.getElementById('home-btn');
const playerVsPlayerBtn = document.getElementById('player-vs-player');
const playerVsComputerBtn = document.getElementById('player-vs-computer');
const homePage = document.getElementById('home-page');
const gamePage = document.getElementById('game-page');
const messageBox = document.getElementById('message-box');
let currentPlayer = 'X';
let board = ['', '', '', '', '', '', '', '', ''];
let isGameActive = true;
let isPlayerVsComputer = false;
function showGamePage() {
homePage.style.display = 'none';
gamePage.style.display = 'block';
}
function showHomePage() {
homePage.style.display = 'flex';
gamePage.style.display = 'none';
}
function handleClick(event) {
const cellIndex = event.target.getAttribute('data-index');
if (board[cellIndex] !== '' || !isGameActive) return;
board[cellIndex] = currentPlayer;
event.target.textContent = currentPlayer;
checkWinner();
if (isPlayerVsComputer && isGameActive) setTimeout(computerMove, 500);
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
}
function checkWinner() {
const winPatterns = [
[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
];
for (const pattern of winPatterns) {
const [a, b, c] = pattern;
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
animateWin(pattern);
showMessage(`${board[a]} wins!`);
isGameActive = false;
return;
}
}
if (!board.includes('')) {
showMessage("It's a tie!");
isGameActive = false;
}
}
function showMessage(message) {
messageBox.textContent = message;
messageBox.classList.add('show');
setTimeout(() => {
messageBox.classList.remove('show');
}, 3000);
}
function animateWin(pattern) {
pattern.forEach(index => {
cells[index].classList.add('winner');
});
setTimeout(() => {
cells.forEach(cell => cell.classList.remove('winner'));
}, 2000);
}
function computerMove() {
const bestMove = findBestMove();
board[bestMove] = 'O';
cells[bestMove].textContent = 'O';
checkWinner();
currentPlayer = 'X';
}
function findBestMove() {
const availableCells = board.map((val, index) => val === '' ? index : null).filter(val => val !== null);
let bestScore = -Infinity;
let move;
for (const cell of availableCells) {
board[cell] = 'O';
const score = minimax(board, 0, false);
board[cell] = '';
if (score > bestScore) {
bestScore = score;
move = cell;
}
}
return move;
}
function minimax(board, depth, isMaximizing) {
const winner = checkWinnerMinimax(board);
if (winner === 'O') return 10 - depth;
if (winner === 'X') return depth - 10;
if (!board.includes('')) return 0;
if (isMaximizing) {
let bestScore = -Infinity;
for (let i = 0; i < board.length; i++) {
if (board[i] === '') {
board[i] = 'O';
const score = minimax(board, depth + 1, false);
board[i] = '';
bestScore = Math.max(score, bestScore);
}
}
return bestScore;
} else {
let bestScore = Infinity;
for (let i = 0; i < board.length; i++) {
if (board[i] === '') {
board[i] = 'X';
const score = minimax(board, depth + 1, true);
board[i] = '';
bestScore = Math.min(score, bestScore);
}
}
return bestScore;
}
}
function checkWinnerMinimax(board) {
const winPatterns = [
[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
];
for (const pattern of winPatterns) {
const [a, b, c] = pattern;
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
return board[a];
}
}
return null;
}
function restartGame() {
board = ['', '', '', '', '', '', '', '', ''];
isGameActive = true;
currentPlayer = 'X';
cells.forEach(cell => {
cell.textContent = '';
cell.classList.remove('winner');
});
}
playerVsPlayerBtn.addEventListener('click', () => {
isPlayerVsComputer = false;
showGamePage();
});
playerVsComputerBtn.addEventListener('click', () => {
isPlayerVsComputer = true;
showGamePage();
});
homeBtn.addEventListener('click', showHomePage);
cells.forEach(cell => cell.addEventListener('click', handleClick));
restartBtn.addEventListener('click', restartGame);