Implement more logic of Wumpus behaviour.
catseye
10 years ago
17 | 17 | this.guano = 0; |
18 | 18 | this.bats = 0; |
19 | 19 | this.pits = 0; |
20 | }; | |
21 | }; | |
22 | ||
23 | Wumpus = function() { | |
24 | this.init = function() { | |
25 | this.room = d(1, 100); | |
26 | this.asleep = (d(1, 2) === 1); | |
20 | 27 | }; |
21 | 28 | } |
22 | 29 | |
74 | 81 | 'Expanse' |
75 | 82 | ]; |
76 | 83 | |
84 | var backlinks = []; | |
77 | 85 | for (var i = 0; i < 10; i++) |
78 | 86 | { |
79 | for (var j = 0; j < 10; j++) | |
80 | { | |
81 | var r = i * 10 + j + 1; | |
82 | var r1 = d(1,100); | |
83 | var r2 = d(1,100); | |
84 | var r3 = d(1,100); | |
85 | var room = new Room(); | |
86 | room.init(loc_adj[i] + ' ' + loc_noun[j], [r1, r2, r3]); | |
87 | this.rooms[r] = room; | |
88 | // backlink exits | |
89 | if (this.rooms[r1] !== undefined) { | |
90 | this.rooms[r1].exits[0] = r; | |
91 | } | |
92 | if (this.rooms[r2] !== undefined) { | |
93 | this.rooms[r2].exits[0] = r; | |
94 | } | |
95 | if (this.rooms[r3] !== undefined) { | |
96 | this.rooms[r3].exits[0] = r; | |
97 | } | |
98 | } | |
87 | for (var j = 0; j < 10; j++) | |
88 | { | |
89 | var r = i * 10 + j + 1; | |
90 | var r1 = d(1,100); | |
91 | var r2 = d(1,100); | |
92 | var r3 = d(1,100); | |
93 | var room = new Room(); | |
94 | room.init(loc_adj[i] + ' ' + loc_noun[j], [r1, r2, r3]); | |
95 | this.rooms[r] = room; | |
96 | backlinks.push([r1, r]); | |
97 | backlinks.push([r2, r]); | |
98 | backlinks.push([r3, r]); | |
99 | } | |
100 | } | |
101 | ||
102 | for (var i = 0; i < backlinks.length; i++) { | |
103 | var src = backlinks[i][0]; | |
104 | var dest = backlinks[i][1]; | |
105 | var exit = Math.floor(Math.random() * 3); | |
106 | this.rooms[src].exits[exit] = dest; | |
99 | 107 | } |
100 | 108 | |
101 | 109 | for (var i = 0; i < 42; i++) |
108 | 116 | |
109 | 117 | for (var i = 0; i < 5; i++) |
110 | 118 | { |
111 | this.wumpi[i] = d(1,100); | |
119 | this.wumpi[i] = new Wumpus(); | |
120 | this.wumpi[i].init(); | |
112 | 121 | } |
113 | 122 | for (var i = 0; i < 11; i++) |
114 | 123 | { |
184 | 193 | |
185 | 194 | var smellWumpus = false; |
186 | 195 | for (var i = 0; i < this.wumpi.length; i++) { |
187 | if (room.exits[i] === this.wumpi[i]) { | |
188 | smellWumpus = true; | |
189 | break; | |
196 | if (this.wumpi[i].room === this.roomNo) { | |
197 | if (!this.wumpi[i].asleep) { | |
198 | if (this.subway) { | |
199 | print("* Right outside the train window is a Wumpus!\n"); | |
200 | } else if (this.camo) { | |
201 | print("* Good thing the Wumpus here can't see you.\n"); | |
202 | } else { | |
203 | print("* Oh No, " + this.name + "! A Wumpus ATE YOU UP!!!\n"); | |
204 | if (this.codliver > 0) { | |
205 | print(" ...and immediately BARFED YOU BACK OUT!!!!\n"); | |
206 | } else { | |
207 | this.pause(); | |
208 | this.done = true; | |
209 | return; | |
210 | } | |
211 | } | |
212 | } else { | |
213 | print("* There's a Wumpus asleep RIGHT IN FRONT OF YOU!!\n"); | |
214 | } | |
215 | } | |
216 | for (var exit = 0; exit < 3; exit++) { | |
217 | if (room.exits[exit] === this.wumpi[i].room) { | |
218 | smellWumpus = true; | |
219 | break; | |
220 | } | |
190 | 221 | } |
191 | 222 | } |
192 | 223 | if (smellWumpus && !this.ustink) { |
339 | 370 | } |
340 | 371 | } |
341 | 372 | } |
373 | ||
374 | this.moveWumpi(); | |
375 | ||
342 | 376 | this.show(); |
343 | 377 | this.ask(); |
344 | 378 | }; |
345 | }⏎ | |
379 | ||
380 | this.moveWumpi = function() { | |
381 | var self = this; | |
382 | var print = function(str) { | |
383 | self.tty.write(str); | |
384 | }; | |
385 | ||
386 | for (var i = 0; i < this.wumpi.length; i++) { | |
387 | var wumpus = this.wumpi[i]; | |
388 | if (wumpus.room === 0) { | |
389 | if (d(1, 5) === 1) { | |
390 | // restart wumpus | |
391 | wumpus.room = d(1, 100); | |
392 | while (wumpus.room === this.roomNo) { | |
393 | wumpus.room = d(1, 100); | |
394 | } | |
395 | wumpus.asleep = false; | |
396 | } | |
397 | continue; | |
398 | } | |
399 | if (wumpus.asleep) { | |
400 | if (d(1, 4) === 1) { | |
401 | wumpus.asleep = false; | |
402 | if (d(1, 5) !== 1) { | |
403 | this.rooms[wumpus.room].guano++; | |
404 | } | |
405 | } | |
406 | } else { | |
407 | if (d(1, 3) === 1) { | |
408 | var dest = this.rooms[wumpus.room].exits[d(1,3)-1]; | |
409 | if (dest !== this.roomNo || !this.moved) { | |
410 | wumpus.room = dest; | |
411 | if (dest === this.roomNo) { | |
412 | print("From around a corner, a hungry-looking Wumpus appears!!\n"); | |
413 | this.pause(); | |
414 | } | |
415 | } | |
416 | } | |
417 | if (d(1,8) === 1) { wumpus.asleep = true; } | |
418 | if (d(1,8) === 1) { this.rooms[wumpus.room].guano++; } | |
419 | } | |
420 | } | |
421 | }; | |
422 | }; |