Check if any King is unsafe on the Chessboard or not | Function to check if any of the two kings is unsafe or not ; Find the position of both the kings ; Check for all pieces which can attack White King ; Check for Knight ; Check for Pawn ; Check for Rook ; Check for Bishop ; Check for Queen ; Check for King ; Check for all pieces which can attack Black King ; Check for Knight ; Check for Pawn ; Check for Rook ; Check for Bishop ; Check for Queen ; Check for King ; Store all possible moves of the king ; incrementing index values ; checking boundary conditions and character match ; Function to check if Queen can attack the King ; Queen 's moves are a combination  of both the Bishop and the Rook ; Function to check if bishop can attack the king ; Check the lower right diagonal ; Check the lower left diagonal ; Check the upper right diagonal ; Check the upper left diagonal ; Check if ; Check downwards ; Check upwards ; Check right ; Check left ; Check if the knight can attack the king ; All possible moves of the knight ; Incrementing index values ; Checking boundary conditions and character match ; Function to check if pawn can attack the king ; Check for white pawn ; Check for black pawn ; Check if the indices are within the matrix or not ; Checking boundary conditions ; Chessboard instance
def checkBoard ( board ) : NEW_LINE INDENT for i in range ( 8 ) : NEW_LINE INDENT for j in range ( 8 ) : NEW_LINE INDENT if board [ i ] [ j ] == ' k ' : NEW_LINE INDENT if lookForn ( board , ' N ' , i , j ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if lookForp ( board , ' P ' , i , j ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if lookForr ( board , ' R ' , i , j ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if lookForb ( board , ' B ' , i , j ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if lookForq ( board , ' Q ' , i , j ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if lookFork ( board , ' K ' , i , j ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT if board [ i ] [ j ] == ' K ' : NEW_LINE INDENT if lookForn ( board , ' n ' , i , j ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT if lookForp ( board , ' p ' , i , j ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT if lookForr ( board , ' r ' , i , j ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT if lookForb ( board , ' b ' , i , j ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT if lookForq ( board , ' q ' , i , j ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT if lookFork ( board , ' k ' , i , j ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT DEDENT DEDENT DEDENT return 1 NEW_LINE DEDENT def lookFork ( board , c , i , j ) : NEW_LINE INDENT x = [ - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 ] NEW_LINE y = [ - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 ] NEW_LINE for k in range ( 8 ) : NEW_LINE INDENT m = i + x [ k ] NEW_LINE n = j + y [ k ] NEW_LINE if inBounds ( m , n ) and board [ m ] [ n ] == c : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def lookForq ( board , c , i , j ) : NEW_LINE INDENT if lookForb ( board , c , i , j ) or lookForr ( board , c , i , j ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def lookForb ( board , c , i , j ) : NEW_LINE INDENT k = 0 NEW_LINE while inBounds ( i + + + k , j + k ) : NEW_LINE INDENT if board [ i + k ] [ j + k ] == c : NEW_LINE INDENT return True NEW_LINE DEDENT if board [ i + k ] [ j + k ] != ' - ' : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT k = 0 NEW_LINE while inBounds ( i + + + k , j - k ) : NEW_LINE INDENT if board [ i + k ] [ j - k ] == c : NEW_LINE INDENT return True NEW_LINE DEDENT if board [ i + k ] [ j - k ] != ' - ' : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT k = 0 NEW_LINE while inBounds ( i - + + k , j + k ) : NEW_LINE INDENT if board [ i - k ] [ j + k ] == c : NEW_LINE INDENT return True NEW_LINE DEDENT if board [ i - k ] [ j + k ] != ' - ' : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT k = 0 NEW_LINE while inBounds ( i - + + k , j - k ) : NEW_LINE INDENT if board [ i - k ] [ j - k ] == c : NEW_LINE INDENT return True NEW_LINE DEDENT if board [ i - k ] [ j - k ] != ' - ' : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def lookForr ( board , c , i , j ) : NEW_LINE INDENT k = 0 NEW_LINE while inBounds ( i + + + k , j ) : NEW_LINE INDENT if board [ i + k ] [ j ] == c : NEW_LINE INDENT return True NEW_LINE DEDENT if board [ i + k ] [ j ] != ' - ' : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT k = 0 NEW_LINE while inBounds ( i + - - k , j ) : NEW_LINE INDENT if board [ i + k ] [ j ] == c : NEW_LINE INDENT return True NEW_LINE DEDENT if board [ i + k ] [ j ] != ' - ' : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT k = 0 NEW_LINE while inBounds ( i , j + + + k ) : NEW_LINE INDENT if board [ i ] [ j + k ] == c : NEW_LINE INDENT return True NEW_LINE DEDENT if board [ i ] [ j + k ] != ' - ' : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT k = 0 NEW_LINE while inBounds ( i , j + - - k ) : NEW_LINE INDENT if board [ i ] [ j + k ] == c : NEW_LINE INDENT return True NEW_LINE DEDENT if board [ i ] [ j + k ] != ' - ' : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def lookForn ( board , c , i , j ) : NEW_LINE INDENT x = [ 2 , 2 , - 2 , - 2 , 1 , 1 , - 1 , - 1 ] NEW_LINE y = [ 1 , - 1 , 1 , - 1 , 2 , - 2 , 2 , - 2 ] NEW_LINE for k in range ( 8 ) : NEW_LINE INDENT m = i + x [ k ] NEW_LINE n = j + y [ k ] NEW_LINE if inBounds ( m , n ) and board [ m ] [ n ] == c : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def lookForp ( board , c , i , j ) : NEW_LINE INDENT if ord ( c ) >= 65 and ord ( c ) <= 90 : NEW_LINE INDENT lookFor = ' P ' NEW_LINE if inBounds ( i + 1 , j - 1 ) and board [ i + 1 ] [ j - 1 ] == lookFor : NEW_LINE INDENT return True NEW_LINE DEDENT if inBounds ( i + 1 , j + 1 ) and board [ i + 1 ] [ j + 1 ] == lookFor : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT lookFor = ' p ' NEW_LINE if inBounds ( i - 1 , j - 1 ) and board [ i - 1 ] [ j - 1 ] == lookFor : NEW_LINE INDENT return True NEW_LINE DEDENT if inBounds ( i - 1 , j + 1 ) and board [ i - 1 ] [ j + 1 ] == lookFor : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def inBounds ( i , j ) : NEW_LINE INDENT return i >= 0 and i < 8 and j >= 0 and j < 8 NEW_LINE DEDENT board = [ [ ' - ' , ' - ' , ' - ' , ' k ' , ' - ' , ' - ' , ' - ' , ' - ' ] , [ ' p ' , ' p ' , ' p ' , ' - ' , ' p ' , ' p ' , ' p ' , ' p ' ] , [ ' - ' , ' - ' , ' - ' , ' - ' , ' - ' , ' b ' , ' - ' , ' - ' ] , [ ' - ' , ' - ' , ' - ' , ' R ' , ' - ' , ' - ' , ' - ' , ' - ' ] , [ ' - ' , ' - ' , ' - ' , ' - ' , ' - ' , ' - ' , ' - ' , ' - ' ] , [ ' - ' , ' - ' , ' - ' , ' - ' , ' - ' , ' - ' , ' - ' , ' - ' ] , [ ' P ' , ' - ' , ' P ' , ' P ' , ' P ' , ' P ' , ' P ' , ' P ' ] , [ ' K ' , ' - ' , ' - ' , ' - ' , ' - ' , ' - ' , ' - ' , ' - ' ] ] NEW_LINE if checkBoard ( board ) == 0 : NEW_LINE INDENT print ( " No ▁ king ▁ in ▁ danger " ) NEW_LINE DEDENT elif checkBoard ( board ) == 1 : NEW_LINE INDENT print ( " White ▁ king ▁ in ▁ danger " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Black ▁ king ▁ in ▁ danger " ) NEW_LINE DEDENT