Octopus_Carnival/ono_sendai/state.py

302 lines
9.3 KiB
Python
Raw Normal View History

2019-08-17 17:40:16 +02:00
import sys
sys.path.append('../')
2019-08-18 17:19:11 +02:00
import json
2019-08-19 00:12:42 +02:00
from copy import deepcopy as copy
from functools import cmp_to_key
2019-08-20 21:39:38 +02:00
from time import sleep
2019-08-19 00:12:42 +02:00
from IPython import embed as fuck
2019-08-18 17:19:11 +02:00
2019-08-17 17:40:16 +02:00
from metro_holografix.cardtypes import *
import symbols as sym
class Table(Tavolo):
def is_valid(self):
2019-08-19 18:24:38 +02:00
return len(self.cards) == 0 or (len(self.singles()) == 0 and len(self.getNonValide()) == 0)
def flatten(self):
return [c for tc in self.cards for c in tc.cards]
2019-08-17 17:40:16 +02:00
2019-08-20 21:39:38 +02:00
def split_repr(self, ocards):
l = len(ocards)
if l == 13:
return ocards
else:
i = l-1
while ocards[i].value-1 == ocards[i-1].value:
i -= 1
return ocards[i:] + ocards[:i]
2019-08-17 17:40:16 +02:00
def widget_repr(self):
for ts in self.cards:
2019-08-20 16:39:33 +02:00
ocards = list(sorted(ts.cards, key=lambda c: c.value))
2019-08-20 21:39:38 +02:00
if ts.tipo == 'Scala' and ocards[-1].value == 13 and ocards[0].value == 1:
ocards = self.split_repr(ocards)
2019-08-17 17:40:16 +02:00
yi = [sym.big['hat']]
2019-08-20 16:39:33 +02:00
seed = ocards[0][0].lower()
2019-08-17 17:40:16 +02:00
yi.append(sym.big[seed])
2019-08-20 16:39:33 +02:00
yi.append(sym.big[ocards[0][1]])
for card in ocards[1:]:
2019-08-17 17:40:16 +02:00
seed = card[0].lower()
yi.append(sym.sym[seed][card[1]])
yield yi
2019-08-19 18:24:38 +02:00
def equality(self, other):
b = set(other.flatten())
a = set(self.flatten())
gt = a - b
lt = b - a
if gt == lt and lt == set():
return True
else:
return False
2019-08-17 18:21:18 +02:00
class Hand:
def __init__(self, cards):
2019-08-19 00:12:42 +02:00
def sortc(a, b):
return -1 if (a[1],a[0]) < (b[1],b[0]) else 1
self.cards = list(sorted(cards, key=cmp_to_key(sortc)))
2019-08-17 18:21:18 +02:00
2019-08-20 16:39:33 +02:00
def __repr__(self):
return 'Hand<'+ str(self.cards) + '>'
2019-08-17 18:21:18 +02:00
def widget_repr(self):
yi = [sym.big['hat']]
seed = self.cards[0][0].lower()
yi.append(sym.big[seed])
yi.append(sym.big[self.cards[0][1]])
for card in self.cards[1:]:
seed = card[0].lower()
yi.append(sym.sym[seed][card[1]])
return yi
2019-08-19 00:12:42 +02:00
def make_deck():
from random import shuffle # TODO: mersenne
def make_set(seed):
for i in range(1, 14):
yield Card(seed, i)
2019-08-20 16:39:33 +02:00
odeck = [m for seed in ['Pikes', 'Hearts', 'Clovers', 'Tiles'] for m in make_set(seed)] * 2
2019-08-19 00:12:42 +02:00
shuffle(odeck)
2019-08-20 21:39:38 +02:00
assert len(odeck) == 104
2019-08-19 00:12:42 +02:00
return odeck
class WrongMoveException(Exception):
pass
class State:
2019-08-21 18:28:41 +02:00
def __init__(self, human, ids):
assert human in ids
self.humanPlayer = human
2019-08-19 00:12:42 +02:00
self.deck = make_deck()
2019-08-19 18:24:38 +02:00
self.winner = None
self.hasEnded = False
2019-08-19 00:12:42 +02:00
self.players = dict()
self.table = Table([])
2019-08-20 16:39:33 +02:00
self.turn = None
2019-08-19 00:12:42 +02:00
self.ids = ids
self.cur_player = ids[0]
2019-08-20 16:39:33 +02:00
self.nrounds = 0
2019-08-19 00:12:42 +02:00
for i in ids:
cards = [self.deck.pop() for i in range(11)]
self.players[i] = Hand(cards)
def draw(self):
hand = self.players[self.cur_player]
nhand = Hand(hand.cards + [self.deck.pop()])
self.players[self.cur_player] = nhand
2019-08-19 18:24:38 +02:00
assert len(self.players[self.cur_player].cards) == len(hand.cards) + 1
2019-08-20 16:39:33 +02:00
self.turn = None
2019-08-19 00:12:42 +02:00
def next_turn(self):
2019-08-20 16:39:33 +02:00
assert self.turn is None
2019-08-19 00:12:42 +02:00
next_player = self.ids[(self.ids.index(self.cur_player) + 1) % len(self.ids)]
self.cur_player = next_player
2019-08-20 16:39:33 +02:00
self.turn = [(copy(self.table), copy(self.players[self.cur_player]))]
self.nrounds = self.nrounds + 1 if self.cur_player == self.ids[0] else self.nrounds
2019-08-19 00:12:42 +02:00
def done(self):
2019-08-20 16:39:33 +02:00
assert self.turn is not None
2019-08-19 00:12:42 +02:00
original = self.table
table, hand = self.last()
2019-08-20 16:39:33 +02:00
if original.equality(table):
raise WrongMoveException()
elif not table.is_valid() or len(set(original.flatten()) - set(table.flatten())) != 0:
2019-08-21 18:28:41 +02:00
if self.cur_player != self.humanPlayer:
fuck() # debug # TODO: should remove
2019-08-19 00:12:42 +02:00
raise WrongMoveException()
else:
self.table, self.players[self.cur_player] = table, hand
2019-08-20 16:39:33 +02:00
self.turn = None
2019-08-19 18:24:38 +02:00
if len(hand.cards) == 0:
# won
self.hasEnded = True
self.winner = self.cur_player
2019-08-19 00:12:42 +02:00
def last(self):
2019-08-20 16:39:33 +02:00
return self.turn[-1]
2019-08-19 00:12:42 +02:00
def backtrack(self):
2019-08-20 16:39:33 +02:00
if len(self.turn) >= 2:
return self.turn.pop()
2019-08-19 00:12:42 +02:00
else:
2019-08-20 16:39:33 +02:00
return self.turn[0]
2019-08-19 00:12:42 +02:00
def size(self):
2019-08-20 16:39:33 +02:00
return len(self.turn)
2019-08-19 00:12:42 +02:00
2019-08-19 18:24:38 +02:00
def advance(self, table, hand):
2019-08-20 16:39:33 +02:00
self.turn.append((table, hand))
2019-08-19 18:24:38 +02:00
return table, hand
2019-08-21 18:28:41 +02:00
def dump(self):
j = dict()
j['table'] = [tc.cards for tc in self.table.cards]
for pl, hand in self.players.items():
j[pl] = hand.cards
j['nrounds'] = self.nrounds
j['players'] = list(self.players.keys())
with open('save.machiavelli', 'w') as f:
f.write(json.dumps(j))
return j
def load(self):
with open('save.machiavelli', 'r') as f:
j = json.loads(f.read())
self.nrounds = j['nrounds']
tcards = []
for tc in j['table']:
tcards.append(TaggedCards([Card(seed, value) for seed, value in tc]))
self.table = Table(tcards)
self.players = dict()
for pl in j['players']:
self.players[pl] = Hand([Card(seed, value) for seed, value in j[pl]])
2019-08-18 17:19:11 +02:00
def fromJson(j):
hcards = [Card(seed, value) for seed, value in j['hand']]
tcards = []
for tc in j['table']:
tcards.append(TaggedCards([Card(seed, value) for seed, value in tc]))
return Table(tcards), Hand(hcards)
def toJson(table, hand):
j = dict()
j['hand'] = hand.cards
j['table'] = []
for tc in table.cards:
j['table'].append(tc.cards)
return json.dumps(j)
2019-08-21 18:28:41 +02:00
# def gioca(tavolo, hand, src, dst):
# giocata = [] if dst == 'Empty' else tavolo.cards[dst]
# da_muovere = hand.cards[src[1]] if src[0] == 'Hand' else tavolo.cards[src[0]].cards[src[1]]
# hcards = hand.cards[:src[1]] + hand.cards[src[1]+1:] if src[0] == 'Hand' else hand.cards
# assert src[0] != 'Hand' or len(hcards) == len(hand.cards) - 1
# assert type(dst) is int or dst == 'Empty'
# assert type(src[0]) is int or src[0] == 'Hand'
# assert type(da_muovere) is Card
# assert type(giocata) is TaggedCards or giocata == []
# idx = -1 if dst == 'Empty' else tavolo.cards.index(giocata)
# if type(giocata) is list: # we want a new empty cell
# news = [TaggedCards([da_muovere])]
# else:
# news = []
def fromHandToEmpty(table, hand, src):
assert type(src) is int
to_move = hand.cards[src]
hcards = hand.cards[:src] + hand.cards[src+1:]
assert len(hcards) == len(hand.cards) - 1
assert type(to_move) is Card
news = [TaggedCards([to_move])] + table.cards
newTable = Table(news)
assert len(newTable.flatten()) + len(hcards) == len(hand.cards) + len(table.flatten()), fuck()
return newTable, Hand(hcards)
def fromHandToTable(table, hand, src, dst):
assert type(dst) is int
assert type(src) is int
in_play = table.cards[dst]
to_move = hand.cards[src]
hcards = hand.cards[:src] + hand.cards[src+1:]
assert len(hcards) == len(hand.cards) - 1
assert type(to_move) is Card
assert type(in_play) is TaggedCards or in_play == []
idx = table.cards.index(in_play)
news = []
news = table.cards[:dst] + table.cards[dst+1:] + [TaggedCards(table.cards[dst].cards + [to_move])]
newTable = Table(news)
assert len(newTable.flatten()) + len(hcards) == len(hand.cards) + len(table.flatten()), fuck()
return newTable, Hand(hcards)
def removeFromTcards(t, to_move):
# tmp = []
# for j, c in enumerate(t.cards):
# if c == to_move:
# break
# tmp.append(c)
# tmp.extend(t.cards[j+1:])
# return tmp
cards = copy(t.cards)
cards.remove(to_move)
return cards
def fromTableToEmpty(table, hand, src, to_move):
assert type(src) is tuple # TODO: unused src[1], even in other TableToTable
in_play = []
tpos, cpos = src
hcards = hand.cards
assert type(src[0]) is int
assert type(to_move) is Card
news = [TaggedCards([to_move])]
for i, t in enumerate(table.cards):
if tpos == i:
tmp = removeFromTcards(t, to_move)
if tmp != []:
news.append(TaggedCards(tmp))
else:
news.append(t)
newTable = Table(news)
assert len(newTable.flatten()) + len(hcards) == len(hand.cards) + len(table.flatten()), fuck()
return newTable, Hand(hcards)
2019-08-17 18:21:18 +02:00
2019-08-21 18:28:41 +02:00
def fromTableToTable(table, hand, src, dst, to_move):
assert type(src) is tuple
in_play = table.cards[dst]
tpos, cpos = src
# to_move = table.cards[tpos].cards[cpos]
hcards = hand.cards
assert type(to_move) is Card
assert type(in_play) is TaggedCards
2019-08-17 18:21:18 +02:00
2019-08-21 18:28:41 +02:00
news = []
2019-08-17 17:40:16 +02:00
rimpiazzata = False
2019-08-21 18:28:41 +02:00
for i, t in enumerate(table.cards):
if i == dst:
p = TaggedCards(in_play.cards + [to_move])
2019-08-17 17:40:16 +02:00
news.append(p)
2019-08-21 18:28:41 +02:00
elif tpos == i:
tmp = removeFromTcards(t, to_move)
if tmp != []:
news.append(TaggedCards(tmp))
2019-08-17 17:40:16 +02:00
else:
news.append(t)
2019-08-21 18:28:41 +02:00
newTable = Table(news)
assert len(newTable.flatten()) + len(hcards) == len(hand.cards) + len(table.flatten()), fuck()
return newTable, Hand(hcards)