-
Notifications
You must be signed in to change notification settings - Fork 2
/
tictactinytoe.js
42 lines (37 loc) · 898 Bytes
/
tictactinytoe.js
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
F = _ => {
x = o = z = 0
t = 1
}
F()
require("net").createServer(c => {
h = "\n"
w = s => c.write(s+h)
if (o)
return w("Game in progress")
c.m = {}
c.w = w
z++ ? (o = c, o.W = x.w, x.W = w) : x = c
G = (e) => {
for (l = "", i = 1; i <= 9; i++)
l += i in x.m ? "X" : o && i in o.m ? "O" : i, l += i % 3 ? "|" : h
e.w(l)
}
G(c)
c.on("end", _ => c.W("Player left")
).on("data", d => {
if (t ^ c == x)
return
v = parseInt(d)
if (isNaN(v) || v<1 || v>9 || v in {...x.m, ...o.m})
return c.w("Invalid move")
t = c.m[v] = 1 - t
G(x)
o && G(o)
f = i => i < 24 ? "123147159369789753258456".slice(i, i+3).split("").every(j => j in c.m) || f(i+3) : 0
if (f(0))
throw c.w("You won"), c.W("You lost")
if (z++ > 9)
throw r = "Draw", c.w(r), c.W(r)
})
}).listen(9191)
process.on('uncaughtException', F)