-
Notifications
You must be signed in to change notification settings - Fork 3
/
tictactoe.html
87 lines (85 loc) · 3.4 KB
/
tictactoe.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
<!doctype html>
<html>
<head>
<style>
table, h2, h5 {
margin: 40px auto;
text-align: center;
font-family: Helvetica;
border-collapse: collapse;
}
td {
width: 40px;
height: 40px;
border-right: 1px solid black;
border-bottom: 1px solid black;
font-size: 40px;
line-height: 40px
}
td:last-child {
border-right: none;
}
tr:last-child td {
border-bottom: none;
}
td.open {
cursor: pointer;
}
</style>
</html>
<body>
<script type="text/jsx">
var X = 'X',
O = 'O',
range = size => Array.apply(0, Array(size)).map((_, i) => i), // helper to create array [0 .. size - 1]
Game = (size, goal) => {
var dim = range(size),
win = range(goal),
zs = range(size - goal + 1), // "gap" between board size and win length
board = dim.map(x => dim.map(y => S.data(null))), // dim x dim array of data signals
turn = S.data(X),
winner = S(() =>
[X, O].find(player =>
zs.some(z =>
dim.some(x => win.every(y => board[x][y + z]() === player)) // win along x-axis
|| dim.some(y => win.every(x => board[x + z][y]() === player)) // win along y-axis
|| zs.some(w => // diagonal wins
win.every(d => board[d + z][d + w]() === player) // top-left-bottom-right direction
|| win.every(d => board[d + z][size - d - w - 1]() === player))))), // top-right-bottom-left direction
over = S(() => winner() || dim.every(x => dim.every(y => board[x][y]())));
return {
board : board,
goal : goal,
turn : turn,
winner: winner,
over : over,
move : (x, y) => {
if (winner() || board[x][y]()) return;
board[x][y](turn());
if (!winner()) turn(turn() === X ? O : X);
}
};
},
View = game =>
<div>
<h2>Tic-Tac-Toe</h2>
<table>
<tbody>{game.board.map((row, x) =>
<tr>{row.map((cell, y) =>
<td class={cell() ? '' : 'open'} onclick = {() => game.move(x, y)}>
{cell()}
</td>
)}</tr>
)}</tbody>
</table>
<h5>Get {game.goal}-in-a-row to win</h5>
<h5 hidden={game.over()}>Turn: {game.turn()}</h5>
<h5 hidden={!game.over()}>{game.winner() ? game.winner() + " wins!" : "draw"}</h5>
</div>,
game = Game(6, 4),
view = View(game);
document.body.appendChild(view);
</script>
<script src="https://unpkg.com/surplus-toys"></script>
</body>
<html>