Skip to content

Instantly share code, notes, and snippets.

@i-am-chitti
Created June 12, 2021 21:19
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 i-am-chitti/b64c6218a48227461ceb198737113377 to your computer and use it in GitHub Desktop.
Save i-am-chitti/b64c6218a48227461ceb198737113377 to your computer and use it in GitHub Desktop.
Find Your Hat | Codecademy Javascript III Project
// class to handle coordinates
class Coordinate {
constructor(X, Y) {
this._X = X;
this._Y = Y;
}
print() {
console.log(`(${this._X}, ${this._Y})`);
}
// generate random coordinate
static generateRandom(height, width) {
return new Coordinate(Math.floor(Math.random()*height),Math.floor(Math.random()*width));
}
}
module.exports = {
Coordinate
};
const { hat, hole, fieldCharacter, pathCharacter } = require('./symbols.js');
const { Coordinate } = require('./Coordinate.js');
class Field {
constructor(field) {
this._field = field;
this._currPos = new Coordinate(0,0);
this._hatPos = this.getHatPos();
}
// get position of hat in the field
getHatPos() {
for(let i=0;i<this._field.length;i++) {
for(let j=0;j<this._field[i].length;j++) {
if(this._field[i][j] === hat) return new Coordinate(i,j);
}
}
}
print() {
for(let i=0;i<this._field.length;i++) {
console.log(this._field[i]);
}
}
//set current X-coordinate
set currX(X) {
this._currPos._X=X-1;
}
// set current Y-coordinate
set currY(Y) {
this._currPos._Y=Y-1;
}
// check if current coordinate are of hat position
checkIfWon() {
if(this._currPos._X === this._hatPos._X && this._currPos._Y === this._hatPos._Y) return true;
else return false;
}
// check if current coordinate are of hole position
checkIfLandedOnHole() {
if(this._field[this._currPos._X][this._currPos._Y] === hole) return true;
else return false;
}
// check if current coordinate are out of field
checkIfOutside() {
if(this._currPos._X >= this._field.length || this._currPos._X < 0 || this._currPos._Y >= this._field[0].length || this._currPos._Y < 0) return true;
else return false;
}
// generate and fill a field randomly
static generateField(height, width, percentage) {
let myField = new Array(height);
let nHoles = Math.floor((percentage*height*width)/100);
// initialize array with field character
for(let i=0;i<height;i++) {
myField[i] = new Array(width);
for(let j=0;j<width;j++) myField[i][j]=fieldCharacter;
}
// set hat random position
let hatRandPos = Coordinate.generateRandom(height, width);
myField[hatRandPos._X][hatRandPos._Y]=hat;
// for each hole, randomize its position
while(nHoles>0) {
let holeRandPos = Coordinate.generateRandom(height,width);
let character = myField[holeRandPos._X][holeRandPos._Y];
if(character!==hole && character !== hat && character === fieldCharacter) {
myField[holeRandPos._X][holeRandPos._Y]=hole;
nHoles--;
}
}
//finally
return myField;
}
}
module.exports = {
Field
};
const prompt = require('prompt-sync')({sigint: true});
const { hat, hole, fieldCharacter, pathCharacter } = require('./symbols.js');
const { Field } = require('./Field.js');
//generate a new field with given height, widht and percentage of holes
const myField = new Field(Field.generateField(3,2, 40));
//myField.print();
while(true) {
myField.currX=prompt('Enter X-position: ');
myField.currY=prompt('Enter Y-position: ');
if(myField.checkIfOutside()) {
console.log('You moved outside. You Lost!');
break;
}
else if(myField.checkIfLandedOnHole()) {
console.log('You landed on hole. You Lost!');
break;
}
else if(myField.checkIfWon()) {
console.log('You got the hat. Well Played! :)');
break;
}
console.log('Try Again');
}
const hat = '^';
const hole = 'O';
const fieldCharacter = '░';
const pathCharacter = '*';
module.exports = {
hat,
hole,
fieldCharacter,
pathCharacter
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment