Last active
February 26, 2020 17:03
-
-
Save josecarneiro/44ee2f5738c3aaba3896550a80ff9259 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Hangman { | |
constructor(words) { | |
this.words = words; | |
this.letters = []; | |
this.guessedLetters = ''; | |
this.errorsLeft = 10; | |
this.secretWord = this.pickWord(); | |
} | |
pickWord() { | |
return this.words[Math.floor(Math.random() * this.words.length)]; | |
} | |
checkIfLetter(keyCode) { | |
return !!(keyCode >= 65 && keyCode <= 90); | |
} | |
checkClickedLetters(letter) { | |
return !this.letters.includes(letter); | |
} | |
addCorrectLetter(letter) { | |
this.guessedLetters += letter; | |
} | |
addWrongLetter(letter) { | |
this.errorsLeft--; | |
} | |
checkGameOver() { | |
return this.errorsLeft < 1; | |
} | |
checkWinner() { | |
return this.secretWord.split('').every(character => this.guessedLetters.includes(character)); | |
} | |
} | |
let hangman; | |
const $startGameButton = document.getElementById('start-game-button'); | |
if ($startGameButton) { | |
$startGameButton.addEventListener('click', event => { | |
hangman = new Hangman(); | |
// HINTS (uncomment when start working on the canvas portion of the lab) | |
// hangman.secretWord = hangman.pickWord(); | |
// hangmanCanvas = new HangmanCanvas(hangman.secretWord); | |
}); | |
} | |
document.addEventListener('keydown', event => { | |
// React to user pressing a key | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment