-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.html
246 lines (203 loc) · 6.87 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
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
<html>
<header>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.14/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.css" integrity="sha256-ECB9bbROLGm8wOoEbHcHRxlHgzGqYpDtNTgDTyDz0wg=" crossorigin="anonymous" />
<style>
#grid>button {
width: 180px;
height: 180px;
vertical-align: top;
margin: 10px;
font-size: 1.1em;
transition: background-color 0.1s ease;
}
body {
text-align: center;
}
#grid {
width: 600px;
margin-right: auto;
margin-left: auto;
}
.ai {
background-color: #33C3F0;
}
.player {
background-color: rgb(240, 196, 51);
}
</style>
</header>
<body>
<div id="app" class="container">
<h2><a href="https://github.com/ZackAkil/deep-tic-tac-toe">Deep Tic-Tac-Toe </a></h2>
<div class="row" style="text-align: center;">
<p style=" width: 500px;
margin-left: auto;
margin-right: auto;
">
Used deep reinforcement learning to train a deep neural network to play tic-tac-toe and deployed using tensorflow.js.
</p>
</div>
<p><a href="https://twitter.com/ZackAkil">@ZackAkil</a> - <a href="https://github.com/ZackAkil/deep-tic-tac-toe">GitHub repo</a></p>
<p><span>Show raw output for model's move: </span><input type="checkbox" v-model="show_pred" checked></p>
<div id="grid">
<button v-for="(cell, index) in grid" v-on:click="move(index)" v-bind:class="{ ai: cell==1, player: cell==2}" :disabled="end_game||(cell != 0)">
<span v-if="cell == 1">X</span>
<span v-else-if="cell == 2">O</span>
<p v-if="show_pred">{{Math.round(pred[index]*100)/100 }}</p>
</button>
</div>
<div>
<br>
<h4 v-if="end_game">{{game_status}}</h4>
<br>
<button v-on:click="reset_game()">Reset Game</button>
</div>
</div>
<script>
const AI_DELAY = 100; // yes the model is so fast we have to slow it down to make it look like it's thinking
var model;
var ai_starts = false;
tf.loadLayersModel('model/model.json').then((models) => {
model = models
console.log('loaded model')
reset_game()
});
function predict(x) {
const input = tf.tensor(x, [1, 3, 3, 2])
// input.print(true)
const pred = model.predict(input).dataSync()
Vue.set(app, 'pred', pred)
return tf.tensor1d(pred).argMax().dataSync()[0]
}
function grid_to_input_array(grid) {
var output = []
for (var i = 0; i < grid_length; i++) {
if (grid[i] == 1) {
// add AIs positions
output.push(1);
output.push(0);
} else if (grid[i] == 2) {
// add PLayers positions
output.push(0);
output.push(1);
} else {
output.push(0);
output.push(0);
}
}
return output
}
var grid = [];
const grid_length = 9;
for (var i = 0; i < grid_length; i++) {
grid.push(0);
}
function move(button) {
console.log(button)
app.grid.splice(button, 1, 2)
get_game_state()
if (app.end_game != true) {
setTimeout(function() {
ai_move()
get_game_state()
}, AI_DELAY);
}
}
function reset_game() {
app.end_game = false
Vue.set(app, 'pred', [0, 0, 0, 0, 0, 0, 0, 0, 0])
Vue.set(app, 'grid', [0, 0, 0, 0, 0, 0, 0, 0, 0])
ai_starts = !ai_starts
if (ai_starts) {
ai_move()
}
}
function ai_move() {
const pos = predict(grid_to_input_array(app.grid))
app.grid.splice(pos, 1, 1)
}
var app = new Vue({
el: '#app',
data: {
grid: [0, 0, 0, 0, 0, 0, 0, 0, 0],
pred: [0, 0, 0, 0, 0, 0, 0, 0, 0],
end_game: false,
game_status: "",
show_pred: true
}
})
function get_game_state() {
const winner = find_winner()
if (winner) {
if (winner == 1) {
app.game_status = "🤖 A.I wins!"
} else {
app.game_status = "Player wins!"
}
console.log('end game :', winner)
app.end_game = true
} else if (is_full_grid()) {
console.log('end game : Tie')
app.game_status = "Tie game!"
app.end_game = true
}
}
function is_winner(player = 1) {
var j = 0;
for (var j = 0; j < 3; j++) {
winner = true
var i = 0;
for (i = 0; i < 3; i++) {
if (app.grid[(j * 3) + i] != player) {
winner = false
}
}
if (winner) {
return true
}
}
var j = 0;
for (j = 0; j < 3; j++) {
winner = true
var i = 0
for (i = 0; i < 3; i++) {
if (app.grid[j + (i * 3)] != player) {
winner = false
}
}
if (winner) {
return true
}
}
if ((app.grid[0] * app.grid[4] * app.grid[8]) == player ** 3) {
return true
}
if ((app.grid[2] * app.grid[4] * app.grid[6]) == player ** 3) {
return true
}
return false
}
function is_full_grid() {
for (var i = 0; i < app.grid.length; i++) {
if (app.grid[i] == 0) {
return false
}
}
return true
}
function find_winner() {
if (is_winner(1)) {
return 1
console.log('player 1 win')
} else if (is_winner(2)) {
return 2
console.log('player 2 win')
} else {
return 0
}
}
</script>
</body>
</html>