-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe2.html
196 lines (175 loc) · 5.68 KB
/
tictactoe2.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
<!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>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background: linear-gradient(135deg, #a2c8f0, #68a4e0);
color: #333;
}
h1 {
margin-bottom: 20px;
color: #fff;
}
.game-board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 10px;
}
.cell {
display: flex;
align-items: center;
justify-content: center;
width: 100px;
height: 100px;
background-color: #e3f2fd;
border: 2px solid #90caf9;
font-size: 24px;
font-weight: bold;
cursor: pointer;
transition: background-color 0.3s;
}
.cell:hover {
background-color: #bbdefb;
}
.cell.x::before {
content: "X";
color: #1e88e5;
}
.cell.circle::before {
content: "O";
color: #0d47a1;
}
.game-info {
margin-top: 20px;
font-size: 18px;
color: #fff;
}
button {
margin-top: 20px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #42a5f5;
border: none;
border-radius: 5px;
color: #fff;
transition: background-color 0.3s;
}
button:hover {
background-color: #1e88e5;
}
</style>
</head>
<body>
<h1>Tic-Tac-Toe</h1>
<div class="game-board" id="gameBoard">
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
</div>
<div class="game-info" id="gameInfo">
Player X's turn
</div>
<button id="restartButton">Restart</button>
<script>
const X_CLASS = 'x';
const CIRCLE_CLASS = 'circle';
const WINNING_COMBINATIONS = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
const cellElements = document.querySelectorAll('[data-cell]');
const board = document.getElementById('gameBoard');
const gameInfo = document.getElementById('gameInfo');
const restartButton = document.getElementById('restartButton');
let circleTurn;
startGame();
restartButton.addEventListener('click', startGame);
function startGame() {
circleTurn = false;
cellElements.forEach(cell => {
cell.classList.remove(X_CLASS);
cell.classList.remove(CIRCLE_CLASS);
cell.removeEventListener('click', handleClick);
cell.addEventListener('click', handleClick, { once: true });
});
setBoardHoverClass();
gameInfo.innerText = `Player ${circleTurn ? "O" : "X"}'s turn`;
}
function handleClick(e) {
const cell = e.target;
const currentClass = circleTurn ? CIRCLE_CLASS : X_CLASS;
placeMark(cell, currentClass);
if (checkWin(currentClass)) {
endGame(false);
} else if (isDraw()) {
endGame(true);
} else {
swapTurns();
setBoardHoverClass();
}
}
function endGame(draw) {
if (draw) {
gameInfo.innerText = 'Draw!';
} else {
gameInfo.innerText = `Player ${circleTurn ? "O" : "X"} Wins!`;
}
cellElements.forEach(cell => {
cell.removeEventListener('click', handleClick);
});
}
function isDraw() {
return [...cellElements].every(cell => {
return cell.classList.contains(X_CLASS) || cell.classList.contains(CIRCLE_CLASS);
});
}
function placeMark(cell, currentClass) {
cell.classList.add(currentClass);
}
function swapTurns() {
circleTurn = !circleTurn;
gameInfo.innerText = `Player ${circleTurn ? "O" : "X"}'s turn`;
}
function setBoardHoverClass() {
board.classList.remove(X_CLASS);
board.classList.remove(CIRCLE_CLASS);
if (circleTurn) {
board.classList.add(CIRCLE_CLASS);
} else {
board.classList.add(X_CLASS);
}
}
function checkWin(currentClass) {
return WINNING_COMBINATIONS.some(combination => {
return combination.every(index => {
return cellElements[index].classList.contains(currentClass);
});
});
}
</script>
</body>
</html>