2019-08-09 20:51:39 +02:00
|
|
|
from copy import deepcopy as copy # FUK
|
2019-08-17 17:40:16 +02:00
|
|
|
# from cardtypes import *
|
|
|
|
from metro_holografix.cardtypes import *
|
2019-08-09 20:51:39 +02:00
|
|
|
|
2019-08-14 15:02:52 +02:00
|
|
|
DO_TESTS = False
|
2019-08-13 23:55:50 +02:00
|
|
|
DO_PRINT = True
|
2019-08-09 20:51:39 +02:00
|
|
|
MAX = -1000
|
|
|
|
best = None
|
|
|
|
DONE = False
|
|
|
|
MEM = set()
|
2019-08-08 19:45:46 +02:00
|
|
|
|
|
|
|
def print_1(tavolo, n):
|
2019-08-13 23:55:50 +02:00
|
|
|
if not DO_PRINT:
|
|
|
|
return
|
2019-08-12 12:27:14 +02:00
|
|
|
global MEM, MAX
|
|
|
|
st = ('------------- '+str(n)+':'+str(tavolo.punteggio())+':'+str(MAX)+' -------------'+'='+str(len(MEM)))
|
2019-08-09 20:51:39 +02:00
|
|
|
print(st)
|
2019-08-08 19:45:46 +02:00
|
|
|
for t in tavolo.cards:
|
|
|
|
print(t)
|
2019-08-09 20:51:39 +02:00
|
|
|
print(st)
|
2019-08-08 19:45:46 +02:00
|
|
|
|
|
|
|
def gioca(tavolo, giocata, da_muovere):
|
|
|
|
assert type(da_muovere) is Card
|
2019-08-17 17:40:16 +02:00
|
|
|
assert type(giocata) is TaggedCards
|
2019-08-08 19:45:46 +02:00
|
|
|
idx = tavolo.cards.index(giocata)
|
2019-08-09 20:51:39 +02:00
|
|
|
news = [TaggedCards(giocata.cards + [da_muovere])]
|
|
|
|
rimpiazzata = False
|
|
|
|
for i, t in enumerate(tavolo.cards):
|
|
|
|
if i == idx:
|
|
|
|
continue # skip the one containing giocata
|
|
|
|
if 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 Tavolo(news)
|
|
|
|
|
|
|
|
def non_migliora(punteggio):
|
|
|
|
assert type(punteggio) is list
|
|
|
|
if len(punteggio) <= 7:
|
|
|
|
return False
|
|
|
|
p = punteggio[-7:]
|
|
|
|
ma = max(p)
|
|
|
|
mi = min(p)
|
|
|
|
return abs(ma-mi) < 2
|
|
|
|
|
|
|
|
def tavolo_rispettato(start, end):
|
|
|
|
start_mano = set([c for c in start.cards if c.tipo == 'Singolo'])
|
|
|
|
for e in end.cards:
|
|
|
|
if e.tipo == 'Singolo' and e not in start_mano:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def alg(tavolo, tavolo_iniziale, soluzioni, n, punteggio):
|
2019-08-08 19:45:46 +02:00
|
|
|
# qua si presume di avere gia` tutte le carte della mano stese sul tavolo come gruppo singolo
|
|
|
|
# di carte non valide (alla prima iterazione)
|
2019-08-09 20:51:39 +02:00
|
|
|
global MAX, DONE, best, MEM
|
2019-08-12 12:27:14 +02:00
|
|
|
if DONE or tavolo.__hash__() in MEM:
|
2019-08-08 19:45:46 +02:00
|
|
|
return
|
|
|
|
else:
|
2019-08-09 20:51:39 +02:00
|
|
|
DONE = len(tavolo.getNonValide()) == 0 # GLOBAL EXIT
|
2019-08-12 12:27:14 +02:00
|
|
|
MEM.add(tavolo.__hash__())
|
2019-08-09 20:51:39 +02:00
|
|
|
|
|
|
|
print_1(tavolo, n)
|
|
|
|
punteggio.append(tavolo.punteggio())
|
|
|
|
startL = tavolo.llen()
|
2019-08-12 12:27:14 +02:00
|
|
|
if tavolo_rispettato(tavolo_iniziale, tavolo) and tavolo.punteggio() > MAX:
|
|
|
|
MAX = tavolo.punteggio()
|
|
|
|
best = copy(tavolo)
|
|
|
|
|
|
|
|
if len(tavolo.getNonValide()) == 0 or n > 14 or non_migliora(punteggio):
|
|
|
|
return
|
2019-08-09 20:51:39 +02:00
|
|
|
else:
|
|
|
|
for carte in tavolo.getAll():
|
|
|
|
assert type(carte) is TaggedCards# and carte.tag == 'NonValido'
|
2019-08-08 19:45:46 +02:00
|
|
|
vicini = find_vicini(carte, tavolo) # lista di Card
|
|
|
|
for v in vicini:
|
2019-08-09 20:51:39 +02:00
|
|
|
next_tavolo = gioca(tavolo, carte, v)
|
|
|
|
assert startL == next_tavolo.llen()
|
|
|
|
alg(next_tavolo, tavolo_iniziale, soluzioni, n+1, copy(punteggio))
|
|
|
|
|
2019-08-08 19:45:46 +02:00
|
|
|
def find_vicini(carte, tavolo):
|
2019-08-09 20:51:39 +02:00
|
|
|
def _find_vicini(carte, all):
|
|
|
|
if carte.tipo == 'Singolo':
|
|
|
|
return [a for a in all if is_tris(carte.cards+[a]) or is_straight(carte.cards+[a])]
|
|
|
|
elif carte.tipo == 'Tris':
|
|
|
|
return [a for a in all if is_tris(carte.cards+[a])]
|
|
|
|
elif carte.tipo == 'Scala':
|
|
|
|
return [a for a in all if is_straight(carte.cards+[a])]
|
|
|
|
else:
|
|
|
|
assert False
|
|
|
|
|
2019-08-08 19:45:46 +02:00
|
|
|
assert type(tavolo) is Tavolo and type(carte) is TaggedCards
|
2019-08-12 12:27:14 +02:00
|
|
|
return _find_vicini(carte, flatten(tavolo.getAll()))
|
2019-08-08 19:45:46 +02:00
|
|
|
|
|
|
|
|
2019-08-13 23:55:50 +02:00
|
|
|
if DO_TESTS:
|
2019-08-14 00:30:28 +02:00
|
|
|
import test
|
|
|
|
|
|
|
|
def riduci(mano, tavolo):
|
|
|
|
assert type(mano) is Mano and type(tavolo) is Tavolo
|
|
|
|
tagged_mano = [TaggedCards([c]) for c in mano.cards]
|
|
|
|
tavolo_e_mano = Tavolo(tavolo.cards+tagged_mano)
|
|
|
|
return [c.cards[0] for c in tagged_mano if find_vicini(c, tavolo_e_mano) != []]
|
|
|
|
|
2019-08-08 19:45:46 +02:00
|
|
|
if __name__ == '__main__':
|
2019-08-13 23:55:50 +02:00
|
|
|
tavolo1 = Tavolo([
|
|
|
|
TaggedCards([
|
|
|
|
Card("picche", 2),
|
|
|
|
Card("fiori", 2),
|
|
|
|
Card("cuori", 2)]),
|
|
|
|
TaggedCards([
|
|
|
|
Card("cuori", 1),
|
|
|
|
Card("fiori", 1),
|
|
|
|
Card("picche", 1),
|
|
|
|
Card("quadri", 1)]),
|
|
|
|
# mano
|
|
|
|
TaggedCards([
|
|
|
|
Card("quadri", 13)]),
|
|
|
|
TaggedCards([
|
|
|
|
Card("quadri", 12)]),
|
|
|
|
TaggedCards([
|
|
|
|
Card("cuori", 13)]),
|
|
|
|
TaggedCards([
|
|
|
|
Card("cuori", 12)]),
|
|
|
|
TaggedCards([
|
|
|
|
Card("fiori", 3)]),
|
|
|
|
TaggedCards([
|
|
|
|
Card("picche", 3)]),
|
2019-08-14 15:02:52 +02:00
|
|
|
# TaggedCards([
|
|
|
|
# Card("cuori", 12)])
|
2019-08-13 23:55:50 +02:00
|
|
|
])
|
|
|
|
tavolo2 = Tavolo([
|
|
|
|
TaggedCards([
|
|
|
|
Card("picche", 2),
|
|
|
|
Card("fiori", 2),
|
|
|
|
Card("quadri", 2),
|
|
|
|
Card("cuori", 2)]),
|
|
|
|
TaggedCards([
|
|
|
|
Card("cuori", 1),
|
|
|
|
Card("fiori", 1),
|
|
|
|
Card("picche", 1),
|
|
|
|
Card("quadri", 1)]),
|
|
|
|
# mano
|
|
|
|
TaggedCards([
|
|
|
|
Card("picche", 3)]),
|
|
|
|
])
|
|
|
|
tavolo3 = Tavolo([
|
2019-08-08 19:45:46 +02:00
|
|
|
TaggedCards([
|
2019-08-12 12:27:14 +02:00
|
|
|
Card("fiori", 7),
|
|
|
|
Card("fiori", 8),
|
|
|
|
Card("fiori", 9)]),
|
2019-08-08 19:45:46 +02:00
|
|
|
TaggedCards([
|
2019-08-12 12:27:14 +02:00
|
|
|
Card("picche", 7),
|
|
|
|
Card("picche", 8),
|
|
|
|
Card("picche", 9)]),
|
2019-08-08 19:45:46 +02:00
|
|
|
TaggedCards([
|
2019-08-12 12:27:14 +02:00
|
|
|
Card("cuori", 7),
|
|
|
|
Card("cuori", 8),
|
|
|
|
Card("cuori", 9),
|
|
|
|
Card("cuori", 10)]),
|
|
|
|
# mano
|
2019-08-13 23:55:50 +02:00
|
|
|
TaggedCards([
|
|
|
|
Card("cuori", 11)]),
|
|
|
|
TaggedCards([
|
|
|
|
Card("cuori", 12)]),
|
|
|
|
TaggedCards([
|
|
|
|
Card("quadri", 7)])
|
|
|
|
])
|
|
|
|
tavolo4 = Tavolo([
|
|
|
|
TaggedCards([
|
|
|
|
Card("fiori", 7),
|
|
|
|
Card("fiori", 8),
|
|
|
|
Card("fiori", 9)]),
|
|
|
|
TaggedCards([
|
|
|
|
Card("picche", 7),
|
|
|
|
Card("picche", 8),
|
|
|
|
Card("picche", 9)]),
|
|
|
|
TaggedCards([
|
|
|
|
Card("cuori", 7),
|
|
|
|
Card("cuori", 8),
|
|
|
|
Card("cuori", 9),
|
|
|
|
Card("cuori", 10)]),
|
|
|
|
# mano
|
|
|
|
TaggedCards([
|
|
|
|
Card("cuori", 6)]),
|
|
|
|
TaggedCards([
|
|
|
|
Card("cuori", 8)])
|
|
|
|
])
|
|
|
|
tavolo5 = Tavolo([
|
|
|
|
# mano
|
|
|
|
TaggedCards([
|
|
|
|
Card("cuori", 7)]),
|
2019-08-08 19:45:46 +02:00
|
|
|
TaggedCards([
|
2019-08-12 12:27:14 +02:00
|
|
|
Card("cuori", 6)]),
|
2019-08-08 19:45:46 +02:00
|
|
|
TaggedCards([
|
2019-08-12 12:27:14 +02:00
|
|
|
Card("cuori", 8)])
|
2019-08-08 19:45:46 +02:00
|
|
|
])
|
2019-08-13 23:55:50 +02:00
|
|
|
|
2019-08-14 00:30:28 +02:00
|
|
|
tavolo = tavolo1
|
2019-08-14 15:02:52 +02:00
|
|
|
alg(tavolo, tavolo, [], 0, [])
|
2019-08-14 00:30:28 +02:00
|
|
|
# print('****BEST:')
|
|
|
|
# print_1(best, MAX)
|