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_apply_player_bankrupt(self): bsmt_phase = BSMT() pay_rent_phase = PayRent() turn_end_phase = TurnEnd() phases = { 'BSMT': bsmt_phase, 'PayRent': pay_rent_phase, 'TurnEnd': turn_end_phase, } board = Board() player_1 = Player(1, position=board.property_at(0)) player_2 = Player(2, amount=10, position=board.property_at(1)) mediterranean_avenue = board.property_at(1) mediterranean_avenue.own(player_1) rent = 100 mediterranean_avenue.rent = MagicMock(return_value=rent) players = [player_1, player_2] game_state = GameState(players, board) game_state.next_player() game_phase = pay_rent_phase context = Context(phases, game_state, game_phase) bsmt_phase.apply = self.fake_bsmt_cycle(rent - 20) new_context, next_action = context.apply() self.assertTrue(new_context.phase is turn_end_phase) self.assertEqual(new_context.state.current_player.amount, 90)
def test_chance_2(self): bsmt_phase = BSMT() special_property_phase = SpecialProperty() phases = { 'BSMT': bsmt_phase, 'SpecialProperty': special_property_phase, } board = Board() board.next_chance = MagicMock(return_value=2) board.passes_go = MagicMock(return_value=True) player_1 = Player(1, amount=100, position=board.property_at(1)) player_2 = Player(2, amount=300, position=board.property_at(36)) players = [player_1, player_2] game_state = GameState(players, board) game_state.next_player() game_phase = special_property_phase context = Context(phases, game_state, game_phase) st_charles_place = board.property_at(11) new_context, next_action = context.apply() self.assertTrue(new_context.phase is bsmt_phase) self.assertTrue(player_2.position is st_charles_place) self.assertEqual(player_2.amount, 500) self.assertEqual(player_1.amount, 100)
def test_chance_3_unowned(self): special_property_phase = SpecialProperty() buy_property_phase = BuyProperty() phases = { 'SpecialProperty': special_property_phase, 'BuyProperty': buy_property_phase } board = Board() board.next_chance = MagicMock(return_value=3) electric_company = board.property_at(12) board.nearest_utility = MagicMock(return_value=electric_company) player_1 = Player(1, amount=100, position=board.property_at(0)) player_2 = Player(2, amount=300, position=board.property_at(36)) players = [player_1, player_2] game_state = GameState(players, board) game_state.next_player() game_phase = special_property_phase context = Context(phases, game_state, game_phase) new_context, next_action = context.apply() self.assertTrue(new_context.phase is buy_property_phase) self.assertTrue(player_2.position is electric_company) self.assertEqual(player_2.amount, 300) self.assertEqual(player_1.amount, 100)
def main(stdscr): curses.curs_set(False) windows = WindowManager() game = GameState(30, windows.push) max_render = game.max_render_point() assert max_render.row <= curses.LINES and max_render.col <= curses.COLS windows.push(MainWindow(game), None) windows.push(TitleScreen(game, windows.push), game.load) windows.refresh() while True: start_time = time.perf_counter() # Process inputs. windows.handle_input() # Update state. windows.update() # Redraw. windows.refresh() # Wait. wait_time = (1 / game.fps()) - (time.perf_counter() - start_time) if (wait_time > 0): time.sleep(wait_time)
def test_chance_4_5_owned(self): dice = utils.Dice() dice.roll = MagicMock(return_value=(1, 2)) special_property_phase = SpecialProperty(dice) bsmt_phase = BSMT() phases = { 'SpecialProperty': special_property_phase, 'BSMT': bsmt_phase } board = Board() board.next_chance = MagicMock(return_value=4) reading_railroad = board.property_at(5) board.nearest_railroad = MagicMock(return_value=reading_railroad) player_1 = Player(1, amount=100, position=board.property_at(0)) player_2 = Player(2, amount=300, position=board.property_at(36)) players = [player_1, player_2] game_state = GameState(players, board) game_state.next_player() game_phase = special_property_phase context = Context(phases, game_state, game_phase) reading_railroad.own(player_1) reading_railroad.rent = MagicMock(return_value=15) new_context, next_action = context.apply() self.assertTrue(new_context.phase is bsmt_phase) self.assertTrue(player_2.position is reading_railroad) self.assertEqual(player_2.amount, 300 - 10 * 15) self.assertEqual(player_1.amount, 100 + 10 * 15)
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 create(self): ''' Creates game state from user input and runs the game until the rounds are concluded. ''' # Create Game State based on user input for game parameters self.game = GameState(self.cli.create_game()) # Run the game until a player has won while not self.game.check_game_end(): print("\nGame Start") self.run_game()
def test_unmortgage_property(self): mortgage_property_phase = MortgageProperty() bsmt_phase = BSMT() phases = { 'MortgagePropertyHouse': mortgage_property_phase, 'BSMT': bsmt_phase, } board = Board() player_1 = Player(1, amount=500, position=board.property_at(1)) player_2 = Player(2, amount=500, position=board.property_at(0)) players = [player_1, player_2] mediterranean_avenue = board.property_at(1) mediterranean_avenue.mortgage_value = 200 mediterranean_avenue.own(player_1) mediterranean_avenue.mortgage() bank = Bank() game_state = GameState(players, board, bank=bank) game_phase = mortgage_property_phase context = Context(phases, game_state, game_phase) action = mediterranean_avenue context.apply(action) self.assertTrue(mediterranean_avenue.type is PropertyType.OWNED) self.assertEqual(player_1.amount, 280) self.assertEqual(player_2.amount, 500)
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 setup(agent_1, agent_2): dice = utils.Dice() roll_mock = Mock() roll_mock.side_effect = cycle([(1, 2), (2, 2)]) dice.roll = roll_mock dice_roll_phase = DiceRoll(dice) square_effect_phase = SquareEffect() bsmt_phase = BSMT() buy_property_phase = BuyProperty() pay_rent_phase = PayRent() auction_phase = Auction() phases = { 'DiceRoll': dice_roll_phase, 'SquareEffect': square_effect_phase, 'BSMT': bsmt_phase, 'BuyProperty': buy_property_phase, 'PayRent': pay_rent_phase, 'Auction': auction_phase, } board = Board() player_1 = Player(1, position=board.property_at(0), agent=agent_1) player_2 = Player(2, position=board.property_at(0), agent=agent_2) players = [player_1, player_2] game_state = GameState(players, board) start_phase = dice_roll_phase context = Context(phases, game_state, start_phase) return context
def test_change_player_on_double_roll(self): dice_roll_phase = DiceRoll() turn_end_phase = TurnEnd() phases = { 'DiceRoll': dice_roll_phase, 'TurnEnd': turn_end_phase, } board = Board() double_roll = ((2, 2), (2, 2)) player_1 = Player(1, position=board.property_at(1)) player_2 = Player(2, position=board.property_at(0), previous_rolls=double_roll) players = [player_1, player_2] game_state = GameState(players, board) game_state.next_player() game_phase = turn_end_phase context = Context(phases, game_state, game_phase) new_context, next_action = context.apply() self.assertTrue(new_context.phase is dice_roll_phase) self.assertTrue(new_context.state.current_player is player_2)
def test_special_property_community_chest(self): square_effect_phase = SquareEffect() special_property_phase = SpecialProperty() phases = { 'SpecialProperty': special_property_phase, } board = Board() player_1 = Player(1, position=board.property_at(2)) player_2 = Player(2, position=board.property_at(1)) players = [player_1, player_2] game_state = GameState(players) game_phase = square_effect_phase context = Context(phases, game_state, game_phase) new_context, next_action = context.apply() self.assertTrue(new_context.phase is special_property_phase)
def test_apply(self): buy_house_phase = BuyHouse() phases = {} board = Board() player_1 = Player(1, amount=500, position=board.property_at(1)) player_2 = Player(2, amount=500, position=board.property_at(0)) players = [player_1, player_2] mediterranean_avenue = board.property_at(1) mediterranean_avenue.own(player_1) mediterranean_avenue.build_costs = {'House': 100} bank = Bank() game_state = GameState(players, board, bank=bank) game_phase = buy_house_phase context = Context(phases, game_state, game_phase) action = (mediterranean_avenue, 'House', 2) context.apply(action) self.assertEqual(mediterranean_avenue.houses, 2) self.assertEqual(player_1.amount, 300) self.assertEqual(player_2.amount, 500)
def test_unowned_property(self): dice_roll_phase = DiceRoll() square_effect_phase = SquareEffect() buy_property_phase = BuyProperty() pay_rent_phase = PayRent() phases = { 'DiceRoll': dice_roll_phase, 'SquareEffect': square_effect_phase, 'BuyProperty': buy_property_phase, 'PayRent': pay_rent_phase, } board = Board() player_1 = Player(1, position=board.property_at(1)) player_2 = Player(2, position=board.property_at(1)) players = [player_1, player_2] game_state = GameState(players) game_phase = square_effect_phase context = Context(phases, game_state, game_phase) new_context, next_action = context.apply() self.assertTrue(isinstance(new_context.phase, BuyProperty))
def test_apply(self): dice = utils.Dice() dice.roll = MagicMock(return_value=(1, 2)) dice_roll_phase = DiceRoll(dice) square_effect_phase = SquareEffect() phases = { 'DiceRoll': dice_roll_phase, 'SquareEffect': square_effect_phase, } board = Board() player_1 = Player(1, position=board.property_at(0)) player_2 = Player(2, position=board.property_at(0)) players = [player_1, player_2] game_state = GameState(players, board) game_phase = dice_roll_phase context = Context(phases, game_state, game_phase) new_context, next_action = context.apply() self.assertTrue(new_context.phase is square_effect_phase) self.assertTrue( new_context.state.current_player.position is board.property_at(3))
def test_selling_property(self): trade_property_phase = TradeProperty() board = Board() player_1 = Player(1, amount=500, position=board.property_at(1)) player_2 = Player(2, amount=500, position=board.property_at(0)) players = [player_1, player_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) 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] }] game_state = GameState(players, board) game_phase = trade_property_phase context = Context({}, game_state, game_phase) action = trades context.apply(action) self.assertEqual(player_1.amount, 500 + 200 - 400) self.assertEqual(player_2.amount, 500 - 200 + 400) self.assertTrue(mediterranean_avenue.owned_by is player_2) self.assertTrue(baltic_avenue.owned_by is player_2) self.assertTrue(reading_railroad.owned_by is player_1) self.assertTrue(oriental_avenue.owned_by is player_1)
def context_factory(agent_1, agent_2, dice_rolls): dice = utils.Dice() roll_mock = Mock() roll_mock.side_effect = iter(dice_rolls) dice.roll = roll_mock dice_roll_phase = DiceRoll(dice) square_effect_phase = SquareEffect() bsmt_phase = BSMT() buy_property_phase = BuyProperty() pay_rent_phase = PayRent() auction_phase = Auction() turn_end_phase = TurnEnd() mortgage_property_phase = MortgageProperty() special_property_phase = SpecialProperty() phases = { 'DiceRoll': dice_roll_phase, 'SquareEffect': square_effect_phase, 'BSMT': bsmt_phase, 'BuyProperty': buy_property_phase, 'MortgageProperty': mortgage_property_phase, 'PayRent': pay_rent_phase, 'Auction': auction_phase, 'SpecialProperty': special_property_phase, 'TurnEnd': turn_end_phase, } board = Board() player_1 = Player(1, amount=1500, position=board.property_at(0), agent=agent_1) player_2 = Player(2, amount=1500, position=board.property_at(0), agent=agent_2) players = [player_1, player_2] game_state = GameState(players, board) start_phase = dice_roll_phase context = Context(phases, game_state, start_phase) return context
def test_self_owned_property(self): bsmt_phase = BSMT() square_effect_phase = SquareEffect() buy_property_phase = BuyProperty() pay_rent_phase = PayRent() phases = { 'BSMT': bsmt_phase, 'SquareEffect': square_effect_phase, 'BuyProperty': buy_property_phase, 'PayRent': pay_rent_phase, } board = Board() player_1 = Player(1, position=board.property_at(1)) player_2 = Player(2, position=board.property_at(1)) mediterranean_avenue = board.property_at(1) mediterranean_avenue.own(player_1) players = [player_1, player_2] game_state = GameState(players) game_phase = square_effect_phase context = Context(phases, game_state, game_phase) new_context, next_action = context.apply() self.assertTrue(new_context.phase is bsmt_phase)
def test_card_ten(self): '''This test check that a card can be played with a double digit rank''' test_input = { 'decks': 1, 'rounds': 1, 'players': 1, 'human players': 1, 'comp_levels': {} } state = GameState(test_input) state.start_new_round() player_obj = state.players.get_player_by_id(state.current_player) player_obj.hand = [c for c in Deck(1)] state.print_round_state_to_cli() test_commands = [] test_commands.append(Command("7CL0", state.layouts)) test_commands.append(Command("8CL0", state.layouts)) test_commands.append(Command("9CL0", state.layouts)) test_commands.append(Command("10CL0", state.layouts)) test_commands.append(Command("JCL0", state.layouts)) test_commands.append(Command("QCL0", state.layouts)) test_commands.append(Command("KCL0", state.layouts)) test_commands.append(Command("ACL0", state.layouts)) test_commands.append(Command("6CL0", state.layouts)) test_commands.append(Command("5CL0", state.layouts)) test_commands.append(Command("4CL0", state.layouts)) test_commands.append(Command("3CL0", state.layouts)) test_commands.append(Command("2CL0", state.layouts)) for test_command in test_commands: state.current_command = test_command state.update() state.print_round_state_to_cli() extra_command = Command("7HL2", state.layouts) state.current_command = extra_command state.update() state.print_round_state_to_cli()
class GameDriver(): ''' This class creates a game_state object and runs the game logic above this level ''' def __init__(self): '''Declares cli class and initialises coloured fonts.''' self.cli = Cli() self.game = None init() def create(self): ''' Creates game state from user input and runs the game until the rounds are concluded. ''' # Create Game State based on user input for game parameters self.game = GameState(self.cli.create_game()) # Run the game until a player has won while not self.game.check_game_end(): print("\nGame Start") self.run_game() def run_game(self): ''' Runs the rounds and starts new rounds if a player is identified as a winner. ''' # Start a new round self.game.start_new_round() # Call run_round until there is a winner while not self.game.check_round_winner(): self.run_round() def run_round(self): ''' Handles the running of a turn, print to cli and requesting user input. ''' # Display current game state in cli output self.game.print_round_state_to_cli() # Process command from current_player self.game.process_command() # Check if this was a winning move if self.game.check_round_winner(): print("Player '{}' is the Winner!".format( self.game.current_player)) else: self.game.end_turn()
def test_round_end(self): '''This test check that a winner is found''' test_input = { 'decks': 1, 'rounds': 2, 'players': 1, 'human players': 1, 'comp_levels': {} } state = GameState(test_input) state.start_new_round() player_obj = state.players.get_player_by_id(state.current_player) player_obj.hand = Deck(1).cards state.print_round_state_to_cli() test_cmd_strs_1 = [ str(i) + s + "L" + str(suit_layout_dict[s]) for s in suit_layout_dict.keys() for i in range(7, 15) ] test_cmd_strs_2 = [ str(i) + s + "L" + str(suit_layout_dict[s]) for s in suit_layout_dict.keys() for i in range(2, 7) ] test_cmd_strs_2.reverse() test_cmd_strs = test_cmd_strs_1 + test_cmd_strs_2 test_commands = [Command(c, state.layouts) for c in test_cmd_strs] player_obj = state.players.get_player_by_id(state.current_player) while not state.check_round_winner(): for test_command in test_commands: state.current_command = test_command state.update() state.print_round_state_to_cli() state.start_new_round() self.assertEqual(state.round_number, 2) self.assertEqual(state.dealer_id, 0) while not state.check_round_winner(): for test_command in test_commands: state.current_command = test_command state.update() state.print_round_state_to_cli() print("Total rounds: ", state.total_rounds) print("round_number: ", state.round_number) self.assertTrue(state.check_game_end())