Skip to content

Commit

Permalink
Add json test cases; linting
Browse files Browse the repository at this point in the history
  • Loading branch information
zarns committed Jul 15, 2024
1 parent 1ff44cd commit 2ebfff0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 27 deletions.
9 changes: 8 additions & 1 deletion tests/integration_tests/test_speed.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
from catanatron.models.player import Color, SimplePlayer, RandomPlayer
from catanatron.players.weighted_random import WeightedRandomPlayer
from catanatron_gym.features import create_sample
from catanatron_experimental.machine_learning.players.minimax import AlphaBetaPlayer, SameTurnAlphaBetaPlayer
from catanatron_experimental.machine_learning.players.minimax import (
AlphaBetaPlayer,
SameTurnAlphaBetaPlayer,
)

RANDOM_SEED = 0

Expand Down Expand Up @@ -62,6 +65,7 @@ def test_simpleplayer_speed(benchmark):
SimplePlayer(Color.ORANGE),
]
game = Game(players, seed=RANDOM_SEED)

def _play_game(game):
for _ in range(100):
game.play_tick()
Expand All @@ -78,6 +82,7 @@ def test_weightedrandom_speed(benchmark):
WeightedRandomPlayer(Color.ORANGE),
]
game = Game(players, seed=RANDOM_SEED)

def _play_game(game):
for _ in range(100):
game.play_tick()
Expand All @@ -94,6 +99,7 @@ def test_alphabeta_speed(benchmark):
AlphaBetaPlayer(Color.ORANGE),
]
game = Game(players, seed=RANDOM_SEED)

def _play_game(game):
for _ in range(100):
game.play_tick()
Expand All @@ -110,6 +116,7 @@ def test_same_turn_alphabeta_speed(benchmark):
SameTurnAlphaBetaPlayer(Color.ORANGE),
]
game = Game(players, seed=RANDOM_SEED)

def _play_game(game):
for _ in range(100):
game.play_tick()
Expand Down
48 changes: 23 additions & 25 deletions tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
player_num_resource_cards,
)
from catanatron.state_functions import player_key
from catanatron.models.actions import (
generate_playable_actions
)
from catanatron.models.actions import generate_playable_actions
from catanatron.models.enums import (
BRICK,
ORE,
Expand Down Expand Up @@ -358,8 +356,8 @@ def test_longest_road_steal():
board.build_road(p0.color, (7, 8))
board.build_road(p0.color, (8, 9))
board.build_road(p0.color, (9, 10))
game.state.player_state[f'{p0_key}_VICTORY_POINTS'] = 1
game.state.player_state[f'{p0_key}_ACTUAL_VICTORY_POINTS'] = 1
game.state.player_state[f"{p0_key}_VICTORY_POINTS"] = 1
game.state.player_state[f"{p0_key}_ACTUAL_VICTORY_POINTS"] = 1

# p1 has longest road of lenght 5
board.build_settlement(p1.color, 28, True)
Expand All @@ -368,14 +366,14 @@ def test_longest_road_steal():
board.build_road(p1.color, (29, 30))
board.build_road(p1.color, (30, 31))
board.build_road(p1.color, (31, 32))
game.state.player_state[f'{p1_key}_VICTORY_POINTS'] = 3
game.state.player_state[f'{p1_key}_ACTUAL_VICTORY_POINTS'] = 3
game.state.player_state[f'{p1_key}_HAS_ROAD'] = True
game.state.player_state[f"{p1_key}_VICTORY_POINTS"] = 3
game.state.player_state[f"{p1_key}_ACTUAL_VICTORY_POINTS"] = 3
game.state.player_state[f"{p1_key}_HAS_ROAD"] = True

# Required to be able to apply actions other than rolling or initial build phase.
game.state.current_prompt = ActionPrompt.PLAY_TURN
game.state.is_initial_build_phase = False
game.state.player_state[f'{p0_key}_HAS_ROLLED'] = True
game.state.player_state[f"{p0_key}_HAS_ROLLED"] = True
game.state.playable_actions = generate_playable_actions(game.state)

# Set up player0 to build two roads and steal longest road.
Expand All @@ -386,25 +384,25 @@ def test_longest_road_steal():

# Matching length of longest road does not steal longest road.
apply_action(game.state, Action(p0.color, ActionType.BUILD_ROAD, road1))
assert game.state.player_state[f'{p0_key}_LONGEST_ROAD_LENGTH'] == 5
assert game.state.player_state[f'{p0_key}_HAS_ROAD'] == False
assert game.state.player_state[f'{p0_key}_VICTORY_POINTS'] == 1
assert game.state.player_state[f'{p0_key}_ACTUAL_VICTORY_POINTS'] == 1
assert game.state.player_state[f'{p1_key}_LONGEST_ROAD_LENGTH'] == 5
assert game.state.player_state[f'{p1_key}_HAS_ROAD'] == True
assert game.state.player_state[f'{p1_key}_VICTORY_POINTS'] == 3
assert game.state.player_state[f'{p1_key}_ACTUAL_VICTORY_POINTS'] == 3
assert game.state.player_state[f"{p0_key}_LONGEST_ROAD_LENGTH"] == 5
assert game.state.player_state[f"{p0_key}_HAS_ROAD"] == False
assert game.state.player_state[f"{p0_key}_VICTORY_POINTS"] == 1
assert game.state.player_state[f"{p0_key}_ACTUAL_VICTORY_POINTS"] == 1
assert game.state.player_state[f"{p1_key}_LONGEST_ROAD_LENGTH"] == 5
assert game.state.player_state[f"{p1_key}_HAS_ROAD"] == True
assert game.state.player_state[f"{p1_key}_VICTORY_POINTS"] == 3
assert game.state.player_state[f"{p1_key}_ACTUAL_VICTORY_POINTS"] == 3

# Surpassing length of longest road steals longest road and VPs.
apply_action(game.state, Action(p0.color, ActionType.BUILD_ROAD, road2))
assert game.state.player_state[f'{p0_key}_LONGEST_ROAD_LENGTH'] == 6
assert game.state.player_state[f'{p0_key}_HAS_ROAD'] == True
assert game.state.player_state[f'{p0_key}_VICTORY_POINTS'] == 3
assert game.state.player_state[f'{p0_key}_ACTUAL_VICTORY_POINTS'] == 3
assert game.state.player_state[f'{p1_key}_LONGEST_ROAD_LENGTH'] == 5
assert game.state.player_state[f'{p1_key}_HAS_ROAD'] == False
assert game.state.player_state[f'{p1_key}_VICTORY_POINTS'] == 1
assert game.state.player_state[f'{p1_key}_ACTUAL_VICTORY_POINTS'] == 1
assert game.state.player_state[f"{p0_key}_LONGEST_ROAD_LENGTH"] == 6
assert game.state.player_state[f"{p0_key}_HAS_ROAD"] == True
assert game.state.player_state[f"{p0_key}_VICTORY_POINTS"] == 3
assert game.state.player_state[f"{p0_key}_ACTUAL_VICTORY_POINTS"] == 3
assert game.state.player_state[f"{p1_key}_LONGEST_ROAD_LENGTH"] == 5
assert game.state.player_state[f"{p1_key}_HAS_ROAD"] == False
assert game.state.player_state[f"{p1_key}_VICTORY_POINTS"] == 1
assert game.state.player_state[f"{p1_key}_ACTUAL_VICTORY_POINTS"] == 1


def test_second_placement_takes_cards_from_bank():
Expand Down
51 changes: 50 additions & 1 deletion tests/test_json.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
import json

from catanatron.game import Game
Expand Down Expand Up @@ -27,9 +28,57 @@ def test_serialization():
assert isinstance(result["actions"], list)


def test_action_from_json():
def test_action_from_json_maritime_trade():
data = ["RED", "MARITIME_TRADE", ["SHEEP", "SHEEP", "SHEEP", "SHEEP", "ORE"]]
action = action_from_json(data)
assert action.color == Color.RED
assert action.action_type == ActionType.MARITIME_TRADE
assert action.value == ("SHEEP", "SHEEP", "SHEEP", "SHEEP", "ORE")


def test_action_from_json_play_year_of_plenty_two_resources():
data = ["RED", "PLAY_YEAR_OF_PLENTY", ["WOOD", "BRICK"]]
action = action_from_json(data)
assert action.color == Color.RED
assert action.action_type == ActionType.PLAY_YEAR_OF_PLENTY
assert action.value == ("WOOD", "BRICK")


def test_action_from_json_play_year_of_plenty_one_resource():
data = ["BLUE", "PLAY_YEAR_OF_PLENTY", ["SHEEP"]]
action = action_from_json(data)
assert action.color == Color.BLUE
assert action.action_type == ActionType.PLAY_YEAR_OF_PLENTY
assert action.value == ("SHEEP",)


def test_action_from_json_play_year_of_plenty_invalid():
data = ["WHITE", "PLAY_YEAR_OF_PLENTY", ["WOOD", "BRICK", "SHEEP"]]
with pytest.raises(
ValueError, match="Year of Plenty action must have 1 or 2 resources"
):
action_from_json(data)


def test_action_from_json_move_robber_with_victim():
data = ["ORANGE", "MOVE_ROBBER", [[0, 0, 0], "RED", None]]
action = action_from_json(data)
assert action.color == Color.ORANGE
assert action.action_type == ActionType.MOVE_ROBBER
assert action.value == ((0, 0, 0), Color.RED, None)


def test_action_from_json_move_robber_without_victim():
data = ["RED", "MOVE_ROBBER", [[1, -1, 0], None, None]]
action = action_from_json(data)
assert action.color == Color.RED
assert action.action_type == ActionType.MOVE_ROBBER
assert action.value == ((1, -1, 0), None, None)


def test_action_from_json_build_road():
data = ["BLUE", "BUILD_ROAD", [0, 1]]
action = action_from_json(data)
assert action.color == Color.BLUE
assert action.action_type == ActionType.BUILD_ROAD
assert action.value == (0, 1)

0 comments on commit 2ebfff0

Please sign in to comment.