-
-
Save codecademydev/d1c312445796ace2ed006a28509e116e to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const prompt = require('prompt-sync')({sigint: true}); | |
const hat = '^'; | |
const hole = 'O'; | |
const fieldCharacter = '░'; | |
const pathCharacter = '*'; | |
let start1=0; //start position coordinate 1 | |
let start2=0 //start position coordinate 2 | |
class Field { | |
constructor(field=[]){ | |
this._field=field | |
} | |
//Prints the field | |
print(){ | |
let f="" | |
let row=this._field.length | |
let col=this._field[0].length | |
for(let i=0;i<row;i++){ | |
for(let j=0;j<col;j++){ | |
f=f+this._field[i][j] | |
} | |
f=f+'\n'; | |
} | |
console.log(f) | |
} | |
//Checks the input and decides the direction of movement | |
getInput(i,sr,sc){ | |
if(i==="u"){ | |
sr=sr-1 | |
if(sr<0 ){ | |
console.log("Game Over!! You left the field") | |
return 'go' | |
}else{ | |
return sr | |
} | |
} | |
else if(i==="d"){ | |
sr=sr+1 | |
if(sr>=this._field.length){ | |
console.log("Game Over!! You left the field") | |
return "go" | |
}else{ | |
return sr | |
} | |
} | |
else if(i==="r"){ | |
sc=sc+1 | |
if(sc>=this._field[0].length){ | |
console.log("Game Over!! You left the field") | |
return "go" | |
}else{ | |
return sc | |
} | |
} | |
else if(i==="l"){ | |
sc=sc-1 | |
if(sc<0){ | |
console.log("Game Over!! You left the field") | |
return "go" | |
}else{ | |
return sc | |
} | |
}else{ | |
return "go" | |
} | |
} | |
//creates field using the entered or generated field array consisting of 1=flied 0=hole 3=hat | |
createField(){ | |
let row=this._field.length | |
let col=this._field[0].length | |
for(let i=0;i<row;i++){ | |
for(let j=0;j<col;j++){ | |
if(this._field[i][j]===0){ | |
this._field[i][j]=hole | |
}else if(this._field[i][j]===1){ | |
this._field[i][j]=fieldCharacter | |
}else if(this._field[i][j]===3){ | |
this._field[i][j]=hat | |
} | |
} | |
} | |
this._field[start1][start2]="+" //sets the start position as + | |
} | |
//checks and fills the position moved to, if the move results in a game ending it returns "go", if the game is won it returns "yes",else it fills in the pathcharacter | |
generateField(sr,sc){ | |
if(this._field[sr][sc]===hole){ | |
return "go" | |
}else if(this._field[sr][sc]===hat){ | |
return "yes" | |
}else if(this._field[sr][sc]===fieldCharacter){ | |
this._field[sr][sc]=pathCharacter | |
} | |
this.print() | |
} | |
//this method contains the main game sequence of events | |
game(){ | |
this.createField() | |
this.print() | |
let sr=start1; | |
let sc=start2; | |
while(true){ | |
let i=prompt("enter your move: u-up, d-down, r-right, l-left: ") | |
if(i==="l"|| i==="r"){ | |
sc=this.getInput(i,sr,sc) | |
if (sc==="go"){ | |
break | |
} | |
}else if(i==="u" || i==="d"){ | |
sr=this.getInput(i,sr,sc) | |
if (sr==="go"){ | |
break | |
} | |
}else{ | |
console.log("Enter valid move") | |
} | |
let result=this.generateField(sr,sc) | |
if(result==='go'){ | |
console.log("Game Over, You fell in a hole") | |
break | |
}else if(result==="yes"){ | |
console.log("You win!! You found the hat") | |
break | |
} | |
} | |
} | |
//this method generates the random game board using dimensions and % of holes as inputs | |
generateGame(d,p){ | |
let arr=[] //holds the generated field | |
let ind=[] //this array holds the indices eg. index 34 is for element in row 3 and colunm 4 | |
for(let i=0;i<d;i++){ | |
let arr1=[] | |
for(let j=0;j<d;j++){ | |
arr1.push(i*10+j) | |
ind.push(i*10+j) | |
} | |
arr.push(arr1) | |
} | |
let nholes=Math.floor(p*d*d) //calculates the number of holes | |
let nhat=(Math.floor(Math.random()*d)*10)+(Math.floor(Math.random()*d)) //calculates the random position of hat | |
arr[Math.floor(nhat/10)][nhat%10]=3 //sets the position of hat to hat char | |
ind.splice(ind.indexOf(nhat),1) | |
for(let k=0;k<nholes;k++){ | |
let rand=Math.floor(Math.random()*ind.length) //selects random index from ind to set as hole | |
//console.log(rand) | |
arr[Math.floor(ind[rand]/10)][ind[rand]%10]=0 | |
ind.splice(rand,1) | |
} | |
let start=ind[Math.floor(Math.random()*ind.length)] //sets a random start position from available field position | |
start1=Math.floor(start/10) | |
start2=start%10 | |
for(let l=0;l<ind.length;l++){ | |
arr[Math.floor(ind[l]/10)][ind[l]%10]=1 | |
} | |
this._field=arr | |
console.log(arr) | |
this.game() | |
} | |
} | |
/*const field1=new Field([ | |
[1,1,0,1,3], | |
[0,1,0,1,1], | |
[0,1,1,1,1], | |
[1,0,1,1,0], | |
[1,1,0,1,3]]) | |
field1.game()*/ | |
const field1=new Field() | |
field1.generateGame(5,0.9) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment