-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
106 lines (85 loc) · 2.63 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style>
html {
font-family: sans-serif;
}
#game-status {
margin-bottom: 10px;
}
.board {
width: 160px;
background: black;
display: grid;
grid-template-columns: repeat(3, 50px);
grid-template-rows: repeat(3, 50px);
gap: 5px;
}
.cell {
font-weight: bold;
background: white;
text-align: center;
line-height: 50px;
cursor: pointer;
}
</style>
<script type="text/javascript" src="brython/brython.js"></script>
<script type="text/javascript" src="brython/brython_stdlib.js"></script>
</head>
<body onload="brython()">
<script type="text/python">
from browser import document, html, bind
import tictactoe
def game_over(state):
score = tictactoe.utility(state)
if score == tictactoe.RESULT_X_WINS:
document["game-status"].text = "GAME OVER: Player x wins"
elif score == tictactoe.RESULT_O_WINS:
document["game-status"].text = "GAME OVER: Player o wins"
else:
document["game-status"].text = "GAME OVER: It's a draw"
def player_o(state):
try:
print('player o')
_, action = tictactoe.min_value(state)
document["game"].text = ""
next_state = tictactoe.result(state, action)
load_state(next_state)
if tictactoe.terminal(next_state):
game_over(next_state)
return
document["game-status"].text = "x is playing..."
except:
pass
def load_state(state):
document["game"] <= html.DIV([
html.DIV(
value if value != "#" else "",
id=f"cell{x}{y}",
Class="cell",
) for y, row in enumerate(state) for x, value in enumerate(row)
], Class="board")
@bind("div.cell", "click")
def make_move(event):
_id = event.currentTarget.id
x = int(_id[-2])
y = int(_id[-1])
try:
new_state = tictactoe.result(state, (x, y))
document["game"].text = ""
load_state(new_state)
if tictactoe.terminal(new_state):
game_over(new_state)
return
document["game-status"].text = "o is playing..."
player_o(new_state)
except:
pass
document <= html.DIV("x is playing...", id="game-status") + html.DIV(id="game")
initial_state = tictactoe.get_state()
load_state(initial_state)
</script>
</body>
</html>