Created
October 27, 2023 15:44
-
-
Save Nostromos/7b149e62ae2c92faa93afe11df069774 to your computer and use it in GitHub Desktop.
Codecademy Challenge Project: Find Your Hat
This file contains hidden or 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
/* | |
I included a lot of extraneous functions in an effort to modularize things and make it easier to handle | |
future improvements. For example, every win/loss condition has its own function, which makes it easier to | |
change & edit them. | |
*/ | |
const prompt = require("prompt-sync")({ sigint: true }); | |
const hat = "^"; | |
const hole = "O"; | |
const fieldCharacter = "░"; | |
const pathCharacter = "*"; | |
const fieldSymbols = [fieldCharacter, hole]; | |
class Field { | |
constructor(field) { | |
this.field = field; | |
this.fieldHeight = field.length - 1; | |
this.fieldWidth = field[0].length - 1; | |
this.playerX = 0; | |
this.playerY = 0; | |
this.hatLocation = [2, 1]; // generate a random spot to put hat | |
} | |
print() { // print the current field | |
for (let row of this.field) { | |
console.log(row.join(' ')); | |
} | |
} | |
start() { | |
this.prepField(); | |
this.move(); | |
} | |
prepField() { // adds the character and the hat to the field | |
this.field[0][0] = pathCharacter; | |
this.field[this.hatLocation[0]][this.hatLocation[1]] = hat; | |
} | |
move() { | |
this.print(); | |
let direction = prompt('Where to next? ').toLowerCase() | |
switch (direction) { | |
case 'w': | |
this.playerY -= 1; | |
break; | |
case 's': | |
this.playerY += 1; | |
break; | |
case 'a': | |
this.playerX -= 1; | |
break; | |
case 'd': | |
this.playerX += 1; | |
break; | |
} | |
this.testWinLoss(); | |
} | |
updateField() { // needs to update all player locations to the path symbol | |
this.field[this.playerY][this.playerX] = pathCharacter; | |
} | |
testWinLoss() { // test for win/loss conditions and react accordingly | |
if (this.playerY === this.hatLocation[0] && this.playerX === this.hatLocation[1]) { // checks if player is on the hat | |
this.winGame(); | |
} else if (this.playerY > this.fieldHeight || this.playerY < 0) { // checks vertical position | |
this.wentOutsideField(); | |
} else if (this.playerX > this.fieldWidth || this.playerX < 0) { // checks horizontal position | |
this.wentOutsideField(); | |
} else if (this.field[this.playerY][this.playerX] === hole) { // checks if player is on (inside of) a hole | |
this.fellInHole(); | |
} else { | |
this.updateField(); | |
this.move(); | |
} | |
} | |
winGame() { // helper to win the game | |
console.log('You found your hat!'); | |
process.exit(); | |
} | |
fellInHole() { // helper when you fall in the hole | |
console.log('You fell in a hole and died!'); | |
process.exit(); | |
} | |
wentOutsideField() { // helper when you go out of a field | |
console.log('You moved outside the field, got lost, and died!'); | |
process.exit(); | |
} | |
static generateField(height, width) { | |
let field = []; | |
// generate rows randomly, push to field | |
for (let r = 0; r < height; r++) { | |
let currentRow = []; | |
for (let i = 0; i < width; i++) { | |
currentRow.push( | |
fieldSymbols[Math.floor(Math.random() * fieldSymbols.length)] | |
); | |
} | |
field.push(currentRow); | |
} | |
return field; // return the field as an array | |
} | |
} | |
const MyField = new Field(Field.generateField(5, 5)); | |
MyField.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment