Skip to content
Browse files

Implement game states.

  • Loading branch information...
1 parent 0c63dd6 commit 2e8866ea85cb184b173cf6485384f5407b553ac0 @cpressey cpressey committed Mar 21, 2013
Showing with 87 additions and 76 deletions.
  1. +87 −76 impl/swl.js/src/swl.js
View
163 impl/swl.js/src/swl.js
@@ -52,10 +52,11 @@ SuperWumpusLand = function() {
this.subway = false;
- this.done = false;
this.skip = false;
this.moved = false;
+ this.gameState = 'stateTitleScreen';
+
var loc_adj = [
'Rocky',
'Stony',
@@ -133,8 +134,12 @@ SuperWumpusLand = function() {
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 @@ SuperWumpusLand = function() {
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 @@ SuperWumpusLand = function() {
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 @@ SuperWumpusLand = function() {
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 @@ SuperWumpusLand = function() {
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 @@ SuperWumpusLand = function() {
}
}
print("\n");
+ return true;
};
this.ask = function() {
@@ -342,21 +345,66 @@ SuperWumpusLand = function() {
print(" [I]nventory and Score\n\n");
print(" [Q]uit\n\n");
print("What would you like to do next, " + this.name + "? ");
+
+ this.gameState = 'stateProcessCommand';
};
-
- this.pause = function() {
- this.tty.write("\n[Press ENTER to continue.] ");
- this.paused = true;
+
+ 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++; }
+ }
+ }
};
- this.handleInput = function(input) {
- if (this.paused) {
- this.paused = false;
- this.show();
+ /* -*-*-*- 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 @@ SuperWumpusLand = function() {
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,15 +459,16 @@ SuperWumpusLand = function() {
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--;
if (d(1,3) == 1) { room.arrows++; }
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 @@ SuperWumpusLand = function() {
this.moveWumpi();
- this.show();
- this.ask();
+ if (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++; }
- }
- }
+ this.handleInput = function(input) {
+ this[this.gameState](input);
};
};

0 comments on commit 2e8866e

Please sign in to comment.
Something went wrong with that request. Please try again.