-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.html
136 lines (133 loc) · 6.79 KB
/
ui.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
background-color: white;
}
</style>
<title>Pacman</title>
</head>
<body>
<div id="board"></div>
<div id="notice"></div>
<script src="game.js"></script>
<script>
let boardDiv = document.getElementById("board");
let noticeDiv = document.getElementById("notice");
/*
[Object id]
-3: `Ghost`(enemy)
-2: `Pacman`(player)
-1: wall(obstacle)
0: empty
1 or more: `Food` (ID value means the size of `Food`)
(ID of `food` is 1)
*/
let levels = [
[
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, 5, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1],
[-1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1],
[-1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1],
[-1, 1, -1, 1, -1, -1, 1, -1, -1, 0, 0, -1, -1, 1, -1, -1, 1, -1, 1, -1],
[-1, 1, 1, 1, 1, 1, 1, -1, -3, 0, 0, -3, -1, 1, 1, 1, 1, 1, 1, -1],
[-1, 1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1],
[-1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -2, 1, 1, 1, -1, 1, -1],
[-1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1],
[-1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 5, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
], // level 0 (proven to be cleared)
[
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -2, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1],
[-1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1],
[-1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1],
[-1, 1, -1, 1, -1, -1, 1, -1, -1, 0, 5, -1, -1, 1, -1, -1, 1, -1, 1, -1],
[-1, 1, 1, 1, 1, 1, 1, -1, 5, 0, 0, -3, -1, 1, 1, 1, 1, 1, 1, -1],
[-1, 1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1],
[-1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1],
[-1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1],
[-1, 0, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, -3, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
], // level 1 (proven to be cleared)
[
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -3, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, -3, -1],
[-1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1],
[-1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1],
[-1, 1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 0, -1, 1, -1, -1, 1, -1, 1, -1],
[-1, 1, 1, 1, 1, 1, 1, -1, 0, 0, 0, 0, -1, 1, 1, 1, 1, 1, 1, -1],
[-1, 1, -1, 1, -1, -1, 1, -1, 0, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1],
[-1, 1, -1, 1, 1, 1, -2, 1, 1, 1, 1, 1, 1, -2, 1, 1, 1, -1, 1, -1],
[-1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1],
[-1, 5, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 5, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
] // level 2 (not proved yet)
];
let game = new Game(levels[2], boardDiv, noticeDiv);
let result;
let totalReward = 0;
let html = `
<h2>Reward: ` + totalReward + `, Done: ` + false + `</h2>
<h3>Possible actions:`;
for (let action of game.possible_actions) {
html += ' ' + action;
}
html += `</h3>`;
/*
for (let feature of Object.keys(game.feature_function())) {
for (let action of game.action_space) {
html += `
<h3>` + feature + `(` + action + `): ` + game.feature_function()[feature](game.state, action) + `</h3>`;
}
}
*/
noticeDiv.insertAdjacentHTML('beforeend', html);
document.addEventListener('keydown', (event) => {
if (event.keyCode === 37) {
game.virtualStep("left");
result = game.step("left");
}
else if (event.keyCode === 38) {
game.virtualStep("up");
result = game.step("up");
}
else if (event.keyCode === 39) {
game.virtualStep("right");
result = game.step("right");
}
else if (event.keyCode === 40) {
game.virtualStep("down");
result = game.step("down");
}
else {
return;
}
if (result != null) {
while (noticeDiv.hasChildNodes()) {
noticeDiv.removeChild(noticeDiv.firstChild);
}
totalReward += result.reward;
let html = `
<h2>Reward: ` + totalReward + `, Done: ` + result.done + `</h2>
<h3>Possible actions:`;
for (let action of game.possible_actions) {
html += ' ' + action;
}
html += `</h3>`;
/*
for (let feature of Object.keys(game.feature_function())) {
for (let action of game.action_space) {
html += `
<h3>` + feature + `(` + action + `): ` + game.feature_function()[feature](game.state, action) + `</h3>`;
}
}
*/
noticeDiv.insertAdjacentHTML('beforeend', html);
}
}, true);
</script>
</body>
</html>