Merge pull request 'feature: bodování hodů a interaktivní herní smyčka' (#7) from feature/vyber-kostek into version/v0.1.0
Reviewed-on: david/kostky#7
This commit was merged in pull request #7.
This commit is contained in:
+30
@@ -0,0 +1,30 @@
|
||||
111111,8000
|
||||
666666,4800
|
||||
11111,4000
|
||||
555555,4000
|
||||
444444,3200
|
||||
66666,2400
|
||||
333333,2400
|
||||
1111,2000
|
||||
55555,2000
|
||||
44444,1600
|
||||
222222,1600
|
||||
123456,1500
|
||||
6666,1200
|
||||
33333,1200
|
||||
111,1000
|
||||
5555,1000
|
||||
23456,1000
|
||||
4444,800
|
||||
22222,800
|
||||
12345,750
|
||||
666,600
|
||||
3333,600
|
||||
555,500
|
||||
444,400
|
||||
2222,400
|
||||
333,300
|
||||
11,200
|
||||
222,200
|
||||
1,100
|
||||
5,50
|
||||
|
@@ -1,20 +1,40 @@
|
||||
from typing import Any
|
||||
from random import randint
|
||||
from os import system, name
|
||||
import re
|
||||
import csv
|
||||
|
||||
def throw(count):
|
||||
score = 0
|
||||
hodnoty = {}
|
||||
|
||||
with open("hodnoty.csv", "r") as csv_file:
|
||||
csv_reader = csv.reader(csv_file)
|
||||
|
||||
for row in csv_reader:
|
||||
key = str(row[0])
|
||||
value = int(row[1])
|
||||
|
||||
hodnoty[key] = value
|
||||
|
||||
def throw(count: int) -> str:
|
||||
count = int(count)
|
||||
new_throw = []
|
||||
for dice in range(count):
|
||||
new_throw.append(str(randint(1, 6)))
|
||||
new_throw = "".join(new_throw)
|
||||
return new_throw
|
||||
|
||||
def show_throw(throw):
|
||||
pretty_list = ", ".join(throw)
|
||||
def show_throw(throw: str) -> str:
|
||||
throw_temp = []
|
||||
for x in throw:
|
||||
throw_temp.append(x)
|
||||
|
||||
pretty_list = ", ".join(throw_temp)
|
||||
pretty_list += "\n"
|
||||
for i in range(1, len(throw) + 1):
|
||||
for i in range(1, len(throw_temp) + 1):
|
||||
pretty_list += "^ "
|
||||
pretty_list += "\n"
|
||||
for i in range(1, len(throw) + 1):
|
||||
for i in range(1, len(throw_temp) + 1):
|
||||
pretty_list += f"{i} "
|
||||
return pretty_list
|
||||
|
||||
@@ -24,17 +44,100 @@ def clear():
|
||||
else:
|
||||
_ = system('clear')
|
||||
|
||||
def game():
|
||||
new_throw = throw(6)
|
||||
clear()
|
||||
print(show_throw(new_throw))
|
||||
input("Prosím, stiskněte enter pro další hod:")
|
||||
game()
|
||||
def evaluate(inp: Any, count: int, throw: str) -> tuple:
|
||||
def eval_score(pick: str) -> bool:
|
||||
if(pick in hodnoty):
|
||||
global score
|
||||
score += hodnoty[pick]
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
inp = str(inp)
|
||||
if re.search("^\d+$", inp):
|
||||
if len(inp) > count:
|
||||
data = ("msg", "ER01", "Tolika kostkami nemůžete hodit. Zkuste to znovu:", count, throw)
|
||||
return data
|
||||
else:
|
||||
for x in inp:
|
||||
x = int(x)
|
||||
if x > count:
|
||||
if count == 1:
|
||||
ran = "1"
|
||||
else:
|
||||
ran = f"1–{count}"
|
||||
data = ("msg", "ER02", f"Zvolte prosím kostky v platném rozsahu ({ran}):", count, throw)
|
||||
return data
|
||||
old_throw = []
|
||||
pick = []
|
||||
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 = "".join(old_throw)
|
||||
pick.sort()
|
||||
pick = "".join(pick)
|
||||
|
||||
new_count = len(old_throw)
|
||||
|
||||
if eval_score(pick) == False:
|
||||
new_count = count
|
||||
old_throw = throw
|
||||
|
||||
data = ("count", new_count, old_throw)
|
||||
return data
|
||||
|
||||
def check_value(throw: str) -> bool:
|
||||
throw_s = sorted(throw)
|
||||
throw_str = "".join(throw_s)
|
||||
hodnota = False
|
||||
for h in hodnoty:
|
||||
if h in throw_str:
|
||||
hodnota = True
|
||||
break
|
||||
|
||||
return hodnota
|
||||
|
||||
def game(data: tuple):
|
||||
if data[0] == "count":
|
||||
count = data[1]
|
||||
th = data[2]
|
||||
if(th == ""):
|
||||
th = throw(count)
|
||||
clear()
|
||||
print(show_throw(th))
|
||||
if check_value(th) or (not check_value(th) and th != ""):
|
||||
msg = ("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"
|
||||
" - Hodit znovu: [h].\n"
|
||||
f" - Ukončit hod a ponechat si skóre z tohoto hodu ({score}): [k].\n"
|
||||
)
|
||||
# if old_throw != "":
|
||||
# msg += f"\nHodnoty odložených kostek: {old_throw}"
|
||||
inp = input(msg)
|
||||
data = evaluate(inp, count, th)
|
||||
game(data)
|
||||
else:
|
||||
print("Smůla, třeba příště.")
|
||||
elif data[0] == "msg":
|
||||
if data[1] == "ER01" or data[1] == "ER02":
|
||||
msg = data[2]
|
||||
count = data[3]
|
||||
th = data[4]
|
||||
inp = input(msg)
|
||||
data = evaluate(inp, count, th)
|
||||
game(data)
|
||||
else:
|
||||
msg = data[1]
|
||||
print(msg)
|
||||
|
||||
def main():
|
||||
print("Vítejte ve hře v kostky!")
|
||||
input("Prosím, stiskněte enter pro první hod:")
|
||||
game()
|
||||
data = ("count", 6, "")
|
||||
game(data)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user