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:
parent
eb90944d6d
commit
61f4aa4fc5
|
@ -1,11 +1,12 @@
|
||||||
"""clean_input"""
|
"""clean_input"""
|
||||||
|
|
||||||
import sys
|
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:
|
# message needs to be a string that is printed for user instruction. options is a list of viable inputs
|
||||||
while True:
|
def io(message: str, options: list) -> str:
|
||||||
|
while True:
|
||||||
try:
|
try:
|
||||||
i = str(input(message))
|
i = str(input(message))
|
||||||
|
|
||||||
|
@ -13,21 +14,27 @@ def io(message: str, options: list) -> str:
|
||||||
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:
|
||||||
return i
|
return i
|
||||||
break
|
break
|
||||||
|
|
||||||
else:
|
else:
|
||||||
try:
|
# try:
|
||||||
print('Noch nicht ganz richtig. Hast du deine Auswahl auch richtig geschrieben?')
|
# print('Noch nicht ganz richtig. Hast du deine Auswahl auch richtig geschrieben?')
|
||||||
|
#
|
||||||
except(i =='q' or i == 'r'):
|
# except(i == 'q' or i == 'r'):
|
||||||
|
#
|
||||||
if i == 'q':
|
# if i == 'q':
|
||||||
print("Spiel wird beendet...")
|
# print("Spiel wird beendet...")
|
||||||
sys.exit()
|
# sys.exit()
|
||||||
|
#
|
||||||
if i == 'r':
|
# if i == 'r':
|
||||||
print("Spiel wird neu gestartet...")
|
# print("Spiel wird neu gestartet...")
|
||||||
#game.main()
|
# 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
71
game.py
|
@ -2,20 +2,29 @@
|
||||||
|
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
import sys
|
# import user_interface.user_interface as ui
|
||||||
|
import clean_input as ci
|
||||||
|
|
||||||
|
|
||||||
def check_special_score(score, p):
|
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:
|
: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
|
return True, score
|
||||||
elif score == 10 and p[1]:
|
elif score == 10 and p[1]: # player has 10
|
||||||
print("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()
|
||||||
print(f"Sie würfeln {r}.")
|
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")
|
print(f"Sie haben insgesamt {score} 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]:
|
elif score >= 16 and p[1]: # player has too much
|
||||||
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]:
|
elif score == 9 and not p[1]: # ai has 9
|
||||||
return True, score
|
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.")
|
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}.")
|
||||||
if score + sum(r) == 16:
|
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 False, "restart"
|
||||||
return True, score + sum(r)
|
return True, score + sum(r)
|
||||||
elif score >= 16 and not p[1]:
|
elif score >= 16 and not p[1]: # ai has too much
|
||||||
print(f"{p[0]} hat {score} gewürfelt.")
|
print(f"{p[0]} hat insgesamt {score} gewürfelt und damit verloren.\n")
|
||||||
print(f"{p[0]} hat damit verloren!\n")
|
|
||||||
return False, "restart"
|
return False, "restart"
|
||||||
else:
|
else:
|
||||||
return False, score
|
return False, score
|
||||||
|
@ -46,8 +54,11 @@ def check_special_score(score, p):
|
||||||
|
|
||||||
def choose_loser(all_results):
|
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:
|
:return:
|
||||||
"""
|
"""
|
||||||
loser_score = 15
|
loser_score = 15
|
||||||
|
@ -85,6 +96,7 @@ def roll_dice(number=1, faces=6, seed=None):
|
||||||
results = []
|
results = []
|
||||||
|
|
||||||
for n in range(number):
|
for n in range(number):
|
||||||
|
# ui.dice_animation()
|
||||||
result = random.randint(1, faces)
|
result = random.randint(1, faces)
|
||||||
results.append(result)
|
results.append(result)
|
||||||
|
|
||||||
|
@ -93,8 +105,11 @@ def roll_dice(number=1, faces=6, seed=None):
|
||||||
|
|
||||||
def sixteen_is_dead(players):
|
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:
|
:return:
|
||||||
"""
|
"""
|
||||||
all_results = []
|
all_results = []
|
||||||
|
@ -118,21 +133,22 @@ def sixteen_is_dead(players):
|
||||||
|
|
||||||
print("Damit haben Sie insgesamt " + str(player_score) + " gewürfelt.")
|
print("Damit haben Sie insgesamt " + str(player_score) + " gewürfelt.")
|
||||||
|
|
||||||
if player_score >= 16:
|
# if player_score >= 16:
|
||||||
print("Sie haben verloren!")
|
# print("Sie haben verloren!")
|
||||||
sys.exit()
|
# sys.exit()
|
||||||
|
|
||||||
if button:
|
if button:
|
||||||
break
|
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})
|
all_results.append({"player": p[0], "final_score": player_score})
|
||||||
|
|
||||||
else: # AI
|
else: # AI
|
||||||
ai_score = 0
|
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()
|
current_roll = roll_dice()
|
||||||
print(f"{p[0]} würfelt {current_roll}.")
|
print(f"{p[0]} würfelt {current_roll}.")
|
||||||
|
|
||||||
|
@ -156,14 +172,29 @@ def sixteen_is_dead(players):
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""
|
"""
|
||||||
|
Assigns human player name and number of players.
|
||||||
|
Construct list of tuples with players.
|
||||||
|
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
|
with open("player_welcome_message", "r", encoding="utf8") as f:
|
||||||
|
welcome = ""
|
||||||
|
for l in f.readlines():
|
||||||
|
welcome += l
|
||||||
|
# print(welcome)
|
||||||
|
eval(welcome)
|
||||||
|
|
||||||
name = ""
|
name = ""
|
||||||
while not name:
|
while not name:
|
||||||
name = input("Wie heißen Sie?")
|
name = input("Wie heißen Sie?")
|
||||||
print(f"Willkommen, {name}!")
|
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
|
# TODO: player_number check for integer
|
||||||
|
|
||||||
players = []
|
players = []
|
||||||
|
|
|
@ -16,7 +16,7 @@ so verlieren diejenigen, die am wenigsten Punkte erreicht haben. \n \n \
|
||||||
Zur Steuerung: \n \
|
Zur Steuerung: \n \
|
||||||
(Neu) gewürfelt wir wenn Sie auf die ENTER-Taste drücken.\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 \
|
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.')
|
und mit "q" das Spiel beenden.')
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def dice_animation():
|
def dice_animation():
|
||||||
for i in range(4):
|
for i in range(4):
|
||||||
plz_print('dice_animation1.txt')
|
plz_print('dice_animation1.txt')
|
||||||
|
@ -12,14 +14,20 @@ def dice_animation():
|
||||||
time.sleep(0.25)
|
time.sleep(0.25)
|
||||||
clear()
|
clear()
|
||||||
|
|
||||||
|
|
||||||
def plz_print(filename):
|
def plz_print(filename):
|
||||||
with open(filename) as f:
|
with open(filename, 'r', encoding='utf8') as f:
|
||||||
bigstring = ""
|
bigstring = ""
|
||||||
for line in f:
|
for line in f:
|
||||||
bigstring = bigstring + line
|
bigstring = bigstring + line
|
||||||
print('\n' + bigstring)
|
print('\n' + bigstring)
|
||||||
|
|
||||||
|
|
||||||
def clear():
|
def clear():
|
||||||
os.system('clear')
|
if os.name == 'nt': # for windows
|
||||||
|
os.system('cls')
|
||||||
|
else: # for linux, mac
|
||||||
|
os.system('clear')
|
||||||
|
|
||||||
|
|
||||||
dice_animation()
|
dice_animation()
|
||||||
|
|
Loading…
Reference in New Issue