refactor: převod rekurze na smyčky v game() a add_players() #12

Merged
david-spacil merged 4 commits from fix/loop into version/v0.1.0 2026-06-25 13:49:11 +02:00
+35 -30
View File
@@ -148,41 +148,46 @@ def check_value(throw: str) -> bool:
return hodnota
def add_players(iter=False):
global players
global pointer
global first
global final_score
def add_players():
global pointer, first, final_score
if not iter:
var = "prvního hráče"
else:
var = "dalšího hráče nebo [x] pro ukončení zadávání hráčů"
inp = input(f"Zadejte jméno {var}: ")
if inp == "x":
if not iter:
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 == "":
while True:
inp = input("Zadejte jméno prvního hráče: ")
if inp == "":
print("Jméno nemůže být prázdné.")
add_players(iter)
else:
if inp in players:
players[inp] = []
aggr[inp] = 0
pointer = inp
first = inp
print(f"Do seznamu přidáno jméno {inp}.")
break
while True:
inp = input("Zadejte jméno dalšího hráče nebo [x] pro ukončení zadávání hráčů: ")
if inp == "":
print("Jméno nemůže být prázdné.")
elif inp == "x":
break
elif inp in players:
print("Zadávejte, prosím, unikátní jména.")
else:
players[inp] = []
aggr[inp] = 0
print(f"Do seznamu přidáno jméno {inp}.")
if not iter:
pointer = inp
first = inp
add_players(True)
while True:
inp = input("Zadejte finální skóre (nechte prázdné pro výchozích 10 000): ")
if inp != "":
try:
inp = int(inp)
final_score = inp
except ValueError:
print("Zadejte prosím platnou číselnou hodnotu.")
else:
break
else:
break
def check_win() -> str:
over_limit = {}
@@ -209,6 +214,7 @@ def check_win() -> str:
return winner
def game(data: tuple):
while True:
if data[0] == "count":
count = data[1]
th = data[2]
@@ -223,7 +229,6 @@ def game(data: tuple):
score = 0
input("Oh no... anyway.")
data = evaluate("k", count, th)
game(data)
else:
if count == 0:
count = 6
@@ -234,7 +239,6 @@ def game(data: tuple):
)
inp = input(msg)
data = evaluate(inp, count, th)
game(data)
elif data[0] == "msg":
if data[1] == "ER01" or data[1] == "ER02" or data[1] == "ER03":
@@ -244,10 +248,10 @@ def game(data: tuple):
clear()
inp = input(msg)
data = evaluate(inp, count, th)
game(data)
else:
msg = data[1]
print(msg)
break
elif data[0] == "win":
winner = data[1]
@@ -256,6 +260,7 @@ def game(data: tuple):
clear()
print(f"Vítězem se stává {winner} s {score} body!")
print("Díky za hru a zase příště.")
break
def main():
clear()