forked from vishal5251/School-Website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.html
More file actions
269 lines (239 loc) · 7.66 KB
/
game.html
File metadata and controls
269 lines (239 loc) · 7.66 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta name="description" content="Tic Tac Toe Game with html, css and js design.">
<title>Tic Tac Toe Game</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: linear-gradient(135deg, #6c9ef5, #5c66f9);
font-family: 'Roboto', sans-serif;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
color: #fff;
overflow: hidden;
user-select: none;
flex-direction: column;
}
#gameContainer {
text-align: center;
border-radius: 15px;
box-shadow: 0 5px 25px rgba(0, 0, 0, 0.5);
background: rgba(0, 0, 0, 0.7);
padding: 30px;
width: 100%;
max-width: 400px; /* Adjust the width for smaller screens */
position: relative;
margin-bottom: 30px;
display: flex;
flex-direction: column;
align-items: center;
}
h1 {
font-size: 2.5em;
margin-bottom: 20px;
color: #fff;
letter-spacing: 1px;
text-shadow: 2px 2px 5px rgba(0, 0, 0, 0.5);
}
#currentPlayerLabel {
font-size: 1.2em;
font-weight: 500;
margin-bottom: 20px;
color: #fff;
font-family: 'Arial', sans-serif;
}
.board {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 10px;
margin-bottom: 20px;
width: 100%;
max-width: 360px; /* Ensure it fits within the game container */
aspect-ratio: 1; /* Ensure the grid is square */
}
.cell {
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.4);
border: 2px solid #fff;
display: flex;
justify-content: center;
align-items: center;
font-size: 3em;
font-weight: bold;
color: #fff;
cursor: pointer;
border-radius: 10px;
transition: background-color 0.3s ease, transform 0.2s ease;
}
.cell:hover {
background-color: rgba(255, 255, 255, 0.6);
transform: scale(1.1);
}
.cell.X {
color: #f6c90e;
font-family: 'Comic Sans MS', cursive, sans-serif;
}
.cell.O {
color: #ff595e;
font-family: 'Courier New', Courier, monospace;
}
.resetBtn {
margin-top: 20px;
padding: 12px 25px;
font-size: 1.2em;
background: #4CAF50;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s ease;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}
.resetBtn:hover {
background: #45a049;
}
/* Popup styles */
.popup {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.8);
padding: 30px;
border-radius: 15px;
box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
color: #fff;
text-align: center;
width: 320px;
animation: popupAnimation 0.5s ease;
}
@keyframes popupAnimation {
0% { transform: translate(-50%, -60%); }
100% { transform: translate(-50%, -50%); }
}
.popup h2 {
font-size: 2.2em;
margin-bottom: 20px;
font-weight: 600;
text-shadow: 1px 1px 5px rgba(0, 0, 0, 0.7);
}
.popup p {
font-size: 1.3em;
margin-bottom: 20px;
}
.popup button {
padding: 10px 20px;
background: #FF4C4C;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1.2em;
}
.popup button:hover {
background: #e54343;
}
</style>
</head>
<body>
<!-- Main Game Container -->
<div id="gameContainer">
<h1>Tic Tac Toe</h1>
<div id="currentPlayerLabel">Player X's Turn</div>
<div class="board" id="board">
<!-- Cells will be added dynamically -->
</div>
<button class="resetBtn" onclick="resetGame()">Restart Game</button>
</div>
<!-- Popup -->
<div class="popup" id="popup">
<h2>Game Over!</h2>
<p id="popupMessage"></p>
<button onclick="closePopup()">Close</button>
</div>
<script>
let currentPlayer = 'X';
let gameBoard = ['', '', '', '', '', '', '', '', ''];
let isGameOver = false;
let popup = document.getElementById('popup');
let popupMessage = document.getElementById('popupMessage');
let currentPlayerLabel = document.getElementById('currentPlayerLabel');
const boardElement = document.getElementById('board');
// Function to create the game board
function createBoard() {
boardElement.innerHTML = '';
gameBoard.forEach((cell, index) => {
const cellElement = document.createElement('div');
cellElement.classList.add('cell');
cellElement.dataset.index = index;
cellElement.textContent = cell;
cellElement.addEventListener('click', () => handleClick(index));
boardElement.appendChild(cellElement);
});
}
// Handle player click on a cell
function handleClick(index) {
if (isGameOver || gameBoard[index]) return;
gameBoard[index] = currentPlayer;
document.querySelector(`[data-index="${index}"]`).classList.add(currentPlayer);
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
currentPlayerLabel.textContent = `Player ${currentPlayer}'s Turn`;
createBoard();
checkWinner();
}
// Check if a player wins or if it's a draw
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
];
winPatterns.forEach(pattern => {
const [a, b, c] = pattern;
if (gameBoard[a] && gameBoard[a] === gameBoard[b] && gameBoard[a] === gameBoard[c]) {
isGameOver = true;
showPopup(`${gameBoard[a]} Wins! 🎉`);
}
});
if (!gameBoard.includes('') && !isGameOver) {
isGameOver = true;
showPopup('It\'s a Draw! 🤔');
}
}
// Show the popup with the result
function showPopup(message) {
popupMessage.textContent = message;
popup.style.display = 'block';
}
// Close the popup and reset the game
function closePopup() {
popup.style.display = 'none';
resetGame();
}
// Reset the game to start a new round
function resetGame() {
gameBoard = ['', '', '', '', '', '', '', '', ''];
currentPlayer = 'X';
isGameOver = false;
currentPlayerLabel.textContent = `Player X's Turn`;
createBoard();
}
// Initialize the game board when the page loads
window.onload = function() {
createBoard();
};
</script>
</body>
</html>