Skip to content

Commit

Permalink
ran black for linting
Browse files Browse the repository at this point in the history
  • Loading branch information
dagarcia7 committed Sep 27, 2020
1 parent 4c296a5 commit 4030b19
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
15 changes: 10 additions & 5 deletions catanatron/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
city_possible_actions,
settlement_possible_actions,
robber_possibilities,
year_of_plenty_possible_actions
year_of_plenty_possible_actions,
)
from catanatron.models.player import Player
from catanatron.models.decks import ResourceDeck, DevelopmentDeck
Expand Down Expand Up @@ -181,7 +181,7 @@ def playable_actions(self, player):
actions.append(action)

# Can only do if the player has not already played a development card
if (player.has_year_of_plenty_card()):
if player.has_year_of_plenty_card():
for action in year_of_plenty_possible_actions(player, self.resource_deck):
actions.append(action)

Expand Down Expand Up @@ -281,12 +281,17 @@ def execute(self, action, initial_build_phase=False):

action = Action(action.player, action.action_type, development_card)
elif action.action_type == ActionType.PLAY_YEAR_OF_PLENTY:
cards_selected = action.value # Assuming action.value is a resource deck
cards_selected = action.value # Assuming action.value is a resource deck
player_to_act = action.player
if not player_to_act.development_deck.count(DevelopmentCard.YEAR_OF_PLENTY) > 0:
if (
not player_to_act.development_deck.count(DevelopmentCard.YEAR_OF_PLENTY)
> 0
):
raise ValueError("Player doesn't have year of plenty card")
if not self.resource_deck.includes(cards_selected):
raise ValueError("Not enough resources of this type (these types?) in bank")
raise ValueError(
"Not enough resources of this type (these types?) in bank"
)
player_to_act.resource_deck += cards_selected
player_to_act.development_deck.draw(1, DevelopmentCard.YEAR_OF_PLENTY)
self.resource_deck -= cards_selected
Expand Down
12 changes: 10 additions & 2 deletions catanatron/models/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,30 @@ class ActionType(Enum):

Action = namedtuple("Action", ["player", "action_type", "value"])


def year_of_plenty_possible_actions(player, resource_deck):
possible_combinations = set()
actions = []
for first_card in Resource:
for second_card in Resource:
if resource_deck.can_draw(1, first_card) and resource_deck.can_draw(1, second_card) and (second_card, first_card) not in possible_combinations:
if (
resource_deck.can_draw(1, first_card)
and resource_deck.can_draw(1, second_card)
and (second_card, first_card) not in possible_combinations
):
possible_combinations.add((first_card, second_card))
cards_selected = ResourceDeck()
cards_selected.replenish(1, first_card)
cards_selected.replenish(1, second_card)
actions.append(Action(player, ActionType.PLAY_YEAR_OF_PLENTY, cards_selected))
actions.append(
Action(player, ActionType.PLAY_YEAR_OF_PLENTY, cards_selected)
)

# TODO: If none of the combinations are possible due to shortages
# in the deck, allow player to draw one card
return actions


def road_possible_actions(player, board):
has_money = player.resource_deck.includes(ResourceDeck.road_cost())

Expand Down
4 changes: 4 additions & 0 deletions tests/models/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from catanatron.game import Game
from catanatron.models.decks import ResourceDeck


def test_playable_actions():
players = [SimplePlayer(Color.RED), SimplePlayer(Color.BLUE)]
game = Game(players)
Expand All @@ -21,17 +22,20 @@ def test_playable_actions():
assert len(actions) == 1
assert actions[0].action_type == ActionType.ROLL


def test_year_of_plenty_possible_actions_full_resource_bank():
player = SimplePlayer(Color.RED)
bank_resource_deck = ResourceDeck.starting_bank()
assert len(year_of_plenty_possible_actions(player, bank_resource_deck)) == 15


def test_year_of_plenty_possible_actions_not_enough_cards():
player = SimplePlayer(Color.RED)
bank_resource_deck = ResourceDeck()
bank_resource_deck.replenish(2, Resource.ORE)
assert len(year_of_plenty_possible_actions(player, bank_resource_deck)) == 1


def test_road_possible_actions():
board = Board()
player = SimplePlayer(Color.RED)
Expand Down
8 changes: 7 additions & 1 deletion tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def test_cant_buy_more_than_max_card():

assert players[0].resource_deck.num_cards() == 3


def test_play_year_of_plenty_gives_player_resources():
players = [SimplePlayer(Color.RED), SimplePlayer(Color.BLUE)]
game = Game(players)
Expand All @@ -146,7 +147,9 @@ def test_play_year_of_plenty_gives_player_resources():
cards_to_add = ResourceDeck()
cards_to_add.replenish(1, Resource.ORE)
cards_to_add.replenish(1, Resource.WHEAT)
action_to_execute = Action(player_to_act, ActionType.PLAY_YEAR_OF_PLENTY, cards_to_add)
action_to_execute = Action(
player_to_act, ActionType.PLAY_YEAR_OF_PLENTY, cards_to_add
)

game.execute(action_to_execute)

Expand All @@ -159,6 +162,7 @@ def test_play_year_of_plenty_gives_player_resources():
assert game.resource_deck.count(card_type) == 19
assert player_to_act.development_deck.count(DevelopmentCard.YEAR_OF_PLENTY) == 0


def test_play_year_of_plenty_not_enough_resources():
players = [SimplePlayer(Color.RED), SimplePlayer(Color.BLUE)]
player_to_act = players[0]
Expand All @@ -174,6 +178,7 @@ def test_play_year_of_plenty_not_enough_resources():
with pytest.raises(ValueError): # not enough cards in bank
game.execute(action_to_execute)


def test_play_year_of_plenty_no_year_of_plenty_card():
players = [SimplePlayer(Color.RED), SimplePlayer(Color.BLUE)]
game = Game(players)
Expand All @@ -186,6 +191,7 @@ def test_play_year_of_plenty_no_year_of_plenty_card():
with pytest.raises(ValueError): # no year of plenty card
game.execute(action_to_execute)


# ===== Yield Resources
def test_yield_resources():
board = Board()
Expand Down

0 comments on commit 4030b19

Please sign in to comment.