-
Notifications
You must be signed in to change notification settings - Fork 0
/
worm.html
117 lines (99 loc) · 2.94 KB
/
worm.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
<!DOCTYPE html>
<head>
<canvas id="canvas" width="500px" height="500px" style="margin: 50px; border: 50px solid #ffdede;"></canvas>
<input type="button" value="very slow" onclick="play(1000);">
<input type="button" value="slow" onclick="play(100);">
<input type="button" value="fast" onclick="play(10);"/>
<input type="button" value="very fast" onclick="play(1);"/>
<input type="button" value="train" onclick="for(var i = 0;i < 100000;i++) loop();"/>
</head>
<body>
<script src="src/q-learning.js"></script>
<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var learner = new QLearner(0.9, 0.9, 0.1, 0.01, 10000);
var feed = [];
var direction = 1;
var hp = 50;
var worm = [[2, 2]];
putFeed();
function loop() {
var s = state();
var a = learner.optimumAction(s, [0, 1, 3]);
direction = (direction + a) % 4;
var trace = [worm[worm.length - 1][0], worm[worm.length - 1][1]];
for (var i = worm.length - 1;i > 0;i--) {
worm[i][0] = worm[i - 1][0];
worm[i][1] = worm[i - 1][1];
}
worm[0][0] += direction == 1 ? 1 : (direction == 3 ? -1 : 0);
worm[0][1] -= direction == 0 ? -1 : (direction == 2 ? 1 : 0);
var check = false;
for (var i = 1;i < worm.length;i++) {
if (worm[i][0] == worm[0][0] && worm[i][1] == worm[0][1]) {
check = true;
break;
}
}
if (check || worm[0][0] < 0 || worm[0][0] > 4 || worm[0][1] < 0 || worm[0][1] > 4 || hp <= 0) {
learner.train(s, a, -1.2, null);
direction = 1;
worm = [[2, 2]];
hp = 50;
putFeed();
}
else {
var s2 = state();
var r = 0;
hp--;
if (worm[0][0] == feed[0] && worm[0][1] == feed[1]) {
r = 1;
hp += 15;
if (hp > 50) hp = 50;
worm.push(trace);
putFeed();
}
learner.train(s, a, r, s2);
}
}
function putFeed() {
while(true) {
var c = true;
feed = [(Math.random() * 5) | 0, (Math.random() * 5) | 0];
for (var i in worm) if (worm[i][0] == feed[0] && worm[i][1] == feed[1]) {
c = false;
break;
}
if (c) break;
}
}
var interval;
function play(n) {
clearInterval(interval);
interval = setInterval(function() {
loop();
draw();
}, n);
}
draw();
function draw() {
canvas.width = canvas.width;
ctx.fillStyle = "#dedeff";
ctx.fillRect(feed[0] * 100, feed[1] * 100, 100, 100);
ctx.fillStyle = "#ffdede";
for (var i in worm) {
ctx.fillRect(worm[i][0] * 100, worm[i][1] * 100, 100, 100);
}
ctx.fillRect(50, 50, 220, 70);
ctx.clearRect(260, 60, (hp - 50) * 2, 50);
}
function state() {
var str = direction + "_";
str += feed.join('') + "_";
for (var i in worm) str += worm[i].join('');
return str;
}
</script>
</body>
</html>