-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
294 lines (253 loc) · 9.75 KB
/
Copy pathindex.html
File metadata and controls
294 lines (253 loc) · 9.75 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tic-Tac-Toe Game</title>
<!-- Load Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
<script>
tailwind.config = {
theme: {
extend: {
fontFamily: {
sans: ['Inter', 'sans-serif'],
},
colors: {
'primary-dark': '#0f172a', /* Slate 900 */
'secondary-teal': '#2dd4bf', /* Teal 400 */
'accent-red': '#f87171', /* Red 400 */
'accent-blue': '#60a5fa', /* Blue 400 */
'win-yellow': '#facc15', /* Amber 400 */
}
}
}
}
</script>
<style>
body {
background-color: #1e293b;
/* Slate 800 */
color: #f8fafc;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
padding: 20px;
font-family: 'Inter', sans-serif;
}
/* Styling for the game grid */
#game-board {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 8px;
width: 100%;
max-width: 400px;
/* Max size for a comfortable square board */
aspect-ratio: 1 / 1;
/* Ensures the board is always square */
}
.cell {
background-color: #334155;
/* Slate 700 */
display: flex;
justify-content: center;
align-items: center;
font-size: 3.5rem;
/* Large font size for X/O */
font-weight: bold;
cursor: pointer;
border-radius: 12px;
transition: background-color 0.2s, transform 0.1s;
user-select: none;
}
.cell:hover:not(.occupied) {
background-color: #475569;
/* Slate 600 on hover */
}
/* Specific colors for players */
.player-x {
color: #f87171;
/* Accent Red */
}
.player-o {
color: #60a5fa;
/* Accent Blue */
}
/* Styling for the winning cells */
.winning-cell {
background-color: #facc15 !important;
/* Win Yellow */
transform: scale(1.05);
}
/* Button styling */
#newGameBtn {
padding: 0.75rem 1.5rem;
border-radius: 9999px;
font-weight: bold;
text-transform: uppercase;
transition: all 0.2s ease-in-out;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
background-color: #2dd4bf;
/* Teal */
color: #0f172a;
/* Dark text */
}
#newGameBtn:hover {
background-color: #14b8a6;
/* Darker Teal */
transform: translateY(-1px);
}
</style>
</head>
<body>
<div
class="w-full max-w-md p-6 sm:p-8 rounded-2xl shadow-2xl bg-gray-900 border-2 border-secondary-teal text-center">
<h1 class="text-3xl font-extrabold mb-6 text-secondary-teal">Tic-Tac-Toe</h1>
<!-- Game Status Display -->
<div class="mb-6 h-8 flex justify-center items-center">
<p id="status" class="text-xl font-semibold text-white">Player X's Turn</p>
</div>
<!-- Game Board -->
<div id="game-board" class="mb-8 mx-auto">
<div class="cell" data-index="0"></div>
<div class="cell" data-index="1"></div>
<div class="cell" data-index="2"></div>
<div class="cell" data-index="3"></div>
<div class="cell" data-index="4"></div>
<div class="cell" data-index="5"></div>
<div class="cell" data-index="6"></div>
<div class="cell" data-index="7"></div>
<div class="cell" data-index="8"></div>
</div>
<!-- New Game Button -->
<button id="newGameBtn" class="w-full sm:w-auto">New Game</button>
</div>
<!-- Game Logic -->
<script>
// --- Game State Variables ---
// Represents the 9 squares of the board: 0-8. Null means empty.
let boardState = Array(9).fill(null);
let currentPlayer = 'X';
let isGameActive = true;
// Winning combinations (indices of the board array)
const winningConditions = [
[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
];
// --- DOM Elements ---
const statusDisplay = document.getElementById('status');
const gameBoard = document.getElementById('game-board');
const cells = document.querySelectorAll('.cell');
const newGameBtn = document.getElementById('newGameBtn');
// --- Core Functions ---
/**
* Updates the status message displayed to the user.
* @param {string} message - The message to display.
* @param {string} colorClass - Tailwind color class for the text.
*/
const updateStatus = (message, colorClass = 'text-white') => {
statusDisplay.textContent = message;
statusDisplay.className = `text-xl font-semibold ${colorClass}`; // Reset and apply new class
};
/**
* Handles a user click on a cell.
* @param {object} event - The click event object.
*/
const handleCellClick = (event) => {
const clickedCell = event.target;
const clickedCellIndex = parseInt(clickedCell.getAttribute('data-index'));
// Ignore click if the cell is already occupied or if the game is over
if (boardState[clickedCellIndex] !== null || !isGameActive) {
return;
}
// 1. Update the game state (model)
boardState[clickedCellIndex] = currentPlayer;
// 2. Update the UI (view)
clickedCell.textContent = currentPlayer;
clickedCell.classList.add('occupied', `player-${currentPlayer.toLowerCase()}`);
// 3. Check for Game End
const result = checkWin();
if (result.isWin) {
handleWin(result.winningCombination);
return;
}
// 4. Check for Draw
if (!boardState.includes(null)) {
handleDraw();
return;
}
// 5. Continue Game: switch player
switchPlayer();
};
/**
* Switches the current player from X to O or vice-versa.
*/
const switchPlayer = () => {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
const colorClass = currentPlayer === 'X' ? 'text-accent-red' : 'text-accent-blue';
updateStatus(`Player ${currentPlayer}'s Turn`, colorClass);
};
/**
* Checks if the current board state results in a win.
* @returns {{isWin: boolean, winningCombination: Array<number>|null}}
*/
const checkWin = () => {
for (let i = 0; i < winningConditions.length; i++) {
const [a, b, c] = winningConditions[i];
if (boardState[a] && boardState[a] === boardState[b] && boardState[a] === boardState[c]) {
// We have a winner!
return { isWin: true, winningCombination: [a, b, c] };
}
}
return { isWin: false, winningCombination: null };
};
/**
* Executes when a player wins.
* @param {Array<number>} winningCombination - The indices of the winning cells.
*/
const handleWin = (winningCombination) => {
isGameActive = false;
const colorClass = currentPlayer === 'X' ? 'text-accent-red' : 'text-accent-blue';
updateStatus(`Player ${currentPlayer} Wins!`, colorClass);
// Highlight the winning cells
winningCombination.forEach(index => {
cells[index].classList.add('winning-cell');
});
};
/**
* Executes when the game results in a draw.
*/
const handleDraw = () => {
isGameActive = false;
updateStatus("It's a Draw!", 'text-win-yellow');
};
/**
* Resets the entire game board and state.
*/
const resetGame = () => {
boardState = Array(9).fill(null);
currentPlayer = 'X';
isGameActive = true;
// Reset UI
cells.forEach(cell => {
cell.textContent = '';
cell.classList.remove('occupied', 'player-x', 'player-o', 'winning-cell');
});
// Reset Status
updateStatus("Player X's Turn", 'text-accent-red');
};
// --- Event Listeners and Initialization ---
// Add event listeners to all 9 cells
cells.forEach(cell => {
cell.addEventListener('click', handleCellClick);
});
// Add listener for the new game button
newGameBtn.addEventListener('click', resetGame);
// Initial setup
updateStatus("Player X's Turn", 'text-accent-red');
</script>
</body>
</html>