Created
June 10, 2025 04:01
-
-
Save kellyminwo/3027f8182f5d1134fd80f7b8a4243f13 to your computer and use it in GitHub Desktop.
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
const prompt = require('prompt-sync')({ sigint: true }); | |
const hat = '^'; | |
const hole = 'O'; | |
const fieldCharacter = '░'; | |
const pathCharacter = '*'; | |
class Field { | |
constructor(newField) { | |
this._newField = newField; | |
this._currentPathRow = this._newField.findIndex(el => el.includes(pathCharacter)); | |
this._currentPathCol = this._newField[this.currentPathRow].indexOf(pathCharacter); | |
this._currentInput = ''; | |
this._endTrigger = false; | |
} | |
get newField() { | |
return this._newField; | |
} | |
get currentPathRow() { | |
return this._currentPathRow; | |
} | |
get currentPathCol() { | |
return this._currentPathCol; | |
} | |
get currentInput() { | |
return this._currentInput; | |
} | |
get endTrigger() { | |
return this._endTrigger; | |
} | |
set currentInput(key) { | |
this._currentInput = key; | |
} | |
set currentPathRow(newPos) { | |
this._currentPathRow = newPos; | |
} | |
set currentPathCol(newPos) { | |
this._currentPathCol = newPos; | |
} | |
set endTrigger(end) { | |
this._endTrigger = end; | |
} | |
static generateField(pathRowNum, pathColNum, perOfHoles) { | |
let arrGrid = new Array(pathRowNum); | |
let numHoles = Math.ceil((perOfHoles / 100) * (pathRowNum * pathColNum)); | |
const addToField = (char) => { | |
const randNum = numOf => Math.floor(Math.random() * numOf); | |
let generatePos = [randNum(pathRowNum), randNum(pathColNum)]; | |
while (arrGrid[generatePos[0]][generatePos[1]] !== fieldCharacter) { | |
generatePos = [randNum(pathRowNum), randNum(pathColNum)]; | |
} | |
arrGrid[generatePos[0]][generatePos[1]] = char; | |
} | |
for (let i = 0; i < arrGrid.length; i++) { | |
arrGrid[i] = new Array(pathColNum).fill(fieldCharacter); | |
} | |
while (numHoles > 0) { | |
addToField(hole); | |
numHoles--; | |
} | |
addToField(pathCharacter); | |
addToField(hat); | |
return arrGrid; | |
} | |
print() { | |
for (let row of this._newField) { | |
console.log(row.join('')); | |
} | |
} | |
checkUserInput(key) { | |
if (key === 'w' || key === 'a' || key === 's' || key === 'd') { | |
this.currentInput = key; | |
} | |
else { | |
console.log('Wrong key input. Please press W, A, S, or D to move.'); | |
} | |
} | |
getUserInput() { | |
this.print(); | |
const userInput = prompt('Which way? '); | |
this.checkUserInput(userInput.toLowerCase()); | |
} | |
move(pathRow, pathCol) { | |
try { | |
if (this.newField[pathRow][pathCol] === undefined) { | |
console.log('Out of bounds! Try again.'); | |
this.endTrigger = true; | |
} | |
else if (this.newField[pathRow][pathCol] === hole) { | |
console.log('You fell in a hole! Try again.'); | |
this.endTrigger = true; | |
} | |
else if (this.newField[pathRow][pathCol] === pathCharacter) { | |
console.log('No backtracking! Try again.'); | |
this.endTrigger = true; | |
} | |
else if (this.newField[pathRow][pathCol] === hat) { | |
console.log('You found the hat! Congrats!'); | |
this.endTrigger = true; | |
} | |
else { | |
this.newField[pathRow][pathCol] = pathCharacter; | |
} | |
} catch (error) { | |
console.log('Out of bounds! Try again.'); | |
this.endTrigger = true; | |
} | |
} | |
runGame() { | |
while (this.endTrigger === false) { | |
this.getUserInput(); | |
switch (this.currentInput) { | |
case 'w': | |
let movePathUp = this.currentPathRow - 1; | |
this.move(movePathUp, this.currentPathCol); | |
this.currentPathRow = movePathUp; | |
break; | |
case 's': | |
let movePathDown = this.currentPathRow + 1; | |
this.move(movePathDown, this.currentPathCol); | |
this.currentPathRow = movePathDown; | |
break; | |
case 'a': | |
let movePathLeft = this.currentPathCol - 1; | |
this.move(this.currentPathRow, movePathLeft); | |
this.currentPathCol = movePathLeft; | |
break; | |
case 'd': | |
let movePathRight = this.currentPathCol + 1; | |
this.move(this.currentPathRow, movePathRight); | |
this.currentPathCol = movePathRight; | |
break; | |
} | |
} | |
} | |
} | |
const genField = new Field(Field.generateField(5, 5, 25)); | |
genField.runGame(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment