diff --git a/impl/swl.js/src/swl.js b/impl/swl.js/src/swl.js index ef923af..5d7f705 100644 --- a/impl/swl.js/src/swl.js +++ b/impl/swl.js/src/swl.js @@ -52,9 +52,10 @@ this.subway = false; - this.done = false; this.skip = false; this.moved = false; + + this.gameState = 'stateTitleScreen'; var loc_adj = [ 'Rocky', @@ -133,8 +134,12 @@ this.arrows = 1; tty.write("SUPER WUMPUS LAND\n\n"); - this.show(); - this.ask(); + this.pause('statePrompt'); + }; + + this.pause = function(nextState) { + this.tty.write("\n[Press ENTER to continue.] "); + this.gameState = nextState; }; this.show = function() { @@ -179,15 +184,14 @@ print("" + room.tokens + " subway tokens lie on the ground here.\n"); } - if (room.guano > 0) - { - print("\nThere is a "); - if (room.guano === 1) { print("small"); } - else if (room.guano === 2) { print("sizable"); } - else if (room.guano === 3) { print("large"); } - else if (room.guano === 4) { print("huge"); } - else { print("gigantic"); } - print(" pile of Wumpus dung here.\n"); + if (room.guano > 0) { + print("\nThere is a "); + if (room.guano === 1) { print("small"); } + else if (room.guano === 2) { print("sizable"); } + else if (room.guano === 3) { print("large"); } + else if (room.guano === 4) { print("huge"); } + else { print("gigantic"); } + print(" pile of Wumpus dung here.\n"); } print("\n"); @@ -205,9 +209,8 @@ if (this.codliver > 0) { print(" ...and immediately BARFED YOU BACK OUT!!!!\n"); } else { - this.pause(); - this.done = true; - return; + this.pause('stateGameOver'); + return false; } } } else { @@ -232,10 +235,10 @@ print("* Super Bats in this location stay well away from your awful stench!\n"); } else { print("* Zap! Super Bat Snatch! Elsewheresville for you!\n"); - this.pause(); this.roomNo = d(1,100); this.skip = 1; - return; + this.pause('statePrompt'); + return false; } } var batsNearby = false; @@ -255,9 +258,8 @@ print("* You deftly stick to the edge of the bottomless pit!\n"); } else { print("* Yiiiieeee!!! Fell in a pit!\n"); - this.pause(); - this.done = true; - return; + this.pause('stateGameOver'); + return false; } } var aDraft = false; @@ -285,6 +287,7 @@ } } print("\n"); + return true; }; this.ask = function() { @@ -342,21 +345,66 @@ print(" [I]nventory and Score\n\n"); print(" [Q]uit\n\n"); print("What would you like to do next, " + this.name + "? "); - }; - - this.pause = function() { - this.tty.write("\n[Press ENTER to continue.] "); - this.paused = true; - }; - - this.handleInput = function(input) { - if (this.paused) { - this.paused = false; - this.show(); + + this.gameState = 'stateProcessCommand'; + }; + + this.moveWumpi = function() { + var self = this; + var print = function(str) { + self.tty.write(str); + }; + + for (var i = 0; i < this.wumpi.length; i++) { + var wumpus = this.wumpi[i]; + if (wumpus.room === 0) { + if (d(1, 5) === 1) { + // restart wumpus + wumpus.room = d(1, 100); + while (wumpus.room === this.roomNo) { + wumpus.room = d(1, 100); + } + wumpus.asleep = false; + } + continue; + } + if (wumpus.asleep) { + if (d(1, 4) === 1) { + wumpus.asleep = false; + if (d(1, 5) !== 1) { + this.rooms[wumpus.room].guano++; + } + } + } else { + if (d(1, 3) === 1) { + var dest = this.rooms[wumpus.room].exits[d(1,3)-1]; + if (dest !== this.roomNo || !this.moved) { + wumpus.room = dest; + if (dest === this.roomNo) { + print("From around a corner, a hungry-looking Wumpus appears!!\n"); + this.pause(); + } + } + } + if (d(1,8) === 1) { wumpus.asleep = true; } + if (d(1,8) === 1) { this.rooms[wumpus.room].guano++; } + } + } + }; + + /* -*-*-*- GAME STATES -*-*-*- */ + + this.statePrompt = function(input) { + if (this.show()) { this.ask(); - return; - } - + } + }; + + this.stateGameOver = function(input) { + this.tty.write("Game over, " + this.name + ".\n"); + }; + + this.stateProcessCommand = function(input) { var self = this; var print = function(str) { self.tty.write(str); @@ -365,12 +413,12 @@ input = input.toUpperCase(); if (input === 'Q') { - this.done = true; + this.pause('stateGameOver'); return; } else if (input === 'I') { //alert('ya'); print("Your score is 7."); - this.pause(); + this.pause('statePrompt'); return; } else if (input === 'A' && this.cans > 0) { this.cans--; @@ -411,7 +459,8 @@ if (this.codliver > 0) this.codliver = 1; if (this.batbgon > 0) this.batbgon = 1; } - this.pause(); + this.pause('statePrompt'); + return; } else if (input === 'D' && room.guano > 0) { this.ustink += d(3,3); room.guano--; @@ -419,7 +468,7 @@ if (d(1,6) == 1) { room.cans++; } if (d(1,12) == 1) { room.tokens++; } print("\nEw. You now stink so bad that you can't smell anything but yourself.\n"); - this.pause(); + this.pause('statePrompt'); return; } else if (input === 'P') { this.arrows += room.arrows; room.arrows = 0; @@ -439,50 +488,12 @@ this.moveWumpi(); - this.show(); - this.ask(); - }; - - this.moveWumpi = function() { - var self = this; - var print = function(str) { - self.tty.write(str); - }; - - for (var i = 0; i < this.wumpi.length; i++) { - var wumpus = this.wumpi[i]; - if (wumpus.room === 0) { - if (d(1, 5) === 1) { - // restart wumpus - wumpus.room = d(1, 100); - while (wumpus.room === this.roomNo) { - wumpus.room = d(1, 100); - } - wumpus.asleep = false; - } - continue; - } - if (wumpus.asleep) { - if (d(1, 4) === 1) { - wumpus.asleep = false; - if (d(1, 5) !== 1) { - this.rooms[wumpus.room].guano++; - } - } - } else { - if (d(1, 3) === 1) { - var dest = this.rooms[wumpus.room].exits[d(1,3)-1]; - if (dest !== this.roomNo || !this.moved) { - wumpus.room = dest; - if (dest === this.roomNo) { - print("From around a corner, a hungry-looking Wumpus appears!!\n"); - this.pause(); - } - } - } - if (d(1,8) === 1) { wumpus.asleep = true; } - if (d(1,8) === 1) { this.rooms[wumpus.room].guano++; } - } - } + if (this.show()) { + this.ask(); + } + }; + + this.handleInput = function(input) { + this[this.gameState](input); }; };