ono_sendai - wip

This commit is contained in:
Francesco Mecca 2019-08-17 17:40:16 +02:00
parent 29c68e3631
commit ca982b07a7
7 changed files with 301 additions and 5 deletions

View file

@ -287,9 +287,9 @@ let () =
assert (alg table1x void_printer |> snd = 2);
assert (alg ~maxiter:21 table1x void_printer |> snd = 3);
assert (alg ~maxiter:14 table1x void_printer |> snd = 2);
(* let table = table1x in
* let res = alg ~maxiter:14 table printer in
* Printf.printf "Best result: %d\n%a\n" (res |> snd) print_table (res |> fst) ; *)
let table = table1x in
let res = alg ~maxiter:14 table printer in
Printf.printf "Best result: %d\n%a\n" (res |> snd) print_table (res |> fst) ;
Printf.printf "Tests done.\n" in
() ;;

View file

@ -1,5 +1,6 @@
from copy import deepcopy as copy # FUK
from cardtypes import *
# from cardtypes import *
from metro_holografix.cardtypes import *
DO_TESTS = False
DO_PRINT = True
@ -20,6 +21,7 @@ def print_1(tavolo, n):
def gioca(tavolo, giocata, da_muovere):
assert type(da_muovere) is Card
assert type(giocata) is TaggedCards
idx = tavolo.cards.index(giocata)
news = [TaggedCards(giocata.cards + [da_muovere])]
rimpiazzata = False

133
ono_sendai/prova.py Normal file
View file

@ -0,0 +1,133 @@
from picotui.context import Context
from picotui.screen import Screen
from time import sleep
from widgets import *
class FrameFactory:
titles = ['0x10', '0x11', '0x12', '0x13', '0x14', '0x15', '0x16', '0x17',
'0x18', '0x19', '0x1A', '0x1B', '0x1D', '0x1E', '0x1F', '0x20',
'0x21', '0x22', '0x23', '0x24', '0x25', '0x26', '0x27',
'0x28', '0x29', '0x2A', '0x2B', '0x2D', '0x2E', '0x2F', '0x30'
'0x31', '0x32', '0x33', '0x34', '0x35', '0x36', '0x37', '0x00'] # more than enough
def __init__(self, d):
self.np = 13
self.d = d
self.widgets = []
self.x = 1 + self.np
self.y = 1
self.titlen = 0
self.hand = None
self.maxwidth = 0
def newFrame(self, cards):
# can handle 9 frames per row
assert type(cards) is list, type(cards)
h = 2 + len(cards) # height ?
self.maxwidth = h if h > self.maxwidth else self.maxwidth
title = self.titles[self.titlen]
w = WCardRadioButton(cards)
self.d.add(self.x, self.y, WColoredFrame(12, h, title))
self.d.add(self.x+1, self.y+1, w)
self.widgets.append(w)
self.advance()
def advance(self):
self.x += self.np
self.titlen+=1
if self.x > self.np * 9:
# second row
self.y += self.maxwidth
self.x = 1 + self.np # first column for hand
def emptyFrame(self):
title = self.titles[-1]
w = WCardRadioButton(['', ''])
self.d.add(self.x, self.y, WColoredFrame(12, 4, title))
self.d.add(self.x+1, self.y+1, w)
self.widgets.append(w)
self.advance()
def newHandFrame(self, cards):
assert type(cards) is list, type(cards)
h = 27 # height ?
self.d.add(1, 1, WColoredFrame(12, h, 'HAND', red))
w = WCardRadioButton([f'{Fore.RED}'+cards[0]] + cards[1:-1] + [cards[-1]+f'{Style.RESET_ALL}'], isHand=True)
self.d.add(2, 2, w)
self.hand = w
def getChoices(self):
src = -1; dst = -1
if self.widgets[0].choice == 0:
dst = 'Empty'
for i, w in enumerate(self.widgets[1:]):
if w.choice == 0:
assert dst != 'Empty'
dst = i
elif w.choice > 1:
src = i, w.choice-2
return src, dst
cc = ['asd', 'asd']
c = ['asd', 'asd']
import state
table = state.table
exit = False
while not exit:
with Context():
Screen.attr_color(C_WHITE, C_GREEN)
Screen.cls()
Screen.attr_reset()
tableDialog = Dialog(1, 1, 120, 30)
f = FrameFactory(tableDialog)
#### BUTTONS ####
buttonOk = WColoredButton(7, "SND", C_RED)
tableDialog.add(108, 28, buttonOk)
buttonOk.finish_dialog = ACTION_OK
buttonRst = WColoredButton(7, "RST", C_RED)
tableDialog.add(100, 28, buttonRst)
buttonRst.finish_dialog = ACTION_OK
buttonMov = WColoredButton(7, "MOV", C_BLUE)
tableDialog.add(92, 28, buttonMov)
buttonMov.finish_dialog = ACTION_OK
buttonDraw = WColoredButton(7, "DRW", C_RED)
tableDialog.add(84, 28, buttonDraw)
buttonDraw.finish_dialog = ACTION_OK
buttonAbort = WColoredButton(13, f'{Fore.BLACK}'+" ABRT "+f'{Style.RESET_ALL}', C_WHITE)
tableDialog.add(4, 28, buttonAbort)
buttonAbort.finish_dialog = ACTION_OK
def doExit(w):
global exit ; exit = True
buttonAbort.on_click = doExit
#### FRAMES ####
f.emptyFrame()
for cards in table.widget_repr():
f.newFrame(cards)
# f.newHandFrame()
tableDialog.redraw()
res = tableDialog.loop()
if res == 1001 or res == 9: # or res == KEY_END or res == KEY_ESC: # 1001 is exit? # 9 is ctrl-c
exit = True
else:
print(*f.getChoices())
sleep(2)
table = state.update_table(table, *f.getChoices())
print('TODO: massimo due scelte')

63
ono_sendai/state.py Normal file
View file

@ -0,0 +1,63 @@
import sys
sys.path.append('../')
from metro_holografix.cardtypes import *
import symbols as sym
Hand = Tavolo
class Table(Tavolo):
def is_valid(self):
return len(self.getNonValide()) == 0
def widget_repr(self):
for ts in self.cards:
yi = [sym.big['hat']]
seed = ts.cards[0][0].lower()
yi.append(sym.big[seed])
yi.append(sym.big[ts.cards[0][1]])
for card in ts.cards[1:]:
seed = card[0].lower()
yi.append(sym.sym[seed][card[1]])
yield yi
table = Table([
TaggedCards([
Card("Pikes", 2),
Card("Clovers", 2),
Card("Tiles", 2),
Card("Hearts", 2)]),
TaggedCards([
Card("Hearts", 1),
Card("Clovers", 1),
Card("Pikes", 1),
Card("Tiles", 1)]),
])
# TODO: refactor language
def gioca(tavolo, src, dst):
giocata = [] if dst == 'Empty' else tavolo.cards[dst]
da_muovere = tavolo.cards[src[0]].cards[src[1]]
assert type(dst) is int or dst == 'Empty' or dst == 'Hand'
assert type(da_muovere) is Card
assert type(giocata) is TaggedCards or giocata == []
idx = -1 if dst == 'Empty' else tavolo.cards.index(giocata)
news = [TaggedCards([da_muovere])] if giocata == [] else []
rimpiazzata = False
for i, t in enumerate(tavolo.cards):
if i == idx:
p = TaggedCards(giocata.cards + [da_muovere])
news.append(p)
elif not rimpiazzata and da_muovere in t.cards:
t = [c for c in t.cards if c != da_muovere]
if t != []:
news.append(TaggedCards(t))
rimpiazzata = True
else:
news.append(t)
return Table(news)
def update_table(table, src, dst):
return gioca(table, src, dst)

View file

@ -83,4 +83,4 @@ big = {
'hat': ' ___ '
}
n = '\n'
sym = {'clovers': clovers, 'pikes': pikes, 'hearts': hearts, 'tiles': tiles}

98
ono_sendai/widgets.py Normal file
View file

@ -0,0 +1,98 @@
from picotui.widgets import *
from picotui.defs import *
from colorama import Style, Fore
red = f'{Fore.RED}'.encode('utf-8')
green = f'{Fore.GREEN}'.encode('utf-8')
white = f'{Fore.WHITE}'.encode('utf-8')
class WColoredButton(WButton):
color = C_GREEN
def __init__(self, w, text, color):
super().__init__(w, text)
self.color = color
def redraw(self):
self.goto(self.x, self.y)
if self.disabled:
self.attr_color(C_WHITE, C_GRAY)
else:
self.attr_color(C_WHITE, self.color)
self.wr(self.t.center(self.w))
self.attr_reset()
def handle_mouse(self, *args, **kwargs):
r = super().handle_mouse(args, kwargs)
self.on_click(self)
return r
def on_click(self, *args, **kwargs):
pass
class WColoredFrame(WFrame):
color = None
rst = f'{Style.RESET_ALL}'.encode('utf-8')
def __init__(self, w, h, title="", color=white):
title = color.decode('utf-8') + title + self.rst.decode('utf-8')
super().__init__(w, h, title)
self.color = color
def draw_box(self, left, top, width, height):
# Use http://www.utf8-chartable.de/unicode-utf8-table.pl
# for utf-8 pseudographic reference
bottom = top + height - 1
self.goto(left, top)
# "┌"
self.wr(self.color+b"\xe2\x94\x8c"+self.rst)
# "─"
hor = self.color+ b"\xe2\x94\x80" * (width - 2) +self.rst
self.wr(hor)
# "┐"
self.wr(self.color + b"\xe2\x94\x90" + self.rst)
self.goto(left, bottom)
# "└"
self.wr(self.color + b"\xe2\x94\x94" + self.rst)
self.wr(hor)
# "┘"
self.wr(self.color + b"\xe2\x94\x98" + self.rst)
top += 1
while top < bottom:
# "│"
bar = self.color + b"\xe2\x94\x82" + self.rst
self.goto(left, top)
self.wr(bar)
self.goto(left + width - 1, top)
self.wr(bar)
top += 1
class WCardRadioButton(WRadioButton):
isHand = False
def __init__(self, items, isHand=False):
super().__init__(items)
self.choice = 1
self.isHand = isHand
def redraw(self):
i = 0
if self.focus:
self.attr_color(C_B_BLUE, None)
for t in self.items:
self.goto(self.x, self.y + i)
self.wr("(*) " if self.choice == i and i > 1
else "[-] " if self.choice == 0 and i == 0 and not self.isHand
else "[ ] " if i == 0 and not self.isHand
else "( ) " if i > 1 else " ")
self.wr(t)
i += 1
self.attr_reset()
def handle_mouse(self, x, y):
newchoice = y - self.y
self.choice = 1 if self.choice == newchoice else newchoice
self.redraw()
self.signal("changed")