コード例 #1
0
    def test_get_valid_moves(self):
        board = blokus.Board()
        player = board.players[0]
        unplayed_piece = blokus.Piece(np.array([
            [1, 0],
            [1, 1],
            [1, 1]
        ]), player)

        player.unplayed_pieces = [ unplayed_piece ]

        valid_moves = player.get_valid_moves(board)
        self.assertEqual(len(valid_moves), 6)

        invalid_move = np.array([
            [0, 1],
            [1, 1],
            [1, 1]
        ])

        self.assertTrue(all([not np.array_equal(invalid_move, move) for move in valid_moves]))

        invalid_move = np.array([
            [0, 1, 1],
            [1, 1, 1]
        ])

        self.assertTrue(all([not np.array_equal(invalid_move, move) for move in valid_moves]))

        move_positions = map(lambda x: x._position, valid_moves)
        self.assertTrue(all([position == (0, 0) for position in move_positions]))
コード例 #2
0
    def test_get_size(self, player):
        raw_piece = np.array([
            [1, 1],
            [1, 0]
        ])

        piece = blokus.Piece(raw_piece, player)
        self.assertEqual(piece.get_size(), 3)

        raw_piece = np.array([
            [1, 0],
            [1, 1],
            [1, 0],
            [1, 0]
        ])

        piece = blokus.Piece(raw_piece, player)
        self.assertEqual(piece.get_size(), 5)
コード例 #3
0
    def test_initialization(self, player):
        raw_piece = np.array([
            [1, 0],
            [1, 1],
            [1, 0],
            [1, 0]
        ])

        piece = blokus.Piece(raw_piece, player)

        self.assertEqual(piece.get_orientation_count(), 8)
        self.assertEqual(piece.player, player)
        self.assertEqual(len(piece._anchors), 8)

        rotated_once = np.array([
            [1, 1, 1, 1],
            [0, 0, 1, 0]
        ])

        self.assertTrue(np.array_equal(piece.orientations[1], rotated_once))

        expected_anchors = [ (0, 0), (3, 0), (2, 1) ]
        self._validate_anchors(rotated_once, expected_anchors)

        rotated_thrice_flipped = np.array([
            [1, 1, 1, 1],
            [0, 1, 0, 0]
        ])

        self.assertTrue(np.array_equal(piece.orientations[2], rotated_thrice_flipped))

        expected_anchors = [ (0, 0), (3, 0), (1, 1) ]
        self._validate_anchors(rotated_thrice_flipped, expected_anchors)

        raw_piece = np.array([
            [1, 1],
            [1, 1]
        ])

        piece = blokus.Piece(raw_piece, player)

        self.assertEqual(piece.get_orientation_count(), 1)
        self.assertEqual(len(piece._anchors), 1)
        self.assertEqual(len(piece.get_anchors(orientation=0)), 4)
コード例 #4
0
    def test_is_played(self):
        player = blokus.RedPlayer()
        piece = blokus.Piece(np.array([
            [1, 1, 1]
        ]), player)

        player.unplayed_pieces = [ piece ]

        self.assertFalse(piece.is_played)

        player.play_piece(piece)

        self.assertTrue(piece.is_played)
コード例 #5
0
    def test_initialization(self, player, board):
        piece = blokus.Piece(np.array([[ 1 ]]), player)

        with self.assertRaises(ValueError):
            # The above piece only has 1 valid orientation
            move = blokus.Move(board, piece, (0, 0), orientation=1)

        move = blokus.Move(board, piece, (5, 3), orientation=0)

        self.assertEqual(move._board, board)
        self.assertEqual(move._piece, piece)
        self.assertEqual(move._position, (5, 3))
        self.assertEqual(move._orientation, 0)
        self.assertEqual(move.x, 5)
        self.assertEqual(move.y, 3)
        self.assertEqual(move.player, player)
コード例 #6
0
    def test_get_piece_matrix(self, player, board):
        piece = blokus.Piece(np.array([
            [1, 1, 1],
            [1, 0, 0],
            [1, 0, 0]
        ]), player)

        move = blokus.Move(board, piece, (10, 15), orientation=3)
        matrix = move.get_piece_matrix()

        expected_matrix = np.array([
            [1, 0, 0],
            [1, 0, 0],
            [1, 1, 1],
        ])

        self.assertTrue(np.array_equal(matrix, expected_matrix))
コード例 #7
0
    def test_get_blocks_affected(self):
        board = blokus.Board()
        player = board.players[0]

        piece = blokus.Piece(np.array([
            [1, 1, 1],
            [0, 1, 0],
            [0, 1, 0]
        ]), player)

        move = blokus.Move(board, piece, (13, 6))
        expected = [
            board.get_block((13, 6)),
            board.get_block((14, 6)),
            board.get_block((15, 6)),
            board.get_block((14, 7)),
            board.get_block((14, 8))
        ]

        self.assertEqual(set(move.get_blocks_affected()), set(expected))

        # [1, 0, 0]
        # [1, 1, 1]
        # [1, 0, 0]
        move = blokus.Move(board, piece, (5, 9), orientation=2)
        expected = [
            board.get_block((5, 9)),
            board.get_block((5, 10)),
            board.get_block((6, 10)),
            board.get_block((7, 10)),
            board.get_block((5, 11))
        ]

        self.assertEqual(set(move.get_blocks_affected()), set(expected))

        move = blokus.Move(board, piece, (-1, 0))
        expected = [
            None,
            board.get_block((0, 0)),
            board.get_block((1, 0)),
            board.get_block((0, 1)),
            board.get_block((0, 2))
        ]

        self.assertEqual(set(move.get_blocks_affected()), set(expected))
コード例 #8
0
    def test_is_valid(self):
        '''
        A valid move is a move where:
            1. At least 1 block for the move is an anchor for the player
            2. All blocks allow the player
            3. The piece isn't falling out of bounds
        '''
        board = blokus.Board()
        player = board.players[0]

        piece = blokus.Piece(np.array([
            [1, 1],
            [1, 0]
        ]), player)

        player.unplayed_pieces.append(piece)

        move = blokus.Move(board, piece, (0, 0))
        self.assertTrue(move.is_valid())

        # Invalid orientation, does not line up with anchor
        # [0, 1]
        # [1, 1]
        move = blokus.Move(board, piece, (0, 0), orientation=2)
        self.assertFalse(move.is_valid())

        # Piece is falling off the board
        move = blokus.Move(board, piece, (0, -1))
        self.assertFalse(move.is_valid())
        move = blokus.Move(board, piece, (-1, 0))
        self.assertFalse(move.is_valid())

        board.get_block((1, 0)).disallow_player(player)

        # Piece covers a block where the player is not allowed
        move = blokus.Move(board, piece, (0, 0))
        self.assertFalse(move.is_valid())

        # Piece has already been played
        with patch('blokus.Piece') as played_piece:
            played_piece.is_played = True
            move = blokus.Move(board, played_piece, (0, 0))
            self.assertFalse(move.is_valid())
コード例 #9
0
    def test_get_anchors(self, player):
        raw_piece = np.array([
            [0, 1, 1],
            [1, 1, 0],
            [0, 1, 0]
        ])

        piece = blokus.Piece(raw_piece, player)

        expected = [ (0, 1), (1, 0), (2, 0), (1, 2) ]
        anchors = piece.get_anchors(orientation=0)

        self.assertEqual(set(anchors), set(expected))

        expected = [ (0, 1), (1, 0), (1, 2), (2, 2) ]
        anchors = piece.get_anchors(orientation=4)

        self.assertEqual(set(anchors), set(expected))

        self.assertIsNone(piece.get_anchors(orientation=-1))
        self.assertIsNone(piece.get_anchors(orientation=8))
        self.assertIsNotNone(piece.get_anchors(orientation=7))
コード例 #10
0
    def test_make_move(self):
        board = blokus.Board()

        with patch('blokus.Move') as invalid_move:
            invalid_move.is_valid.return_value = False

            with self.assertRaises(RuntimeError):
                board.make_move(invalid_move)

        player = board.players[2]
        piece = blokus.Piece(np.array([
            [1, 1, 1],
            [0, 1, 0],
            [0, 1, 0]
        ]), player)

        player.unplayed_pieces = [ piece ]

        # Player 2's start position is (19, 19)
        # [0, 0, 1]
        # [1, 1, 1]
        # [0, 0, 1]
        move = blokus.Move(board, piece, (17, 17), orientation=1)
        board.make_move(move)

        self.assertNotIn(move._piece, player.unplayed_pieces)

        self._verify_make_move(
            board=board,
            player=player,
            expected_assigned=set([ (19, 17), (19, 18), (19, 19), (17, 18), (18, 18) ]),
            expected_anchors=set([ (18, 16), (16, 17), (16, 19) ]),
            expected_disallowed=set([ (19, 16), (18, 17), (17, 17), (18, 19), (17, 19), (16, 18) ])
        )

        piece_2 = blokus.Piece(np.array([
            [1, 1],
            [1, 0],
            [1, 1]
        ]), player)

        player.unplayed_pieces = [ piece_2 ]

        move = blokus.Move(board, piece_2, (15, 17))
        board.make_move(move)

        self._verify_make_move(
            board=board,
            player=player,
            expected_assigned=set([
                (19, 17), (19, 18), (19, 19), (17, 18), (18, 18),
                (16, 17), (15, 17), (15, 18), (15, 19), (16, 19)
            ]),
            expected_anchors=set([
                (18, 16), (17, 16), (14, 16)
            ]),
            expected_disallowed=set([
                (19, 16), (18, 17), (17, 17), (18, 19), (17, 19), (16, 18),
                (14, 19), (14, 18), (14, 17), (15, 16), (16, 16)
            ])
        )