Пример #1
0
    def test_generate_pieces(self):
        pieces = generate_pieces()

        w_pieces = [p for p in pieces if p.color == c.Color.white]
        b_pieces = [p for p in pieces if p.color == c.Color.black]
        self.assertTrue(len(w_pieces) == len(b_pieces) == 16)

        w_pawns = [p for p in w_pieces if p.type == c.PieceType.pawn]
        b_pawns = [p for p in b_pieces if p.type == c.PieceType.pawn]
        self.assertTrue(len(w_pawns) == len(b_pawns) == 8)

        w_knights = [p for p in w_pieces if p.type == c.PieceType.knight]
        b_knights = [p for p in b_pieces if p.type == c.PieceType.knight]
        self.assertTrue(len(w_knights) == len(b_knights) == 2)

        w_bishops = [p for p in w_pieces if p.type == c.PieceType.bishop]
        b_bishops = [p for p in b_pieces if p.type == c.PieceType.bishop]
        self.assertTrue(len(w_bishops) == len(b_bishops) == 2)

        w_bishops = [p for p in w_pieces if p.type == c.PieceType.bishop]
        b_bishops = [p for p in b_pieces if p.type == c.PieceType.bishop]
        self.assertTrue(len(w_bishops) == len(b_bishops) == 2)

        w_rooks = [p for p in w_pieces if p.type == c.PieceType.rook]
        b_rooks = [p for p in b_pieces if p.type == c.PieceType.rook]
        self.assertTrue(len(w_rooks) == len(b_rooks) == 2)

        w_queens = [p for p in w_pieces if p.type == c.PieceType.queen]
        b_queens = [p for p in b_pieces if p.type == c.PieceType.queen]
        self.assertTrue(len(w_queens) == len(b_queens) == 1)

        w_kings = [p for p in w_pieces if p.type == c.PieceType.king]
        b_kings = [p for p in b_pieces if p.type == c.PieceType.king]
        self.assertTrue(len(w_kings) == len(b_kings) == 1)
Пример #2
0
def generate_piece(piece_type, color, order=0):
    all_pieces = generate_pieces()
    required_pieces = [
        p for p in all_pieces
        if p.type == piece_type and p.color == color and p.order == order
    ]
    assert len(required_pieces) == 1
    return required_pieces[0]
Пример #3
0
 def test_uid(self):
     expected_result = [
         '0bb', '0bw', '0kb', '0kw', '0nb', '0nw', '0pb', '0pw', '0qb',
         '0qw', '0rb', '0rw', '1bb', '1bw', '1nb', '1nw', '1pb', '1pw',
         '1rb', '1rw', '2pb', '2pw', '3pb', '3pw', '4pb', '4pw', '5pb',
         '5pw', '6pb', '6pw', '7pb', '7pw'
     ]
     pieces = generate_pieces()
     uids = sorted([p.uid for p in pieces])
     self.assertEqual(uids, expected_result)
Пример #4
0
 def test_is_orthogonal(self):
     for piece in generate_pieces():
         for move_data in moves_for_testing(piece):
             src_address, dst_address = move_data.move
             src = Square(src_address)
             dst = Square(dst_address)
             m = Move(
                 piece=piece,
                 src=src,
                 dst=dst,
             )
             self.assertEqual(m.is_orthogonal, move_data.is_orthogonal)
Пример #5
0
 def test_first_row(self):
     pieces = generate_pieces()
     expected_result = None
     for p in pieces:
         if p.color == c.Color.black:
             expected_result = 7
             if p.type == c.PieceType.pawn:
                 expected_result = 6
         else:
             expected_result = 0
             if p.type == c.PieceType.pawn:
                 expected_result = 1
         self.assertEqual(p.first_row, expected_result)
Пример #6
0
    def test_get_pieces_row_place(self):
        places = {
            (c.PieceType.rook, 0): 0,
            (c.PieceType.knight, 0): 1,
            (c.PieceType.bishop, 0): 2,
            (c.PieceType.queen, 0): 3,
            (c.PieceType.king, 0): 4,
            (c.PieceType.bishop, 1): 5,
            (c.PieceType.knight, 1): 6,
            (c.PieceType.rook, 1): 7,
        }

        for piece in generate_pieces():
            if piece.type == c.PieceType.pawn:
                expected_result = piece.order
            else:
                expected_result = places[(piece.type, piece.order)]
            row_place = get_piece_row_place(piece)
            self.assertEqual(row_place, expected_result)
Пример #7
0
 def test_repr(self):
     for piece in generate_pieces():
         for move_data in moves_for_testing(piece):
             src_address, dst_address = move_data.move
             src = Square(src_address)
             dst = Square(dst_address)
             m = Move(
                 piece=piece,
                 src=src,
                 dst=dst,
             )
             expected_result = ('<'
                                f'{m.__class__.__name__}'
                                ': '
                                f'{piece.code}'
                                f'{piece.order}'
                                f'({piece.color_code}) '
                                f'{src.address}'
                                f' - '
                                f'{dst.address}'
                                '>')
             self.assertEqual(repr(m), expected_result)
Пример #8
0
    def test_is_legal(self):
        for piece in generate_pieces():
            for move_data in moves_for_testing(piece):
                src_address, dst_address = move_data.move
                src = Square(src_address)
                dst = Square(dst_address)
                m = Move(
                    piece=piece,
                    src=src,
                    dst=dst,
                )

                assert_to_use = (self.assertTrue
                                 if move_data.is_legal else self.assertFalse)
                try:
                    assert_to_use(m.is_legal)
                except AssertionError:
                    error_msg = (f'{piece}:\n'
                                 f'{src.address} -> {dst.address}\n'
                                 f'move_data.is_legal: {move_data.is_legal}\n'
                                 f'm.is_legal: {m.is_legal}\n'
                                 f'(x, y): ({dst.x - src.x}, {dst.y - src.y})')
                    print(error_msg)
                    raise
Пример #9
0
 def test_move_path(self):
     expected_paths = {
         c.PieceType.pawn: (((-1, 1), ), ((0, 1), ), ((1, 1), )),
         c.PieceType.knight: (
             ((1, 2), ),
             ((1, -2), ),
             ((-1, 2), ),
             ((-1, -2), ),
             ((2, 1), ),
             ((2, -1), ),
             ((-2, 1), ),
             ((-2, -1), ),
         ),
         c.PieceType.bishop: (
             (
                 (-1, -1),
                 (-2, -2),
                 (-3, -3),
                 (-4, -4),
                 (-5, -5),
                 (-6, -6),
                 (-7, -7),
             ),
             (
                 (1, 1),
                 (2, 2),
                 (3, 3),
                 (4, 4),
                 (5, 5),
                 (6, 6),
                 (7, 7),
             ),
             (
                 (-1, 1),
                 (-2, 2),
                 (-3, 3),
                 (-4, 4),
                 (-5, 5),
                 (-6, 6),
                 (-7, 7),
             ),
             (
                 (1, -1),
                 (2, -2),
                 (3, -3),
                 (4, -4),
                 (5, -5),
                 (6, -6),
                 (7, -7),
             ),
         ),
         c.PieceType.rook: (
             ((0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7)),
             ((0, -1), (0, -2), (0, -3), (0, -4), (0, -5), (0, -6), (0,
                                                                     -7)),
             ((1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0)),
             ((-1, 0), (-2, 0), (-3, 0), (-4, 0), (-5, 0), (-6, 0), (-7,
                                                                     0)),
         ),
         c.PieceType.queen: (
             (
                 (-1, -1),
                 (-2, -2),
                 (-3, -3),
                 (-4, -4),
                 (-5, -5),
                 (-6, -6),
                 (-7, -7),
             ),
             (
                 (1, 1),
                 (2, 2),
                 (3, 3),
                 (4, 4),
                 (5, 5),
                 (6, 6),
                 (7, 7),
             ),
             (
                 (-1, 1),
                 (-2, 2),
                 (-3, 3),
                 (-4, 4),
                 (-5, 5),
                 (-6, 6),
                 (-7, 7),
             ),
             (
                 (1, -1),
                 (2, -2),
                 (3, -3),
                 (4, -4),
                 (5, -5),
                 (6, -6),
                 (7, -7),
             ),
             ((0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7)),
             ((0, -1), (0, -2), (0, -3), (0, -4), (0, -5), (0, -6), (0,
                                                                     -7)),
             ((1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0)),
             ((-1, 0), (-2, 0), (-3, 0), (-4, 0), (-5, 0), (-6, 0), (-7,
                                                                     0)),
         ),
         c.PieceType.king: (
             ((-1, 1), ),
             ((0, 1), ),
             ((1, 1), ),
             ((1, 0), ),
             ((1, -1), ),
             ((0, -1), ),
             ((-1, -1), ),
             ((-1, 0), ),
         ),
     }
     for p in generate_pieces():
         expected_result = expected_paths[p.type]
         self.assertEqual(p.move_paths, expected_result)