From 4e9654845c8b3b674ef056119563a549ef468abd Mon Sep 17 00:00:00 2001 From: Steinadler Date: Fri, 11 Dec 2020 14:45:12 +0100 Subject: [PATCH] added welcome() added choose_loser() --- game.py | 84 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 63 insertions(+), 21 deletions(-) diff --git a/game.py b/game.py index fdb55e7..dabdf21 100644 --- a/game.py +++ b/game.py @@ -2,30 +2,70 @@ import random import time +import sys -def check_special_score(score): + +def check_special_score(score, p): """ :param score: :return: """ - if score == 9: + if score == 9 and p[1]: print("9 erreicht. Ende.") return True, score - elif score == 10: + elif score == 10 and p[1]: print("10 erreicht. Noch ein letzter Wurf.") time.sleep(3) r = roll_dice() print(r) return True, score + sum(r) - elif score >= 16: - print("Sie haben 16 erreicht.") + elif score >= 16 and p[1]: + print("Sie haben 16 erreicht/überschritten.") + return True, score + elif score == 9 and not p[1]: + return True, score + elif score == 10 and not p[1]: + r = roll_dice() + return True, score + sum(r) + elif score >= 16 and not p[1]: + print(str(p[0]) + " hat 16 erreicht/überschritten.") return True, score else: return False, score +def welcome(): + """ + + :return: + """ + name = input("Wie heißen Sie?") + player_number = input("Gegen wie viele Personen wollen Sie spielen?") + + players = [] + human = (name, True) + players.append(human) + + for p in range(int(player_number)): + players.append(("P" + str(p), False)) + + sixteen_is_dead(players) + + +def choose_loser(all_results): + 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"]) + def roll_dice(number=1, faces=6, seed=None): """ Throws a certain number of dice with a certain number of faces. @@ -67,31 +107,33 @@ def sixteen_is_dead(players): player_score += sum(current_roll) print("Damit haben Sie insgesamt " + str(player_score) + " gewürfelt.") - button, player_score = check_special_score(player_score) + button, player_score = check_special_score(player_score, p) + + if player_score >= 16: + print("Sie haben verloren!") + sys.exit() if button: break button = input("Wollen Sie weiterwürfeln?") - all_results.append(player_score) + all_results.append({"player": p[0], "final_score": player_score}) else: # AI - pass + ai_score = 0 - # TODO: implement winner decision process, max(all_results) and min(all_results) - print(all_results) + while ai_score < 9: # roll until score at least 9 + current_roll = roll_dice() + ai_score += sum(current_roll) + + button, ai_score = check_special_score(ai_score, p) + + if button: + break + + all_results.append({"player": p[0], "final_score": ai_score}) if __name__ == "__main__": - name = input("Wie heißen Sie?") - player_number = input("Gegen wie viele Personen wollen Sie spielen?") - - players = [] - human = (name, True) - players.append(human) - - for p in range(int(player_number)): - players.append(("P" + str(p), False)) - - sixteen_is_dead(players) + welcome()