Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created October 13, 2020 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/f231196997c66d589cb5f9bf6f794b68 to your computer and use it in GitHub Desktop.
Save codecademydev/f231196997c66d589cb5f9bf6f794b68 to your computer and use it in GitHub Desktop.
Codecademy export
const prompt = require('prompt-sync')({sigint: true});
const hat = '^';
const hole = 'O';
const fieldCharacter = '░';
const pathCharacter = '*';
const xstart = 0;
const ystart = 0;
let xpos = 0;
let ypos = 0;
class Field {
constructor(field){
this._field = field;
}
print(){
for(let i=0; i<this._field.length; i++){
console.log(this._field[i].join(''));
}
}
play(){
while(true){
let direction = prompt("\nPlease choose the direction, you would like to go ((t)->top, (r)->right, (b)->bottom, (l)->left)\n");
if(direction === 'd'){
this._field[xpos][ypos] = '-';
xpos += 1;
if(this._field[xpos][ypos] === hole){
console.log('You fell into a hole');
break;
} else if(this._field[xpos][ypos] === fieldCharacter){
console.log();
} else if(this._field[xpos][ypos] === hat){
console.log('Congrats, You Win!!');
break;
} else {
console.log('Out of Bounds');
break;
}
this._field[xpos][ypos] = pathCharacter;
this.print();
}
if(direction === 'r'){
this._field[xpos][ypos] = '-';
ypos += 1;
if(this._field[xpos][ypos] === hole){
console.log('You fell into a hole');
break;
} else if(this._field[xpos][ypos] === fieldCharacter){
console.log();
} else if(this._field[xpos][ypos] === hat){
console.log('Congrats, You Win!!');
break;
} else {
console.log('Out of Bounds');
break;
}
this._field[xpos][ypos] = pathCharacter;
this.print();
}
if(direction === 'l'){
this._field[xpos][ypos] = '-';
xpos -= 1;
if(this._field[xpos][ypos] === hole){
console.log('You fell into a hole');
break;
} else if(this._field[xpos][ypos] === fieldCharacter){
console.log();
} else if(this._field[xpos][ypos] === hat){
console.log('Congrats, You Win!!');
break;
} else {
console.log('Out of Bounds');
break;
}
this._field[xpos][ypos] = pathCharacter;
this.print();
}
if(direction === 't'){
this._field[xpos][ypos] = '-';
ypos -= 1;
if(this._field[xpos][ypos] === hole){
console.log('You fell into a hole');
break;
} else if(this._field[xpos][ypos] === fieldCharacter){
console.log();
} else if(this._field[xpos][ypos] === hat){
console.log('Congrats, You Win!!');
break;
} else {
console.log('Out of Bounds');
break;
}
this._field[xpos][ypos] = pathCharacter;
this.print();
}
}
}
}
console.log("### Welcome to 'Find Your Hat' Game ###\n");
console.log("You are at the point(*).\nNavigate to your HAT(^) without falling down one of the holes(o) or stepping outside of the field.\n");
const myField = new Field([
['*', '░', 'O'],
['░', 'O', '░'],
['░', '^', '░'],
]);
myField.print();
myField.play();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment