-
-
Save codecademydev/8ea8dcc74e5ee8766be693872c0561fd 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 = '*'; | |
const player = 'H'; | |
class Field { | |
constructor(field){ | |
this._field = field; | |
this._gameOver = false; | |
this._playerX = 0; | |
this._playerY = 0; | |
} | |
get field(){ | |
return this._field; | |
} | |
static generateField(height, width, percent = 25){ | |
//Creates a new field by filling in the width as a row, then repeating until desired height is reached. | |
const newField = []; | |
//Create an empty field | |
for(let i = 0; i < height; i++){ | |
const tempArr = []; | |
//Fill in row of field | |
for(let j = 0; j < width; j++){ | |
tempArr.push(fieldCharacter); | |
} | |
//Add to final field | |
newField.push(tempArr); | |
} | |
//Add holes | |
//Calc number of holes needed by taking the percentage from the total area | |
const numHoles = Math.ceil((percent/100)*(height*width)); | |
let i=0; | |
for(let i = 0; i < numHoles; i++){ | |
let randX = this.pos(width); | |
let randY = this.pos(height); | |
while(newField[randX][randY] === hole){ | |
randX = this.pos(width); | |
randY = this.pos(height); | |
} | |
newField[randX][randY] = hole; | |
randX = this.pos(width); | |
randY = this.pos(height); | |
} | |
let currentX = this.pos(width); | |
let currentY = this.pos(height); | |
newField[currentY][currentX] = hat; | |
return newField; | |
} | |
static pos(axis){ | |
return Math.floor(Math.random() * axis); | |
} | |
playGame(){ | |
this._field[this._playerY][this._playerX] = player; | |
this.print(); | |
while(!this._gameOver){ | |
const direction = prompt('Which direction do you want to go?'); | |
this.move(direction); | |
this.print(); | |
} | |
} | |
print(){ | |
//uses map() to join the inner array elements, then join() to join the outer elements. | |
console.log(this._field.map(x=>{return x.join('');}).join('\n')); | |
} | |
move(direction){ | |
this._field[this._playerY][this._playerX] = pathCharacter; | |
direction = direction.toLowerCase(); | |
switch(direction){ | |
case 'l': | |
this._playerX += 1; | |
break; | |
case 'r': | |
this._playerX -= 1; | |
break; | |
case 'u': | |
this._playerY -= 1; | |
break; | |
case 'd': | |
this._playerY += 1; | |
break; | |
default: | |
console.log('You\'ve entered an invalid direction.\nPlease enter l, r, u or d'); | |
break; | |
} | |
if(this.outOfBounds()){ | |
console.log('You went out of bounds!\nGAME OVER!'); | |
this._gameOver = true; | |
}else{ | |
//Array has to be accessed outside/y-coord first | |
const currentLoc = this._field[this._playerY][this._playerX]; | |
if(currentLoc === 'O'){ | |
//Fell in hole | |
console.log('You fell in a hole!\nGAME OVER!'); | |
this._gameOver = true; | |
}else if(currentLoc === '^'){ | |
//Found hat | |
this._field[this._playerY][this._playerX] = player; | |
console.log('You found your hat!\nGAME OVER!'); | |
this._gameOver = true; | |
}else{ | |
//Move | |
this._field[this._playerY][this._playerX] = player; | |
this._gameOver = false; | |
} | |
} | |
} | |
outOfBounds(){ | |
if(this._playerX < 0 || this._playerX >= this._field[0].length || this._playerY < 0 || this._playerY >= this._field.length){ | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
} | |
/* | |
const testField = new Field([ | |
['*', '░', 'O'], | |
['░', 'O', '░'], | |
['░', '^', '░'], | |
]); | |
testField.playGame(); | |
*/ | |
const myField = new Field(Field.generateField(12, 12, 20)); | |
myField.playGame(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment