2023-02-28 14:19:01 +01:00
|
|
|
from pysmt.shortcuts import Symbol, Bool, Not, Implies, Iff, And, Or, AtMostOne, ExactlyOne, get_model, get_atoms, get_formula_size, get_unsat_core
|
|
|
|
from pysmt.rewritings import conjunctive_partition
|
2023-02-28 17:28:34 +01:00
|
|
|
import json
|
2023-02-28 22:30:27 +01:00
|
|
|
|
2023-03-02 11:03:33 +01:00
|
|
|
MAX_MOVES = 63
|
2023-02-28 18:59:41 +01:00
|
|
|
NUM_STRIKES = 3
|
2023-02-28 22:30:27 +01:00
|
|
|
COLORS = 'rygbp'
|
2023-03-02 11:03:33 +01:00
|
|
|
NUM_CARDS = 50
|
2023-02-28 14:19:01 +01:00
|
|
|
|
|
|
|
deck_str = 'p5 p3 b4 r5 y4 y4 y5 r4 b2 y2 y3 g5 g2 g3 g4 p4 r3 b2 b3 b3 p4 b1 p2 b1 b1 p2 p1 p1 g1 r4 g1 r1 r3 r1 g1 r1 p1 b4 p3 g2 g3 g4 b5 y1 y1 y1 r2 r2 y2 y3'
|
2023-02-28 22:30:27 +01:00
|
|
|
|
|
|
|
# instance independent names of variables
|
2023-02-28 14:19:01 +01:00
|
|
|
|
|
|
|
# clues[m][i] == "after move m we have at least i clues"
|
2023-02-28 19:11:23 +01:00
|
|
|
clues = {-1: {i: Bool(i < 9) for i in range(0, 10)}, **{m: {0: Bool(True), 9: Bool(False), **{i: Symbol('m{}c{}'.format(m, i)) for i in range(1, 9)}} for m in range(MAX_MOVES)}}
|
2023-02-28 15:41:23 +01:00
|
|
|
# strikes[m][i] == "after move m we have at least i strikes"
|
2023-02-28 19:11:23 +01:00
|
|
|
strikes = {-1: {i: Bool(i == 0) for i in range(0,NUM_STRIKES+1)}, **{m: {0: Bool(True), NUM_STRIKES: Bool(False), **{s: Symbol('m{}s{}'.format(m,s)) for s in range(1,NUM_STRIKES)}} for m in range(MAX_MOVES)} }
|
2023-02-28 18:59:41 +01:00
|
|
|
# extraturn[m] = "turn m is a move part of the extra round or a dummy turn"
|
2023-03-02 11:03:33 +01:00
|
|
|
extraround = {-1: Bool(False), **{m: Bool(False) if m <= 29 else Symbol('m{}e'.format(m)) for m in range(0, MAX_MOVES)}}
|
2023-02-28 19:09:13 +01:00
|
|
|
# dummyturn[m] = "turn m is a dummy nurn and not actually part of the game"
|
2023-03-02 11:03:33 +01:00
|
|
|
dummyturn = {-1: Bool(False), **{m: Bool(False) if m <= 34 else Symbol('m{}dt'.format(m)) for m in range(0, MAX_MOVES)}}
|
2023-02-28 15:41:23 +01:00
|
|
|
# strike[m] = "at move m we get a strike"
|
2023-02-28 19:11:23 +01:00
|
|
|
strike = {-1: Bool(False), **{m: Symbol('m{}s+'.format(m)) for m in range(MAX_MOVES)}}
|
2023-02-28 14:19:01 +01:00
|
|
|
# draw[m][i] == "at move m we play/discard deck[i]"
|
2023-03-02 11:03:33 +01:00
|
|
|
discard = {m: {i: Symbol('m{}-{}'.format(m, i)) for i in range(NUM_CARDS)} for m in range(MAX_MOVES)}
|
2023-02-28 14:19:01 +01:00
|
|
|
# progress[m][c, k] == "after move m we have played in color c until k"
|
2023-02-28 22:30:27 +01:00
|
|
|
progress = {-1: {(c, k): Bool(k == 0) for c in COLORS for k in range(6)}, **{m: {**{(c, 0): Bool(True) for c in COLORS}, **{(c, k): Symbol('m{}:{}{}'.format(m, c, k)) for c in COLORS for k in range(1, 6)}} for m in range(MAX_MOVES)}}
|
2023-02-28 14:19:01 +01:00
|
|
|
# discard_any[m] == "at move m we play/discard a card"
|
2023-02-28 19:11:23 +01:00
|
|
|
discard_any = {m: Symbol('m{}d'.format(m)) for m in range(MAX_MOVES)}
|
2023-02-28 14:19:01 +01:00
|
|
|
# draw_any[m] == "at move m we draw a card"
|
2023-02-28 19:11:23 +01:00
|
|
|
draw_any = {m: Symbol('m{}D'.format(m)) for m in range(MAX_MOVES)}
|
2023-02-28 14:19:01 +01:00
|
|
|
# play[m] == "at move m we play a card"
|
2023-02-28 19:11:23 +01:00
|
|
|
play = {m: Symbol('m{}p'.format(m)) for m in range(MAX_MOVES)}
|
2023-02-28 14:19:01 +01:00
|
|
|
# play5[m] == "at move m we play a 5"
|
2023-02-28 19:11:23 +01:00
|
|
|
play5 = {m: Symbol('m{}p5'.format(m)) for m in range(MAX_MOVES)}
|
2023-02-28 14:19:01 +01:00
|
|
|
# incr_clues[m] == "at move m we obtain a clue"
|
2023-02-28 19:11:23 +01:00
|
|
|
incr_clues = {m: Symbol('m{}c+'.format(m)) for m in range(MAX_MOVES)}
|
2023-02-28 14:19:01 +01:00
|
|
|
|
2023-03-02 11:03:33 +01:00
|
|
|
### this is dependent on the number of players
|
|
|
|
# draw[m][i] == "at move m we draw deck[i]"
|
|
|
|
|
|
|
|
hand_size = {2: 5, 3: 5, 4: 4, 5: 4, 6: 3}
|
|
|
|
last_hand_card = {2: 9, 3: 14, 4: 15, 5: 19, 6: 17}
|
|
|
|
max_moves = {2: -1, 3: 63, 4: 63, 5: 56, 6: 62}
|
|
|
|
draw = {p: {-1: {i: Bool(i == last_hand_card[p]) for i in range(last_hand_card[p], NUM_CARDS)}, **{m: {last_hand_card[p]: Bool(False), **{i: Symbol('m{}+{}'.format(m, i)) for i in range(last_hand_card[p] + 1, NUM_CARDS)}} for m in range(max_moves[p])}} for p in range(2,7)}
|
|
|
|
|
2023-02-28 22:30:27 +01:00
|
|
|
|
2023-03-02 11:03:33 +01:00
|
|
|
def solve(deck_str, num_players=5):
|
2023-02-28 22:30:27 +01:00
|
|
|
deck = [(s[0], int(s[1])) for s in deck_str.split(' ')]
|
2023-03-02 11:03:33 +01:00
|
|
|
hand_size = globals()['hand_size'][num_players]
|
|
|
|
last_hand_card = globals()['last_hand_card'][num_players]
|
|
|
|
max_moves = globals()['max_moves'][num_players]
|
|
|
|
draw = globals()['draw'][num_players]
|
2023-02-28 22:30:27 +01:00
|
|
|
|
|
|
|
valid_move = lambda m: And(
|
|
|
|
Implies(dummyturn[m], Not(discard_any[m])),
|
|
|
|
# definition of discard_any
|
2023-03-02 11:03:33 +01:00
|
|
|
Iff(discard_any[m], Or(discard[m][i] for i in range(NUM_CARDS))),
|
2023-02-28 22:30:27 +01:00
|
|
|
# definition of draw_any
|
2023-03-02 11:03:33 +01:00
|
|
|
Iff(draw_any[m], Or(draw[m][i] for i in range(last_hand_card + 1, NUM_CARDS))),
|
2023-02-28 22:30:27 +01:00
|
|
|
# draw implies discard (and converse true before last 5 moves)
|
|
|
|
Implies(draw_any[m], discard_any[m]),
|
|
|
|
Implies(discard_any[m], Or(extraround[m], draw_any[m])),
|
|
|
|
# play requires discard
|
|
|
|
Implies(play[m], discard_any[m]),
|
|
|
|
# definition of play5
|
2023-03-02 11:03:33 +01:00
|
|
|
Iff(play5[m], And(play[m], Or(discard[m][i] for i in range(NUM_CARDS) if deck[i][1] == 5))),
|
2023-02-28 22:30:27 +01:00
|
|
|
# definition of incr_clues
|
|
|
|
Iff(incr_clues[m], And(discard_any[m], Implies(play[m], And(play5[m], Not(clues[m-1][8]))))),
|
|
|
|
#Iff(incr_clues[m], And(discard_any[m], Implies(play[m], play5[m]))),
|
|
|
|
# change of clues
|
|
|
|
*[Iff(clues[m][i], Or(clues[m-1][i+1], And(clues[m-1][i], Or(discard_any[m], dummyturn[m])), And(clues[m-1][i-1], incr_clues[m]))) for i in range(1, 9)],
|
|
|
|
## more than 8 clues not allowed, discarding produces a strike
|
|
|
|
Iff(strike[m], And(discard_any[m], Not(play[m]), clues[m-1][8])),
|
|
|
|
# change of strikes
|
|
|
|
*[Iff(strikes[m][i], Or(strikes[m-1][i], And(strikes[m-1][i-1], strike[m]))) for i in range(1, NUM_STRIKES+1)],
|
|
|
|
# less than 0 clues not allowed
|
|
|
|
Implies(Not(discard_any[m]), Or(clues[m-1][1], dummyturn[m])),
|
|
|
|
# we can only draw card i if the last drawn card was i-1
|
2023-03-02 11:03:33 +01:00
|
|
|
*[Implies(draw[m][i], Or(And(draw[m0][i-1], *[Not(draw_any[m1]) for m1 in range(m0+1, m)]) for m0 in range(max(-1, m-9), m))) for i in range(last_hand_card + 1, NUM_CARDS)],
|
2023-02-28 22:30:27 +01:00
|
|
|
#*[Implies(draw[m][i], Not(draw[m0][i])) for m0 in range(m) for i in range(20, 50)],
|
|
|
|
#*[Implies(draw[m][i], Or(draw[m0][i-1] for m0 in range(max(-1, m-9), m))) for i in range(20, 50)],
|
|
|
|
# we can only draw at most one card (NOTE: redundant, FIXME: avoid quadratic formula)
|
2023-03-02 11:03:33 +01:00
|
|
|
AtMostOne(draw[m][i] for i in range(last_hand_card + 1, NUM_CARDS)),
|
2023-02-28 22:30:27 +01:00
|
|
|
#*[Not(And(draw[m][i], draw[m][j])) for i in range(20, 50) for j in range(20, i)],
|
|
|
|
# we can only discard a card if we drew it earlier...
|
2023-03-02 11:03:33 +01:00
|
|
|
*[Implies(discard[m][i], Or(draw[m0][i] for m0 in range(m-num_players, -1, -num_players))) for i in range(last_hand_card + 1, NUM_CARDS)],
|
2023-02-28 22:30:27 +01:00
|
|
|
# ...or if it was part of the initial hand
|
2023-03-02 11:03:33 +01:00
|
|
|
*[Not(discard[m][i]) for i in range(last_hand_card + 1) if i//hand_size != m % num_players],
|
2023-02-28 22:30:27 +01:00
|
|
|
# we can only discard a card if we did not discard it yet
|
2023-03-02 11:03:33 +01:00
|
|
|
*[Implies(discard[m][i], And(Not(discard[m0][i]) for m0 in range(m-num_players, -1, -num_players))) for i in range(NUM_CARDS)],
|
2023-02-28 22:30:27 +01:00
|
|
|
# we can only discard at most one card (FIXME: avoid quadratic formula)
|
2023-03-02 11:03:33 +01:00
|
|
|
AtMostOne(discard[m][i] for i in range(NUM_CARDS)),
|
|
|
|
#*[Not(And(discard[m][i], discard[m][j])) for i in range(NUM_CARDS) for j in range(i)],
|
2023-02-28 22:30:27 +01:00
|
|
|
# we can only play a card if it matches the progress
|
2023-03-02 11:03:33 +01:00
|
|
|
*[Implies(And(discard[m][i], play[m]), And(Not(progress[m-1][deck[i]]), progress[m-1][deck[i][0], deck[i][1]-1])) for i in range(NUM_CARDS)],
|
2023-02-28 22:30:27 +01:00
|
|
|
# change of progress
|
2023-03-02 11:03:33 +01:00
|
|
|
*[Iff(progress[m][c, k], Or(progress[m-1][c, k], And(play[m], Or(discard[m][i] for i in range(NUM_CARDS) if deck[i] == (c, k))))) for c in COLORS for k in range(1, 6)],
|
2023-02-28 22:30:27 +01:00
|
|
|
# extra round bool
|
2023-03-02 11:03:33 +01:00
|
|
|
Iff(extraround[m], Or(extraround[m-1], draw[m-1][NUM_CARDS-1])),
|
2023-02-28 22:30:27 +01:00
|
|
|
# dummy turn bool
|
2023-03-02 11:03:33 +01:00
|
|
|
*[Iff(dummyturn[m], Or(dummyturn[m-1], draw[m-1 - num_players][NUM_CARDS-1])) for i in range(0,1) if m >= num_players]
|
2023-02-28 22:30:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
win = And(
|
|
|
|
# maximum progress at each color
|
2023-03-02 11:03:33 +01:00
|
|
|
*[progress[max_moves-1][c, 5] for c in COLORS],
|
2023-02-28 22:30:27 +01:00
|
|
|
# played every color/value combination (NOTE: redundant)
|
2023-03-02 11:03:33 +01:00
|
|
|
*[Or(And(discard[m][i], play[m]) for m in range(max_moves) for i in range(50) if deck[i] == (c, k)) for c in COLORS for k in range(1, 6)]
|
2023-02-28 22:30:27 +01:00
|
|
|
)
|
|
|
|
|
2023-03-02 11:03:33 +01:00
|
|
|
constraints = And(*[valid_move(m) for m in range(max_moves)], win)
|
|
|
|
# print('Solving instance with {} variables, {} nodes'.format(len(get_atoms(constraints)), get_formula_size(constraints)))
|
2023-02-28 22:30:27 +01:00
|
|
|
|
|
|
|
model = get_model(constraints)
|
|
|
|
if model:
|
|
|
|
# print_model(model, deck)
|
2023-03-02 11:03:33 +01:00
|
|
|
solution = toJSON(model, deck, num_players)
|
2023-02-28 22:33:06 +01:00
|
|
|
return True, solution
|
2023-02-28 22:30:27 +01:00
|
|
|
else:
|
|
|
|
print('unsatisfiable')
|
2023-02-28 22:33:06 +01:00
|
|
|
return False, None
|
2023-02-28 22:30:27 +01:00
|
|
|
#conj = list(conjunctive_partition(constraints))
|
|
|
|
#print('statements: {}'.format(len(conj)))
|
|
|
|
#ucore = get_unsat_core(conj)
|
|
|
|
#print('unsat core size: {}'.format(len(ucore)))
|
|
|
|
#for f in ucore:
|
|
|
|
# print(f.serialize())
|
|
|
|
|
2023-03-02 11:03:33 +01:00
|
|
|
def print_model(model, deck, num_players):
|
|
|
|
draw = globals()['draw'][num_players]
|
|
|
|
for m in range(max_moves[num_players]):
|
2023-02-28 14:19:01 +01:00
|
|
|
print('=== move {} ==='.format(m))
|
|
|
|
print('clues: ' + ''.join(str(i) for i in range(1, 9) if model.get_py_value(clues[m][i])))
|
2023-02-28 15:41:23 +01:00
|
|
|
print('strikes: ' + ''.join(str(i) for i in range(1, NUM_STRIKES) if model.get_py_value(strikes[m][i])))
|
2023-02-28 14:19:01 +01:00
|
|
|
print('draw: ' + ', '.join('{} [{}{}]'.format(i, deck[i][0], deck[i][1]) for i in range(20, 50) if model.get_py_value(draw[m][i])))
|
|
|
|
print('discard: ' + ', '.join('{} [{}{}]'.format(i, deck[i][0], deck[i][1]) for i in range(50) if model.get_py_value(discard[m][i])))
|
2023-02-28 22:30:27 +01:00
|
|
|
for c in COLORS:
|
2023-02-28 14:19:01 +01:00
|
|
|
print('progress {}: '.format(c) + ''.join(str(k) for k in range(1, 6) if model.get_py_value(progress[m][c, k])))
|
2023-02-28 19:09:13 +01:00
|
|
|
flags = ['discard_any', 'draw_any', 'play', 'play5', 'incr_clues', 'strike', 'extraround', 'dummyturn']
|
2023-02-28 14:19:01 +01:00
|
|
|
print(', '.join(f for f in flags if model.get_py_value(globals()[f][m])))
|
|
|
|
|
2023-03-02 11:03:33 +01:00
|
|
|
def toJSON(model, deck, num_players):
|
2023-02-28 22:30:27 +01:00
|
|
|
deck_json = [{"suitIndex": COLORS.index(s), "rank": r} for (s,r) in deck]
|
2023-03-02 11:03:33 +01:00
|
|
|
players = ["Alice", "Bob", "Cathy", "Donald", "Emily"][:num_players]
|
|
|
|
hands = [deck[hand_size[num_players]*p:hand_size[num_players]*(p+1)] for p in range(0,num_players)]
|
|
|
|
draw = globals()['draw'][num_players]
|
2023-02-28 17:28:34 +01:00
|
|
|
actions = []
|
2023-03-02 11:03:33 +01:00
|
|
|
for m in range(max_moves[num_players]):
|
2023-02-28 19:10:43 +01:00
|
|
|
if model.get_py_value(dummyturn[m]):
|
|
|
|
break
|
2023-02-28 17:28:34 +01:00
|
|
|
if model.get_py_value(discard_any[m]):
|
2023-03-02 11:03:33 +01:00
|
|
|
discarded = next(i for i in range(0,NUM_CARDS) if model.get_py_value(discard[m][i]))
|
|
|
|
icard = hands[m % num_players].index(deck[discarded])
|
|
|
|
for i in range(icard, hand_size[num_players] - 1):
|
|
|
|
hands[m % num_players][i] = hands[m % num_players][i + 1]
|
2023-02-28 17:28:34 +01:00
|
|
|
if model.get_py_value(draw_any[m]):
|
2023-03-02 11:03:33 +01:00
|
|
|
hands[m % num_players][hand_size[num_players] - 1] = next(deck[i] for i in range(last_hand_card[num_players] + 1, NUM_CARDS) if model.get_py_value(draw[m][i]))
|
2023-02-28 17:28:34 +01:00
|
|
|
if model.get_py_value(play[m]) or model.get_py_value(strike[m]):
|
|
|
|
actions.append({
|
|
|
|
"type": 0,
|
|
|
|
"target": discarded
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
actions.append({
|
|
|
|
"type": 1,
|
|
|
|
"target": discarded
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
actions.append({
|
|
|
|
"type": 3,
|
2023-03-02 11:03:33 +01:00
|
|
|
"target": (m + 1) % num_players,
|
|
|
|
"value": hands[(m+1) % num_players][0][1]
|
2023-02-28 17:28:34 +01:00
|
|
|
})
|
|
|
|
actions.append({
|
|
|
|
"type": 4,
|
|
|
|
"value": 1
|
|
|
|
})
|
|
|
|
game = {
|
|
|
|
"deck": deck_json,
|
|
|
|
"players": players,
|
|
|
|
"actions": actions,
|
|
|
|
"first_player": 0,
|
|
|
|
"options": {
|
2023-02-28 18:59:41 +01:00
|
|
|
"variant": "No Variant",
|
2023-02-28 17:28:34 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-28 22:30:27 +01:00
|
|
|
return json.dumps(game)
|
2023-02-28 14:19:01 +01:00
|
|
|
|
2023-03-02 11:03:33 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
solvable, sol = solve(deck_str)
|
|
|
|
if solvable:
|
|
|
|
print(sol)
|