0
0
LLDsystem_design~10 mins

Move validation and check detection in LLD - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a move is valid based on piece rules.

LLD
def is_valid_move(piece, start, end):
    return piece.can_move_to([1])
Drag options to blanks, or click blank then click option'
Aboard
Bstart
Cpiece
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using the start position instead of the end position for validation.
2fill in blank
medium

Complete the code to detect if the king is in check after a move.

LLD
def is_king_in_check(board, color):
    king_pos = board.find_king([1])
    return board.is_under_attack(king_pos, color)
Drag options to blanks, or click blank then click option'
Acolor
Bboard
Cking_pos
Dopponent_color
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the board or opponent color instead of the player's color.
3fill in blank
hard

Fix the error in the code that simulates a move to check for check detection.

LLD
def move_causes_check(board, move, color):
    board_copy = board.copy()
    board_copy.apply_move(move)
    return board_copy.is_king_in_check([1])
Drag options to blanks, or click blank then click option'
Acolor
Bmove.piece.color
Cboard_copy
Dmove
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the move or piece color instead of the player's color.
4fill in blank
hard

Fill both blanks to create a function that validates a move and checks if it leaves the king safe.

LLD
def validate_move(board, move, color):
    if not board.is_valid_move([1]):
        return False
    if move_causes_check(board, move, [2]):
        return False
    return True
Drag options to blanks, or click blank then click option'
Amove
Bboard
Ccolor
Dmove.piece
Attempts:
3 left
💡 Hint
Common Mistakes
Using board instead of move for validation.
Using move.piece instead of color for check detection.
5fill in blank
hard

Fill all three blanks to implement a function that filters legal moves from all possible moves.

LLD
def get_legal_moves(board, color):
    possible_moves = board.get_all_moves([1])
    legal_moves = [move for move in possible_moves if validate_move([2], move, [3])]
    return legal_moves
Drag options to blanks, or click blank then click option'
Acolor
Bboard
Dmove
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up move and color in the list comprehension.
Passing wrong parameters to validate_move.