Skip to main content

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 currentPosition of the last letter added to the grid. (starting from 0)
    • The currentGuess the 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 currentPosition by 1.
    • When a user presses the backspace key:
      • decrease the currentPosition by 1.
      • remove the last letter from the grid.
        • By adding an empty string to the cell at the currentPosition (after it was decreased).
      • remove the last letter from the currentGuess.
    • When a user presses the enter key:
      • If the currentPosition is less than the length of the word, indicate that the word is not complete.
      • If the currentPosition is 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 currentAttempt is more than the game allowed number of attempts, end the game.

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.
  • 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, or wrong.

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 checkWord function 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, or wrong.
  • Update the enter event listener to call the revealAttemptResult function if the currentPosition is equal to the length of the word.