Beispiel #1
0
 def _rook_moves(self, square: Square) -> List[List[Square]]:
     moves = []
     original = self.get_square(square=square)
     color = original.piece.color
     for func in (Square.up, Square.down, Square.left, Square.right):
         line = []
         square = original
         while True:
             square = self.get_square(square=func(square))
             if square == Square('00'):
                 break
             if not square.piece and square != Square('00'):
                 line.append(square)
             elif square.piece.color is not color:
                 line.append(square)
                 break
             else:
                 break
         if len(line) >= 1:
             moves.append(line.copy())
     return moves
Beispiel #2
0
 def _bishop_moves(self, square: Square) -> List[List[Square]]:
     moves = []
     original = self.get_square(square=square)
     color = original.piece.color
     for func1 in (Square.up, Square.down):
         for func2 in (Square.left, Square.right):
             diagonal = []
             square = original
             while True:
                 square = self.get_square(square=func2(func1(square)))
                 if square == Square('00'):
                     break
                 if not square.piece and square != Square('00'):
                     diagonal.append(square)
                 elif square.piece.color is not color:
                     diagonal.append(square)
                     break
                 else:
                     break
             if len(diagonal) >= 1:
                 moves.append(diagonal.copy())
     return moves
Beispiel #3
0
 def test_king_moves(self):
     self.set_test_position_1()
     # white king
     self.assertEqual(self.board._king_moves(Square('g1')),
                      [[Square('g2')], [Square('h1')], [Square('f1')]])
     # black king
     self.assertEqual(self.board._king_moves(Square('g8')),
                      [[Square('h8')]])
Beispiel #4
0
    def test_not_in_check(self):
        self.board.set_board_state([
            '........', '.....B..', '.K.k....', 'r.......', '........',
            '........', '........', '........'
        ])

        self.assertRaises(
            InvalidNotationError,
            lambda: Move(self.board, Color.BLACK, Square('b6'), Square('c6')))
        self.assertRaises(
            InvalidNotationError,
            lambda: Move(self.board, Color.WHITE, Square('d6'), Square('c6')))
        self.assertRaises(
            InvalidNotationError,
            lambda: Move(self.board, Color.WHITE, Square('d6'), Square('e6')))
        self.assertRaises(
            InvalidNotationError,
            lambda: Move(self.board, Color.BLACK, Square('b6'), Square('b5')))
Beispiel #5
0
 def _king_moves(self, square: Square) -> List[List[Square]]:
     original = self.get_square(square=square)
     color = original.piece.color
     moves = [
         self.get_square(square=original.up().left()),
         self.get_square(square=original.up()),
         self.get_square(square=original.up().right()),
         self.get_square(square=original.right()),
         self.get_square(square=original.right().down()),
         self.get_square(square=original.down()),
         self.get_square(square=original.down().left()),
         self.get_square(square=original.left())
     ]
     possible_moves = []
     for move in moves:
         if move != Square('00') and (not move.piece or move.piece.color is not color):
             possible_moves.append([move])
     return possible_moves
Beispiel #6
0
    def test_en_passant(self):
        self.board.set_board_state([
            '........', '........', '........', '..Pp....', '.....PpP',
            '........', '........', '........'
        ])
        m1 = Move(self.board,
                  Color.WHITE,
                  Square('d5'),
                  Square('c6'),
                  en_passant=True)
        m2 = Move(self.board,
                  Color.BLACK,
                  Square('f4'),
                  Square('g3'),
                  en_passant=True)
        m3 = Move(self.board,
                  Color.BLACK,
                  Square('h4'),
                  Square('g3'),
                  en_passant=True)

        self.assertEqual(m1.piece_captured, Piece(PieceType.PAWN, Color.BLACK))
        self.assertEqual(m2.piece_captured, Piece(PieceType.PAWN, Color.WHITE))
        self.assertEqual(m3.piece_captured, Piece(PieceType.PAWN, Color.WHITE))
 def setUp(self):
     self.square = Square('a1')
Beispiel #8
0
 def test_rook_moves(self):
     self.set_test_position_1()
     # white rooks
     self.assertEqual(self.board._rook_moves(Square('b1')),
                      [[Square('a1')], [Square('c1')]])
     self.assertEqual(self.board._rook_moves(Square('e1')),
                      [[Square('e2'), Square('e3')], [Square('f1')]])
     # black rooks
     self.assertEqual(self.board._rook_moves(Square('a8')),
                      [[Square('b8'), Square('c8')]])
     self.assertEqual(self.board._rook_moves(Square('f8')),
                      [[Square('e8')]])
 def test_right(self):
     self.assertEqual(self.square.right(), Square('b1'))
     self.assertEqual(Square('b1').right(), Square('c1'))
     self.assertEqual(Square('c1').right(), Square('d1'))
     self.assertEqual(Square('d1').right(), Square('e1'))
     self.assertEqual(Square('e1').right(), Square('f1'))
     self.assertEqual(Square('f1').right(), Square('g1'))
     self.assertEqual(Square('g1').right(), Square('h1'))
     self.assertEqual(Square('h1').right(), Square('00'))
Beispiel #10
0
 def test_down(self):
     self.assertEqual(Square('a8').down(), Square('a7'))
     self.assertEqual(Square('a7').down(), Square('a6'))
     self.assertEqual(Square('a6').down(), Square('a5'))
     self.assertEqual(Square('a5').down(), Square('a4'))
     self.assertEqual(Square('a4').down(), Square('a3'))
     self.assertEqual(Square('a3').down(), Square('a2'))
     self.assertEqual(Square('a2').down(), Square('a1'))
     self.assertEqual(self.square.down(), Square('00'))
Beispiel #11
0
 def test_up(self):
     self.assertEqual(self.square.up(), Square('a2'))
     self.assertEqual(Square('a2').up(), Square('a3'))
     self.assertEqual(Square('a3').up(), Square('a4'))
     self.assertEqual(Square('a4').up(), Square('a5'))
     self.assertEqual(Square('a5').up(), Square('a6'))
     self.assertEqual(Square('a6').up(), Square('a7'))
     self.assertEqual(Square('a7').up(), Square('a8'))
     self.assertEqual(Square('a8').up(), Square('00'))
Beispiel #12
0
    def test_pawn_moves(self):
        # TODO: en passant
        self.set_test_position_1()
        # white pawns
        self.assertEqual(self.board._pawn_moves(Square('a2')),
                         [[Square('a3'), Square('a4')]])
        self.assertEqual(self.board._pawn_moves(Square('b2')),
                         [[Square('b3'), Square('b4')]])
        self.assertEqual(self.board._pawn_moves(Square('c3')),
                         [[Square('c4')], [Square('d4')]])
        self.assertEqual(self.board._pawn_moves(Square('d3')), [])
        self.assertEqual(self.board._pawn_moves(Square('e4')),
                         [[Square('e5')]])
        self.assertEqual(self.board._pawn_moves(Square('f5')),
                         [[Square('f6')], [Square('e6')]])
        self.assertEqual(self.board._pawn_moves(Square('g5')), [])
        self.assertEqual(self.board._pawn_moves(Square('h2')),
                         [[Square('h3'), Square('h4')]])
        self.assertEqual(self.board._pawn_moves(Square('c5')), [])

        # black pawns
        self.assertEqual(self.board._pawn_moves(Square('a7')), [])
        self.assertEqual(self.board._pawn_moves(Square('b7')),
                         [[Square('b6')], [Square('a6')]])
        self.assertEqual(self.board._pawn_moves(Square('c6')),
                         [[Square('b5')]])
        self.assertEqual(self.board._pawn_moves(Square('d7')),
                         [[Square('d6'), Square('d5')]])
        self.assertEqual(self.board._pawn_moves(Square('f7')),
                         [[Square('f6')], [Square('g6')]])
        self.assertEqual(self.board._pawn_moves(Square('g7')), [])
        self.assertEqual(self.board._pawn_moves(Square('h7')),
                         [[Square('h6'), Square('h5')], [Square('g6')]])
Beispiel #13
0
 def test_board_construction(self):
     self.assertEqual(len(self.board.squares), 8)
     self.assertEqual(len(self.board.squares[0]), 8)
     self.assertEqual(self.board.squares[0][0], Square('a1'))
     self.assertEqual(self.board.squares[7][7], Square('h8'))
Beispiel #14
0
 def test_queen_moves(self):
     self.set_test_position_1()
     # white queen
     self.assertEqual(
         self.board._queen_moves(Square('d1')),
         [[Square('d2')], [Square('c1')],
          [Square('c2'), Square('b3'),
           Square('a4')],
          [Square('e2'),
           Square('f3'),
           Square('g4'),
           Square('h5')]])
     # black queen
     self.assertEqual(
         self.board._queen_moves(Square('e6')),
         [[Square('e7'), Square('e8')], [Square('e5'),
                                         Square('e4')], [Square('d6')],
          [Square('f6'), Square('g6')],
          [Square('d5'),
           Square('c4'),
           Square('b3'),
           Square('a2')], [Square('f5')]])
Beispiel #15
0
 def clear(self):
     """ This method will only be used for debugging purposes
     """
     self.squares = [[Square(file + rank) for file in Square.files] for rank in Square.ranks]
Beispiel #16
0
 def test_left(self):
     self.assertEqual(Square('h8').left(), Square('g8'))
     self.assertEqual(Square('g8').left(), Square('f8'))
     self.assertEqual(Square('f8').left(), Square('e8'))
     self.assertEqual(Square('e8').left(), Square('d8'))
     self.assertEqual(Square('d8').left(), Square('c8'))
     self.assertEqual(Square('c8').left(), Square('b8'))
     self.assertEqual(Square('b8').left(), Square('a8'))
     self.assertEqual(self.square.left(), Square('00'))
Beispiel #17
0
 def __init__(self):
     self.squares: List[List[Square]] = [[Square(file + rank) for file in Square.files] for rank in Square.ranks]
     self.set_up()
Beispiel #18
0
 def test_knight_moves(self):
     self.set_test_position_1()
     # white knights
     self.assertEqual(self.board._knight_moves(Square('a6')),
                      [[Square('b8')], [Square('b4')], [Square('c7')]])
     self.assertEqual(self.board._knight_moves(Square('g6')),
                      [[Square('f8')], [Square('h8')], [Square('f4')],
                       [Square('h4')], [Square('e7')], [Square('e5')]])
     self.assertEqual(self.board._knight_moves(Square('f2')),
                      [[Square('g4')], [Square('h3')], [Square('h1')]])
     # black knights
     self.assertEqual(self.board._knight_moves(Square('d8')), [])