-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
231 lines (202 loc) · 7.03 KB
/
game.js
File metadata and controls
231 lines (202 loc) · 7.03 KB
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
goog.provide('soko.game');
goog.require('soko.Solver');
goog.require('soko.Level');
goog.require('soko.Heap');
goog.require('soko.constants');
goog.require('soko.levels.microa');
goog.require('soko.levels.microc');
goog.scope(function() {
var constants = soko.constants;
/**
* @typedef {{name: string, levels: !Array.<!soko.Level>}}
*/
soko.LevelPack;
/**
* Collections of levels (manually converted from android boxpusher game)
* @type {!Array.<soko.LevelPack>}
*/
soko.levelpacks = [
{"name": "microa", "levels": soko.levels.microa},
{"name": "microc", "levels": soko.levels.microc}
];
/**
* @constructor
*/
soko.Game = function() {
this.levelPackIndex = 0;
this.levelIndex = 0;
this.solution = undefined;
this.nameEl = document.getElementById('name');
this.resultsEl = document.getElementById('results');
this.allResultsEl = document.getElementById('results-all');
this.heuristicsEl = document.getElementById('heuristics');
var canvas = document.getElementById('canvas');
this.context = canvas.getContext('2d');
this.level = undefined;
this.state = undefined;
this.solvers = undefined;
this.heuristics = undefined;
this.solverIndex = 0;
this.advanceLevel(0);
document.getElementById('next').onclick = this.advanceLevel.bind(this, 1);
document.getElementById('prev').onclick = this.advanceLevel.bind(this, -1);
document.getElementById('solve').onclick = this.solve.bind(this, undefined);
document.getElementById('solve-all').onclick = this.solveAll.bind(this);
document.getElementById('move').onclick = this.move.bind(this);
document.addEventListener('keypress', this.onKeyPress.bind(this), false);
};
soko.Game.prototype.updateHeuristicsScores = function() {
var info = '';
this.heuristics.forEach(function(heuristic) {
info += heuristic[0] + ' ' + heuristic[1].evaluate(this.state) + '<br>';
}.bind(this));
this.heuristicsEl.innerHTML = info;
};
/**
* Advance to the next level.
* @param {number} offset the offset to add to current level.
*/
soko.Game.prototype.advanceLevel = function(offset) {
var levelpacks = soko.levelpacks;
this.levelIndex += offset;
if (this.levelIndex < 0) {
this.levelPackIndex = (this.levelPackIndex - 1 + levelpacks.length) % levelpacks.length;
this.levelIndex = levelpacks[this.levelPackIndex].levels.length - 1;
}
if (this.levelIndex >= levelpacks[this.levelPackIndex].levels.length) {
this.levelPackIndex = (this.levelPackIndex + 1) % levelpacks.length;
this.levelIndex = 0;
}
this.nameEl.innerHTML = levelpacks[this.levelPackIndex].name +
' level:' + this.levelIndex;
this.level = new soko.Level(
levelpacks[this.levelPackIndex].levels[this.levelIndex]);
this.state = this.level.getInitialState();
this.level.draw(this.state, this.context);
this.solution = [];
this.solvers = [];
this.heuristics = [
['Simple', new soko.heuristic.SimpleHeuristic(this.level)],
['Better', new soko.heuristic.BetterHeuristic(this.level)],
['Abstract', new soko.heuristic.AbstractHeuristic(this.level)]
//['Max', new soko.heuristic.MaxHeuristic(level)]
];
this.resultsEl.innerHTML = '';
this.updateHeuristicsScores();
};
/**
* Some solvers can take some time on some levels. So run each one individually
* using set timeout and queue the others after it is done so we can get some
* quick hacky feedback
*/
soko.Game.prototype.solveSingle = function(finishedCallback) {
if (this.solverIndex < this.solvers.length) {
var solver = this.solvers[this.solverIndex];
var solverSolution = solver[1].solve(this.level, this.state);
if (this.solverIndex == 0) {
this.solution = solverSolution;
this.state = this.solution.pop();
}
}
var results = '<table><thead><td>name</td><td>elapsed time</td>' +
'<td>expanded</td><td>sol.length</td></thead>';
this.solvers.forEach(function(solver) {
results += '<tr><td>' + solver[0] + '</td><td>' +
solver[1].solverStats.elapsedTime + '</td><td>' +
solver[1].solverStats.nodesVisited + '</td><td>' +
solver[1].solverStats.solutionLength + '</td></tr>';
});
results += '</table>';
this.resultsEl.innerHTML = results;
this.solverIndex++;
if (this.solverIndex < this.solvers.length) {
setTimeout(this.solveSingle.bind(this, finishedCallback), 1);
} else if (finishedCallback) {
finishedCallback();
}
};
/**
* Queue up all the solvers to run.
*/
soko.Game.prototype.solve = function(finishedCallback) {
if (this.solution === undefined ||
this.solution.length == 0) {
this.solverIndex = 0;
this.solvers = [
["abs+cond", new soko.Solver(soko.heuristic.AbstractHeuristic, soko.Heap, true)],
["abs", new soko.Solver(soko.heuristic.AbstractHeuristic, soko.Heap, false)],
["simple+cond", new soko.Solver(soko.heuristic.SimpleHeuristic, soko.Heap, true)],
["simple", new soko.Solver(soko.heuristic.SimpleHeuristic, soko.Heap)],
// Ideally we could use a queue here, which has less overhead, but that would give min
// number of box pushes (what box pusher uses) rather than min num moves.
["bfs+conde", new soko.Solver(soko.heuristic.NullHeuristic, soko.Heap, true)],
["bfs", new soko.Solver()]
];
this.solveSingle(finishedCallback);
}
};
soko.Game.prototype.solveAll = function() {
var results = '';
function solveFinished() {
var line = '';
if (results == '') {
line = '<table><thead><tr>';
this.solvers.forEach(function(solver) {
line += '<td>' + solver[0] + '</td>';
});
line += '</tr></thead>';
}
line += '<tr>';
this.solvers.forEach(function(solver) {
line += '<td>' + solver[1].solverStats.elapsedTime + '</td>';
});
line += '</tr>';
results += line;
this.allResultsEl.innerHTML = results;
this.advanceLevel(1);
if (this.levelIndex > 0 || this.levelPackIndex > 0) {
this.solve(solveFinished.bind(this));
}
};
this.solve(solveFinished.bind(this));
};
soko.Game.prototype.move = function() {
if (this.solution !== undefined && this.solution.length > 0) {
this.state = this.solution.pop();
this.updateHeuristicsScores();
this.level.draw(this.state, this.context);
}
};
soko.Game.prototype.onKeyPress = function(event) {
var level = this.level;
var code = event.keyCode || event.charCode;
if (code == 49 || code == 50) {
this.advanceLevel(code == 49 ? -1 : 1);
}
if (code == 119) {
level.move(this.state, constants.Directions.UP);
}
if (code == 97) {
level.move(this.state, constants.Directions.LEFT);
}
if (code == 100) {
level.move(this.state, constants.Directions.RIGHT);
}
if (code == 115) {
level.move(this.state, constants.Directions.DOWN);
}
if (code == 63) { // ?
// Show the invalid states for this level.
var invalidMap = new soko.heuristic.InvalidMap(level);
for (var y = 1; y < level.grid.length - 1; y++) {
for (var x = 1; x < level.grid[y].length - 1; x++) {
if (invalidMap.isInvalidPoint([x, y])) {
this.level.grid[y][x] |= 0x80;
}
}
}
}
this.updateHeuristicsScores();
this.level.draw(this.state, this.context);
};
});