ntt123 commited on
Commit
7542fdd
1 Parent(s): a1fbef0

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +22 -16
index.html CHANGED
@@ -37,20 +37,18 @@
37
  this.canvas_ctx.font = '1px monospace';
38
  this.canvas_ctx.fillText(text, 1, 1);
39
  };
40
- this.submit_board = async function () {
41
- document.body.style.cursor = 'progress';
42
- const response = await fetch("submit", {
43
- method: 'POST',
44
- mode: 'cors', // no-cors, *cors, same-origin
45
- cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
46
- credentials: 'same-origin', // include, *same-origin, omit
47
- headers: { 'Content-Type': 'application/json' },
48
- redirect: 'follow', // manual, *follow, error
49
- referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
50
- body: JSON.stringify({ "board": this_.board, "ai-player": this_.ai_player }) // body data type must match "Content-Type" header
51
- });
52
- return response.json(); // parses JSON response into native JavaScript objects
53
- }
54
  this.end_game = function () {
55
  this.game_ended = true;
56
  setTimeout(function() {this_.reset();}, 3000);
@@ -178,9 +176,17 @@
178
  setTimeout(function () { this_.render(); });
179
  };
180
  };
 
 
 
 
 
 
181
  document.addEventListener("DOMContentLoaded", function (event) {
182
- game = new BoardGame(6, 7);
183
- game.render();
 
 
184
  });
185
  </script>
186
  </body>
 
37
  this.canvas_ctx.font = '1px monospace';
38
  this.canvas_ctx.fillText(text, 1, 1);
39
  };
40
+ this.submit_board = async function (action_) {
41
+ const obs = tf.tensor(this_.board, [this_.num_rows, this_.num_cols], 'float32');
42
+ const normalized_obs = tf.mul(obs, this_.ai_player);
43
+ normalized_obs.print();
44
+ const [action_logits, value] = this_.agent.predict(normalized_obs);
45
+ action_logits.print();
46
+ const action = await tf.argMax(action_logits).array();
47
+ return {
48
+ "terminated": false,
49
+ "action": action,
50
+ };
51
+ };
 
 
52
  this.end_game = function () {
53
  this.game_ended = true;
54
  setTimeout(function() {this_.reset();}, 3000);
 
176
  setTimeout(function () { this_.render(); });
177
  };
178
  };
179
+ const modelUrl = 'policy/model.json';
180
+ const init_fn = async function() {
181
+ const model = await tf.loadGraphModel(modelUrl);
182
+ await tf.setBackend('wasm');
183
+ return model;
184
+ };f
185
  document.addEventListener("DOMContentLoaded", function (event) {
186
+ init_fn().then(function (agent) {
187
+ game = new BoardGame(agent, 6, 7);
188
+ game.render();
189
+ });
190
  });
191
  </script>
192
  </body>