-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathround_1.html
79 lines (72 loc) · 2.82 KB
/
round_1.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
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
</head>
<body>
<div style="text-align:center;">
<canvas id="battlefield-canvas" width="800" height="800" style="border:1px solid #000000;"></canvas>
</div>
<input type="file" id="inputWasm" />
<script src="robot-battle.js"></script>
<script src="canvas-render.js"></script>
<script>
const inputElement = document.getElementById("inputWasm");
inputElement.addEventListener("change", handleFile, false);
function handleFile() {
const wasmFile = this.files[0];
const wasmURL = window.URL.createObjectURL(wasmFile);
initializeWasm(wasmURL).then(main);
}
async function initializeWasm(wasmURL) {
const fetchPromise = fetch(wasmURL);
const { instance } = await WebAssembly.instantiateStreaming(fetchPromise);
return instance;
}
function main(instance) {
const numRows = 20;
const numColumns = 20;
const strategyFirst = () => { return instance.exports.whatToDo(); }
const strategySecond = () => { return game.actions.doNothing; }
const game = new RobotGame(numRows,
numColumns,
strategyFirst,
strategySecond,
instance.exports.memory,
instance.exports.getAddressOfData());
game.setupPlayer(0, 0);
game.setupEnemy(numRows - 1, Math.floor(Math.random() * numColumns));
const canvas = document.getElementById("battlefield-canvas");
const render = new CanvasRender(canvas, game);
render.render();
const speed = 500;
var timer = setInterval(() => {
let state = game.getState();
if (state != game.gameStates.InProgress) {
switch (state) {
case game.gameStates.Victory: {
alert("VICTORY, what a battle");
break;
}
case game.gameStates.Defeat: {
alert("DEFEAT, try again");
break;
}
case game.gameStates.Draw: {
alert("DRAW, what a mess");
break;
}
default: {
throw new Error("unknown game state");
}
}
clearInterval(timer);
return;
}
game.doStep();
render.render();
}, speed);
}
</script>
</body>
</html>