Skip to content

Commit

Permalink
Implement faster winner check
Browse files Browse the repository at this point in the history
  • Loading branch information
tonypr committed Sep 11, 2022
1 parent aa43d9a commit 218d8e3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
17 changes: 4 additions & 13 deletions catanatron_core/catanatron/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ def __init__(
random.seed(self.seed)

self.id = str(uuid.uuid4())
self.vps_to_win = vps_to_win
self.state = State(players, catan_map, discard_limit=discard_limit)
self.state = State(
players, catan_map, discard_limit=discard_limit, vps_to_win=vps_to_win
)

def play(self, accumulators=[], decide_fn=None):
"""Executes game until a player wins or exceeded TURNS_LIMIT.
Expand Down Expand Up @@ -173,16 +174,7 @@ def winning_color(self) -> Union[Color, None]:
Returns:
Union[Color, None]: Might be None if game truncated by TURNS_LIMIT
"""
result = None
for color in self.state.colors:
key = player_key(self.state, color)
if (
self.state.player_state[f"{key}_ACTUAL_VICTORY_POINTS"]
>= self.vps_to_win
):
result = color

return result
return self.state.winning_color

def copy(self) -> "Game":
"""Creates a copy of this Game, that can be modified without
Expand All @@ -194,6 +186,5 @@ def copy(self) -> "Game":
game_copy = Game([], None, None, initialize=False)
game_copy.seed = self.seed
game_copy.id = self.id
game_copy.vps_to_win = self.vps_to_win
game_copy.state = self.state.copy()
return game_copy
10 changes: 10 additions & 0 deletions catanatron_core/catanatron/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
player_can_afford_dev_card,
player_can_play_dev,
player_clean_turn,
player_check_is_winner,
player_freqdeck_add,
player_deck_draw,
player_deck_random_draw,
Expand Down Expand Up @@ -120,13 +121,16 @@ class State:
free_roads_available (int): Number of roads available left in Road Building
phase.
playable_actions (List[Action]): List of playable actions by current player.
vps_to_win (int): Number of VPS points needed to win.
winning_color (bool): If there is a winner, store the winner's color. Othewrise None.
"""

def __init__(
self,
players: List[Any],
catan_map=None,
discard_limit=7,
vps_to_win=10,
initialize=True,
):
if initialize:
Expand Down Expand Up @@ -171,6 +175,8 @@ def __init__(
self.acceptees = tuple(False for _ in self.colors)

self.playable_actions = generate_playable_actions(self)
self.vps_to_win = vps_to_win
self.winning_color = None

def current_player(self):
"""Helper for accessing Player instance who should decide next"""
Expand Down Expand Up @@ -223,6 +229,9 @@ def copy(self):
state_copy.acceptees = self.acceptees

state_copy.playable_actions = self.playable_actions

state_copy.vps_to_win = self.vps_to_win
state_copy.winning_color = self.winning_color
return state_copy


Expand Down Expand Up @@ -320,6 +329,7 @@ def apply_action(state: State, action: Action):

if action.action_type == ActionType.END_TURN:
player_clean_turn(state, action.color)
player_check_is_winner(state, action.color)
advance_turn(state)
state.current_prompt = ActionPrompt.PLAY_TURN
state.playable_actions = generate_playable_actions(state)
Expand Down
6 changes: 6 additions & 0 deletions catanatron_core/catanatron/state_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,9 @@ def player_clean_turn(state, color):
key = player_key(state, color)
state.player_state[f"{key}_HAS_PLAYED_DEVELOPMENT_CARD_IN_TURN"] = False
state.player_state[f"{key}_HAS_ROLLED"] = False


def player_check_is_winner(state, color):
key = player_key(state, color)
if state.player_state[f"{key}_ACTUAL_VICTORY_POINTS"] >= state.vps_to_win:
state.winning_color = color

0 comments on commit 218d8e3

Please sign in to comment.