140 lines 3.3 KiB Python Raw Blame History

 ```"""game""" ``` ``` ``` ```import random ``` ```import time ``` ```import sys ``` ``` ``` ``` ``` ```def check_special_score(score, p): ``` ``` """ ``` ``` ``` ``` :param score: ``` ``` :return: ``` ``` """ ``` ``` ``` ``` if score == 9 and p[1]: ``` ``` print("9 erreicht. Ende.") ``` ``` return True, score ``` ``` 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 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. ``` ``` ``` ``` :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) ``` ``` print("Damit haben Sie insgesamt " + str(player_score) + " gewürfelt.") ``` ``` ``` ``` 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": p[0], "final_score": player_score}) ``` ``` ``` ``` else: # AI ``` ``` ai_score = 0 ``` ``` ``` ``` 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__": ``` ``` welcome() ```