2023-07-04 18:52:59 +02:00
|
|
|
from typing import List
|
2023-05-13 23:09:28 +02:00
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
from hanabi import hanab_game
|
2023-07-04 18:52:59 +02:00
|
|
|
from hanabi import constants
|
2023-07-04 20:06:06 +02:00
|
|
|
from hanabi.live import variants
|
2023-05-13 23:09:28 +02:00
|
|
|
|
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
class HanabLiveInstance(hanab_game.HanabiInstance):
|
2023-05-13 23:09:28 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
2023-07-04 20:06:06 +02:00
|
|
|
deck: List[hanab_game.DeckCard],
|
2023-05-13 23:09:28 +02:00
|
|
|
num_players: int,
|
2023-05-14 16:44:23 +02:00
|
|
|
variant_id: int,
|
|
|
|
one_extra_card: bool = False,
|
|
|
|
one_less_card: bool = False,
|
|
|
|
*args, **kwargs
|
2023-05-13 23:09:28 +02:00
|
|
|
):
|
2023-05-14 16:44:23 +02:00
|
|
|
assert 2 <= num_players <= 6
|
|
|
|
hand_size = constants.HAND_SIZES[num_players]
|
|
|
|
if one_less_card:
|
|
|
|
hand_size -= 1
|
|
|
|
if one_extra_card:
|
|
|
|
hand_size += 1
|
|
|
|
|
|
|
|
super().__init__(deck, num_players, hand_size=hand_size, *args, **kwargs)
|
2023-05-13 23:09:28 +02:00
|
|
|
self.variant_id = variant_id
|
2023-07-04 20:06:06 +02:00
|
|
|
self.variant = variants.Variant.from_db(self.variant_id)
|
2023-05-13 23:09:28 +02:00
|
|
|
|
2023-05-19 12:43:49 +02:00
|
|
|
@staticmethod
|
2023-07-04 20:06:06 +02:00
|
|
|
def select_standard_variant_id(instance: hanab_game.HanabiInstance):
|
2023-05-19 12:43:49 +02:00
|
|
|
err_msg = "Hanabi instance not supported by hanab.live, cannot convert to HanabLiveInstance: "
|
|
|
|
assert 3 <= instance.num_suits <= 6, \
|
|
|
|
err_msg + "Illegal number of suits ({}) found, must be in range [3,6]".format(instance.num_suits)
|
|
|
|
assert 0 <= instance.num_dark_suits <= 2, \
|
|
|
|
err_msg + "Illegal number of dark suits ({}) found, must be in range [0,2]".format(instance.num_dark_suits)
|
|
|
|
assert 4 <= instance.num_suits - instance.num_dark_suits, \
|
|
|
|
err_msg + "Illegal ratio of dark suits to suits, can have at most {} dark suits with {} total suits".format(
|
|
|
|
max(instance.num_suits - 4, 0), instance.num_suits
|
|
|
|
)
|
|
|
|
return constants.VARIANT_IDS_STANDARD_DISTRIBUTIONS[instance.num_suits][instance.num_dark_suits]
|
|
|
|
|
2023-05-13 23:09:28 +02:00
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
class HanabLiveGameState(hanab_game.GameState):
|
2023-07-07 14:32:11 +02:00
|
|
|
def __init__(self, instance: HanabLiveInstance):
|
|
|
|
super().__init__(instance)
|
2023-05-13 23:09:28 +02:00
|
|
|
self.instance: HanabLiveInstance = instance
|
|
|
|
|
|
|
|
def make_action(self, action):
|
|
|
|
match action.type:
|
2023-07-04 20:06:06 +02:00
|
|
|
case hanab_game.ActionType.ColorClue | hanab_game.ActionType.RankClue:
|
2023-05-13 23:09:28 +02:00
|
|
|
assert(self.clues > 0)
|
|
|
|
self.actions.append(action)
|
|
|
|
self.clues -= self.instance.clue_increment
|
|
|
|
self._make_turn()
|
|
|
|
# TODO: could check that the clue specified is in fact legal
|
2023-07-04 20:06:06 +02:00
|
|
|
case hanab_game.ActionType.Play:
|
2023-05-13 23:09:28 +02:00
|
|
|
self.play(action.target)
|
2023-07-04 20:06:06 +02:00
|
|
|
case hanab_game.ActionType.Discard:
|
2023-05-13 23:09:28 +02:00
|
|
|
self.discard(action.target)
|
2023-07-04 20:06:06 +02:00
|
|
|
case hanab_game.ActionType.EndGame | hanab_game.ActionType.VoteTerminate:
|
2023-05-13 23:09:28 +02:00
|
|
|
self.over = True
|
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
def _waste_clue(self) -> hanab_game.Action:
|
2023-05-13 23:09:28 +02:00
|
|
|
for player in range(self.turn + 1, self.turn + self.num_players):
|
|
|
|
for card in self.hands[player % self.num_players]:
|
|
|
|
for rank in self.instance.variant.ranks:
|
|
|
|
if self.instance.variant.rank_touches(card, rank):
|
2023-07-04 20:06:06 +02:00
|
|
|
return hanab_game.Action(
|
|
|
|
hanab_game.ActionType.RankClue,
|
2023-05-13 23:09:28 +02:00
|
|
|
player % self.num_players,
|
|
|
|
rank
|
|
|
|
)
|
|
|
|
for color in range(self.instance.variant.num_colors):
|
|
|
|
if self.instance.variant.color_touches(card, color):
|
2023-07-04 20:06:06 +02:00
|
|
|
return hanab_game.Action(
|
|
|
|
hanab_game.ActionType.ColorClue,
|
2023-05-13 23:09:28 +02:00
|
|
|
player % self.num_players,
|
|
|
|
color
|
|
|
|
)
|
|
|
|
raise RuntimeError("Current game state did not permit any legal clue."
|
|
|
|
"This case is incredibly rare and currently not handled.")
|