Merge pull request 'feature: konec hry – cílové skóre a vyhlášení vítěze' (#10) from feature/konec-hry into version/v0.1.0

Reviewed-on: david/kostky#10
This commit was merged in pull request #10.
This commit is contained in:
2026-06-24 23:57:32 +02:00
+47 -1
View File
@@ -3,6 +3,7 @@ from random import randint
from os import system, name from os import system, name
import re import re
import csv import csv
from collections import Counter
score = 0 score = 0
hodnoty = {} hodnoty = {}
@@ -11,6 +12,7 @@ pointer = ""
first = "" first = ""
h_bool = True h_bool = True
aggr = {} aggr = {}
final_score = 10000
with open("hodnoty.csv", "r") as csv_file: with open("hodnoty.csv", "r") as csv_file:
csv_reader = csv.reader(csv_file) csv_reader = csv.reader(csv_file)
@@ -126,6 +128,10 @@ def evaluate(inp: Any, count: int, throw: str) -> tuple:
h_bool = True h_bool = True
next_player() next_player()
data = ("count", 6, "") data = ("count", 6, "")
if pointer == first:
winner = check_win()
if winner:
data = ("win", winner, aggr[winner])
return data return data
else: else:
data = ("msg", "ER03", "Zadejte prosím platnou hodnotu:", count, throw) data = ("msg", "ER03", "Zadejte prosím platnou hodnotu:", count, throw)
@@ -146,6 +152,7 @@ def add_players(iter=False):
global players global players
global pointer global pointer
global first global first
global final_score
if not iter: if not iter:
var = "prvního hráče" var = "prvního hráče"
@@ -157,6 +164,11 @@ def add_players(iter=False):
if inp == "x": if inp == "x":
if not iter: if not iter:
add_players() add_players()
inp = input("Zadejte finální skóre (nechte prázdné pro výchozích 10 000): ")
if inp != "":
inp = int(inp)
final_score = inp
elif inp == "": elif inp == "":
print("Jméno nemůže být prázdné.") print("Jméno nemůže být prázdné.")
add_players(iter) add_players(iter)
@@ -172,6 +184,30 @@ def add_players(iter=False):
first = inp first = inp
add_players(True) add_players(True)
def check_win() -> str:
over_limit = {}
for p in aggr:
if aggr[p] >= final_score:
over_limit[p] = aggr[p]
winner = ""
if over_limit:
over_limit_sorted = dict(sorted(over_limit.items(), key=lambda x: x[1], reverse=True))
over_limit_sorted_list = []
for ols in over_limit_sorted:
over_limit_sorted_list.append((ols, over_limit_sorted[ols]))
count = Counter(list(over_limit_sorted.values()))
for c in count:
if count[c] == 1:
winner = over_limit_sorted_list[0][0]
break
return winner
def game(data: tuple): def game(data: tuple):
if data[0] == "count": if data[0] == "count":
count = data[1] count = data[1]
@@ -179,7 +215,8 @@ def game(data: tuple):
if(th == ""): if(th == ""):
th = throw(count) th = throw(count)
clear() clear()
print(f"Na tahu je {pointer} ({aggr[pointer]})") print(f"Vítězné skóre: {final_score}.")
print(f"Na tahu je {pointer} ({aggr[pointer]}).")
print(show_throw(th)) print(show_throw(th))
if not check_value(th) and data[2] == "" and count != 0: if not check_value(th) and data[2] == "" and count != 0:
global score global score
@@ -212,7 +249,16 @@ def game(data: tuple):
msg = data[1] msg = data[1]
print(msg) print(msg)
elif data[0] == "win":
winner = data[1]
score = data[2]
clear()
print(f"Vítězem se stává {winner} s {score} body!")
print("Díky za hru a zase příště.")
def main(): def main():
clear()
print("Vítejte ve hře v kostky!") print("Vítejte ve hře v kostky!")
add_players() add_players()
input("Prosím, stiskněte enter pro první hod:") input("Prosím, stiskněte enter pro první hod:")