refactor: vytažení funkcí z evaluate

Funkce eval_score vytažena z evaluate; translate_letters_to_numbers,
pick_throw_proc a eval_wrapper nově vytvořeny z fragmentů evaluate.
Oproti dřívějšímu pokusu odstraněny i přesunuté (redundantní) řádky.
This commit is contained in:
david-spacil committed 2026-07-12 20:23:26 +02:00
1 parent caa743fcc9
commit e9cf875192
1 file changed
+102 -76
+102 -76
View File
@@ -53,36 +53,111 @@ def show_throw(throw: str) -> str:
def clear() -> None:
_ = system('cls') if name == 'nt' else system('clear')
def translate_letters_to_numbers(inp_hod: str, count: int) -> tuple[bool, str]:
inp_temp: str = ""
for x in inp_hod:
x_int: int = ord(x)-96
if x_int > count:
ran = "1" if count == 1 else f"1–{count}"
return False, f"Zvolte prosím kostky v platném rozsahu ({ran}): "
else:
inp_temp += str(x_int)
return True, inp_temp
def pick_throw_proc(count: int, inp_hod: str, throw: str) -> tuple[str, str]:
old_throw: list[str] = []
pick: list[str] = []
for y in range(count):
if str(y+1) not in inp_hod:
old_throw.append(throw[y])
if str(y+1) in inp_hod:
pick.append(throw[y])
old_throw_str: str = "".join(old_throw)
pick.sort()
pick_str: str = "".join(pick)
return old_throw_str, pick_str
def eval_score(pick: str) -> int:
score = 0
pick_counter = Counter(pick)
while pick_counter:
bool_temp = False
for k in hodnoty:
k_counter = Counter(k)
if k_counter <= pick_counter:
pick_counter = pick_counter - k_counter
score += hodnoty[k]
bool_temp = True
if not bool_temp:
break
if pick_counter:
score = 0
return score
def eval_wrapper(count: int, throw: str, inp_hod: str) -> tuple[bool, int, int, str]:
if not inp_hod:
data = (
True,
0,
count,
"",
)
elif re.search(r"^[a-f]+$", inp_hod):
if len(inp_hod) > count:
data = (
False,
0,
count,
"Tolika kostkami nemůžete hodit. Zkuste to znovu: ",
)
else:
tltn = translate_letters_to_numbers(inp_hod, count)
if tltn[0]:
inp_hod = tltn[1]
old_throw_str, pick_str = pick_throw_proc(count, inp_hod, throw)
new_count: int = len(old_throw_str)
score = eval_score(pick_str)
if not score:
new_count = count
old_throw_str = throw
data = (
True,
score,
new_count,
"",
)
else:
data = (
False,
0,
count,
tltn[1]
)
else:
data = (
False,
0,
count,
"Zadejte prosím platnou hodnotu: ",
)
return data
def evaluate(inp: Any, count: int, throw: str) -> tuple[Any, ...]:
global h_bool
def eval_score(pick: str) -> bool:
global score
score_temp = 0
pick_ok = False
pick_counter = Counter(pick)
while pick_counter:
bool_temp = False
for k in hodnoty:
k_counter = Counter(k)
if k_counter <= pick_counter:
pick_counter = pick_counter - k_counter
score_temp += hodnoty[k]
pick_ok = True
bool_temp = True
if not bool_temp:
break
print(pick_counter)
if pick_counter:
pick_ok = False
else:
score += score_temp
return pick_ok
def next_player() -> None:
global pointer
@@ -95,57 +170,8 @@ def evaluate(inp: Any, count: int, throw: str) -> tuple[Any, ...]:
pointer = nxt_key
inp = str(inp)
inp_temp: str = ""
if re.search(r"^[a-f]+$", inp):
if len(inp) > count:
data: tuple[Any, ...] = (
"msg",
"ER01",
"Tolika kostkami nemůžete hodit. Zkuste to znovu: ",
count,
throw
)
else:
for x in inp:
x = ord(x)-96
if x > count:
ran = "1" if count == 1 else f"1–{count}"
data = (
"msg",
"ER02",
f"Zvolte prosím kostky v platném rozsahu ({ran}): ",
count,
throw
)
return data
else:
inp_temp += str(x)
inp = inp_temp
old_throw: list[str] = []
pick: list[str] = []
for y in range(count):
if str(y+1) not in inp:
old_throw.append(throw[y])
if str(y+1) in inp:
pick.append(throw[y])
old_throw_str: str = "".join(old_throw)
pick.sort()
pick_str: str = "".join(pick)
new_count: int = len(old_throw_str)
if not eval_score(pick_str):
new_count = count
old_throw_str = throw
else:
h_bool = False
data = ("count", new_count, old_throw_str)
elif inp == "h":
if inp == "h":
if h_bool:
clear()
input("Nemůžete házet znovu, dokud nevyhodnotíte některé kostky.")