-
-
Save codecademydev/8c4111924fc96b27baca17a81ae4834d 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 outOfBounds = "Out of bounds instruction."; | |
const foundHat = "Congrats! You found your hat."; | |
const fellDown = "Sorry, you fell down a hole."; | |
const invalid = "This is an invalid direction."; | |
const success = "You have completed the game. Well done!"; | |
const failure = "You failed this time. Play again!"; | |
class Field { | |
constructor() { | |
this._field = []; | |
this._currentPosX = 0; | |
this._currentPosY = 0; | |
} | |
generateField(width, height, percentage) { | |
// GENERATE FIELD WITH PLAIN CHARACTERS | |
for (let i = 0; i < width; i++) { | |
let row = []; | |
for (let j = 0; j < height; j++) { | |
row.push(fieldCharacter); | |
} | |
this._field.push(row); | |
} | |
// HAT POSITIONED ANYWHERE | |
this._field[Math.floor(Math.random() * width)][Math.floor(Math.random() * height)] = hat; | |
// PATH CHARACTER ON TOP-LEFT CORNER | |
if (this._field[0][0] !== hat) { this._field[0][0] = pathCharacter; } | |
else { this._field[width - 1][height - 1] = pathCharacter; } | |
// GENERATE RANDOM PLACES FOR THE HOLES | |
const holes = Math.floor((percentage / 100) * (width * height)); | |
let count = 0; | |
while (count < holes) { | |
let candidateX = Math.floor(Math.random() * width); | |
let candidateY = Math.floor(Math.random() * height); | |
if ((this._field[candidateX][candidateY] === fieldCharacter) && | |
(this._field[candidateX][candidateY] !== pathCharacter) && | |
(this._field[candidateX][candidateY] !== hat)) { | |
this._field[candidateX][candidateY] = hole; | |
count++; | |
} | |
} | |
} | |
print() { | |
for (let i = 0; i < this._field.length; i++) { | |
let row = ""; | |
for (let j = 0; j < this._field[i].length; j++) { | |
row += this._field[i][j]; | |
} | |
console.log(row); | |
} | |
} | |
isValid(x, y) { | |
const next = (x < 0 || y < 0) ? "" : this._field[x][y]; | |
switch (next) { | |
case fieldCharacter: return true; | |
case hole: console.log(fellDown); return false; | |
case hat: console.log(foundHat); return true; | |
default: console.log(outOfBounds); return false; | |
} | |
} | |
move(direction) { | |
let candidateX = this._currentPosX; | |
let candidateY = this._currentPosY; | |
switch (direction) { | |
case "d": candidateX++; break; | |
case "u": candidateX--; break; | |
case "r": candidateY++; break; | |
case "l": candidateY--; break; | |
default: return invalid; | |
} | |
if (this.isValid(candidateX, candidateY)) { | |
if (this._field[candidateX][candidateY] === hat) return foundHat; | |
this._field[candidateX][candidateY] = pathCharacter; | |
this._currentPosX = candidateX; | |
this._currentPosY = candidateY; | |
} else return false; | |
} | |
} | |
/* | |
SAMPLE FIELD FOR TESTING PURPOSES | |
const myField = new Field([ | |
['*', '░', 'O', '░', '░', '░', '░', '░', '░', 'O'], | |
['░', '░', '░', '░', '░', '░', '░', 'O', 'O', 'O'], | |
['░', '░', '░', '░', '░', 'O', '░', '░', '░', '░'], | |
['░', '░', 'O', '░', '░', '░', 'O', '░', '░', '░'], | |
['░', '░', 'O', '░', 'O', '░', 'O', 'O', '░', 'O'], | |
['░', 'O', 'O', '░', '░', '░', '░', '░', '░', 'O'], | |
['░', '░', 'O', '░', 'O', '░', '░', '░', '░', '░'], | |
['O', '░', '░', '░', '░', '░', '░', '^', '░', '░'], | |
['░', '░', '░', '░', 'O', '░', '░', '░', '░', '░'], | |
['░', '░', 'O', '░', '░', '░', '░', '░', '░', 'O'], | |
]); | |
*/ | |
// START THE GAME | |
const myField = new Field(); | |
/* | |
GENERATE FIELD BY | |
- width ~ Number of columns | |
- height ~ Number of rows | |
- percentage ~ Percentage of the holes | |
*/ | |
myField.generateField(7, 8, 10); | |
myField.print(); | |
/* | |
DIRECTIONS: | |
- d - DOWN | |
- r - RIGHT | |
- l - LEFT | |
- u - UP | |
*/ | |
let gameOver = false; | |
let winning = false; | |
while (!gameOver) { | |
let direction = prompt('Which way? '); | |
let next = myField.move(direction); | |
if (next === foundHat) { | |
gameOver = true; | |
winning = true; | |
}; | |
if (next === invalid) console.log(invalid); | |
if (next === false) { gameOver = true; }; | |
myField.print(); | |
} | |
if (winning) { | |
console.log(success); | |
} else { | |
console.log(failure); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment