181 lines
4.5 KiB
Python
181 lines
4.5 KiB
Python
"""game"""
|
|
|
|
import random
|
|
import time
|
|
import sys
|
|
|
|
|
|
def check_special_score(score, p):
|
|
"""
|
|
|
|
:param score:
|
|
:return:
|
|
"""
|
|
|
|
if score == 9 and p[1]:
|
|
return True, score
|
|
elif score == 10 and p[1]:
|
|
print("10 erreicht. Noch ein letzter Wurf.")
|
|
time.sleep(1)
|
|
r = roll_dice()
|
|
print(f"Sie würfeln {r}.")
|
|
if score + sum(r) == 16:
|
|
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]:
|
|
print(f"Sie haben insgesamt {score} gewürfelt und damit verloren.\n")
|
|
return False, "restart"
|
|
elif score == 9 and not p[1]:
|
|
return True, score
|
|
elif score == 10 and not p[1]:
|
|
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")
|
|
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")
|
|
return False, "restart"
|
|
else:
|
|
return False, score
|
|
|
|
|
|
def choose_loser(all_results):
|
|
"""
|
|
|
|
:param all_results:
|
|
:return:
|
|
"""
|
|
loser_score = 15
|
|
losers = []
|
|
|
|
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
|
|
if r["final_score"] == loser_score:
|
|
losers.append(r["player"])
|
|
|
|
loser_string = "Die folgenden Spieler haben verloren: "
|
|
losers = ",".join(losers)
|
|
loser_string = loser_string + losers
|
|
|
|
print(loser_string)
|
|
|
|
main()
|
|
|
|
|
|
def roll_dice(number=1, faces=6, seed=None):
|
|
"""
|
|
Throws a certain number of dice with a certain number of faces.
|
|
|
|
:param number: Number of dice
|
|
:param faces: Number of faces of a die
|
|
:param seed: Number for random.seed() to reproduce randomness
|
|
:return: List of results
|
|
"""
|
|
if seed:
|
|
random.seed(seed)
|
|
|
|
results = []
|
|
|
|
for n in range(number):
|
|
result = random.randint(1, faces)
|
|
results.append(result)
|
|
|
|
return results
|
|
|
|
|
|
def sixteen_is_dead(players):
|
|
"""
|
|
|
|
:param players:
|
|
:return:
|
|
"""
|
|
all_results = []
|
|
|
|
for p in players:
|
|
|
|
if p[1]: # human player
|
|
button = input("Sie sind an der Reihe!")
|
|
player_score = 0
|
|
|
|
while button != "n":
|
|
current_roll = roll_dice()
|
|
print("Sie haben " + str(current_roll) + " gewürfelt!")
|
|
|
|
player_score += sum(current_roll)
|
|
|
|
button, player_score = check_special_score(player_score, p)
|
|
|
|
if not button and player_score == "restart":
|
|
main()
|
|
|
|
print("Damit haben Sie insgesamt " + str(player_score) + " gewürfelt.")
|
|
|
|
if player_score >= 16:
|
|
print("Sie haben verloren!")
|
|
sys.exit()
|
|
|
|
if button:
|
|
break
|
|
|
|
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
|
|
current_roll = roll_dice()
|
|
print(f"{p[0]} würfelt {current_roll}.")
|
|
|
|
ai_score += sum(current_roll)
|
|
|
|
button, ai_score = check_special_score(ai_score, p)
|
|
|
|
if not button and ai_score == "restart":
|
|
main()
|
|
|
|
print(f"{p[0]} hat damit insgesamt {ai_score} gewürfelt.")
|
|
|
|
if button:
|
|
break
|
|
|
|
all_results.append({"player": p[0], "final_score": ai_score})
|
|
else:
|
|
print(all_results)
|
|
choose_loser(all_results)
|
|
|
|
|
|
def main():
|
|
"""
|
|
|
|
:return:
|
|
"""
|
|
name = ""
|
|
while not name:
|
|
name = input("Wie heißen Sie?")
|
|
print(f"Willkommen, {name}!")
|
|
player_number = input("Gegen wie viele Personen wollen Sie spielen?")
|
|
# TODO: player_number check for integer
|
|
|
|
players = []
|
|
human = (name, True)
|
|
players.append(human)
|
|
|
|
for p in range(int(player_number)):
|
|
players.append(("P" + str(p), False))
|
|
|
|
sixteen_is_dead(players)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|