-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathomok.html
308 lines (257 loc) · 7.73 KB
/
omok.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<html>
<head>
<title>오목</title>
<meta charset="utf-8" />
<style>
:root {
--cell-size: 40px;
}
.center {
transform: translate(-50%, -50%);
position: absolute;
top: 50%;
left: 50%;
}
.board {
border: 1px solid black;
}
.board-padding {
background: #723820;
border: 2px solid black;
box-shadow: 10px 10px 30px rgba(0, 0, 0, 0.5);
padding: calc(var(--cell-size) / 2);
}
.row {
display: flex;
}
.cell {
border: 1px solid black;
width: var(--cell-size);
height: var(--cell-size);
box-sizing: border-box;
}
.container .cell,
.points .cell {
border: none;
}
.unit {
width: 90%;
height: 90%;
margin: 5%;
border-radius: 50%;
box-shadow: 3px 3px 5px rgba(0, 0, 0, 0.5);
}
.black {
background: black;
color: white;
}
.white {
background: white;
color: black;
}
.black.shadow {
background: rgba(0, 0, 0, 0.4);
}
.white.shadow {
background: rgba(255, 255, 255, 0.4);
}
.hide {
display: none;
}
.info {
font-size: 4rem;
z-index: 100;
text-align: center;
width: 50%;
padding: 1rem;
}
.wrapper {
height: 100%;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.point {
background: black;
width: 10px;
height: 10px;
border-radius: 50%;
}
#undo {
position: absolute;
right: 20;
bottom: 20;
background: #723820;
border: 1px solid black;
border-radius: 5px;
font-size: 1rem;
box-shadow: 3px 3px 5px rgba(0, 0, 0, 0.5);
}
</style>
</head>
<body>
<div class="center board-padding">
<div class="board"></div>
</div>
<div class="points center"></div>
<div class="container center"></div>
<button id="undo">물리기</button>
<script>
const range = function (n, m) {
if (arguments.length === 1)
return Array.from({ length: n }).map((_, i) => i);
if (arguments.length === 2) {
if (n === m) return [n];
else if (n < m) {
return Array.from({ length: m - n + 1 }).map((_, i) => i + n);
} else {
return Array.from({ length: n - m + 1 }).map(
(_, i) => m - i + (n - m)
);
}
}
};
const repeat = (n) => (v) => Array.from({ length: n }).map(() => v);
const zip = (arr1, arr2) => {
const length = Math.min(arr1.length, arr2.length);
return Array.from({ length }).map((_, i) => [arr1[i], arr2[i]]);
};
const delay = (n) => new Promise((r) => setTimeout(r, n));
const createGrid = (n) =>
Array.from({ length: n }).map(() =>
Array.from({ length: n }).map(() => 0)
);
const draw = (container, grid) => {
const template = grid
.map(
(row) =>
`<div class="row">${row
.map((i) => `<div class="cell"></div>`)
.join("")}</div>`
)
.join("");
container.innerHTML = template;
};
const BOARD_SIZE = 19;
const [BLACK, WHITE] = [1, 2];
const turnStr = ["empty", "black", "white"];
const repeat5 = repeat(5);
const isInBoard = ([x, y]) =>
0 <= x && x < BOARD_SIZE && 0 <= y && y < BOARD_SIZE;
const index = (x, y) => y * BOARD_SIZE + x;
const board = document.querySelector(".board");
draw(board, createGrid(BOARD_SIZE - 1));
const points = document.querySelector(".points");
draw(points, createGrid(BOARD_SIZE));
const pointIndexList = [3, 9, 15].flatMap((x) =>
[3, 9, 15].map((y) => index(x, y))
);
const point = `<div class="wrapper"><div class="point"></div></div>`;
const pointCells = points.querySelectorAll(".cell");
pointIndexList.forEach((index) => (pointCells[index].innerHTML = point));
const container = document.querySelector(".container");
draw(container, createGrid(BOARD_SIZE));
class UI {
constructor() {
this.info = document.createElement("div");
this.info.classList.add("center", "hide", "info");
container.appendChild(this.info);
}
win(turn) {
this.info.classList.remove("hide", "black", "white");
this.info.classList.add(turnStr[turn]);
this.info.innerText = `${turn === BLACK ? "흑" : "백"} 승리`;
setTimeout(() => this.info.classList.add("hide"), 3000);
}
}
const ui = new UI();
class GameStatus {
constructor(container) {
this.grid = createGrid(BOARD_SIZE);
this.turn = BLACK;
this.cells = container.querySelectorAll(".cell");
this.logs = [];
}
switchTurn() {
this.turn = this.turn === BLACK ? WHITE : BLACK;
}
get turnClass() {
return turnStr[this.turn];
}
point(x, y) {
if (this.grid[y][x]) return;
this.grid[y][x] = this.turn;
const unit = `<div class="unit ${this.turnClass}"></div>`;
this.cells[y * BOARD_SIZE + x].innerHTML = unit;
this.logs.push([x, y]);
this.checkWin();
this.switchTurn();
}
checkWin() {
const turnPositions = this.grid.flatMap((row, y) =>
row.reduce(
(acc, turn, x) => (turn === this.turn ? [...acc, [x, y]] : acc),
[]
)
);
for (const [x, y] of turnPositions) {
const right = zip(range(x, x + 4), repeat5(y));
const left = zip(range(x, x - 4), repeat5(y));
const top = zip(repeat5(x), range(y, y + 4));
const bottom = zip(repeat5(x), range(y, y - 4));
const bottomLeft = zip(range(x, x + 4), range(y, y + 4));
const bottomRight = zip(range(x, x - 4), range(y, y + 4));
const topRight = zip(range(x, x + 4), range(y, y - 4));
const topLeft = zip(range(x, x - 4), range(y, y - 4));
const toTurn = ([x, y]) => this.grid[y][x];
const isSameTurn = (turn) => turn === this.turn;
const fiveInRow = !![
right,
left,
top,
bottom,
topRight,
topLeft,
bottomRight,
bottomLeft,
]
.filter((poss) => poss.every(isInBoard))
.filter((poss) => poss.map(toTurn).every(isSameTurn)).length;
if (fiveInRow) {
return ui.win(this.turn);
}
}
}
undo() {
if (!this.logs.length) return;
const [x, y] = this.logs.pop();
this.grid[y][x] = 0;
this.cells[y * BOARD_SIZE + x].innerHTML = "";
this.switchTurn();
}
}
const gameStatus = new GameStatus(container);
[...document.querySelectorAll(".container .cell")].forEach(function (
elem,
idx
) {
elem.addEventListener("mouseenter", function (e) {
if (!this.innerHTML)
this.innerHTML = `<div class="unit ${gameStatus.turnClass} shadow"></div>`;
});
elem.addEventListener("mouseleave", function (e) {
if (this.innerHTML.includes("shadow")) this.innerHTML = "";
});
elem.addEventListener("click", function (e) {
const y = ~~(idx / BOARD_SIZE);
const x = idx % BOARD_SIZE;
gameStatus.point(x, y);
});
});
document
.querySelector("#undo")
.addEventListener("click", () => gameStatus.undo());
</script>
</body>
</html>