2023-03-18 01:17:32 +01:00
|
|
|
from typing import Optional, List
|
|
|
|
from enum import Enum
|
2023-03-18 14:08:18 +01:00
|
|
|
from termcolor import colored
|
2023-03-18 01:17:32 +01:00
|
|
|
|
|
|
|
import constants
|
|
|
|
|
|
|
|
|
2023-05-13 17:27:34 +02:00
|
|
|
class DeckCard:
|
2023-03-18 01:17:32 +01:00
|
|
|
def __init__(self, suitIndex: int, rank: int, deck_index=None):
|
|
|
|
self.suitIndex: int = suitIndex
|
|
|
|
self.rank: int = rank
|
|
|
|
self.deck_index: Optional[int] = deck_index
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_json(deck_card):
|
|
|
|
return DeckCard(**deck_card)
|
|
|
|
|
|
|
|
def colorize(self):
|
|
|
|
color = ["green", "blue", "magenta", "yellow", "white", "cyan"][self.suitIndex]
|
|
|
|
return colored(str(self), color)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.suitIndex == other.suitIndex and self.rank == other.rank
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return constants.COLOR_INITIALS[self.suitIndex] + str(self.rank)
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
# should be injective enough, we never use cards with ranks differing by 1000
|
|
|
|
return 1000 * self.suitIndex + self.rank
|
|
|
|
|
2023-05-13 17:27:34 +02:00
|
|
|
|
2023-03-18 15:18:04 +01:00
|
|
|
def pp_deck(deck: List[DeckCard]) -> str:
|
|
|
|
return "[" + ", ".join(card.colorize() for card in deck) + "]"
|
|
|
|
|
2023-03-18 01:17:32 +01:00
|
|
|
|
|
|
|
class ActionType(Enum):
|
|
|
|
Play = 0
|
|
|
|
Discard = 1
|
|
|
|
ColorClue = 2
|
|
|
|
RankClue = 3
|
|
|
|
EndGame = 4
|
2023-05-13 17:27:34 +02:00
|
|
|
VoteTerminate = 5 ## hack: online, this is encoded as a 10
|
2023-03-18 01:17:32 +01:00
|
|
|
|
|
|
|
|
2023-05-13 17:27:34 +02:00
|
|
|
class Action:
|
2023-03-18 01:17:32 +01:00
|
|
|
def __init__(self, type_: ActionType, target: int, value: Optional[int] = None):
|
|
|
|
self.type = type_
|
|
|
|
self.target = target
|
|
|
|
self.value = value
|
2023-05-06 19:02:38 +02:00
|
|
|
# enforce no values on play / discard
|
|
|
|
if self.type in [ActionType.Discard, ActionType.Play]:
|
|
|
|
self.value = None
|
2023-03-18 01:17:32 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_json(action):
|
|
|
|
return Action(
|
2023-05-13 17:27:34 +02:00
|
|
|
ActionType(action['type']),
|
|
|
|
int(action['target']),
|
|
|
|
action.get('value', None)
|
|
|
|
)
|
2023-03-18 01:17:32 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
match self.type:
|
|
|
|
case ActionType.Play:
|
|
|
|
return "Play card {}".format(self.target)
|
|
|
|
case ActionType.Discard:
|
|
|
|
return "Discard card {}".format(self.target)
|
|
|
|
case ActionType.ColorClue:
|
|
|
|
return "Clue color {} to player {}".format(self.value, self.target)
|
|
|
|
case ActionType.RankClue:
|
|
|
|
return "Clue rank {} to player {}".format(self.value, self.target)
|
|
|
|
case ActionType.EndGame:
|
|
|
|
return "Player {} ends the game (code {})".format(self.target, self.value)
|
|
|
|
case ActionType.VoteTerminate:
|
|
|
|
return "Players vote to terminate the game (code {})".format(self.value)
|
|
|
|
return "Undefined action"
|
|
|
|
|
2023-05-06 19:02:38 +02:00
|
|
|
def __eq__(self, other):
|
|
|
|
return self.type == other.type and self.target == other.target and self.value == other.value
|
|
|
|
|
2023-03-18 01:17:32 +01:00
|
|
|
|
2023-05-14 16:44:23 +02:00
|
|
|
class HanabiInstance:
|
2023-03-18 01:17:32 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
2023-05-13 17:27:34 +02:00
|
|
|
deck: List[DeckCard],
|
|
|
|
# assumes a default deck, every suit has to be distributed either [1,1,1,2,2,3,3,4,4,5] or [1,2,3,4,5]
|
|
|
|
num_players: int, # number of players that play this deck, in range [2,6]
|
2023-05-11 15:15:15 +02:00
|
|
|
|
2023-05-13 17:27:34 +02:00
|
|
|
hand_size: Optional[int] = None, # number of cards that each player holds
|
2023-03-18 01:17:32 +01:00
|
|
|
num_strikes: Optional[int] = None, # number of strikes that leads to game loss
|
2023-05-13 17:27:34 +02:00
|
|
|
clue_starved: bool = False, # if true, discarding and playing fives only gives back half a clue
|
|
|
|
fives_give_clue: bool = True, # if false, then playing a five will not change the clue count
|
2023-05-14 16:44:23 +02:00
|
|
|
deck_plays: bool = False,
|
|
|
|
all_or_nothing: bool = False
|
2023-05-13 17:27:34 +02:00
|
|
|
):
|
2023-03-18 01:17:32 +01:00
|
|
|
# defining properties
|
|
|
|
self.deck = deck
|
|
|
|
self.num_players = num_players
|
|
|
|
self.hand_size = hand_size or constants.HAND_SIZES[self.num_players]
|
|
|
|
self.num_strikes = num_strikes or constants.NUM_STRIKES
|
2023-05-10 17:30:57 +02:00
|
|
|
self.clue_starved = clue_starved
|
2023-05-11 15:15:15 +02:00
|
|
|
self.fives_give_clue = fives_give_clue
|
2023-05-14 16:44:23 +02:00
|
|
|
self.deck_plays = deck_plays,
|
|
|
|
self.all_or_nothing = all_or_nothing
|
2023-06-24 17:21:55 +02:00
|
|
|
assert not self.all_or_nothing, "All or nothing not implemented"
|
2023-03-18 01:17:32 +01:00
|
|
|
|
|
|
|
# normalize deck indices
|
|
|
|
for (idx, card) in enumerate(self.deck):
|
|
|
|
card.deck_index = idx
|
|
|
|
|
|
|
|
# deducable properties, to be calculated once
|
|
|
|
self.num_suits = max(map(lambda c: c.suitIndex, deck)) + 1
|
|
|
|
self.num_dark_suits = (len(deck) - 10 * self.num_suits) // (-5)
|
|
|
|
self.player_names = constants.PLAYER_NAMES[:self.num_players]
|
|
|
|
self.deck_size = len(self.deck)
|
|
|
|
|
2023-05-25 16:59:49 +02:00
|
|
|
self.initial_pace = self.deck_size - 5 * self.num_suits - self.num_players * (self.hand_size - 1)
|
|
|
|
|
2023-05-13 17:27:34 +02:00
|
|
|
# # maximum number of moves in any game that can achieve max score each suit gives 15 moves, as we can play
|
|
|
|
# and discard 5 cards each and give 5 clues. dark suits only give 5 moves, since no discards are added number
|
|
|
|
# of cards that remain in players hands after end of game. they cost 2 turns each, since we cannot discard
|
|
|
|
# them and also have one clue less 8 clues at beginning, one further clue for each suit but one (the clue of
|
|
|
|
# the last 5 is never useful since it is gained in the extra-round) subtract a further move for a second
|
|
|
|
# 5-clue that can't be used in 5 or 6-player games, since the extraround starts too soon
|
|
|
|
self.max_winning_moves = 15 * self.num_suits - 10 * self.num_dark_suits \
|
|
|
|
- 2 * self.num_players * (self.hand_size - 1) \
|
|
|
|
+ 8 + (self.num_suits - 1) \
|
|
|
|
+ (-1 if self.num_players >= 5 else 0)
|
2023-03-18 01:17:32 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def num_dealt_cards(self):
|
|
|
|
return self.num_players * self.hand_size
|
|
|
|
|
|
|
|
@property
|
|
|
|
def draw_pile_size(self):
|
|
|
|
return self.deck_size - self.num_dealt_cards
|
|
|
|
|
2023-05-06 20:13:36 +02:00
|
|
|
@property
|
|
|
|
def max_score(self):
|
|
|
|
return 5 * self.num_suits
|
|
|
|
|
2023-05-10 17:30:57 +02:00
|
|
|
@property
|
|
|
|
def clue_increment(self):
|
|
|
|
return 0.5 if self.clue_starved else 1
|
|
|
|
|
2023-03-18 01:17:32 +01:00
|
|
|
|
2023-05-14 16:44:23 +02:00
|
|
|
class GameState:
|
2023-06-24 17:21:55 +02:00
|
|
|
def __init__(self, instance: HanabiInstance, starting_player: int = 0):
|
2023-03-18 01:17:32 +01:00
|
|
|
# will not be modified
|
|
|
|
self.instance = instance
|
|
|
|
|
|
|
|
# dynamic game state
|
2023-05-13 17:27:34 +02:00
|
|
|
self.progress = self.instance.num_players * self.instance.hand_size # index of next card to be drawn
|
|
|
|
self.hands = [self.instance.deck[self.instance.hand_size * p: self.instance.hand_size * (p + 1)] for p in
|
|
|
|
range(0, self.instance.num_players)]
|
2023-03-18 01:17:32 +01:00
|
|
|
self.stacks = [0 for i in range(0, self.instance.num_suits)]
|
|
|
|
self.strikes = 0
|
|
|
|
self.clues = 8
|
2023-06-24 17:21:55 +02:00
|
|
|
self.turn = starting_player
|
2023-05-25 16:59:49 +02:00
|
|
|
self.pace = self.instance.initial_pace
|
2023-03-18 01:17:32 +01:00
|
|
|
self.remaining_extra_turns = self.instance.num_players + 1
|
|
|
|
self.trash = []
|
|
|
|
|
|
|
|
# can be set to true if game is known to be in a lost state
|
|
|
|
self.in_lost_state = False
|
|
|
|
|
2023-05-13 17:27:34 +02:00
|
|
|
# automatically set upon third strike, when extar round is over or when explicitly taking EndGame or
|
|
|
|
# VoteTerminate actions
|
2023-05-06 17:41:33 +02:00
|
|
|
self.over = False
|
|
|
|
|
2023-03-18 01:17:32 +01:00
|
|
|
# will track replay as game progresses
|
|
|
|
self.actions = []
|
|
|
|
|
2023-05-13 17:27:34 +02:00
|
|
|
# Methods to control game state change
|
2023-03-18 01:17:32 +01:00
|
|
|
|
|
|
|
def play(self, card_idx):
|
|
|
|
card = self.instance.deck[card_idx]
|
|
|
|
if card.rank == self.stacks[card.suitIndex] + 1:
|
|
|
|
self.stacks[card.suitIndex] += 1
|
2023-05-13 17:27:34 +02:00
|
|
|
if card.rank == 5 and self.clues != 8 and self.instance.fives_give_clue:
|
2023-05-10 17:30:57 +02:00
|
|
|
self.clues += self.instance.clue_increment
|
2023-03-18 01:17:32 +01:00
|
|
|
else:
|
|
|
|
self.strikes += 1
|
2023-05-06 12:29:52 +02:00
|
|
|
self.trash.append(self.instance.deck[card_idx])
|
2023-05-25 16:59:49 +02:00
|
|
|
self.pace -= 1
|
2023-03-18 01:17:32 +01:00
|
|
|
self.actions.append(Action(ActionType.Play, target=card_idx))
|
2023-05-14 16:44:23 +02:00
|
|
|
self._replace(card_idx, allow_not_present=self.instance.deck_plays and (card_idx == self.deck_size - 1))
|
2023-05-13 17:27:34 +02:00
|
|
|
self._make_turn()
|
2023-05-06 17:41:33 +02:00
|
|
|
if all(s == 5 for s in self.stacks) or self.strikes >= self.instance.num_strikes:
|
|
|
|
self.over = True
|
2023-03-18 01:17:32 +01:00
|
|
|
|
|
|
|
def discard(self, card_idx):
|
2023-05-13 17:27:34 +02:00
|
|
|
assert (self.clues < 8)
|
2023-03-18 01:17:32 +01:00
|
|
|
self.actions.append(Action(ActionType.Discard, target=card_idx))
|
2023-05-10 17:30:57 +02:00
|
|
|
self.clues += self.instance.clue_increment
|
2023-03-18 01:17:32 +01:00
|
|
|
self.pace -= 1
|
2023-03-18 10:17:09 +01:00
|
|
|
self.trash.append(self.instance.deck[card_idx])
|
2023-05-13 17:27:34 +02:00
|
|
|
self._replace(card_idx)
|
|
|
|
self._make_turn()
|
2023-03-18 01:17:32 +01:00
|
|
|
|
|
|
|
def clue(self):
|
2023-05-13 17:27:34 +02:00
|
|
|
assert (self.clues > 0)
|
|
|
|
self.actions.append(self._waste_clue())
|
2023-03-18 01:17:32 +01:00
|
|
|
self.clues -= 1
|
2023-05-13 17:27:34 +02:00
|
|
|
self._make_turn()
|
2023-03-18 10:17:09 +01:00
|
|
|
|
|
|
|
# Forward some properties of the underlying instance
|
|
|
|
@property
|
|
|
|
def num_players(self):
|
|
|
|
return self.instance.num_players
|
|
|
|
|
|
|
|
@property
|
|
|
|
def num_suits(self):
|
|
|
|
return self.instance.num_suits
|
|
|
|
|
|
|
|
@property
|
|
|
|
def num_dark_suits(self):
|
|
|
|
return self.instance.num_dark_suits
|
|
|
|
|
|
|
|
@property
|
|
|
|
def deck(self):
|
|
|
|
return self.instance.deck
|
|
|
|
|
|
|
|
@property
|
|
|
|
def hand_size(self):
|
|
|
|
return self.instance.hand_size
|
2023-03-18 01:17:32 +01:00
|
|
|
|
2023-03-18 13:18:04 +01:00
|
|
|
@property
|
|
|
|
def deck_size(self):
|
|
|
|
return self.instance.deck_size
|
|
|
|
|
2023-03-18 01:17:32 +01:00
|
|
|
# Properties of GameState
|
2023-05-13 17:27:34 +02:00
|
|
|
|
2023-03-18 01:17:32 +01:00
|
|
|
def is_over(self):
|
2023-05-06 17:41:33 +02:00
|
|
|
return self.over or self.is_known_lost()
|
2023-03-18 01:17:32 +01:00
|
|
|
|
|
|
|
def is_won(self):
|
2023-05-25 16:59:49 +02:00
|
|
|
return self.score == self.instance.max_score
|
2023-03-18 01:17:32 +01:00
|
|
|
|
|
|
|
def is_known_lost(self):
|
|
|
|
return self.in_lost_state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def score(self):
|
2023-05-06 17:41:33 +02:00
|
|
|
if self.strikes >= self.instance.num_strikes:
|
|
|
|
return 0
|
2023-03-18 01:17:32 +01:00
|
|
|
return sum(self.stacks)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def cur_hand(self):
|
|
|
|
return self.hands[self.turn]
|
|
|
|
|
|
|
|
# Utilities
|
|
|
|
|
|
|
|
def holding_players(self, card):
|
|
|
|
for (player, hand) in enumerate(self.hands):
|
|
|
|
if card in hand:
|
|
|
|
yield player
|
|
|
|
|
|
|
|
def to_json(self):
|
|
|
|
# ensure we have at least one action
|
|
|
|
if len(self.actions) == 0:
|
|
|
|
self.actions.append(Action(
|
|
|
|
ActionType.EndGame,
|
|
|
|
target=0
|
2023-05-13 17:27:34 +02:00
|
|
|
)
|
2023-03-18 01:17:32 +01:00
|
|
|
)
|
|
|
|
return {
|
2023-03-18 10:17:09 +01:00
|
|
|
"deck": self.instance.deck,
|
|
|
|
"players": self.instance.player_names,
|
2023-03-18 01:17:32 +01:00
|
|
|
"actions": self.actions,
|
|
|
|
"first_player": 0,
|
|
|
|
"options": {
|
|
|
|
"variant": "No Variant",
|
|
|
|
}
|
2023-05-13 17:27:34 +02:00
|
|
|
}
|
|
|
|
|
2023-05-25 16:59:49 +02:00
|
|
|
# Query helpers for implementing bots
|
|
|
|
def copy_holders(self, card: DeckCard, exclude_player: Optional[int]):
|
|
|
|
return [
|
|
|
|
player for player in range(self.num_players)
|
|
|
|
if player != exclude_player and card in self.hands[player]
|
|
|
|
]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def in_strict_order(player_a, player_b, player_c):
|
|
|
|
"""
|
|
|
|
Check whether the three given players sit in order, where equality is not allowed
|
|
|
|
:param player_a:
|
|
|
|
:param player_b:
|
|
|
|
:param player_c:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
return player_a < player_b < player_c or player_b < player_c < player_a or player_c < player_a < player_b
|
|
|
|
|
|
|
|
def is_in_extra_round(self):
|
|
|
|
return self.remaining_extra_turns <= self.instance.num_players
|
|
|
|
|
2023-03-18 01:17:32 +01:00
|
|
|
# Private helpers
|
2023-05-13 17:27:34 +02:00
|
|
|
|
2023-03-18 01:17:32 +01:00
|
|
|
# increments turn counter and tracks extra round
|
2023-05-13 17:27:34 +02:00
|
|
|
def _make_turn(self):
|
|
|
|
assert (not self.over)
|
2023-03-18 01:17:32 +01:00
|
|
|
self.turn = (self.turn + 1) % self.instance.num_players
|
|
|
|
if self.progress == self.instance.deck_size:
|
|
|
|
self.remaining_extra_turns -= 1
|
2023-05-06 17:41:33 +02:00
|
|
|
if self.remaining_extra_turns == 0:
|
|
|
|
self.over = True
|
2023-03-18 01:17:32 +01:00
|
|
|
|
|
|
|
# replaces the specified card (has to be in current player's hand) with the next card of the deck (if nonempty)
|
2023-05-14 16:44:23 +02:00
|
|
|
def _replace(self, card_idx, allow_not_present: bool = False):
|
|
|
|
try:
|
2023-06-24 17:21:55 +02:00
|
|
|
idx_in_hand = next((i for (i, card) in enumerate(self.cur_hand) if card.deck_index == card_idx))
|
2023-05-14 16:44:23 +02:00
|
|
|
except StopIteration:
|
|
|
|
if not allow_not_present:
|
|
|
|
raise
|
|
|
|
self.progress += 1
|
|
|
|
return
|
2023-03-18 01:17:32 +01:00
|
|
|
|
|
|
|
for i in range(idx_in_hand, self.instance.hand_size - 1):
|
|
|
|
self.cur_hand[i] = self.cur_hand[i + 1]
|
|
|
|
if self.progress < self.instance.deck_size:
|
|
|
|
self.cur_hand[self.instance.hand_size - 1] = self.instance.deck[self.progress]
|
|
|
|
self.progress += 1
|
|
|
|
|
2023-05-13 17:27:34 +02:00
|
|
|
# in HanabLiveInstances, this will be overridden with something that checks defaults
|
|
|
|
def _waste_clue(self) -> Action:
|
|
|
|
return Action(
|
|
|
|
ActionType.RankClue,
|
|
|
|
target=(self.turn + 1) % self.instance.num_players, # clue next plyaer
|
|
|
|
value=self.hands[(self.turn + 1) % self.instance.num_players][0].rank # clue index 0
|
|
|
|
)
|