-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht10.html
264 lines (222 loc) · 8.44 KB
/
t10.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
258
259
260
261
262
263
<!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</title>
<style>
/* Add your CSS styles here updating comments*/
/* Animated background */
body {
background: linear-gradient(45deg, #f06, #9f6, #06f, #f06);
background-size: 400% 400%;
animation: gradientAnimation 15s ease infinite;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
}
@keyframes gradientAnimation {
0% {
background-position: 0% 50%;
}
50% {
background-position: 100% 50%;
}
100% {
background-position: 0% 50%;
}
}
.container {
text-align: center;
}
.board {
display: grid;
grid-template-columns: repeat(3, 100px);
gap: 5px;
}
.cell {
width: 100px;
height: 100px;
font-size: 36px; /* Increased font size for X and O */
text-align: center;
vertical-align: middle;
border: 1px solid #ccc;
display: flex;
justify-content: center; /* Center contents horizontally */
align-items: center; /* Center contents vertically */
animation: colorChange 3s infinite; /* Continuous color change animation */
}
/* Apply styles to the cell and its children (X or O) when hovering */
.cell:hover {
background-color: #f8f8f8; /* Change background color on hover */
transition: background-color 0.3s; /* Smoothly transition background color */
/* Change color of X or O on hover */
color: #ff5733; /* Change text color to a different color on hover */
transition: color 0.3s; /* Smoothly transition text color */
}
/* Style player name labels */
label {
font-size: 18px;
color: #333; /* Dark gray text color */
margin-right: 10px; /* Add some spacing between labels and input fields */
}
/* Style player name input fields */
input[type="text"] {
padding: 8px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 5px;
width: 200px; /* Adjust the width as needed */
}
/* Style the dropdown for choosing the first player */
select {
padding: 8px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 5px;
width: 100px; /* Adjust the width as needed */
}
/* Style the "Start Game" button */
button {
padding: 10px 20px;
font-size: 18px;
background-color: #333; /* Dark gray background color */
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
margin-top: 10px; /* Add some spacing between button and input fields */
}
/* Add hover effect to the button */
button:hover {
background-color: #444; /* Slightly darker gray on hover */
}
</style>
</head>
<body>
<div class="container">
<fieldset><legend><h1><i>Tic Tac Toe</i></h1></legend>
<div>
<label for="player1">Enter Player 1 (X): </label>
<input type="text" id="player1" placeholder="Player 1">
<br>
<br>
<label for="player2">Enter Player 2 (O): </label>
<input type="text" id="player2" placeholder="Player 2">
<br>
<br>
<label for="firstPlayer">Choose who goes first: </label>
<select id="firstPlayer">
<option value="X">X</option>
<option value="O">O</option>
</select>
<br>
<button onclick="startGame()">Start Game</button>
</div>
</fieldset>
<div id="game" style="display: none;">
<h2>Game Number: <span id="gameNumber">1</span></h2>
<h2>Current Turn: <span id="currentPlayer">X</span></h2>
<div class="board" id="board"></div>
</div>
<div id="result" style="display: none;">
<h2 id="resultMessage">Game Over!</h2>
<p><span id="winner"></span></p>
<button onclick="startNewGame()">New Game</button>
</div>
</div>
<!-- Audio for sound effect -->
<audio id="soundEffect" src="sound effect.mp3"></audio>
<script>
let currentPlayer;
let player1;
let player2;
let board;
let gameover = false;
let gameNumber = 1;
function startGame() {
player1 = document.getElementById('player1').value;
player2 = document.getElementById('player2').value;
currentPlayer = document.getElementById('firstPlayer').value;
// Show game board
document.getElementById('game').style.display = 'block';
// Update the game number
document.getElementById('gameNumber').textContent = gameNumber;
board = Array(9).fill('');
renderBoard();
}
function renderBoard() {
const boardElement = document.getElementById('board');
boardElement.innerHTML = '';
for (let i = 0; i < 9; i++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.textContent = board[i];
cell.addEventListener('click', () => cellClick(i));
boardElement.appendChild(cell);
}
}
function cellClick(index) {
if (!gameover && board[index] === '') {
// Play the sound effect when a cell is clicked
const soundEffect = document.getElementById('soundEffect');
soundEffect.play();
board[index] = currentPlayer;
renderBoard();
if (checkWinner()) {
announceWinner();
} else {
currentPlayer = (currentPlayer === 'X') ? 'O' : 'X';
document.getElementById('currentPlayer').textContent = currentPlayer;
}
}
}
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]) {
return true;
}
}
if (!board.includes('')) {
gameover = true;
return true; // It's a draw
}
return false;
}
function announceWinner() {
document.getElementById('result').style.display = 'block';
const winnerElement = document.getElementById('winner');
const resultMessageElement = document.getElementById('resultMessage');
if (checkWinner() && currentPlayer === 'X') {
winnerElement.textContent = player1 + ' wins!';
resultMessageElement.textContent = 'Game Over!';
} else if (checkWinner() && currentPlayer === 'O') {
winnerElement.textContent = player2 + ' wins!';
resultMessageElement.textContent = 'Game Over!';
} else {
winnerElement.textContent = 'It\'s a draw!';
resultMessageElement.textContent = 'Game Over!';
}
gameover = true;
}
function startNewGame() {
gameNumber++;
currentPlayer = document.getElementById('firstPlayer').value;
board = Array(9).fill('');
gameover = false;
renderBoard();
document.getElementById('result').style.display = 'none';
document.getElementById('currentPlayer').textContent = currentPlayer;
}
</script>
</body>
</html>