-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathindex.html
More file actions
179 lines (164 loc) · 4.05 KB
/
index.html
File metadata and controls
179 lines (164 loc) · 4.05 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
<title>Tic Tac Toe</title>
<meta charset="UTF-8" />
<style>
body,
td,
input,
button {
font-size: 24px;
line-height: 1.5;
text-align: center;
font-family: system-ui, sans-serif;
}
td {
height: 3rem;
width: 3rem;
text-align: center;
}
table {
margin: 5rem auto;
}
@media (prefers-color-scheme: dark) {
body,
input {
background: #222;
color: #ccc;
}
}
</style>
<h1>Tic Tac Toe</h1>
<table>
<tr>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
</tr>
</table>
<h2 hidden>Game over</h2>
<button id="play-again" hidden>Play again</button>
<script>
// Initialization
const boxes = Array.from(document.querySelectorAll("td"));
const inputs = Array.from(document.querySelectorAll("[type=checkbox]"));
const rows = [1, 2, 3].map((n) =>
Array.from(document.querySelectorAll(`tr:nth-child(${n}) td`))
);
const columns = [1, 2, 3].map((n) =>
Array.from(document.querySelectorAll(`td:nth-child(${n})`))
);
const diagonals = [
[
[1, 1],
[2, 2],
[3, 3],
],
[
[3, 1],
[2, 2],
[1, 3],
],
].map((set) =>
set.map(([x, y]) =>
document.querySelector(`tr:nth-child(${x}) td:nth-child(${y})`)
)
);
const gameOver = document.querySelector("h2");
const playAgainBtn = document.getElementById("play-again");
const HUMAN = "🧑💻";
const ROBOT = "🤖";
// Decide who goes first
if (Math.random() > 0.5) {
runRobotTurn();
}
// Handle when a user clicks an input
for (const input of inputs) {
input.addEventListener("click", handleClickInput);
}
// Play again: reload the page to reset state
playAgainBtn.addEventListener("click", () => window.location.reload());
// The user chose a box, check for win, robot turn, check for win
function handleClickInput(evt) {
evt.target.closest("td").innerHTML = HUMAN;
if (hasWin()) {
return endGame("human");
}
if (hasDraw()) {
return endGame("draw");
}
runRobotTurn();
if (hasWin()) {
return endGame("robot");
}
if (hasDraw()) {
return endGame("draw");
}
}
// Choose a random available box
function runRobotTurn() {
const robotBoxes = shuffle(boxes);
while (robotBoxes.length) {
const box = robotBoxes.shift();
if (box.querySelector("input")) {
box.innerHTML = ROBOT;
break;
}
}
}
// Determine win
function hasWin() {
return (
rows.some(hasAllSame) ||
columns.some(hasAllSame) ||
diagonals.some(hasAllSame)
);
}
// Determine draw (no more available inputs)
function hasDraw() {
return !document.querySelector("td input[type=checkbox]");
}
// Determine if every cell matches
function hasAllSame(arr) {
return arr.every(
(td) => !td.querySelector("input") && td.innerHTML === arr[0].innerHTML
);
}
// Display game over, cancel events
function endGame(result) {
if (result === "draw") {
gameOver.textContent = "Game over: Draw";
} else if (result === "human") {
gameOver.textContent = "Game over: You win";
} else if (result === "robot") {
gameOver.textContent = "Game over: Robot wins";
}
gameOver.hidden = false;
playAgainBtn.hidden = false;
// Move focus to the button for quick replay
playAgainBtn.focus();
for (const input of inputs) {
input.removeEventListener("click", handleClickInput);
input.remove();
}
}
// Shuffle a copy of the input array
function shuffle(array) {
array = array.slice(0);
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * i);
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
</script>