I have created a simple Guess the Number game. It chooses a random number between 1 and 100 and then challenges the player to guess the number in 10 turns. After each turn the player is told if they are right or wrong. In the case that they are wrong, they are told whether the guess was too low or too high. It also tells the player what numbers they previously guessed. The game ends once the player guesses correctly, or once they run out of turns. When the game ends, the player is given an option to start playing again.
- Generate a random number between 1 and 100.
- Record the turn number the player is on. Start it on 1.
- Provide the player with a way to guess what the number is.
- Once a guess has been submitted first record it somewhere so the user can see their previous guesses.
- Next, check whether it is the correct number.
- If it is correct:
- Display congratulations message.
- Stop the player from being able to enter more guesses (this would mess the game up).
- Display control allowing the player to restart the game.
- If it is wrong and the player has turns left:
- Tell the player they are wrong.
- Allow them to enter another guess.
- Increment the turn number by 1.
- If it is wrong and the player has no turns left:
- Tell the player it is game over.
- Stop the player from being able to enter more guesses (this would mess the game up).
- Display control allowing the player to restart the game.
- Once the game restarts, make sure the game logic and UI are completely reset, then go back to step 1.
Credit to: MDN Documentation