generated from skills/review-pull-requests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
152 lines (139 loc) · 3.19 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
<title>Tic Tac Toe</title>
<meta charset="UTF-8" />
<style>
body,
td,
input {
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>
<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 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);
}
// 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();
}
runRobotTurn();
if (hasWin()) {
return endGame();
}
}
// 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 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() {
gameOver.hidden = false;
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>