Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created October 2, 2020 20:56
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/2ed4a3be9c67a86adf4ff64302944b57 to your computer and use it in GitHub Desktop.
Save codecademydev/2ed4a3be9c67a86adf4ff64302944b57 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 = '*';
class Field {
constructor(field,rStart){
this._field = field;
this._vLocation = 0;
this._hLocation = 0;
this.randomStart(rStart);
}
get field(){
return this._field;
}
get vLocation(){
return this._vLocation;
}
get hLocation(){
return this._hLocation;
}
set hLocation(position){
this._hLocation = position;
}
set vLocation(position){
this._vLocation = position;
}
print(){
console.clear();
this.field.forEach(item => console.log(item.join('')));
}
keepPlaying(nVLocation,nHLocation){
let keepGoing = true;
if(nVLocation < 0 || nVLocation >= this.field.length || nHLocation < 0 || nHLocation >= this.field[0].length){
console.log('Out of boundaries instruction!');
keepGoing = false;
}else if(this.field[nVLocation] [nHLocation] === hole){
console.log('You fell down a hole!')
keepGoing = false;
}else if(this.field[nVLocation] [nHLocation] === hat){
console.log('Congrats you have found your hat!!!');
keepGoing = false;
}
return keepGoing;
}
checkMovement(input){
let oneMore = false;
if (input === 'L' || input ==='R'){
input === 'L' ? this.hLocation--: this.hLocation++;
}else if(input === 'U' || input === 'D'){
input === 'U'? this.vLocation-- : this.vLocation++;
}else{ // Invalid input. Keep Playing
return false; //Keep playing Invalid key Nothing Happened
}
oneMore = this.keepPlaying(this.vLocation,this.hLocation);
if (oneMore){
this.field[this.vLocation] [this.hLocation] = pathCharacter;
this.print();
return false; //Keep playing
}else{
return true; // Leave the game
}
}
play (hardMode){
let finish = false;
let turn = 0;
while (!finish){
if (hardMode && turn > 5){
this.digHole();
}
this.print();
let response = prompt('Which way? ');
response = response.toUpperCase();
finish = juego.checkMovement(response);
turn++;
}
console.log('GAME OVER!!');
}
static generateField(height,width,percentage){
let newArray = [];
let bigArray =[];
let firstTime = true;
let hatX = 0;
let hatY = 0;
let pasa = '';
for (let i = 0; i <= height; i++){
for(let j = 0; j <= width; j++){
let randomNumber = Math.random();
newArray.push(pasa = randomNumber > percentage ? fieldCharacter: hole); //Randomly assign holes
}
bigArray.push(newArray);
newArray = [];
}
while(hatX === 0 && hatY === 0){ // Make sure the hat is not assigned at [0][0]
hatX = Math.floor(Math.random() * width); // Assing hat to a random place in the 2D array
hatY = Math.floor(Math.random() * height);
}
bigArray[hatX][hatY] = hat; // Assign the hat
return bigArray;
}
randomStart(randStart){
let pathX = 0;
let pathY = 0;
if (randStart){
while(pathX === 0 && pathY === 0 && this.field[pathX][pathY] !== hat){
pathX = Math.floor(Math.random() * this.field.length);
pathY = Math.floor(Math.random() * this.field[0].length);
}
}
this.field[pathY][pathX] = pathCharacter; // Assign Path Character *
this.vLocation = pathY;
this.hLocation = pathX;
}
digHole(){
let holeY;
let holeX;
do{
holeX = Math.floor(Math.random() * this.field.length);
holeY = Math.floor(Math.random() * this.field[0].length);
} while(this.field[holeY][holeX] === hat || this.field[holeY][holeX] === pathCharacter );
console.log(holeY,holeX);
console.log('Continue?');
const continua = prompt();
this.field[holeY][holeX] = hole; // Assign a hole O
}
};
const hatGame = () => {
let rows = 10;
let columns = 10;
let percent = .2;
let proceed = 'N';
let rStart ;
console.log('Enter desired grid height (max 20):');
rows = prompt();
console.log('Enter desired grid width (max 50):');
columns = prompt();
console.log('Enter percentage of starting holes (0-75):');
percent = prompt();
if (percent < 0) {
percent = 0;
} else if (percent > .75) {
percent = .75;
}
console.log('Activate hard mode Y/N');
const hMode = prompt();
let modeActive;
if (hMode.toUpperCase() === 'Y') {
modeActive = true;
} else {
modeActive = false;
}
console.log('Random start? Y/N');
proceed = prompt();
proceed = proceed.toUpperCase();
if (proceed === 'Y'){
rStart = true;
}
else {
rStart = false;
}
const juego = new Field(Field.generateField(rows,columns,percent),rStart);
juego.play(modeActive);
};
hatGame();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment