diff --git a/dice.py b/dice.py index 664f90c..02b46c9 100644 --- a/dice.py +++ b/dice.py @@ -1,4 +1,5 @@ import sys +from collections import Counter from os import name, system from tabulate import tabulate @@ -7,6 +8,7 @@ hraci = {} score = {} pointer = "" first = "" +final_score = 10000 def clear() -> None: _ = system('cls') if name == 'nt' else system('clear') @@ -14,6 +16,7 @@ def clear() -> None: def char_create() -> bool: global pointer global first + global final_score while True: inp: str = input("Zadejte jméno prvního hráče: ") @@ -43,6 +46,19 @@ def char_create() -> bool: else: break + while True: + inp = input("Zadejte finální skóre (nechte prázdné pro výchozích 10 000): ") + if inp != "": + try: + inp_int: int = int(inp) + final_score = inp_int + except ValueError: + print("Zadejte prosím platnou číselnou hodnotu.") + else: + break + else: + break + return True def next_player() -> None: @@ -69,18 +85,93 @@ def table(): return tabulate(hraci_tab, headers=table_header, tablefmt="fancy_grid") def add_score(hrac, hodnota): + if hodnota == 0: + last_two = hraci[hrac][-2:] + if len(last_two) > 1: + last_two_sum = sum(last_two) + if last_two_sum == 0: + hodnota = -sum(hraci[hrac]) + clear() + input("Po třech nulových hodech bylo vaše skóre vynulováno.") + hraci[hrac].append(hodnota) - score[hrac] += hodnota + score[hrac] = sum(hraci[hrac]) + +def to_sorted_tuple(d: dict[str, int], s: bool = True) -> tuple[list[tuple[int, str, int]], + dict[str, int]]: + L: list[tuple[int, str, int]] = [] + + if s: + d = dict(sorted(d.items(), key=lambda x: x[1], reverse=True)) + + for i, (k, v) in enumerate(d.items()): + L.append((i+1, k, v)) + + return L, d + +def check_win() -> str: + over_limit: dict[str, int] = {} + for p in score: + if score[p] >= final_score: + over_limit[p] = score[p] + + winner: str = "" + + if over_limit: + L, d = to_sorted_tuple(over_limit) + + count: Counter = Counter(list(d.values())) + + for c in count: + if count[c] == 1: + winner = L[0][1] + break + + return winner + +def win(winner: str, vitezne_skore: int) -> None: + results, d = to_sorted_tuple(score) + + headers = ["Pořadí", "Hráč", "Skóre"] + vysledky = tabulate(results, headers=headers, tablefmt="fancy_grid") + + print(f"Vítězem se stává {winner} s {vitezne_skore} body!") + print("Díky za hru a zase příště.\n") + + print(vysledky) def game(): while True: clear() print(table()) - inp = input(f"Zadejte skóre hráče {pointer}: ") - inp_int = int(inp) + + strikes: str = "" + + if len(hraci[pointer]) > 0: + if hraci[pointer][-1] == 0: + strikes = "x" + if len(hraci[pointer]) > 1 and hraci[pointer][-2] == 0: + strikes += "x" + else: + strikes = "" + + inp = input(f"Zadejte skóre hráče {pointer} ({score[pointer]}{strikes}/{final_score}): ") + try: + inp_int = int(inp) + except ValueError: + clear() + input("Zadejte prosím platnou číselnou hodnotu.") + continue + add_score(pointer, inp_int) next_player() - # break + + if pointer == first: + winner: str = check_win() + if winner: + clear() + win(winner, score[winner]) + break def main() -> None: