This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
280 lines (229 loc) · 8.09 KB
/
index.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<!-- A simple Tic Tac Toe game -->
<!--
Step 1: HTML Structure
Create an HTML file and define the basic structure.
Add a title for the game.
Create a grid-like structure to represent the Tic Tac Toe board using HTML elements (e.g., <div> or <table>).
Step 2: CSS Styling
Add CSS styles to make the Tic Tac Toe board visually appealing.
Customize the styles for the grid cells, including their size, borders, and colors.
Apply any additional styling as desired to enhance the game's appearance.
Step 3: Game Initialization
Use a <script> tag within the HTML file to write JavaScript code.
Define variables to represent the game state, such as the board, current player, and game status (ongoing, won, or tied).
Initialize the game state, such as setting an empty board, the starting player, and the game status.
Step 4: Event Handling
Add event listeners to the grid cells to handle player interactions.
Define a function to handle the click event on a grid cell.
Inside the event handler function, update the board based on the clicked cell, check for a win condition, and update the game status accordingly.
Step 5: Game Logic
Create functions to check for a win condition, such as checking rows, columns, and diagonals.
Implement a function to switch players after each move.
Update the game status based on the win condition or a tie.
Step 6: UI Updates
Define a function to update the UI after each move or when the game ends.
Update the grid cells to reflect the current state of the board.
Display the game status (e.g., "Player X's turn" or "Player O wins!").
Provide a button to restart the game.
Step 7: Game Restart
Implement a function to reset the game state and UI when the restart button is clicked.
Clear the board, reset the player, and set the game status to ongoing.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tic Tac Toe</title>
<link rel="stylesheet" href="style.css">
<style>
body {
background-color: #f5f5f5;
font-family: sans-serif;
margin: 0;
}
h1 {
text-align: center;
margin: 0;
padding: 1rem;
background-color: #333;
color: #fff;
}
.board {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 1px;
width: 300px;
margin: 0 auto;
margin-top: 1rem;
margin-bottom: 1rem;
}
.cell {
background-color: #fff;
border: 1px solid #333;
width: 100px;
height: 100px;
font-size: 3rem;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
}
.cell:hover {
background-color: #ddd;
}
.game-status {
text-align: center;
margin-bottom: 1rem;
}
.status {
font-size: 1.5rem;
margin-bottom: 0.5rem;
}
.restart-button {
font-size: 1rem;
padding: 0.5rem 1rem;
border: 2px solid #333;
background-color: #fff;
color: #333;
cursor: pointer;
}
.restart-button:hover {
background-color: #333;
color: #fff;
}
.footer-text {
text-align: center;
font-size: 0.75rem;
width: 100%;
bottom: 0;
position: absolute;
margin-top: 1rem;
padding: 1rem;
background-color: #333;
color: #fff;
}
.black-text {
width: 100%;
position: absolute;
bottom: 100px;
color: #333;
text-align: center;
}
</style>
</head>
<body>
<h1>Tic Tac Toe</h1>
<div class="board">
<div class="cell" id="0"></div>
<div class="cell" id="1"></div>
<div class="cell" id="2"></div>
<div class="cell" id="3"></div>
<div class="cell" id="4"></div>
<div class="cell" id="5"></div>
<div class="cell" id="6"></div>
<div class="cell" id="7"></div>
<div class="cell" id="8"></div>
</div>
<div class="game-status">
<div class="status">Player X's turn</div>
<button class="restart-button">Restart Game</button>
</div>
<script>
// tic tac toe game's JavaScript code goes here
// Define variables to represent the game state
let board = ["", "", "", "", "", "", "", "", ""];
let playerTurn = "X";
let gameStatus = "Game On";
// Add event listeners to the grid cells to handle player interactions
const cells = document.querySelectorAll(".cell");
const restartButton = document.querySelector(".restart-button");
cells.forEach((cell) => {
cell.addEventListener("click", handleClick);
});
restartButton.addEventListener("click", restartGame);
// Define a function to handle the click event on a grid cell
function handleClick(event) {
const cell = event.target;
const cellIndex = parseInt(cell.getAttribute("id"));
// If the cell is not empty or the game is over, return
if (board[cellIndex] !== "" || gameStatus !== "Game On") {
return;
}
// Update the board based on the clicked cell
board[cellIndex] = playerTurn;
cell.innerHTML = playerTurn;
// Check for a win condition
if (checkWin()) {
gameStatus = `${playerTurn} wins!`;
document.querySelector(".status").innerHTML = gameStatus;
return;
}
// Update the game status
if (checkTie()) {
gameStatus = "Tie Game!";
document.querySelector(".status").innerHTML = gameStatus;
return;
}
// Switch players
playerTurn = playerTurn === "X" ? "O" : "X";
document.querySelector(".status").innerHTML = `Player ${playerTurn}'s turn`;
}
// Create functions to check for a win condition
function checkWin() {
// Check rows
for (let i = 0; i < 9; i += 3) {
if (checkThree(i, i + 1, i + 2)) {
return true;
}
}
// Check columns
for (let i = 0; i < 3; i++) {
if (checkThree(i, i + 3, i + 6)) {
return true;
}
}
// Check diagonals
if (checkThree(0, 4, 8)) {
return true;
}
if (checkThree(2, 4, 6)) {
return true;
}
return false;
}
function checkThree(a, b, c) {
// this function will check if the values of the board array at positions a, b, and c are the same
if (board[a] === board[b] && board[b] === board[c] && board[a] !== "") {
return true;
}
}
// Create a function to check for a tie game
function checkTie() {
let boardFull = true;
for (let i = 0; i < board.length; i++) {
if (board[i] === "") {
boardFull = false;
break;
}
}
return boardFull;
}
// Create a function to restart the game
function restartGame() {
board = ["", "", "", "", "", "", "", "", ""];
playerTurn = "X";
gameStatus = "Game On";
document.querySelector(".status").innerHTML = `Player ${playerTurn}'s turn`;
cells.forEach((cell) => {
cell.innerHTML = "";
});
}
</script>
<span>
<a class="black-text" href="https://github.com/khot-aditya/tic-tac-toe">Source Code</a>
</span>
<p class="footer-text">
This game is 99.99% generated using ChatGPT and GitHub Copilot.
</p>
</body>
</html>