Created
August 13, 2015 04:18
-
-
Save awhit012/dc4baeaed44cbc35e708 to your computer and use it in GitHub Desktop.
Console game for OO JavaScript practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var prompt = require('prompt'); | |
prompt.start(); | |
WELCOME = [ | |
"Welcome to Camel Run!" , | |
"You have stolen a camel (you egg!) to make your way across the great Mobi desert." , | |
"The townspeople want their camel back and are chasing you down! Survive your" , | |
"desert trek and outrun the blockos." , | |
"Only drink when necessary. Always use E. to display your status!" , | |
"Do not let your thirst go above 6 and your camel's tiredness above 8." | |
].join("\n") | |
MENU = [ | |
"Here are your choices:", | |
"A. Drink from your canteen.", | |
"B. Ahead moderate speed.", | |
"C. Ahead full speed.", | |
"D. Stop for the night.", | |
"E. Status check.", | |
"Q. Quit.", | |
].join("\n") | |
function Game() { | |
this.miles_traveled = 0, | |
this.thirst = 0, | |
this.camel_tiredness = 0, | |
this.blockos_travel = -30, | |
this.canteen = 5, | |
this.done = false, | |
this.oasis = function(){randomFromRange(8, 15)} | |
}; | |
Game.prototype.welcome = function(){ | |
console.log(WELCOME) | |
} | |
Game.prototype.turn = function(){ | |
that = this; | |
console.log('-----------------------') | |
console.log(MENU) | |
console.log('-----------------------') | |
prompt.get( 'choice', function(error, result){ | |
that.parseResponse(result) | |
}); | |
}; | |
Game.prototype.parseResponse = function(result){ | |
var choice = result['choice'].toUpperCase(); | |
if (choice === 'A'){ | |
this.glug() | |
} | |
else if (choice === 'B'){ | |
this.trot() | |
} | |
else if (choice === 'C'){ | |
this.gallop() | |
} | |
else if (choice === 'D'){ | |
this.rest() | |
} | |
else if (choice === 'Q'){ | |
process.exit() | |
} | |
game.status(); | |
game.checkInWithYourself(); | |
game.turn(); | |
} | |
Game.prototype.checkInWithYourself = function(){ | |
if(this.thirst >= 6){ | |
console.log('YOU DEAD!') | |
} | |
else if (this.camel_tiredness >= 8) { | |
console.log('Your camel must rest!') | |
this.rest() | |
} | |
else if (this.blockos_travel >= 0) { | |
console.log('YOU CAUGHT! you egg.') | |
} | |
else if(this.miles_traveled >= this.oasis){ | |
console.log('You reached an oasis! You filled your canteen and your belly.') | |
this.canteen += this.randomFromRange(1, 5); | |
this.thirst = 0; | |
this.oasis += this.randomFromRange(8, 15); | |
}; | |
} | |
Game.prototype.glug = function(){ | |
if(this.canteen === 0){ | |
console.log("You're out of water!"); | |
} | |
else { | |
this.thirst = 0; | |
this.canteen -= 1; | |
// blockos travel between 1-5 mi | |
this.blockos_travel += this.randomFromRange(1, 5); | |
console.log("You drank from your canteen. You have " + this.canteen + " drinks left.") | |
} | |
}; | |
Game.prototype.trot = function(){ | |
// travel between 5-12 mi | |
this.miles_traveled += this.randomFromRange(5, 13); | |
this.thirst += 1 | |
this.camel_tiredness += 2 | |
this.blockos_travel += this.randomFromRange(1, 10); | |
console.log("You are traveling at a trot.") | |
console.log("You have traveled " + this.miles_traveled + " miles so far.") | |
} | |
Game.prototype.gallop = function(){ | |
// travel between 11 - 20 mi | |
this.miles_traveled += this.randomFromRange(11, 20); | |
this.thirst += 2 | |
this.camel_tiredness += 3 | |
this.blockos_travel += this.randomFromRange(1, 10); | |
console.log("You galloping!") | |
console.log("You have traveled " + this.miles_traveled + " miles so far.") | |
} | |
Game.prototype.rest = function(){ | |
this.thirst += 2 | |
this.camel_tiredness = 0, | |
this.blockos_travel += this.randomFromRange(1, 10); | |
console.log("The camel is happy that you have chosen to stop for the night.") | |
} | |
Game.prototype.status = function(){ | |
console.log('-----------------------') | |
console.log('Miles traveled: ' + this.miles_traveled) | |
console.log('Thirst level: ' + this.thirst) | |
console.log('Camel tiredness: ' + this.camel_tiredness) | |
console.log('Blockos distance behind you: ' + this.blockos_travel ) | |
console.log('Drinks in canteen: ' + this.canteen ) | |
console.log('-----------------------') | |
console.log(this) | |
} | |
var game = new Game(); | |
game.welcome() | |
game.turn() | |
///HELPER METHODS | |
Game.prototype.randomFromRange = function(min, max){ | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment