Example #1
0
def test_knight_valid_start_moves():
    board = Board()
    board.reset_pieces()
    knight = Knight(7, 3, 'w')
    knight.valid_moves(board)
    actual = knight.move_list
    expected = [[5, 2], [5, 4]]
Example #2
0
def test_knight_valid_moves():
    board = Board()
    knight = Knight(4, 4, 'w')
    knight.valid_moves(board)
    actual = knight.move_list
    expected = [[2, 3], [2, 5], [3, 2], [3, 6], [6, 3], [6, 5], [5, 2], [5, 6]]
    assert actual == expected
Example #3
0
def test_knight_valid_attack():
    board = Board()
    knight = Knight(4, 4, 'w')
    board.board[6][5] = Pawn(6, 5, 'b')
    board.board[6][3] = Pawn(6, 3, 'b')
    board.board[5][6] = Pawn(5, 6, 'b')
    knight.valid_moves(board)
    actual = knight.attack_list
    expected = [[6, 3], [6, 5], [5, 6]]
    assert actual == expected
Example #4
0
    def is_controlled_sq(self, req_pos, turn):
        '''
        Checks whether a square is controlled by a piece.
        'turn' means controlled by piece of which colour/turn
        '''

        # Along knight routes (L)
        knight = Knight(0)
        knight.set_pos(req_pos)
        # print(self.get_sq_notation(req_pos))
        # print()
        for move in knight.valid_moves():
            # print(self.get_sq_notation(move), end=': ')
            piece = self.fetch_piece(move)
            # print(piece)
            if piece and type(piece).__name__ == 'Knight' and piece.colour == turn:
                # self.pos_of_piece_causing_check = move
                # print('yeah')
                del knight
                return True

        # Along diagonals
        bishop = Bishop(0)
        bishop.set_pos(req_pos)
        for move in bishop.valid_moves():
            piece = self.fetch_piece(move)

            if piece and \
            (type(piece).__name__ == 'Bishop' or
            type(piece).__name__ == 'Queen') and \
            piece.colour == turn and \
            not piece.is_path_obstructed(self, req_pos, move):
                # print('yes')
                # self.pos_of_piece_causing_check = move
                del bishop
                # print('Obstruct')
                return True

        # Along ranks and files
        rook = Rook(0)
        rook.set_pos(req_pos)
        for move in rook.valid_moves():
            # print(self.get_sq_notation(move), end=": ")
            piece = self.fetch_piece(move)
            # print(piece)

            if piece and (type(piece).__name__ == 'Rook' or
            type(piece).__name__ == 'Queen') and \
            piece.colour == turn and \
            not piece.is_path_obstructed(self, req_pos, move):
                # self.pos_of_piece_causing_check = move
                # print('here')
                del rook
                return True

        # King opposition
        king = King()
        king.set_pos(req_pos)
        for move in king.valid_moves():
            piece = self.fetch_piece(move)
            if piece and type(piece).__name__ == 'King' and piece.colour == turn:
                if not self.is_controlled_sq(move, self.turn):
                    # print('here')
                    # self.pos_of_piece_causing_check = move
                    del king
                    return True

        # Pawns
        if turn == 'White':
            pawn = Pawn(colour='Black')
        else:
            pawn = Pawn()
        pawn.set_pos(req_pos)
        for move in pawn.valid_moves(self.is_flipped):
            # If directly in front/back
            if move[0] - req_pos[0] == 0:
                continue
            piece = self.fetch_piece(move)
            if piece and type(piece).__name__ == 'Pawn' and piece.colour == turn:
                # self.pos_of_piece_causing_check = move
                del pawn
                return True

        del knight, bishop, rook, king, pawn

        # print('Nope')
        return False