Example #1
0
 def setUp(self):
     self.move_origin = Move((0, 0), (1, 0))
     self.move2_1 = Move((0, 0), (2, 1))
     self.move2_0 = Move((0, 0), (2, 0))
     self.move2_0from = Move((2, 0), (1, 0))
     self.move2_1from = Move((2, 1), (1, 0))
     self.skip_action = Skip()
 def test_get_possible_moves_another_penguin(self):
     self.player1.add_penguin((0, 0))
     self.player2.add_penguin((2, 1))
     actions = [Move((0, 0), (1, 0)), Move((0, 0), (2, 0))]
     for action in self.state_full.get_possible_moves():
         self.assertTrue(action in actions)
     self.assertEqual(len(self.state_full.get_possible_moves()), len(actions))
 def test_update_with_action2(self):
     action = Move((0, 0), (3, 1))
     state = self.state.move_penguin(action.get_from_posn(),
                                     action.get_to_posn())
     # 0   X   X   X
     #   X   W   X   B
     # X   X   X   X
     #   X   R   X   X
     self.adapted_player.set_state(self.state)
     self.adapted_player.update_with_action(action)
     self.assertEqual(state,
                      self.adapted_player._LegacyToLogicalPlayer__state)
Example #4
0
    def test_run_tournament_everyone_fails(self):
        self.player1.choose_next_move = MagicMock(return_value=Move((0, 0), (1, 1)))  # illegal move
        self.player2.choose_placement = MagicMock(return_value=(0, 0))  # penguin already there
        self.player3.choose_placement = MagicMock(return_value=(-1, 0))
        self.player4.choose_next_move = MagicMock(
            return_value=Move((0, 0), (2, 0)))  # no penguin to move
        self.player5.choose_next_move = MagicMock(return_value=Move((0, 0), (1, 1)))
        self.player6.choose_next_move = MagicMock(return_value=Move((0, 0), (1, 1)))

        winners, cheaters = self.manager.run_tournament(self.players)
        self.assertEqual(winners, [])
        self.assertEqual(cheaters, set(self.players))
Example #5
0
 def test_from_json(self):
     jsons = [[[0, 0], [1, 0]], [[2, 0], [3, 0]], [[3, 1], [1, 2]],
              [[0, 0], [6, 3]]]
     moves = [
         Move((0, 0), (1, 0)),
         Move((2, 0), (3, 0)),
         Move((3, 1), (1, 2)),
         Move((0, 0), (6, 3))
     ]
     for json, move in zip(jsons, moves):
         m = Move.from_json(json)
         self.assertEqual(move.print_json(), m.print_json())
         self.assertEqual(move, m)
 def test_tt_move_pass(self):
     self.json_sock.recv_json.return_value = [[0, 0], [0, 2]]
     result = self.proxy_player.tt(self.state, [])
     self.assertEqual(Move((0, 0), (0, 2)), result)
     self.json_sock.send_json.assert_called_once_with(
         ["take-turn", [self.state.print_json(), []]])
     self.json_sock.recv_json.assert_called_once()
    def __handle_take_turn(self, arguments):
        """
        Handle a start message.

        JSON value -> JSON value
        """
        state = State.from_json(arguments[0])
        actions = [Move.from_json(action) for action in arguments[1]]
        return self.__player.tt(state, actions).print_json()
Example #8
0
class ActionTestCase(unittest.TestCase):
    def setUp(self):
        self.move_origin = Move((0, 0), (1, 0))
        self.move2_1 = Move((0, 0), (2, 1))
        self.move2_0 = Move((0, 0), (2, 0))
        self.move2_0from = Move((2, 0), (1, 0))
        self.move2_1from = Move((2, 1), (1, 0))
        self.skip_action = Skip()

    def test_break_tie_origin(self):
        # also row to
        self.assertEqual(self.move_origin.break_tie(self.move2_1),
                         self.move_origin)
        self.assertEqual(self.move2_1.break_tie(self.move_origin),
                         self.move_origin)

    def test_break_tie_skip(self):
        self.assertEqual(self.skip_action.break_tie(self.move2_1),
                         self.move2_1)
        self.assertEqual(self.move2_1.break_tie(self.skip_action),
                         self.move2_1)

    def test_break_tie_col_to(self):
        self.assertEqual(self.move2_1.break_tie(self.move2_0), self.move2_0)
        self.assertEqual(self.move2_0.break_tie(self.move2_1), self.move2_0)

    def test_break_tie_row_from(self):
        self.assertEqual(self.move2_0from.break_tie(self.move_origin),
                         self.move_origin)
        self.assertEqual(self.move_origin.break_tie(self.move2_0from),
                         self.move_origin)

    def test_break_tie_col_from(self):
        self.assertEqual(self.move2_1from.break_tie(self.move2_0from),
                         self.move2_0from)
        self.assertEqual(self.move2_0from.break_tie(self.move2_1from),
                         self.move2_0from)

    def test_break_tie_equal(self):
        move_copy = Move((2, 0), (1, 0))
        self.assertEqual(self.move2_0from.break_tie(move_copy),
                         self.move2_0from)
        self.assertEqual(move_copy.break_tie(self.move2_0from), move_copy)
Example #9
0
 def tt(self, state, actions):
     error = "Player did not return an Action to tt method"
     actions = [action.print_json() for action in actions]
     response = self.__send_request("take-turn",
                                    [state.print_json(), actions],
                                    is_json_action, error)
     if response is False:
         return Skip()
     else:
         return Move.from_json(response)
    def setUp(self):
        self.state = State(
            [PlayerData(Color.RED), PlayerData(Color.BROWN), PlayerData(Color.WHITE)],
            Board(4, 4))

        self.sock = socket.socket()
        self.json_sock = JSONSocket(self.sock)

        self.player = LogicalPlayerInterface()

        self.player.start = MagicMock()
        self.player.end = MagicMock()
        self.player.play_as = MagicMock()
        self.player.play_with = MagicMock()

        self.posn = (0, 0)
        self.player.setup = MagicMock(return_value=self.posn)

        self.move = Move((0, 0), (1, 0))
        self.player.tt = MagicMock(return_value=self.move)

        self.server_proxy = ServerProxy(self.player, self.json_sock)
 def test_update_with_multiple_actions(self):
     action1 = Move((0, 0), (3, 1))
     action2 = Move((1, 1), (0, 2))
     state = self.state.move_penguin(action1.get_from_posn(),
                                     action1.get_to_posn())
     state = state.move_penguin(action2.get_from_posn(),
                                action2.get_to_posn())
     # 0   X   W   X
     #   X   0   X   B
     # X   X   X   X
     #   X   R   X   X
     self.adapted_player.set_state(self.state)
     self.adapted_player.update_with_action(action1)
     self.adapted_player.update_with_action(action2)
     self.assertEqual(state,
                      self.adapted_player._LegacyToLogicalPlayer__state)
Example #12
0
 def test_run_round_failing_player(self):
     self.player1.choose_next_move = MagicMock(
         return_value=Move((0, 0), (4, 4)))  # runs into another penguin
     match_players = [
         self.players[0:4],
         self.players[4:6]
     ]
     expected = {
         "winners": {self.player3, self.player5},
         "losers": {self.player2, self.player4, self.player6},
         "kicked_players": {self.player1},
         "games_played": 2
     }
     round_result = self.manager._Manager__run_round(match_players)
     self.assertEqual(round_result, expected)
Example #13
0
    def get_possible_moves(self):
        """
        Gets a list of all of the possible moves for the current player in this state.

        :returns: List of Action	possible actions for player
        """
        penguins = self.players[0].get_penguins()
        possible_moves = []
        for p in penguins:
            for reachable in self.board.get_all_reachable_posn(
                    *p, self.get_all_penguins()):
                if self.valid_move(p, reachable):
                    possible_moves.append(Move(p, reachable))
        if len(possible_moves) == 0:  # there are no possible moves for player
            possible_moves = [Skip()]
        return possible_moves
Example #14
0
 def test_break_tie_equal(self):
     move_copy = Move((2, 0), (1, 0))
     self.assertEqual(self.move2_0from.break_tie(move_copy),
                      self.move2_0from)
     self.assertEqual(move_copy.break_tie(self.move2_0from), move_copy)
class ServerProxyTest(unittest.TestCase):

    def setUp(self):
        self.state = State(
            [PlayerData(Color.RED), PlayerData(Color.BROWN), PlayerData(Color.WHITE)],
            Board(4, 4))

        self.sock = socket.socket()
        self.json_sock = JSONSocket(self.sock)

        self.player = LogicalPlayerInterface()

        self.player.start = MagicMock()
        self.player.end = MagicMock()
        self.player.play_as = MagicMock()
        self.player.play_with = MagicMock()

        self.posn = (0, 0)
        self.player.setup = MagicMock(return_value=self.posn)

        self.move = Move((0, 0), (1, 0))
        self.player.tt = MagicMock(return_value=self.move)

        self.server_proxy = ServerProxy(self.player, self.json_sock)

    def tearDown(self):
        self.sock.close()

    def test_constructor(self):
        ServerProxy(self.player, self.json_sock)
        with self.assertRaises(ValueError):
            ServerProxy(self.player, None)
        with self.assertRaises(ValueError):
            ServerProxy(None, self.json_sock)

    def test_listen(self):
        self.json_sock.send_json = MagicMock()
        self.json_sock.recv_json = MagicMock()
        self.json_sock.recv_json.side_effect = [
            ["start", [True]],
            ["playing-as", ["red"]],
            ["playing-with", [["red", "black"]]],
            ["setup", [self.state.print_json()]],
            ["take-turn", [self.state.print_json(), []]],
            ["end", [True]]
        ]
        self.server_proxy.listen()
        self.json_sock.send_json.assert_has_calls([
            call("void"),
            call("void"),
            call("void"),
            call(self.posn),
            call(self.move.print_json()),
            call("void")
        ])

    def test_start(self):
        request = ["start", [True]]
        self.server_proxy._ServerProxy__handle_request(request)
        request = ["start", [False]]
        self.assertEqual("void", self.server_proxy._ServerProxy__handle_request(request))
        self.player.start.assert_has_calls([call(True), call(False)])

    def test_end(self):
        request = ["end", [True]]
        self.server_proxy._ServerProxy__handle_request(request)
        request = ["end", [False]]
        self.assertEqual("void", self.server_proxy._ServerProxy__handle_request(request))
        self.player.end.assert_has_calls([call(True), call(False)])

    def test_play_as(self):
        request = ["playing-as", ["red"]]
        self.server_proxy._ServerProxy__handle_request(request)
        request = ["playing-as", ["black"]]
        self.assertEqual("void", self.server_proxy._ServerProxy__handle_request(request))
        self.player.play_as.assert_has_calls([call(Color.RED), call(Color.BLACK)])

    def test_play_with(self):
        request = ["playing-with", [["red", "black"]]]
        self.server_proxy._ServerProxy__handle_request(request)
        request = ["playing-with", [["white", "brown"]]]
        self.assertEqual("void", self.server_proxy._ServerProxy__handle_request(request))
        self.player.play_with.assert_has_calls([call([Color.RED, Color.BLACK]),
                                                call([Color.WHITE, Color.BROWN])])

    def test_setup(self):
        request = ["setup", [self.state.print_json()]]
        self.assertEqual(self.posn,
                         self.server_proxy._ServerProxy__handle_request(request))
        self.player.setup.assert_called_once_with(self.state)

    def test_tt(self):
        actions = [self.move.print_json()]
        request = ["take-turn", [self.state.print_json(), actions]]
        self.assertEqual(self.move.print_json(),
                         self.server_proxy._ServerProxy__handle_request(request))
        self.player.tt.assert_called_once_with(self.state, [self.move])
Example #16
0
 def test_play_game_player_kicked(self):
     self.player1.choose_next_move = MagicMock(
         return_value=Move((0, 0), (1, 1)))
     game_result = self.referee.play_game(self.ext_players)
     self.assertEqual(game_result["winners"], [self.player2])
     self.assertEqual(game_result["kicked_players"], {self.player1})
Example #17
0
 def test_check_move_validity_game_setup(self):
     self.assertFalse(self.referee.check_move_validity(Move((2, 0),
                                                            (3, 0))))
Example #18
0
 def test_check_move_validity_false(self):
     game = self.referee.initialize_game(self.ext_players)
     self.assertFalse(self.referee.check_move_validity(Move((2, 0),
                                                            (1, 0))))
Example #19
0
 def test_check_move_validity(self):
     game = self.referee.initialize_game(self.ext_players)
     game_tree = self.referee.check_move_validity(Move((2, 0), (3, 0)))
     self.assertEqual(type(game_tree), GameTree)