-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathmaze.js
More file actions
306 lines (250 loc) · 7.79 KB
/
maze.js
File metadata and controls
306 lines (250 loc) · 7.79 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//
// Copyright (c) 2017 Cisco Systems
// Licensed under the MIT License
//
function debug(entry) {
console.log(entry)
};
function fine(entry) {
console.debug(entry)
};
function Maze(structure, walls, phrases) {
this.structure = structure;
this.walls = walls;
this.phrases = phrases;
};
Maze.prototype.tryMove = function (direction) {
debug(`trying ${direction}`)
var result
switch (direction) {
case 'up':
result = this._move(this.position, -1, 0);
break
case 'down':
result = this._move(this.position, 1, 0)
break
case 'left':
result = this._move(this.position, 0, -1)
break
case 'right':
result = this._move(this.position, 0, 1)
break
default:
throw new Error("unknown direction");
}
this.position = result.pos
result.direction = direction
return result
}
Maze.prototype.up = function () {
return this.tryMove('up');
}
Maze.prototype.down = function () {
return this.tryMove('down');
}
Maze.prototype.left = function () {
return this.tryMove('left');
}
Maze.prototype.right = function () {
return this.tryMove('right');
}
Maze.prototype._move = function (pos, x, y) {
let newX = pos[0] + x
let newY = pos[1] + y
let thing = this.structure[newX][newY]
// What did we meed
var story = this.phrases[thing]
debug('> ' + story)
// If this is a wall, stay at current position
if (this.walls.includes(thing)) {
fine(`still at: ${pos}`)
this.position = pos
return {
'success': false,
'outcome': story,
'thing': this.look(pos),
'pos': pos
}
}
var newPos = [newX, newY]
fine(`now at: ${newPos}`)
this.position = pos
return {
'success': true,
'thing': this.look(newPos),
'pos': newPos
}
}
Maze.prototype.look = function (pos) {
var x = pos[0]
var y = pos[1]
var thing = this.structure[x][y]
return this.phrases[thing];
}
// Use this function if the output supports Newlines
Maze.prototype.buildMap = function () {
var pos = this.position
var poster = ""
for (var y = 0; y < this.structure.length; y++) {
var line = ""
for (var x = 0; x < this.structure[0].length; x++) {
var char = this.structure[y][x]
if ((y == pos[0]) && x == pos[1]) {
char = 'o'
}
line += char
}
poster += line + "\n"
}
debug("poster:\n" + poster)
return poster
}
// Use this function if the output does NOT supports newlines
Maze.prototype.buildMapAsWrapped = function (linewidth, skipBorders) {
var map = ""
var pos = this.position
for (var y = 0; y < this.structure.length; y++) {
if (!(skipBorders && ((y == 0) || (y == this.structure.length - 1)))) {
var line = ""
for (var x = 0; x < this.structure[0].length; x++) {
var char = this.structure[y][x]
if ((y == pos[0]) && x == pos[1]) {
char = 'o'
}
line += char
}
var left = Math.round((linewidth - line.length) / 2)
var mazeline = line.padStart(left + line.length, "-")
map += mazeline.padEnd(linewidth, "-")
map += " "
}
}
debug("maze map:" + map)
return map
}
Maze.prototype.pickInitialPosition = function (emptyChar) {
if (!emptyChar) emptyChar = ' '
// Ping a random number, on an empty spot
while (true) {
var y = Math.round(Math.random() * (this.structure.length - 2) + 1)
var x = Math.round(Math.random() * (this.structure[0].length - 2) + 1)
fine(`picked x: ${x}, y: ${y}`)
if (this.structure[y][x] == emptyChar) {
fine(`position is clear, storing...`)
return this.setInitialPosition(x, y)
}
}
}
Maze.prototype.setInitialPosition = function (x, y) {
this.position = [y, x]
return this.position
}
//
// Utilities
//
function showProgress(count, delay, cb, final, initial) {
var counter = 0
if (!initial) initial = ""
var progress = ".....".padEnd(count, '.')
var timer = setInterval(function (event) {
cb(initial + progress.substring(0, counter + 1))
counter++;
if (counter >= count) {
clearInterval(timer)
cb(final)
}
}, delay)
}
//
// Room Controls
//
const xapi = require('xapi');
function showInstructions(message) {
console.debug('updating instructions')
xapi.command('UserInterface Extensions Widget SetValue', {
WidgetId: 'instructions',
Value: message
})
}
// Displays the outcome of the latest move
function showOutcome(res, initial) {
console.debug('computing instructions from latest move')
if (res.success) {
// Tell the thing met at the new location
showProgress(5, 500, showInstructions, res.thing, initial)
return
}
// Tell the reason why the move was not successful
showProgress(5, 500, showInstructions, res.outcome, initial)
}
// Displays help on the Touch10 or send the configuration to Cisco Spark
function showHelp() {
console.log("showing map in Alert Panel")
// Width depends on device
// [TODO] check device
xapi.status.get("SystemUnit ProductPlatform").then((product) => {
console.debug(`running on a ${product}`)
let width = 51 // when the Alert is shown on a Touch 10
if (product == "DX80") {
console.debug('has no Touch10 attached')
width = 31 // when the Alert is shown on a screen
}
// show Aletr panel
xapi.command('UserInterface Message Alert Display', {
Title: 'With a little help from ... the bot',
Text: game.buildMapAsWrapped(width, true),
Duration: 5
})
})
}
function onGui(event) {
if (event.Type == 'clicked') {
if (event.WidgetId == 'directions') {
var direction = event.Value
if (direction == 'center') {
showHelp()
return
}
showInstructions(`moving ${direction}`)
var res = game.tryMove(direction)
showOutcome(res, `moving ${direction}`)
return
}
if (event.WidgetId == 'restart') {
restart()
return
}
// No more event listener
return
}
}
xapi.event.on('UserInterface Extensions Widget Action', onGui);
function restart() {
console.log('resetting the maze')
var structure = []
structure[0] = ['|', '-', '-', '-', '-', '-', '|']
structure[1] = ['|', '_', 'C', '_', '_', '_', '|']
structure[2] = ['|', '_', '_', 'X', '_', '_', '|']
structure[3] = ['|', '_', 'X', '?', 'X', '_', '|']
structure[4] = ['|', '_', '_', '_', '_', 'X', '|']
structure[5] = ['|', '-', '-', '-', '-', '-', '|']
var walls = ['|', '-', 'X']
var phrases = {}
phrases['|'] = "cannot get there, this a maze border you just hitted"
phrases['-'] = "cannot get there, this a maze border you just hitted"
phrases['|'] = "cannot get there, this a maze border you just hitted"
phrases['X'] = "ouch, you bumped a wall"
phrases['_'] = "nothing here, let's continue exploring."
phrases['C'] = "hello kitty, you look hungry. Are you lost too? Jump in."
phrases['D'] = "WOW, an agressive dog is lying here. Better run away!"
phrases['?'] = "CONGRATS, you found the treasure!!!"
game = new Maze(structure, walls, phrases)
game.pickInitialPosition('_')
showProgress(10, 200, showInstructions, 'Then here you are: lost in an hostile maze, looking for a treasure. Pick a direction...', 'initializing the Maze')
}
var game;
if (!game) {
// First run:
// - initialize maze
restart()
}