示例#1
0
def test_pawn_no_valid_moves():
    board = Board()
    board.board[1][0] = Pawn(1, 0, 'b')
    white_pawn = Pawn(2, 0, 'w')
    white_pawn.valid_moves(board)
    actual = white_pawn.move_list
    expected = []
    assert actual == expected
示例#2
0
def test_move_method_for_pawn():
    board = Board()
    pawn = Pawn(6, 1, 'w')
    pawn.valid_moves(board)
    board.board[6][1] = pawn
    board.move([6, 1], [4, 1])
    actual1 = isinstance(board.board[4][1], Pawn)
    expected1 = True
    actual2 = isinstance(board.board[6][1], Pawn)
    expected2 = False
    actual3 = board.board[4][1].move_list
    expected3 = [[3, 1]]
    actual4 = board.player_turn
    expected4 = 'b'
    assert actual1 == expected1
    assert actual2 == expected2
    assert actual3 == expected3
    assert actual4 == expected4
示例#3
0
def test_capture_a_piece():
    board = Board()
    w_pawn = Pawn(6, 1, 'w')
    b_knight = Knight(5, 2, 'b')
    board.board[6][1] = w_pawn
    board.board[5][2] = b_knight
    w_pawn.valid_moves(board)
    board.move([6, 1], [5, 2])
    print(w_pawn.attack_list)
    actual1 = isinstance(board.board[5][2], Pawn)
    expected1 = True
    actual2 = isinstance(board.board[6][1], Pawn)
    expected2 = False
    actual3 = board.board[5][2].move_list
    expected3 = [[4, 2]]
    actual4 = board.player_turn
    expected4 = 'b'
    assert actual1 == expected1
    assert actual2 == expected2
    assert actual3 == expected3
    assert actual4 == expected4
示例#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