generated from BrianLusina/game-of-life
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from BrianLusina/feat/game-logic
Game logic & Game Loop
- Loading branch information
Showing
10 changed files
with
113 additions
and
92 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
""" | ||
Contains the game logic | ||
""" | ||
from typing import Set | ||
|
||
MAX_INCORRECT_GUESSES = 6 | ||
|
||
|
||
def game_over(wrong_guesses: int, target_word: str, guessed_letters: Set[str]) -> bool: | ||
""" | ||
Checks if the game is over based on the number of wrong guesses, target word and the guessed letters. It returns | ||
True if the number of wrong guesses is equal to the maximum allowed incorrect guesses, which is defaulted to 6. | ||
Also, it returns true if the set of the target word is a member of every letter in the guessed letters | ||
Args: | ||
wrong_guesses (int): number of incorrect guesses | ||
target_word (str): the target word | ||
guessed_letters (set): set of alredy guessed letters by the player. | ||
Returns: | ||
bool: True indicating the game is over, false indicating that the game is still ongoing | ||
""" | ||
if wrong_guesses >= MAX_INCORRECT_GUESSES: | ||
return True | ||
|
||
# checks if every item on the left-hand side set is a member of the right-hand side set. In other words, are the | ||
# letters in the target word in the guessed letters set from the player? | ||
if set(target_word) <= guessed_letters: | ||
return True | ||
|
||
return False |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import unittest | ||
from hangman.game import game_over | ||
|
||
|
||
class GameTestCase(unittest.TestCase): | ||
def test_returns_true_for_incorrect_guesses(self): | ||
"""should return True if the wrong guesses equals the maximum allowed wrong guesses""" | ||
wrong_guesses = 6 | ||
actual = game_over(wrong_guesses, "airplane", {"a"}) | ||
self.assertTrue(actual) | ||
|
||
def test_returns_true_if_target_word_is_in_guessed_letters(self): | ||
"""should return True if the letters in the target word are in the guessed letters""" | ||
wrong_guesses = 3 | ||
target_word = "airplane" | ||
guessed_letters = {"a", "i", "r", "p", "l", "n", "e"} | ||
actual = game_over(wrong_guesses, target_word, guessed_letters) | ||
self.assertTrue(actual) | ||
|
||
def test_returns_true_if_wrong_guesses_is_more_than_allowed_maximum(self): | ||
"""should return True if the number of wrong guesses is more than allowed maximum""" | ||
wrong_guesses = 7 | ||
target_word = "airplane" | ||
guessed_letters = {"a", "i", "r", "p", "l", "n"} | ||
actual = game_over(wrong_guesses, target_word, guessed_letters) | ||
self.assertTrue(actual) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |