make starting_player a property of instances, not games

This commit is contained in:
Maximilian Keßler 2023-07-07 14:32:11 +02:00
parent 3ab35eb10d
commit 722838243f
Signed by: max
GPG key ID: BCC5A619923C0BA5
4 changed files with 18 additions and 11 deletions

View file

@ -116,7 +116,8 @@ class HanabiInstance:
clue_starved: bool = False, # if true, discarding and playing fives only gives back half a clue 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 fives_give_clue: bool = True, # if false, then playing a five will not change the clue count
deck_plays: bool = False, deck_plays: bool = False,
all_or_nothing: bool = False all_or_nothing: bool = False,
starting_player: int = 0 # defines index of player that starts the game
): ):
# defining properties # defining properties
self.deck = deck self.deck = deck
@ -128,6 +129,7 @@ class HanabiInstance:
self.deck_plays = deck_plays, self.deck_plays = deck_plays,
self.all_or_nothing = all_or_nothing self.all_or_nothing = all_or_nothing
assert not self.all_or_nothing, "All or nothing not implemented" assert not self.all_or_nothing, "All or nothing not implemented"
self.starting_player = starting_player
# normalize deck indices # normalize deck indices
for (idx, card) in enumerate(self.deck): for (idx, card) in enumerate(self.deck):
@ -170,7 +172,7 @@ class HanabiInstance:
class GameState: class GameState:
def __init__(self, instance: HanabiInstance, starting_player: int = 0): def __init__(self, instance: HanabiInstance):
# will not be modified # will not be modified
self.instance = instance self.instance = instance
@ -181,7 +183,7 @@ class GameState:
self.stacks = [0 for i in range(0, self.instance.num_suits)] self.stacks = [0 for i in range(0, self.instance.num_suits)]
self.strikes = 0 self.strikes = 0
self.clues = 8 self.clues = 8
self.turn = starting_player self.turn = self.instance.starting_player
self.pace = self.instance.initial_pace self.pace = self.instance.initial_pace
self.remaining_extra_turns = self.instance.num_players + 1 self.remaining_extra_turns = self.instance.num_players + 1
self.trash = [] self.trash = []

View file

@ -19,7 +19,7 @@ from hanabi.solvers import sat
def check_game(game_id: int) -> Tuple[int, hanab_game.GameState]: def check_game(game_id: int) -> Tuple[int, hanab_game.GameState]:
logger.debug("Analysing game {}".format(game_id)) logger.debug("Analysing game {}".format(game_id))
with database.conn.cursor() as cur: with database.conn.cursor() as cur:
cur.execute("SELECT games.num_players, deck, actions, score, games.variant_id FROM games " cur.execute("SELECT games.num_players, deck, actions, score, games.variant_id, starting_player FROM games "
"INNER JOIN seeds ON seeds.seed = games.seed " "INNER JOIN seeds ON seeds.seed = games.seed "
"WHERE games.id = (%s)", "WHERE games.id = (%s)",
(game_id,) (game_id,)
@ -27,11 +27,16 @@ def check_game(game_id: int) -> Tuple[int, hanab_game.GameState]:
res = cur.fetchone() res = cur.fetchone()
if res is None: if res is None:
raise ValueError("No game associated with id {} in database.".format(game_id)) raise ValueError("No game associated with id {} in database.".format(game_id))
(num_players, compressed_deck, compressed_actions, score, variant_id) = res (num_players, compressed_deck, compressed_actions, score, variant_id, starting_player) = res
deck = compress.decompress_deck(compressed_deck) deck = compress.decompress_deck(compressed_deck)
actions = compress.decompress_actions(compressed_actions) actions = compress.decompress_actions(compressed_actions)
instance = hanab_live.HanabLiveInstance(deck, num_players, variant_id=variant_id) instance = hanab_live.HanabLiveInstance(
deck,
num_players,
variant_id=variant_id,
starting_player=starting_player
)
# check if the instance is already won # check if the instance is already won
if instance.max_score == score: if instance.max_score == score:

View file

@ -118,9 +118,9 @@ def detailed_export_game(
deck_plays=deck_plays, deck_plays=deck_plays,
one_less_card=one_less_card, one_less_card=one_less_card,
one_extra_card=one_extra_card, one_extra_card=one_extra_card,
all_or_nothing=all_or_nothing all_or_nothing=all_or_nothing,
), starting_player=starting_player
starting_player )
) )
for action in actions: for action in actions:
game.make_action(action) game.make_action(action)

View file

@ -41,8 +41,8 @@ class HanabLiveInstance(hanab_game.HanabiInstance):
class HanabLiveGameState(hanab_game.GameState): class HanabLiveGameState(hanab_game.GameState):
def __init__(self, instance: HanabLiveInstance, starting_player: int = 0): def __init__(self, instance: HanabLiveInstance):
super().__init__(instance, starting_player) super().__init__(instance)
self.instance: HanabLiveInstance = instance self.instance: HanabLiveInstance = instance
def make_action(self, action): def make_action(self, action):