19 Commits
Author SHA1 Message Date
David Spáčil 7b0c9cadbb feature/vysledky: definice funkce win() a její volání z bloku win ve funkci game() 2026-06-27 17:15:47 +02:00
David Spáčil bdd66379f7 feature/vysledky: list z to_sorted_tuple() se vrací s číslováním pořadí 2026-06-27 17:11:10 +02:00
David Spáčil c3a2e94cab feature/vysledky: funkce pro řazení výsledku na základě kódu v check_win(); kód v check_win() nahrazen funkcí 2026-06-27 16:58:10 +02:00
david-spacil 485f152a41 Merge pull request 'Refactoring: typové anotace a úklid' (#23) from refactoring into version/v0.2.0
Reviewed-on: david/kostky#23
2026-06-27 12:59:05 +02:00
David Spáčil 26ad8508b5 refactoring: drobné úpravy UI 2026-06-27 12:55:51 +02:00
David Spáčil 2f8d5b12c2 refactoring: čištění 2026-06-27 12:34:04 +02:00
David Spáčil 5aa5beff69 refactoring: oprava tuple[Any, ...] anotace 2026-06-27 12:30:47 +02:00
David Spáčil a58487e9a6 refactoring: type annotation 2026-06-27 12:23:28 +02:00
David Spáčil 9967682373 refactoring: ve funkci evaluate sloučeny přebytečné returny 2026-06-27 09:24:56 +02:00
david-spacil c225161374 Merge pull request 'Vynulování' (#19) from feature/vynulovani into version/v0.2.0
Reviewed-on: david/kostky#19
2026-06-26 20:40:47 +02:00
david-spacil e7c8d99c96 feature/vynulovani: přidána kondolenční hláška 2026-06-26 20:37:38 +02:00
david-spacil cef9468ad0 feature/vynulovani: přidán ukazatel pčtu "striků" v podobě jednoho až dvou X vedle celkového skóre hráče 2026-06-26 20:33:33 +02:00
david-spacil 0682ad88ce feature/vynulovani: byla implementována logika pro vynulování: po třetím tahu končícím nulou dojde k vynulování celkového skóre hráče 2026-06-26 20:21:08 +02:00
david-spacil a6a256992e Merge pull request 'Přidání možnosti exit' (#18) from feature/exit into version/v0.2.0
Reviewed-on: david/kostky#18
2026-06-26 20:02:35 +02:00
david-spacil 1fcf01152d feature/exit: loučící hláška a konec 2026-06-26 20:00:30 +02:00
david-spacil 2fa0aa8a95 feature/exit: přidáno elif do evaluate() 2026-06-26 19:59:05 +02:00
david-spacil 73e8747b73 feature/exit: přidána možnost do herní nabídky 2026-06-26 19:47:20 +02:00
david-spacil 14f1ebbd29 Merge pull request 'Odstraněno volání funkce clear() při renderování chybových hlášek souvisejících s hodem (je vidět hodnoty na kostkách)' (#16) from fix/chybove-hlasky into version/v0.2.0
Reviewed-on: david/kostky#16
2026-06-26 19:45:13 +02:00
david-spacil 7e3639ab12 fix/chybove-hlasky: odstraněno volání funkce clear() při renderování chybových hlášek souvisejících s hodem (je vidět hodnoty na kostkách) 2026-06-26 19:43:06 +02:00
+106 -60
View File
@@ -4,15 +4,16 @@ from os import system, name
import re import re
import csv import csv
from collections import Counter from collections import Counter
from tabulate import tabulate
score = 0 score: int = 0
hodnoty = {} hodnoty: dict[str, int] = {}
players = {} players: dict[str, list[int]] = {}
pointer = "" pointer: str = ""
first = "" first: str = ""
h_bool = True h_bool: bool = True
aggr = {} aggr: dict[str, int] = {}
final_score = 10000 final_score: int = 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)
@@ -25,18 +26,18 @@ with open("hodnoty.csv", "r") as csv_file:
def throw(count: int) -> str: def throw(count: int) -> str:
count = int(count) count = int(count)
new_throw = [] new_throw: list[str] = []
for dice in range(count): for dice in range(count):
new_throw.append(str(randint(1, 6))) new_throw.append(str(randint(1, 6)))
new_throw = "".join(new_throw) new_throw_str: str = "".join(new_throw)
return new_throw return new_throw_str
def show_throw(throw: str) -> str: def show_throw(throw: str) -> str:
throw_temp = [] throw_temp: list[str] = []
for x in throw: for x in throw:
throw_temp.append(x) throw_temp.append(x)
pretty_list = ", ".join(throw_temp) pretty_list: str = ", ".join(throw_temp)
pretty_list += "\n" pretty_list += "\n"
for i in range(1, len(throw_temp) + 1): for i in range(1, len(throw_temp) + 1):
pretty_list += "^ " pretty_list += "^ "
@@ -45,13 +46,13 @@ def show_throw(throw: str) -> str:
pretty_list += f"{i} " pretty_list += f"{i} "
return pretty_list return pretty_list
def clear(): def clear() -> None:
if name == 'nt': if name == 'nt':
_ = system('cls') _ = system('cls')
else: else:
_ = system('clear') _ = system('clear')
def evaluate(inp: Any, count: int, throw: str) -> tuple: def evaluate(inp: Any, count: int, throw: str) -> tuple[Any, ...]:
global h_bool global h_bool
@@ -77,8 +78,7 @@ def evaluate(inp: Any, count: int, throw: str) -> tuple:
inp = str(inp) inp = str(inp)
if re.search(r"^\d+$", inp): if re.search(r"^\d+$", inp):
if len(inp) > count: if len(inp) > count:
data = ("msg", "ER01", "Tolika kostkami nemůžete hodit. Zkuste to znovu:", count, throw) data: tuple[Any, ...] = ("msg", "ER01", "Tolika kostkami nemůžete hodit. Zkuste to znovu: ", count, throw)
return data
else: else:
for x in inp: for x in inp:
x = int(x) x = int(x)
@@ -87,30 +87,30 @@ def evaluate(inp: Any, count: int, throw: str) -> tuple:
ran = "1" ran = "1"
else: else:
ran = f"1–{count}" ran = f"1–{count}"
data = ("msg", "ER02", f"Zvolte prosím kostky v platném rozsahu ({ran}):", count, throw) data = ("msg", "ER02", f"Zvolte prosím kostky v platném rozsahu ({ran}): ", count, throw)
return data return data
old_throw = [] old_throw: list[str] = []
pick = [] pick: list[str] = []
for y in range(count): for y in range(count):
if str(y+1) not in inp: if str(y+1) not in inp:
old_throw.append(throw[y]) old_throw.append(throw[y])
if str(y+1) in inp: if str(y+1) in inp:
pick.append(throw[y]) pick.append(throw[y])
old_throw = "".join(old_throw) old_throw_str: str = "".join(old_throw)
pick.sort() pick.sort()
pick = "".join(pick) pick_str: str = "".join(pick)
new_count = len(old_throw) new_count: int = len(old_throw_str)
if not eval_score(pick): if not eval_score(pick_str):
new_count = count new_count = count
old_throw = throw old_throw_str = throw
else: else:
h_bool = False h_bool = False
data = ("count", new_count, old_throw) data = ("count", new_count, old_throw_str)
return data
elif inp == "h": elif inp == "h":
if h_bool: if h_bool:
clear() clear()
@@ -119,9 +119,19 @@ def evaluate(inp: Any, count: int, throw: str) -> tuple:
else: else:
h_bool = True h_bool = True
data = ("count", count, "") data = ("count", count, "")
return data
elif inp == "k": elif inp == "k":
global score global score
if score == 0:
last_two = players[pointer][-2:]
if len(last_two) > 1:
last_two_sum = sum(last_two)
if last_two_sum == 0:
score = -sum(players[pointer])
clear()
input("Po třech nulových hodech bylo vaše skóre vynulováno.")
players[pointer].append(score) players[pointer].append(score)
aggr[pointer] = sum(players[pointer]) aggr[pointer] = sum(players[pointer])
score = 0 score = 0
@@ -129,18 +139,22 @@ def evaluate(inp: Any, count: int, throw: str) -> tuple:
next_player() next_player()
data = ("count", 6, "") data = ("count", 6, "")
if pointer == first: if pointer == first:
winner = check_win() winner: str = check_win()
if winner: if winner:
data = ("win", winner, aggr[winner]) data = ("win", winner, aggr[winner])
return data
elif inp == "exit":
data = ("exit",)
else: else:
data = ("msg", "ER03", "Zadejte prosím platnou hodnotu:", count, throw) data = ("msg", "ER03", "Zadejte prosím platnou hodnotu:", count, throw)
return data
return data
def check_value(throw: str) -> bool: def check_value(throw: str) -> bool:
throw_s = sorted(throw) throw_s: list[str] = sorted(throw)
throw_str = "".join(throw_s) throw_str: str = "".join(throw_s)
hodnota = False hodnota: bool = False
for h in hodnoty: for h in hodnoty:
if h in throw_str: if h in throw_str:
hodnota = True hodnota = True
@@ -148,11 +162,11 @@ def check_value(throw: str) -> bool:
return hodnota return hodnota
def add_players(): def add_players() -> None:
global pointer, first, final_score global pointer, first, final_score
while True: while True:
inp = input("Zadejte jméno prvního hráče: ") inp: str = input("Zadejte jméno prvního hráče: ")
if inp == "": if inp == "":
print("Jméno nemůže být prázdné.") print("Jméno nemůže být prázdné.")
else: else:
@@ -180,8 +194,8 @@ def add_players():
inp = input("Zadejte finální skóre (nechte prázdné pro výchozích 10 000): ") inp = input("Zadejte finální skóre (nechte prázdné pro výchozích 10 000): ")
if inp != "": if inp != "":
try: try:
inp = int(inp) inp_int: int = int(inp)
final_score = inp final_score = inp_int
except ValueError: except ValueError:
print("Zadejte prosím platnou číselnou hodnotu.") print("Zadejte prosím platnou číselnou hodnotu.")
else: else:
@@ -189,40 +203,69 @@ def add_players():
else: else:
break break
def to_sorted_tuple(d: dict[str, int], s: bool = True) -> tuple[list[tuple[str, int]], dict[str, int]]:
L: list[tuple[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: def check_win() -> str:
over_limit = {} over_limit: dict[str, int] = {}
for p in aggr: for p in aggr:
if aggr[p] >= final_score: if aggr[p] >= final_score:
over_limit[p] = aggr[p] over_limit[p] = aggr[p]
winner = "" winner: str = ""
if over_limit: if over_limit:
over_limit_sorted = dict(sorted(over_limit.items(), key=lambda x: x[1], reverse=True)) L, d = to_sorted_tuple(over_limit)
over_limit_sorted_list = []
for ols in over_limit_sorted:
over_limit_sorted_list.append((ols, over_limit_sorted[ols]))
count: Counter = Counter(list(d.values()))
count = Counter(list(over_limit_sorted.values()))
for c in count: for c in count:
if count[c] == 1: if count[c] == 1:
winner = over_limit_sorted_list[0][0] winner = L[0][1]
break break
return winner return winner
def game(data: tuple): def win(winner: str, score: int) -> None:
results, d = to_sorted_tuple(aggr)
headers = ["Pořadí", "Hráč", "Skóre"]
table = tabulate(results, headers=headers, tablefmt="fancy_grid")
print(f"Vítězem se stává {winner} s {score} body!")
print("Díky za hru a zase příště.\n")
print(table)
def game(data: tuple) -> None:
while True: while True:
if data[0] == "count": if data[0] == "count":
count = data[1] count: int = data[1]
th = data[2] th: str = data[2]
if(th == ""): if(th == ""):
th = throw(count) th = throw(count)
clear() clear()
print(f"Vítězné skóre: {final_score}.") # print(f"Vítězné skóre: {final_score}.")
print(f"Na tahu je {pointer} ({aggr[pointer]}).")
strikes: str = ""
if len(players[pointer]) > 0:
if players[pointer][-1:][0] == 0:
strikes = "x"
if players[pointer][-2:][0] == 0:
strikes += "x"
else:
strikes = ""
print(f"Na tahu je {pointer} ({aggr[pointer]}{strikes}/{final_score}).")
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
@@ -232,12 +275,13 @@ def game(data: tuple):
else: else:
if count == 0: if count == 0:
count = 6 count = 6
msg = ("Jakou akci chcete provést?\n" msg: str = ("Jakou akci chcete provést?\n"
" - Zadejte kombinaci kostek k ponechání (dojde k jejich vyhodnocení): [pozice kostek] (např. 1 nebo 34 nebo 123456).\n" " - Zadejte kombinaci kostek k ponechání (dojde k jejich vyhodnocení): [pozice kostek] (např. 1 nebo 34 nebo 123456).\n"
" - Hodit znovu: [h].\n" " - Hodit znovu: [h].\n"
f" - Ukončit hod a ponechat si skóre z tohoto hodu ({score}): [k].\n" f" - Ukončit hod a ponechat si skóre z tohoto hodu ({score}): [k].\n"
" - Ukončit hru: [exit].\n"
) )
inp = input(msg) inp: str = input(msg)
data = evaluate(inp, count, th) data = evaluate(inp, count, th)
elif data[0] == "msg": elif data[0] == "msg":
@@ -245,7 +289,6 @@ def game(data: tuple):
msg = data[2] msg = data[2]
count = data[3] count = data[3]
th = data[4] th = data[4]
clear()
inp = input(msg) inp = input(msg)
data = evaluate(inp, count, th) data = evaluate(inp, count, th)
else: else:
@@ -254,19 +297,22 @@ def game(data: tuple):
break break
elif data[0] == "win": elif data[0] == "win":
winner = data[1] winner: str = data[1]
score = data[2] score = data[2]
clear() clear()
print(f"Vítězem se stává {winner} s {score} body!") win(winner, score)
print("Díky za hru a zase příště.")
break break
def main(): elif data[0] == "exit":
clear()
print("Hra byla předčasně ukončena. Snad se zase brzy uvidíme.")
break
def main() -> None:
clear() 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:")
data = ("count", 6, "") data = ("count", 6, "")
game(data) game(data)