Example #1
0
    def test_get_next_state_success(self):
        # Test successful get next state
        json_obj = {
            'players': [
                {'score': 0, 'places': [[1, 0], [2, 0], [0, 0]], 'color': 'white'},
                {'score': 0, 'places': [[1, 1], [2, 1], [3, 1]], 'color': 'black'},
                {'score': 0, 'places': [[0, 1], [1, 2], [3, 2]], 'color': 'brown'}
            ],
            'board': [[4, 4, 4], [4, 4, 4], [4, 4, 4], [4, 4, 4]]
        }

        # Initialize state from json object
        state = initialize_state(json_obj)

        # Expected state after getting next state
        expected_state = {
            'players': [
                {'score': 0, 'places': [[1, 1], [2, 1], [3, 1]], 'color': 'black'},
                {'score': 0, 'places': [[0, 1], [1, 2], [3, 2]], 'color': 'brown'},
                {'score': 4, 'places': [[3, 0], [2, 0], [0, 0]], 'color': 'white'}
            ],
            'board': [[4, 4, 4], [0, 4, 4], [4, 4, 4], [4, 4, 4]]
        }

        # Get next state
        next_state = _get_next_state(state)
        
        # Make sure json representations of the states are as expected
        self.assertDictEqual(expected_state, _state_to_json(next_state))
Example #2
0
    def test_get_action_success(self):
        # Test that RPP can successfully request, receive, and decode an Action
        rpp = RemotePlayerProxy('name', 1.0, self.dummy_socket)
        rpp._RemotePlayerProxy__socket = self.mock_socket
        self.mock_socket.recv.return_value = b'[[0, 1], [2, 2]]'

        state = State(self.__b, players=[self.__p1, self.__p2, self.__p3])
        json_state = _state_to_json(state)

        expected_client_request = f'["take-turn", [{json.dumps(json_state)}, []]]'

        with patch.object(rpp, '_RemotePlayerProxy__send_message') as mock:
            action = rpp.get_action(state)
            mock.assert_called_with(expected_client_request)
            self.assertEqual(action, Action(Position(0, 1), Position(2, 2)))
Example #3
0
    def test_get_placement_success(self):
        # Test that RPP can successfully request, receive and decode a position
        # This is done to bypass the constructor type checking
        rpp = RemotePlayerProxy('name', 1.0, self.dummy_socket)
        rpp._RemotePlayerProxy__socket = self.mock_socket
        self.mock_socket.recv.return_value = b'[0, 1]'

        state = State(self.__b, players=[self.__p1, self.__p2, self.__p3])
        json_state = _state_to_json(state)

        expected_client_request = f'["setup", [{json.dumps(json_state)}]]'

        with patch.object(rpp, '_RemotePlayerProxy__send_message') as mock:
            pos = rpp.get_placement(state)
            mock.assert_called_with(expected_client_request)
            self.assertEqual(pos, Position(0, 1))
Example #4
0
    def test_get_action_returns_position(self):
        # Test that receiving a position when asking for an action throws JsonDecodeException and returns None
        rpp = RemotePlayerProxy('name', 1.0, self.dummy_socket)
        rpp._RemotePlayerProxy__socket = self.mock_socket
        self.mock_socket.recv.return_value = b'[0, 1]'

        state = State(self.__b, players=[self.__p1, self.__p2, self.__p3])
        json_state = _state_to_json(state)

        expected_client_request = f'["take-turn", [{json.dumps(json_state)}, []]]'

        with patch.object(rpp, '_RemotePlayerProxy__send_message') as mock:
            with self.assertRaises(JsonDecodeException):
                action = rpp.get_action(state)
                mock.assert_called_with(expected_client_request)
                self.assertEqual(action, None)
Example #5
0
    def test_get_next_state_success(self):
        # Test successful get next state
        json_obj = {
            'players': [{
                'score': 0,
                'places': [[1, 0], [2, 0], [0, 0]],
                'color': 'white'
            }, {
                'score': 0,
                'places': [[1, 1], [2, 1], [3, 1]],
                'color': 'black'
            }, {
                'score': 0,
                'places': [[0, 1], [1, 2], [3, 2]],
                'color': 'brown'
            }],
            'board': [[4, 4, 4], [4, 4, 4], [4, 4, 4], [4, 4, 4]]
        }

        # Initialize state from json object
        state = initialize_state(json_obj)

        # From position and to position for a valid action
        pos1 = Position(2, 0)
        pos2 = Position(3, 0)

        # Get next state
        next_state = _get_next_state(state, pos1, pos2)

        expected_state = {
            'players': [{
                'score': 0,
                'places': [[1, 1], [2, 1], [3, 1]],
                'color': 'black'
            }, {
                'score': 0,
                'places': [[0, 1], [1, 2], [3, 2]],
                'color': 'brown'
            }, {
                'score': 4,
                'places': [[1, 0], [3, 0], [0, 0]],
                'color': 'white'
            }],
            'board': [[4, 4, 4], [4, 4, 4], [0, 4, 4], [4, 4, 4]]
        }

        self.assertDictEqual(_state_to_json(next_state), expected_state)
Example #6
0
    def test_state_to_json_success1(self):
        # Successful state to json. State is one in which
        # all avatars have been placed
        state = State(Board.homogeneous(4, 4, 3), [
            PlayerEntity('bob', Color.WHITE),
            PlayerEntity('bob', Color.BROWN),
            PlayerEntity('bob', Color.BLACK)
        ])

        # Player 1 place
        state.place_avatar(Color.WHITE, Position(0, 1))
        # Player 2 place
        state.place_avatar(Color.BROWN, Position(1, 0))
        # Player 3 place
        state.place_avatar(Color.BLACK, Position(1, 1))
        # Player 1 place
        state.place_avatar(Color.WHITE, Position(1, 2))
        # Player 2 place
        state.place_avatar(Color.BROWN, Position(2, 0))
        # Player 3 place
        state.place_avatar(Color.BLACK, Position(2, 1))
        # Player 1 place
        state.place_avatar(Color.WHITE, Position(2, 2))
        # Player 2 place
        state.place_avatar(Color.BROWN, Position(3, 1))
        # Player 3 place
        state.place_avatar(Color.BLACK, Position(3, 2))

        # Get jsonified version
        result = _state_to_json(state)

        # Make up expected json
        expected_json = {
            'players': [
                {'score': 0, 'places': [[0, 1], [1, 2], [2, 2]], 'color': 'white'},
                {'score': 0, 'places': [[1, 0], [2, 0], [3, 1]], 'color': 'brown'},
                {'score': 0, 'places': [[1, 1], [2, 1], [3, 2]], 'color': 'black'}
            ],
            'board': [[4, 4, 4], [4, 4, 4], [4, 4, 4], [4, 4, 4]]
        }

        self.assertDictEqual(result, expected_json)
Example #7
0
    def test_state_to_json_success3(self):
        # Successful state to json. State is one in which
        # no avatars have been placed
        state = State(Board.homogeneous(4, 4, 3), [
            PlayerEntity('bob', Color.WHITE),
            PlayerEntity('bob', Color.BROWN),
            PlayerEntity('bob', Color.BLACK)
        ])

        # Get jsonified version
        result = _state_to_json(state)

        # Make up expected json
        expected_json = {
            'players': [
                {'score': 0, 'places': [], 'color': 'white'},
                {'score': 0, 'places': [], 'color': 'brown'},
                {'score': 0, 'places': [], 'color': 'black'}
            ],
            'board': [[4, 4, 4], [4, 4, 4], [4, 4, 4], [4, 4, 4]]
        }

        self.assertDictEqual(result, expected_json)
Example #8
0
 def encode_take_turn(self, state: State, actions: [Action]) -> str:
     json_actions = [self.action_to_json(action) for action in actions]
     msg = ['take-turn', [_state_to_json(state), json_actions]]
     return json.dumps(msg)
Example #9
0
 def encode_setup(self, state: State) -> str:
     msg = ['setup', [_state_to_json(state)]]
     return json.dumps(msg)
Example #10
0
 def test_state_to_json_fail1(self):
     # Fails due to state provided not being State
     with self.assertRaises(TypeError):
         _state_to_json('blah')