Skip to content

Instantly share code, notes, and snippets.

@codecademydev

codecademydev/main.js Secret

Created Oct 13, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.