-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTic-Tac-Toe.html
257 lines (231 loc) · 7.86 KB
/
Tic-Tac-Toe.html
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="data:image/png;base64,AAABAAEAEBACAAEAAQCwAAAAFgAAACgAAAAQAAAAIAAAAAEAAQAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA">
<title>Tic-Tac-Toe</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #2f2f2f;
color: white;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
text-align: center;
}
.container {
background-color: #444;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
width: 400px;
display: flex;
flex-direction: column;
align-items: center;
}
h1 {
color: #fff;
font-size: 24px;
margin-bottom: 20px;
}
.board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 5px;
margin: 20px auto;
}
.cell {
width: 100px;
height: 100px;
text-align: center;
line-height: 100px;
font-size: 36px;
border: 2px solid #666;
background-color: #333;
cursor: pointer;
border-radius: 8px;
transition: background-color 0.3s ease, transform 0.2s;
}
.cell:hover {
background-color: #555;
transform: scale(1.05);
}
.cell.taken {
cursor: not-allowed;
background-color: #555;
}
.message {
text-align: center;
font-size: 20px;
color: #fff;
margin-top: 20px;
}
.btn {
padding: 10px 20px;
font-size: 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
margin-top: 20px;
}
.btn:hover {
background-color: #45a049;
}
.btn:focus {
outline: none;
}
.reset-btn {
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>AI Tic-Tac-Toe</h1>
<div id="board" class="board"></div>
<div id="message" class="message"></div>
<button class="btn reset-btn" onclick="resetGame()">Reset Game</button>
</div>
<script>
const board = document.getElementById('board');
const message = document.getElementById('message');
const playerSymbol = 'X';
const aiSymbol = 'O';
let currentBoard = [
['', '', ''],
['', '', ''],
['', '', '']
];
let isGameOver = false;
// Create the board UI
function createBoard() {
board.innerHTML = '';
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 3; col++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.addEventListener('click', () => makeMove(row, col));
cell.dataset.row = row;
cell.dataset.col = col;
board.appendChild(cell);
}
}
}
// Handle player's move
function makeMove(row, col) {
if (currentBoard[row][col] !== '' || isGameOver) return;
currentBoard[row][col] = playerSymbol;
renderBoard();
if (checkWinner(playerSymbol)) {
message.textContent = 'You win!';
isGameOver = true;
return;
}
aiMove();
}
// AI makes its move
function aiMove() {
const bestMove = getBestMove();
currentBoard[bestMove.row][bestMove.col] = aiSymbol;
renderBoard();
if (checkWinner(aiSymbol)) {
message.textContent = 'AI wins!';
isGameOver = true;
}
}
// Get the best move for AI using the minimax algorithm
function getBestMove() {
let bestScore = -Infinity;
let move;
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 3; col++) {
if (currentBoard[row][col] === '') {
currentBoard[row][col] = aiSymbol;
const score = minimax(currentBoard, false);
currentBoard[row][col] = '';
if (score > bestScore) {
bestScore = score;
move = { row, col };
}
}
}
}
return move;
}
// Minimax algorithm to evaluate the board
function minimax(board, isMaximizing) {
if (checkWinner(aiSymbol)) return 1;
if (checkWinner(playerSymbol)) return -1;
if (isBoardFull()) return 0;
let bestScore = isMaximizing ? -Infinity : Infinity;
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 3; col++) {
if (board[row][col] === '') {
board[row][col] = isMaximizing ? aiSymbol : playerSymbol;
const score = minimax(board, !isMaximizing);
board[row][col] = '';
bestScore = isMaximizing ? Math.max(score, bestScore) : Math.min(score, bestScore);
}
}
}
return bestScore;
}
// Check if a player has won
function checkWinner(symbol) {
for (let i = 0; i < 3; i++) {
if (currentBoard[i][0] === symbol && currentBoard[i][1] === symbol && currentBoard[i][2] === symbol) return true;
if (currentBoard[0][i] === symbol && currentBoard[1][i] === symbol && currentBoard[2][i] === symbol) return true;
}
if (currentBoard[0][0] === symbol && currentBoard[1][1] === symbol && currentBoard[2][2] === symbol) return true;
if (currentBoard[0][2] === symbol && currentBoard[1][1] === symbol && currentBoard[2][0] === symbol) return true;
return false;
}
// Check if the board is full (tie condition)
function isBoardFull() {
return currentBoard.every(row => row.every(cell => cell !== ''));
}
// Render the board
function renderBoard() {
const cells = board.getElementsByClassName('cell');
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 3; col++) {
const index = row * 3 + col;
const cell = cells[index];
cell.textContent = currentBoard[row][col];
if (currentBoard[row][col] !== '') {
cell.classList.add('taken');
} else {
cell.classList.remove('taken');
}
}
}
if (!isGameOver && !isBoardFull()) {
message.textContent = 'Your turn!';
} else if (isBoardFull()) {
message.textContent = 'It\'s a tie!';
}
}
// Reset the game
function resetGame() {
currentBoard = [
['', '', ''],
['', '', ''],
['', '', '']
];
isGameOver = false;
message.textContent = 'Your turn!';
renderBoard();
}
// Initialize the game
createBoard();
</script>
</body>
</html>