-
-
Save codecademydev/ad33c17dff248b6afba83700f88a62c0 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(rowCount, colCount) { | |
this._rowCount = rowCount; | |
this._colCount = colCount; | |
this._boardSize = rowCount * colCount; | |
this._gameBoard = []; | |
//sets numHoles to 30% of board | |
this._numHoles = Math.floor(this._boardSize*.3); | |
this._playerCol = 0; | |
this._playerRow = 0; | |
this._prevPlayerCol = 0; | |
this._prevPlayerRow = 0; | |
} | |
/*********create/initialize gameBoard****** */ | |
generateField() { | |
this._gameBoard[0] = pathCharacter; | |
//initialize gameBoard to field char | |
for(let i = 1; i < this._boardSize; i++){ | |
this._gameBoard[i] = fieldCharacter; | |
} | |
//set random holes in gameBoard | |
for(let i = 0; i < this._numHoles; i++){ | |
this._gameBoard[this.randIndex()] = hole; | |
} | |
//set 1 rand hat | |
this._gameBoard[this.randIndex()] = hat; | |
} | |
randIndex() { | |
let index; | |
do{ | |
let randCol = Math.floor(Math.random() * this._colCount); | |
let randRow = Math.floor(Math.random() * this._rowCount); | |
index = randRow * this._colCount + randCol; | |
index = index === 0 ? 1 : index; | |
}while(this._gameBoard[index] != fieldCharacter); | |
return index; | |
} | |
print(){ | |
for(let i = 0; i < this._colCount; i++){ | |
console.log(this._gameBoard[i].join("")); | |
} | |
} | |
spliceArray() { | |
let newArr = []; | |
for(let i = 0; i < this._rowCount; i++){ | |
newArr[i] = this._gameBoard.splice(0,this._colCount); | |
} | |
this._gameBoard = newArr; | |
for(let i = 0; i < this._colCount; i++){ | |
this._gameBoard[i].join(""); | |
} | |
} | |
/********Player control**** */ | |
playerMove(){ | |
let validMove = false; | |
let move; | |
while(!validMove){ | |
move = prompt('Move your player with WASD. CTRL + C to quit'); | |
if(move === 'a' || move === 's' || move === 'd' || move === 'w'){ | |
validMove = true; | |
console.log(move); | |
} | |
}; | |
switch (move) { | |
case "a": | |
this._prevPlayerCol = this._playerCol; | |
this._prevPlayerRow = this._playerRow; | |
this._playerCol--; | |
break; | |
case "s": | |
this._prevPlayerRow = this._playerRow; | |
this._prevPlayerCol = this._playerCol; | |
this._playerRow++; | |
break; | |
case "d": | |
this._prevPlayerCol = this._playerCol; | |
this._prevPlayerRow = this._playerRow; | |
this._playerCol++; | |
break; | |
case "w": | |
this._prevPlayerRow = this._playerRow | |
this._prevPlayerCol = this._playerCol; | |
this._playerRow--; | |
break; | |
default: | |
} | |
} | |
isGameOver() { | |
if(this._playerRow < 0 || this._playerCol < 0 || this._playerRow >= this._rowCount || this._playerCol >= this._colCount){ | |
console.log("You Lost! You moved out of Bounds! Try again.") | |
return true; | |
}else if(this._gameBoard[this._playerRow][this._playerCol] === hole){ | |
console.log("You Lost! You fell in a hole! try again."); | |
return true; | |
}else if(this._gameBoard[this._playerRow][this._playerCol] === hat){ | |
console.log("You Won!!!!! try again."); | |
return true; | |
}else{ | |
this._gameBoard[this._prevPlayerRow][this._prevPlayerCol] = fieldCharacter; | |
this._gameBoard[this._playerRow][this._playerCol] = pathCharacter; | |
return false} | |
} | |
} | |
let board = new Field(10, 10); | |
let gameOver = false; | |
board.generateField(); | |
board.spliceArray(); | |
board.print(); | |
do{ | |
board.playerMove(); | |
gameOver = board.isGameOver(); | |
board.print(); | |
}while(!gameOver); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment