Added docstrings and reformatting to clean_input.py and game.py
This commit is contained in:
parent
30389951b0
commit
0f66134c40
|
@ -1,16 +1,28 @@
|
||||||
"""clean_input"""
|
"""This module handles most user inputs."""
|
||||||
|
|
||||||
|
__author__ = ""
|
||||||
|
__credits__ = ""
|
||||||
|
__email__ = ""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import game
|
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:
|
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:
|
while True:
|
||||||
try:
|
try:
|
||||||
i = str(input(message))
|
i = str(input(message))
|
||||||
|
|
||||||
except (ValueError, NameError):
|
except (ValueError, NameError):
|
||||||
|
i = 0
|
||||||
print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
|
print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
|
||||||
|
|
||||||
if i in options:
|
if i in options:
|
||||||
|
@ -18,25 +30,36 @@ def io_str(message: str, options: list) -> str:
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if i == "q":
|
if i == "q":
|
||||||
print("--------------------------\nSpiel wird beendet...\n--------------------------")
|
print(
|
||||||
|
"---------------------\nSpiel wird beendet...\n---------------------")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
elif i == "r":
|
elif i == "r":
|
||||||
print("--------------------------\nSpiel wird neu gestartet...\n--------------------------")
|
print(
|
||||||
|
"---------------------\nSpiel wird neu gestartet...\n---------------------")
|
||||||
game.main()
|
game.main()
|
||||||
else:
|
else:
|
||||||
print('Noch nicht ganz richtig. Hast du deine Auswahl auch richtig geschrieben?')
|
print('Noch nicht ganz richtig. Hast du deine Auswahl auch richtig geschrieben?')
|
||||||
|
|
||||||
|
|
||||||
def io_int(message: str) -> int:
|
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:
|
while True:
|
||||||
|
|
||||||
unfiltered_input = input(message)
|
unfiltered_input = input(message)
|
||||||
|
|
||||||
if unfiltered_input == "q":
|
if unfiltered_input == "q":
|
||||||
print("--------------------------\nSpiel wird beendet...\n--------------------------")
|
print("---------------------\nSpiel wird beendet...\n---------------------")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
elif unfiltered_input == "r":
|
elif unfiltered_input == "r":
|
||||||
print("--------------------------\nSpiel wird neu gestartet...\n--------------------------")
|
print(
|
||||||
game.main() # DOCH TUT ES
|
"---------------------\nSpiel wird neu gestartet...\n---------------------")
|
||||||
|
game.main()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
i = int(unfiltered_input)
|
i = int(unfiltered_input)
|
||||||
|
@ -48,9 +71,3 @@ def io_int(message: str) -> int:
|
||||||
|
|
||||||
except (ValueError, NameError):
|
except (ValueError, NameError):
|
||||||
print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
|
print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#testing
|
|
||||||
#io_int("Bitte gib eine Zahl ein.")
|
|
||||||
|
|
||||||
|
|
30
game.py
30
game.py
|
@ -28,9 +28,9 @@ def check_special_score(score, p):
|
||||||
:return: bool, 'restart' or score
|
: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
|
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...")
|
print("Sie haben 10 erreicht. Noch ein letzter Wurf...")
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
r = roll_dice()
|
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")
|
print(f"Sie haben insgesamt {score + sum(r)} gewürfelt und damit verloren.\n")
|
||||||
return False, "restart"
|
return False, "restart"
|
||||||
return True, score + sum(r)
|
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")
|
print(f"Sie haben insgesamt {score} gewürfelt und damit verloren.\n")
|
||||||
return False, "restart"
|
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
|
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.")
|
print(f"{p[0]} hat insgesamt 10 gewürfelt und muss noch einmal würfeln.")
|
||||||
r = roll_dice()
|
r = roll_dice()
|
||||||
print(f"{p[0]} würfelt {r}.")
|
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")
|
print(f"{p[0]} hat insgesamt {score + sum(r)} gewürfelt und damit verloren.\n")
|
||||||
return False, "restart"
|
return False, "restart"
|
||||||
return True, score + sum(r)
|
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")
|
print(f"{p[0]} hat insgesamt {score} gewürfelt und damit verloren.\n")
|
||||||
return False, "restart"
|
return False, "restart"
|
||||||
else:
|
else:
|
||||||
|
@ -67,17 +67,17 @@ def choose_loser(all_results):
|
||||||
and prints them.
|
and prints them.
|
||||||
Then starts a new game.
|
Then starts a new game.
|
||||||
|
|
||||||
:param all_results: list of dicts with player and final score
|
:param all_results: list of dicts with players and final score.
|
||||||
:return:
|
:return: None
|
||||||
"""
|
"""
|
||||||
loser_score = 15
|
loser_score = 15
|
||||||
losers = []
|
losers = []
|
||||||
|
|
||||||
for r in all_results: # get lowest score
|
for r in all_results: # Get lowest score.
|
||||||
if r["final_score"] < loser_score:
|
if r["final_score"] < loser_score:
|
||||||
loser_score = r["final_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:
|
if r["final_score"] == loser_score:
|
||||||
losers.append(r["player"])
|
losers.append(r["player"])
|
||||||
|
|
||||||
|
@ -118,10 +118,10 @@ def sixteen_is_dead(players):
|
||||||
"""
|
"""
|
||||||
Most important method.
|
Most important method.
|
||||||
Rotates over all players and keeps track of their score.
|
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
|
:param players: list of tuples
|
||||||
:return:
|
:return: None
|
||||||
"""
|
"""
|
||||||
all_results = []
|
all_results = []
|
||||||
|
|
||||||
|
@ -159,7 +159,7 @@ def sixteen_is_dead(players):
|
||||||
|
|
||||||
time.sleep(1.5)
|
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()
|
current_roll = roll_dice()
|
||||||
print(f"{p[0]} würfelt {current_roll}.")
|
print(f"{p[0]} würfelt {current_roll}.")
|
||||||
|
|
||||||
|
@ -188,7 +188,7 @@ def main():
|
||||||
Assigns human player name and number of players.
|
Assigns human player name and number of players.
|
||||||
Construct list of tuples with players.
|
Construct list of tuples with players.
|
||||||
|
|
||||||
:return:
|
:return: None
|
||||||
"""
|
"""
|
||||||
with open("player_welcome_message", "r", encoding="utf8") as f:
|
with open("player_welcome_message", "r", encoding="utf8") as f:
|
||||||
welcome = ""
|
welcome = ""
|
||||||
|
@ -225,5 +225,3 @@ def main():
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
# Tests:
|
|
||||||
|
|
Loading…
Reference in New Issue