Skip to content

Instantly share code, notes, and snippets.

@cford-14
Last active January 19, 2024 15:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save cford-14/0ff506149b60856cbc48e5e3c1d231ce to your computer and use it in GitHub Desktop.
Save cford-14/0ff506149b60856cbc48e5e3c1d231ce to your computer and use it in GitHub Desktop.
find the hat exercise
//https://gist.github.com/74bd26dd414b4fae14b218919d20d458
const prompt = require('prompt-sync')({sigint: true});
const hat = '^';
const hole = 'O';
const fieldCharacter = '░';
const pathCharacter = '*';
//define class
class Field {
constructor(hatAndHoles, field) {
this._field = field;
this._hatAndHoles = hatAndHoles;
}
//play game method
playGame() {
let y = 0;
let x = 0;
this.print(this._field);
while (this._hatAndHoles[y][x] === pathCharacter || this._hatAndHoles[y][x] === fieldCharacter) {
const direction = prompt('Which direction would you like to move? Please ennter N for North, S for South, E for East, or W for West. \n');
if (direction.toUpperCase() === 'N') {
if (y === 0) {
console.log('You cannot move any further North. Please choose another direction')
} else {
y -=1
}
} else if (direction.toUpperCase() === 'S') {
if (y >= this._field.length) {
console.log('You cannot move any further South. Please choose another direction')
} else {
y +=1
}
} else if (direction.toUpperCase() === 'W') {
if (x === 0) {
console.log('You cannot move any further West. Please choose another direction')
} else {
x -= 1
}
} else if (direction.toUpperCase() === 'E') {
if (x >= this._field[y].length) {
console.log('You cannot move any further East. Please choose another direction')
} else {
x += 1
}
} else {
console.log('Invalid entry. Please enter N, S, E, or W')
}
if (this._hatAndHoles[y][x] === hat) {
console.log('You found the hat! You win!')
} else if (this._hatAndHoles[y][x] === hole) {
console.log('You fell in a hole. Game Over')
} else {
this._field[y][x] = pathCharacter;
this.print(this._field);
}
}
}
//print field method
print() {
for (let row of this._field){
console.log(row.join(' '));
}
}
//generate field with hat and holes
static generateField(height, width, holes) {
let newField = [];
for (let i = 0; i < height; i++) {
newField.push([]);
for (let j = 0; j < height; j++) {
newField[i].push(fieldCharacter)
};
};
newField[0][0] = pathCharacter;
let hatX = Math.floor(Math.random() * width);
let hatY = Math.floor(Math.random() * height);
newField[hatY][hatX] = hat;
for (let k = holes; k > 0; k--) {
let holeX = hatX;
let holeY = hatY;
while (holeX === hatX) {
holeX = Math.floor(Math.random() * width)
};
while (holeY === hatY) {
holeY = Math.floor(Math.random() * height)
};
newField[holeY][holeX] = hole;
}
return newField;
}
//generate blank field for the user to traverse without seeing the hat and holes
static generateBlankField(height, width){
let newField = [];
for (let i = 0; i < height; i++) {
newField.push([]);
for (let j = 0; j < height; j++) {
newField[i].push(fieldCharacter)
};
};
newField[0][0] = pathCharacter;
return newField;
}
}
let myField
//create the blank field for the user to see
const blankField = Field.generateBlankField(5, 5)
//created the field with the hat and holes
const newField = Field.generateField(5, 5, 1);
console.log(blankField);
//instantiate a Field object using newField = hatAndHoles and field = blankField
myField = new Field (newField, blankField);
//call playGame method
myField.playGame();
@14thchief
Copy link

Your approach to the code implementation helps me understand it better, more study for me and I will implement my version of the game. Good one.

@nekky907
Copy link

nekky907 commented Aug 2, 2023

Thank you this help me learn a lot. THANK YOU!!!

@JoeT1327
Copy link

Let me confess that I can't make my project at all. But reading your project code helps me a lot. Thank you.

@FerMon98
Copy link

Hey there! I didn't even know where to start but your project helped me a lot. Thanks!

@Meera-create
Copy link

this was really helpful, i was really stuck, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment