-
-
Save codecademydev/06e54066e1016fb1f4951313a1adb35e to your computer and use it in GitHub Desktop.
Codecademy export
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
const prompt = require('prompt-sync')({sigint: true}); | |
const hat = '^'; | |
const hole = 'O'; | |
const fieldCharacter = '░'; | |
const pathCharacter = '*'; | |
class Field { | |
constructor(field){ | |
this._field = field; | |
this._running = true; | |
this._direction = ''; | |
this._playerX = 0; | |
this._playerY = 0; | |
} // constructor | |
get field(){ | |
return this._field; | |
} // get Field | |
print(){ | |
const numRows = this._field.length; | |
const numCols = this._field[0].length; | |
for(let y=0; y<numRows; y++){ | |
let output = ''; | |
for(let x=0; x<numCols; x++){ | |
output += this._field[y][x]; | |
} | |
console.log(output); | |
} | |
getInput(){ | |
this._direction = prompt('Which way do you wish to go?'); | |
} // getInput | |
movePlayer(y, x){ | |
// Move the player to the specifed yx coordinates in the field | |
this._playerY += y; | |
this._playerX += x; | |
} | |
checkFieldTile(y, x){ | |
/* Checks the field tile the player has gone to and then | |
either the player looses, wins or we just update the path | |
the player has taken | |
*/ | |
if(typeof this._field[y] !== 'undefined'){ | |
if(typeof this._field[x] !== 'undefined'){ | |
if(this._field[y][x] == hole){ | |
// Player fell in a hole so it's game over man | |
this._running = false; | |
console.log('You fell into a hole. GAME OVER'); | |
} | |
if(this._field[y][x] == hat){ | |
// Player found their hat. Well done player, YAY!!!!! | |
this._running = false; | |
console.log('You found your hat. CONGRATULATIONS you win.'); | |
} | |
if(this._field[y][x] == fieldCharacter){ | |
// Player stepped onto a normal field | |
// Update the field to say the player has been here before | |
this._field[y][x] = pathCharacter; | |
console.log('You discovered a new field and nothing of interest.'); | |
} | |
} | |
} | |
if((y < 0 || y > (this._field.length -1)) || (x < 0 || x > (this._field[0].length -1))){ | |
// Player *fell* out of the world | |
this._running = false; | |
console.log('Whoops! You fell out the world.'); | |
} | |
} | |
processInput(){ | |
if(this._direction.toLowerCase() == 'u'){ | |
// UP | |
this.movePlayer(-1, 0); | |
} else if (this._direction.toLowerCase() == 'd'){ | |
// DOWN | |
this.movePlayer(1, 0); | |
} else if (this._direction.toLowerCase() == 'l'){ | |
// LEFT | |
this.movePlayer(0, -1); | |
} else if (this._direction.toLowerCase() == 'r'){ | |
// RIGHT | |
this.movePlayer(0, 1); | |
} else if (this._direction.toLowerCase() == 'q'){ | |
// QUIT | |
console.log("The player quit the game."); | |
this._running = false; | |
} else { | |
console.log("Please use a valid direction or q to quit."); | |
} | |
// Check the tile we have moved to and determine next cause of action | |
this.checkFieldTile(this._playerY, this._playerX); | |
} // processInput | |
run() { | |
while(this._running){ | |
this.print(); | |
this.getInput(); | |
this.processInput(); | |
} | |
} // run | |
static generateField(rows, cols){ | |
// Generates a new field of the specified size | |
let field = new Array(rows); | |
for( let i = 0; i < field.length; i++){ | |
field[i] = new Array(cols); | |
} | |
// Loop through the array and set player start, holes and hat(s) | |
for ( let y = 0; y < field.length; y++){ | |
for ( let x = 0; x < field[0].length; x++){ | |
if(y == 0 && x == 0 ){ | |
// Player Start | |
field[y][x] = pathCharacter; | |
} else { | |
// Generate an random number between 0 and 50 for the field tile type | |
let tileType = Math.floor(Math.random() * 50); | |
if(tileType >=0 && tileType <= 10){ | |
// Hole | |
field[y][x] = hole; | |
} else if (tileType >=11 && tileType <= 49){ | |
// Field | |
field[y][x] = fieldCharacter; | |
} | |
} | |
} | |
} | |
// Add in the hat | |
let hatX = Math.floor(Math.random() * field[0].length); | |
let hatY = Math.floor(Math.random() * field.length); | |
field[hatY][hatX] = hat; | |
return field; | |
} | |
} | |
/* | |
gameField = [ | |
['*', '░', 'O'], | |
['░', 'O', '░'], | |
['░', '^', '░'], | |
]; | |
*/ | |
gameField = Field.generateField(10, 10); | |
const game = new Field(gameField); | |
game.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment