Part 5: Game Logic and Controller
Game State
- Create a variable that captures the game state:
- The word that the user is trying to guess.
- the
currentAttempt(starting from 0) - the
currentPositionof the last letter added to the grid. (starting from 0) - The
currentGuessthe user has provided so far. (starting from an empty string)
Controller
- Modify the event listener such that:
- When a user presses a letter:
- add it to the grid at the right position.
- append the letter to the
currentGuess. - increase the
currentPositionby 1.
- When a user presses the backspace key:
- decrease the
currentPositionby 1. - remove the last letter from the grid.
- By adding an empty string to the cell at the
currentPosition(after it was decreased).
- By adding an empty string to the cell at the
- remove the last letter from the
currentGuess.
- decrease the
- When a user presses the enter key:
- If the
currentPositionis less than the length of the word, indicate that the word is not complete. - If the
currentPositionis equal to the length of the word:- Check if the game is over.
- If the word guessed is correct, end the game.
- If the word guessed is not a valid word, indicate that the word is not valid.
- If the word guessed is valid but incorrect, if the
currentAttemptis more than the game allowed number of attempts, end the game.
- Check if the game is over.
- If the
- When a user presses a letter:
Game Logic
-
Create a function
checkGuess()that checks the user's guess.- The function should:
- Check if the word guessed is valid
isWordValid.- If not, indicate that the word is not valid.
- If yes, check the position of the letters in the guess
checkWord.- If all letters are in the correct position, then the word is correct and the game is over.
- Check if the word guessed is valid.
- Check if the word guessed is valid
- The function should:
-
Create a function
isWordValid(word)that checks if the word is valid.- The function should ultimately make a network request to some API to check if the word is valid.
- For now, we can start by checking if the word is in a list of valid words.
- The function should return a boolean value.
-
Create a function
getRandomWord()that gets a random word.- The function should ultimately make a network request to some API to get a random word.
- For now, we can start by getting a random word from a list of valid words.
- The function should return a string.
- Call this function to set the word instead of hardcoding it.
-
Create a function
checkWord(word, guess)that checks the position of the letters in the guess.- The function should return an array of strings.
- The strings indicate whether the letter at that position is
correct,misplaced, orwrong.
- The strings indicate whether the letter at that position is
- The function should return an array of strings.
Back to the Controller
Update the Letters on the grid
-
Create a function
revealAttemptResult()that updates the grid with the current guess.- The function should:
- The function should call the
checkWordfunction and get the result. - Loop over the result and for each letter, it would update the cell at the specific position with the correct class.
- The class would be
correct,misplaced, orwrong.
- The class would be
- The function should call the
- The function should:
-
Update the
enterevent listener to call therevealAttemptResultfunction if thecurrentPositionis equal to the length of the word.