edited clean_input.py try..except

replaced "" in player_welcome_messaage
added os option in user_interface.py
started implementing clena_input.io()
This commit is contained in:
Steinadler 2020-12-17 11:27:28 +01:00
parent eb90944d6d
commit 61f4aa4fc5
4 changed files with 88 additions and 42 deletions

View File

@ -1,11 +1,12 @@
"""clean_input"""
import sys
import game.py
import game
#message needs to be a string that is printed for user instruction. options is a list of viable inputs
def io(message: str, options: list) -> str:
while True:
# message needs to be a string that is printed for user instruction. options is a list of viable inputs
def io(message: str, options: list) -> str:
while True:
try:
i = str(input(message))
@ -13,21 +14,27 @@ def io(message: str, options: list) -> str:
print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
if i in options:
return i
return i
break
else:
try:
print('Noch nicht ganz richtig. Hast du deine Auswahl auch richtig geschrieben?')
except(i =='q' or i == 'r'):
if i == 'q':
print("Spiel wird beendet...")
sys.exit()
if i == 'r':
print("Spiel wird neu gestartet...")
#game.main()
# try:
# print('Noch nicht ganz richtig. Hast du deine Auswahl auch richtig geschrieben?')
#
# except(i == 'q' or i == 'r'):
#
# if i == 'q':
# print("Spiel wird beendet...")
# sys.exit()
#
# if i == 'r':
# print("Spiel wird neu gestartet...")
# game.main()
if i == "q":
print("Spiel wird beendet...")
sys.exit()
elif i == "r":
print("Spiel wird neu gestartet...")
game.main()
else:
print('Noch nicht ganz richtig. Hast du deine Auswahl auch richtig geschrieben?')

71
game.py
View File

@ -2,20 +2,29 @@
import random
import time
import sys
# import user_interface.user_interface as ui
import clean_input as ci
def check_special_score(score, p):
"""
Checks if player of AI have a score of 9, 10 or >=16.
If a score is >=16, 'restart' gets returned and the game
is immediately over.
Otherwise the score is returned.
False == break for loop
True == do nothing
:param p: tuple with current player
:param score:
:return:
:return: bool, 'restart' or score
"""
if score == 9 and p[1]:
if score == 9 and p[1]: # player has 9
return True, score
elif score == 10 and p[1]:
print("10 erreicht. Noch ein letzter Wurf.")
elif score == 10 and p[1]: # player has 10
print("Sie haben 10 erreicht. Noch ein letzter Wurf...")
time.sleep(1)
r = roll_dice()
print(f"Sie würfeln {r}.")
@ -23,22 +32,21 @@ def check_special_score(score, p):
print(f"Sie haben insgesamt {score} gewürfelt und damit verloren.\n")
return False, "restart"
return True, score + sum(r)
elif score >= 16 and p[1]:
elif score >= 16 and p[1]: # player has too much
print(f"Sie haben insgesamt {score} gewürfelt und damit verloren.\n")
return False, "restart"
elif score == 9 and not p[1]:
elif score == 9 and not p[1]: # ai has 9
return True, score
elif score == 10 and not p[1]:
elif score == 10 and not p[1]: # ai has 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}.")
if score + sum(r) == 16:
print(f"{p[0]} habt insgeamt {score} 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 True, score + sum(r)
elif score >= 16 and not p[1]:
print(f"{p[0]} hat {score} gewürfelt.")
print(f"{p[0]} hat damit verloren!\n")
elif score >= 16 and not p[1]: # ai has too much
print(f"{p[0]} hat insgesamt {score} gewürfelt und damit verloren.\n")
return False, "restart"
else:
return False, score
@ -46,8 +54,11 @@ def check_special_score(score, p):
def choose_loser(all_results):
"""
Determines the players with the lowest score
and prints them.
Then starts a new game.
:param all_results:
:param all_results: list of dicts with player and final score
:return:
"""
loser_score = 15
@ -85,6 +96,7 @@ def roll_dice(number=1, faces=6, seed=None):
results = []
for n in range(number):
# ui.dice_animation()
result = random.randint(1, faces)
results.append(result)
@ -93,8 +105,11 @@ def roll_dice(number=1, faces=6, seed=None):
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.
:param players:
:param players: list of tuples
:return:
"""
all_results = []
@ -118,21 +133,22 @@ def sixteen_is_dead(players):
print("Damit haben Sie insgesamt " + str(player_score) + " gewürfelt.")
if player_score >= 16:
print("Sie haben verloren!")
sys.exit()
# if player_score >= 16:
# print("Sie haben verloren!")
# sys.exit()
if button:
break
button = input("Wollen Sie weiterwürfeln?")
button = ci.io("Wollen Sie weiterwürfeln?",["", "n"])
# button = input("Wollen Sie weiterwürfeln?")
all_results.append({"player": p[0], "final_score": player_score})
else: # AI
ai_score = 0
while ai_score < 13: # roll until score at least 9
while ai_score < 13: # roll until score at least 12
current_roll = roll_dice()
print(f"{p[0]} würfelt {current_roll}.")
@ -156,14 +172,29 @@ def sixteen_is_dead(players):
def main():
"""
Assigns human player name and number of players.
Construct list of tuples with players.
:return:
"""
with open("player_welcome_message", "r", encoding="utf8") as f:
welcome = ""
for l in f.readlines():
welcome += l
# print(welcome)
eval(welcome)
name = ""
while not name:
name = input("Wie heißen Sie?")
print(f"Willkommen, {name}!")
player_number = input("Gegen wie viele Personen wollen Sie spielen?")
player_number = 0
while not player_number:
try:
player_number = int(input("Gegen wie viele Personen wollen Sie spielen?"))
except:
print("Ihre Eingabe war nicht korrekt. Bitte versuchen Sie es nochmal.")
# TODO: player_number check for integer
players = []

View File

@ -16,7 +16,7 @@ so verlieren diejenigen, die am wenigsten Punkte erreicht haben. \n \n \
Zur Steuerung: \n \
(Neu) gewürfelt wir wenn Sie auf die ENTER-Taste drücken.\n \
Mit der Eingabe "n" reichen sie den Würfelbecher an die nächste Spielerin weiter.\n \
Zu jeder Zeit während dem Spiel können sie durch die Eingabe von 'r' das Spiel neu starten \n \
Zu jeder Zeit während dem Spiel können sie durch die Eingabe von "r" das Spiel neu starten \n \
und mit "q" das Spiel beenden.')

View File

@ -3,6 +3,8 @@
import time
import os
def dice_animation():
for i in range(4):
plz_print('dice_animation1.txt')
@ -12,14 +14,20 @@ def dice_animation():
time.sleep(0.25)
clear()
def plz_print(filename):
with open(filename) as f:
with open(filename, 'r', encoding='utf8') as f:
bigstring = ""
for line in f:
bigstring = bigstring + line
print('\n' + bigstring)
def clear():
os.system('clear')
if os.name == 'nt': # for windows
os.system('cls')
else: # for linux, mac
os.system('clear')
dice_animation()