Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created February 21, 2022 11:50
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/c142627d8aa1182dd2877c2e20e38df8 to your computer and use it in GitHub Desktop.
Save codecademydev/c142627d8aa1182dd2877c2e20e38df8 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 = '*';
var inGame = false;
var vertical = 0;
var horizontal = 0;
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}
class Field {
constructor(field){
this._field = field;
}
get field(){
return this._field;
}
print(){
for(var i = 0; i < this._field.length; i++){
var row = "";
for(var j = 0; j < this._field[i].length; j++){
row += this._field[i][j];
}
console.log(row);
}
}
static generateField(height, width, holePerc){
var newField = [];
for(var i = 0; i < height; i++){
newField.push([]);
for(var j = 0; j < width; j++){
newField[i].push(fieldCharacter);
}
}
//HoleGen
if(holePerc <= 0 || holePerc > 0.5){
holePerc = 0.1;
}
let numberOfHoles = Math.floor((height * width) * holePerc);
console.log(numberOfHoles + " holes.");
for(var i = 0; i < numberOfHoles; i++){
let holeY = getRandomInt(0, height);
let holeX = getRandomInt(0, width);
newField[holeY][holeX] = hole;
}
//PlayerPos
newField[0][0] = pathCharacter;
//Win Pos
let winY = getRandomInt(0, height);
let winX = getRandomInt(0, width);
if(winY === 0 && winX === winY){
winX = 1;
}
newField[winY][winX] = hat;
//print function for debug
return newField;
}
}
var myField = new Field([
['*', '░', 'O'],
['░', 'O', '░'],
['░', '^', '░'],
]);
const generateField = (height, width, holePerc) =>{
myField = new Field(Field.generateField(height , width , holePerc));
}
const win = ()=>{
inGame = false;
console.log ("Good job! You won!")
return;
}
const gameOver = ()=>{
console.log("You fall into an hole, GAME OVER");
inGame = false;
return;
}
var move = (ver, hor) =>{
vertical += ver;
horizontal += hor;
if( vertical > myField.field.length -1 || vertical < 0 || horizontal > myField.field[vertical].length -1 || horizontal < 0){
console.log("You moved out of bounds, GAME OVER");
inGame = false;
return;
}
switch(myField.field[vertical][horizontal]){
case hat:
win();
break;
case hole:
gameOver();
break;
default:
myField.field[vertical][horizontal] = pathCharacter;
break;
}
}
while (true){
while(inGame){
myField.print();
let way = prompt('Which way? ');
switch(way){
case 'w':
move(-1, 0);
break;
case 'a':
move(0, -1);
break;
case 's':
move(1, 0);
break;
case 'd':
move(0, 1);
break;
default:
console.log("Not a valid input");
}
}
if(!inGame){
prompt('Press ENTER to start');
var x = prompt('Insert height of the field');
var y = prompt('Insert width of the field');
var h = prompt('Insert hole percentage of the field');
try{
generateField(x, y, h);
}
catch(e){
console.log("Invalid paramenters, retry");
return;
}
inGame = true;
vertical = 0;
horizontal = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment