-
-
Save codecademydev/78cf5d3acc9eabda48475052937ef102 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 = '*'; | |
class Field { | |
constructor(field){ | |
this.field = field; | |
this.locationX = 0; | |
this.locationY = 0; | |
} | |
play_game(){ | |
console.log('WELCOME TO: FIND YOUR HAT! :D') | |
this.print(); | |
while (this.inputUser()){ | |
this.print(); | |
} | |
return true; | |
} | |
print(){ | |
for (let i = 0; i < this.field.length; i++){ | |
console.log(this.field[i].join(" ")); | |
} | |
} | |
inputUser(){ | |
//PREGUNTA Y ACEPTA EL MOVIMIENTO, ACTUALIZA LA UBICACION | |
const question = prompt('Wich way? -> '); | |
if (question.toLowerCase() === 'a'){ | |
//derecha | |
this.locationY -= 1; | |
if (this.locationCurrent()){ | |
return true; | |
} else{ | |
return false; | |
} | |
} else if (question.toLowerCase() === 'd'){ | |
//izquierda | |
this.locationY += 1; | |
if (this.locationCurrent()){ | |
return true; | |
} else{ | |
return false; | |
} | |
} else if (question.toLowerCase() === 'w'){ | |
//arriba | |
this.locationX -= 1; | |
if (this.locationCurrent()){ | |
return true; | |
} else{ | |
return false; | |
} | |
} else if (question.toLowerCase() === 's'){ | |
//abajo | |
this.locationX += 1; | |
if (this.locationCurrent()){ | |
return true; | |
} else{ | |
return false; | |
} | |
} else { | |
console.log('Ha ocurrido un error!'); | |
return false; | |
} | |
} | |
static generateField(height, width){ | |
let auxField = []; | |
const countHole = Math.floor(Math.random()*(height - 1) + 1); //cantidad aleatoria de huecos, mayor o igual 1 | |
for (let y = 0; y < height; y++){ | |
auxField.push([]); | |
for (let x = 0; x < width; x++){ | |
auxField[y][x] = fieldCharacter; | |
} | |
} | |
auxField[0][0] = pathCharacter; //ubicación del jugador | |
auxField[Math.floor(Math.random()*(height - 1) + 1)][Math.floor(Math.random()*(width -1) + 1)] = hat; //ubicación del sombrero. Sin incluir la posición (0,0) | |
for (let i = 1; i <= countHole; i++){ | |
let locationY = Math.floor(Math.random()*(height - 1) + 1);//----------- | |
let locationX = Math.floor(Math.random()*(width - 1) + 1);//-- ambas excluyen el 0, es imposible que se ubique un hueco en (0,0) | |
while (auxField[locationY][locationX] === hat || auxField[locationY][locationX] === hole){ | |
//loop para repetir los números si en las coordenadas está el sombrero o un hueco | |
locationY = Math.floor(Math.random()*(height - 1) + 1); | |
locationX = Math.floor(Math.random()*(width - 1) + 1); | |
} | |
auxField[locationY][locationX] = hole; | |
} | |
return auxField; | |
} | |
locationCurrent(){ | |
// prueba si la ubicacion es un sombrero, hueco pared o se sustituye * | |
if(this.locationX < this.field.length && this.locationY < this.field.length){ | |
if (this.field[this.locationX][this.locationY] === hat){ | |
console.log('YOU WIN!!!'); | |
return false; | |
} else if (this.field[this.locationX][this.locationY] === hole){ | |
console.log('YOU LOSE :('); | |
return false; | |
} else if (this.field[this.locationX][this.locationY] === fieldCharacter){ | |
this.field[this.locationX][this.locationY] = '*'; | |
return true; | |
} else { | |
console.log('YOU LOSE! YOU WAS OUT OF BOUNDS :('); | |
return false; | |
} | |
} else{ | |
console.log('YOU LOSE! YOU WAS OUT OF BOUNDS :('); | |
return false; | |
} | |
} | |
} | |
const newInstance = Field.generateField(10, 5); | |
const myField = new Field(newInstance); | |
while(myField.play_game()){ | |
let instance = Field.generateField(10, 5); | |
let mield = new Field(instance); | |
mield.play_game(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment