def test_player_buys_property(self): buy_property_phase = BuyProperty() bsmt_phase = BSMT() phases = { 'BuyProperty': buy_property_phase, 'BSMT': bsmt_phase, } board = Board() agent = Agent() agent.buy_property = MagicMock(return_value=True) current_position = board.property_at(1) current_position.cost = 100 player_1 = Player(1, amount=20, position=current_position, agent=agent) player_2 = Player(2) players = [player_1, player_2] game_state = GameState(players, board) game_phase = buy_property_phase context = Context(phases, game_state, game_phase) bsmt_phase.apply = self.fake_bsmt_cycle(100) new_context, next_action = context.apply() self.assertTrue(new_context.phase is bsmt_phase) self.assertTrue( new_context.state.current_player.position.owned_by is player_1) self.assertEqual(new_context.state.current_player.amount, 20) self.assertEqual(new_context.state.current_player._debt['bank'], 0)
def test_player_declines_buying_property(self): buy_property_phase = BuyProperty() bsmt_phase = BSMT() auction_phase = Auction() phases = { 'BuyProperty': buy_property_phase, 'BSMT': bsmt_phase, 'Auction': auction_phase, } board = Board() agent = Agent() agent.buy_property = MagicMock(return_value=False) player_1 = Player(1, amount=900, position=board.property_at(1), agent=agent) player_2 = Player(2, position=board.property_at(0)) players = [player_1, player_2] game_state = GameState(players, board) game_phase = buy_property_phase context = Context(phases, game_state, game_phase) new_context, next_action = context.apply() self.assertTrue(new_context.phase is auction_phase) self.assertEqual(new_context.state.current_player.amount, 900)
def test_parse(self): self.skipTest('wip') agent_1, agent_2 = Agent(), Agent() context = setup(agent_1, agent_2) game_state = adapter.game_state.parse(context) self.assertEqual(game_state, None)
def player_5(): external_agent = mock.Mock() external_agent.buyProperty = MagicMock(return_value=True) bsmt_decision_mock = Mock() bsmt_decision_mock.side_effect = chain(repeat((None, None), times=5), [('M', [9])], repeat((None, None))) external_agent.getBMSTDecision = bsmt_decision_mock return Agent(external_agent)
def player_3(): external_agent = mock.Mock() external_agent.buyProperty = MagicMock(return_value=True) bsmt_decision_mock = Mock() bsmt_decision_mock.side_effect = iter([(None, None), ('M', [3]), (None, None), (None, None)]) external_agent.getBMSTDecision = bsmt_decision_mock return Agent(external_agent)
def test_mortgage_property_action(self): bsmt_phase = BSMT() mortgage_property_phase = MortgageProperty() turn_end_phase = TurnEnd() phases = { 'MortgageProperty': mortgage_property_phase, 'BSMT': bsmt_phase, 'TurnEnd': turn_end_phase, } board = Board() agent_1 = Agent() agent_2 = Agent() player_1 = Player(1, position=board.property_at(0), agent=agent_1) player_2 = Player(2, position=board.property_at(1), agent=agent_2) mediterranean_avenue = board.property_at(1) mediterranean_avenue.build(2) mediterranean_avenue.own(player_2) agent_1.bsmt_decision = MagicMock(return_value=(None, None)) agent_2_mock = Mock() agent_2_mock.side_effect = iter([(Action.MORTGAGE_PROPERTY, mediterranean_avenue), (None, None)]) agent_2.bsmt_decision = agent_2_mock players = [player_1, player_2] game_state = GameState(players, board) game_state.next_player() game_phase = bsmt_phase context = Context(phases, game_state, game_phase) mortgage_property_phase.apply = MagicMock(return_value=(context, None)) context.apply() mortgage_property_phase.apply.assert_called_once()
def test_sell_house_action(self): bsmt_phase = BSMT() sell_house_phase = SellHouse() turn_end_phase = TurnEnd() phases = { 'SellHouse': sell_house_phase, 'BSMT': bsmt_phase, 'TurnEnd': turn_end_phase, } board = Board() agent_1 = Agent() agent_1.bsmt_decision = MagicMock(return_value=(None, None)) agent_2 = Agent() sell_action = [1, 1] agent_2_mock = Mock() agent_2_mock.side_effect = iter([(Action.SELL_HOUSE, sell_action), (None, None)]) agent_2.bsmt_decision = agent_2_mock player_1 = Player(1, position=board.property_at(0), agent=agent_1) player_2 = Player(2, position=board.property_at(1), agent=agent_2) mediterranean_avenue = board.property_at(1) mediterranean_avenue.build(2) mediterranean_avenue.own(player_2) players = [player_1, player_2] game_state = GameState(players, board) game_state.next_player() game_phase = bsmt_phase context = Context(phases, game_state, game_phase) sell_house_phase.apply = MagicMock(return_value=(context, None)) context.apply() sell_house_phase.apply.assert_called_once()
def test_trade_property_action(self): bsmt_phase = BSMT() trade_property_phase = TradeProperty() turn_end_phase = TurnEnd() phases = { 'TradeProperty': trade_property_phase, 'BSMT': bsmt_phase, 'TurnEnd': turn_end_phase, } board = Board() agent_1 = Agent() agent_2 = Agent() player_1 = Player(1, position=board.property_at(0), agent=agent_1) player_2 = Player(2, position=board.property_at(1), agent=agent_2) mediterranean_avenue = board.property_at(1) mediterranean_avenue.own(player_1) baltic_avenue = board.property_at(3) baltic_avenue.own(player_1) reading_railroad = board.property_at(5) reading_railroad.own(player_2) oriental_avenue = board.property_at(6) oriental_avenue.own(player_2) agent_1_mock = Mock() agent_1_mock.side_effect = iter([ (Action.TRADE_PROPERTY, (200, [mediterranean_avenue, baltic_avenue], 400, [reading_railroad, oriental_avenue])), (None, None) ]) agent_1.bsmt_decision = agent_1_mock agent_2.bsmt_decision = MagicMock(return_value=(None, None)) agent_2.respond_trade = MagicMock(return_value=(True, None)) players = [player_1, player_2] game_state = GameState(players, board) game_state.next_player() game_phase = bsmt_phase context = Context(phases, game_state, game_phase) trade_property_phase.apply = MagicMock(return_value=(context, None)) expected_trades = [{ 'buyer': player_2, 'seller': player_1, 'price': 200, 'properties': [mediterranean_avenue, baltic_avenue] }, { 'buyer': player_1, 'seller': player_2, 'price': 400, 'properties': [reading_railroad, oriental_avenue] }] context.apply() trade_property_phase.apply.assert_called_once_with( context, expected_trades)
def demo(): # Agent 1 always buys, Agent 2 does not buy properties agent_1 = Agent() agent_1.buy_property = MagicMock(return_value=False) agent_1.auction_property = MagicMock(return_value=0) agent_1.bsmt_decision = MagicMock(return_value=(None, None)) agent_2 = Agent() agent_2.buy_property = MagicMock(return_value=False) agent_2.auction_property = MagicMock(return_value=10) agent_2.bsmt_decision = MagicMock(return_value=(None, None)) context = setup(agent_1, agent_2) print(context.phase) # print(context.state) # will be changed to terminal state based event looping num_moves = 10 action = None for _ in range(5): context, action = context.apply(action) print(context.phase) print(context.state) result = adapter.game_state.parse(context) print(result)
def player_1(): external_agent = mock.Mock() external_agent.buyProperty = MagicMock(return_value=False) external_agent.auctionProperty = MagicMock(return_value=0) external_agent.getBMSTDecision = MagicMock(return_value=(None, None)) return Agent(external_agent)
def player_6(): external_agent = mock.Mock() external_agent.buyProperty = MagicMock(return_value=True) external_agent.getBMSTDecision = MagicMock(return_value=(None, None)) return Agent(external_agent)