Added docstrings and reformatting to clean_input.py and game.py

This commit is contained in:
Steinadler 2020-12-18 14:59:16 +01:00
parent 30389951b0
commit 0f66134c40
2 changed files with 47 additions and 32 deletions

View File

@ -1,16 +1,28 @@
"""clean_input"""
"""This module handles most user inputs."""
__author__ = ""
__credits__ = ""
__email__ = ""
import sys
import game
# message needs to be a string that is printed for user instruction. options is a list of viable inputs
def io_str(message: str, options: list) -> str:
"""
This method checks if a user input fits the options.
It also handles 'r' for restarting and 'q' for quitting.
:param message: message needs to be a string that is printed for user instruction.
:param options: options is a list of viable inputs.
:return: Cleaned user input
"""
while True:
try:
i = str(input(message))
except (ValueError, NameError):
i = 0
print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
if i in options:
@ -18,29 +30,40 @@ def io_str(message: str, options: list) -> str:
else:
if i == "q":
print("--------------------------\nSpiel wird beendet...\n--------------------------")
print(
"---------------------\nSpiel wird beendet...\n---------------------")
sys.exit()
elif i == "r":
print("--------------------------\nSpiel wird neu gestartet...\n--------------------------")
print(
"---------------------\nSpiel wird neu gestartet...\n---------------------")
game.main()
else:
print('Noch nicht ganz richtig. Hast du deine Auswahl auch richtig geschrieben?')
def io_int(message: str) -> int:
"""
This method checks if a user input is an integer >1.
It also handles 'r' for restarting and 'q' for quitting.
:param message: message needs to be a string that is printed for user instruction
:return: Integer >1
"""
while True:
unfiltered_input = input(message)
if unfiltered_input == "q":
print("--------------------------\nSpiel wird beendet...\n--------------------------")
sys.exit()
print("---------------------\nSpiel wird beendet...\n---------------------")
sys.exit()
elif unfiltered_input == "r":
print("--------------------------\nSpiel wird neu gestartet...\n--------------------------")
game.main() # DOCH TUT ES
print(
"---------------------\nSpiel wird neu gestartet...\n---------------------")
game.main()
try:
i = int(unfiltered_input)
if i >= 1:
return i
else:
@ -48,9 +71,3 @@ def io_int(message: str) -> int:
except (ValueError, NameError):
print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
#testing
#io_int("Bitte gib eine Zahl ein.")

30
game.py
View File

@ -28,9 +28,9 @@ def check_special_score(score, p):
:return: bool, 'restart' or score
"""
if score == 9 and p[1]: # player has 9
if score == 9 and p[1]: # Player has score of 9.
return True, score
elif score == 10 and p[1]: # player has 10
elif score == 10 and p[1]: # Player has score of 10.
print("Sie haben 10 erreicht. Noch ein letzter Wurf...")
time.sleep(1)
r = roll_dice()
@ -41,12 +41,12 @@ def check_special_score(score, p):
print(f"Sie haben insgesamt {score + sum(r)} gewürfelt und damit verloren.\n")
return False, "restart"
return True, score + sum(r)
elif score >= 16 and p[1]: # player has too much
elif score >= 16 and p[1]: # Player score is too high.
print(f"Sie haben insgesamt {score} gewürfelt und damit verloren.\n")
return False, "restart"
elif score == 9 and not p[1]: # ai has 9
elif score == 9 and not p[1]: # AI has score of 9.
return True, score
elif score == 10 and not p[1]: # ai has 10
elif score == 10 and not p[1]: # AI has score of 10.
print(f"{p[0]} hat insgesamt 10 gewürfelt und muss noch einmal würfeln.")
r = roll_dice()
print(f"{p[0]} würfelt {r}.")
@ -54,7 +54,7 @@ def check_special_score(score, p):
print(f"{p[0]} hat insgesamt {score + sum(r)} gewürfelt und damit verloren.\n")
return False, "restart"
return True, score + sum(r)
elif score >= 16 and not p[1]: # ai has too much
elif score >= 16 and not p[1]: # AI score is too high.
print(f"{p[0]} hat insgesamt {score} gewürfelt und damit verloren.\n")
return False, "restart"
else:
@ -67,17 +67,17 @@ def choose_loser(all_results):
and prints them.
Then starts a new game.
:param all_results: list of dicts with player and final score
:return:
:param all_results: list of dicts with players and final score.
:return: None
"""
loser_score = 15
losers = []
for r in all_results: # get lowest score
for r in all_results: # Get lowest score.
if r["final_score"] < loser_score:
loser_score = r["final_score"]
for r in all_results: # check if multiple players with lowest score
for r in all_results: # Check if there are multiple players with lowest score.
if r["final_score"] == loser_score:
losers.append(r["player"])
@ -118,10 +118,10 @@ def sixteen_is_dead(players):
"""
Most important method.
Rotates over all players and keeps track of their score.
If the human player presses 'n', the dice get handed to the AI.
If the human player presses 'n', the die gets handed to the AI.
:param players: list of tuples
:return:
:return: None
"""
all_results = []
@ -159,7 +159,7 @@ def sixteen_is_dead(players):
time.sleep(1.5)
while ai_score < 13: # roll until score at least 12
while ai_score < 13: # Roll until score at least 12.
current_roll = roll_dice()
print(f"{p[0]} würfelt {current_roll}.")
@ -188,7 +188,7 @@ def main():
Assigns human player name and number of players.
Construct list of tuples with players.
:return:
:return: None
"""
with open("player_welcome_message", "r", encoding="utf8") as f:
welcome = ""
@ -225,5 +225,3 @@ def main():
if __name__ == "__main__":
main()
# Tests: