pangiann / hangman

Hangman is a paper and pencil guessing game for two or more players.
0 stars 0 forks source link

[hangman-round] Document what happens when the game starts #5

Open pangiann opened 2 years ago

pangiann commented 2 years ago

Hangman game execution

Before the game starts a user has to options:

If the user wants to start playing, they have to choose a dictionary to load.

Eventually, the game is ready to start with a valid dictionary of words loaded. What happens next?

pangiann commented 2 years ago

Flow of the game

  1. Computer chooses randomly a word from the loaded dictionary. Then, the first round starts:

  2. The player has to choose a character for one of the word's positions.

  3. For every position in the easy mode we provide possible characters sorted with their probabilities to be the right ones for the corresponding place.

  4. How do we calculate this probability? It is the percentage of a character found in all the candidate words in the dictionary when the word:

    • Has the same length with the hidden selected word.

    • Has the same already opened characters in other positions with the hidden selected word.

  5. Then, the player selects one character for a position. There are two possible outcomes: a. Player finds the hidden character:

    • Τhe character appears in the relevant position.
    • Remove all the candidate words that don't have this character in the corresponding position.
    • For the remaining empty places we compute again all the character probabilites.
    • The player wins points in respect to these rules:
      • 5 points if P > 0.6
      • 10 points if 0.4 < P <= 0.6
      • 15 points if 0.25 < P <= 0.4
      • 30 points if P <= 0.25

    b. Player doesn't find the hidden character:

    • Remove all the candidate words that have the character in the corresponding position.
    • For the remaining empty places we compute again all the character probabilites.
    • Reduce by one the player's lifes.
    • Remove 15 points from the player (points must be always greater or equal to zero).
  6. The game ends: a. When the player guesses the word correct. b. When player has 6 wrong guesses.

pangiann commented 2 years ago

Round Class

This class represents an active round during the game. That is:

pangiann commented 2 years ago

compute_probabilities()

Computes the probability of a character to be the right one for the corresponding position by finding the percentage of words that have this character in the same position.

This function does the following:

  1. Iterates over the dictionary
  2. For each word of the dictionary gets each char individually and its position:
    • If it is the first char found in this position add it to the list with a counter = 1
    • Else, increase counter for this char by one.
  3. Divides the number of appearances of a char in a position with the number of words in total in the dictionary to compute the asked percentage.
pangiann commented 2 years ago

remove_unlike_words()

Removes unlike words in regard to the one current playing. There are two possible cases when a user guesses a char c in a position x in the word:

  1. Success: Removes all the words that DO NOT have char c in position x
  2. Failure: Removes all the words that DO have char c in position x
pangiann commented 2 years ago

play_round()

Plays a new round in the game. The player guesses a character in a certain position in the word. Computes the points of the player considering if the guess was correct and updates the state of the game accordingly. More specifically:

  1. Add guess to past guesses for keeping tract of history
  2. Increment the total number of guesses by one
  3. Check if the guess is correct
  4. If yes:
    • Update current guessing word
    • Increment number of correct guesses by one
  5. Compute number of points of this round for the player in regards to probability.
  6. Check if player wins.
    • If so: return True
    • If not:
      • remove unlike words
      • compute new probabilities