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
function checkBoard ( board ) { for ( let i = 0 ; i < 8 ; i ++ ) { for ( let j = 0 ; j < 8 ; j ++ ) { if ( board [ i ] [ j ] == ' ' ) { if ( lookForn ( board , ' ' , i , j ) ) return 1 ; if ( lookForp ( board , ' ' , i , j ) ) return 1 ; if ( lookForr ( board , ' ' , i , j ) ) return 1 ; if ( lookForb ( board , ' ' , i , j ) ) return 1 ; if ( lookForq ( board , ' ' , i , j ) ) return 1 ; if ( lookFork ( board , ' ' , i , j ) ) return 1 ; } if ( board [ i ] [ j ] == ' ' ) { if ( lookForn ( board , ' ' , i , j ) ) return 2 ; if ( lookForp ( board , ' ' , i , j ) ) return 2 ; if ( lookForr ( board , ' ' , i , j ) ) return 2 ; if ( lookForb ( board , ' ' , i , j ) ) return 2 ; if ( lookForq ( board , ' ' , i , j ) ) return 2 ; if ( lookFork ( board , ' ' , i , j ) ) return 2 ; } } } return 0 ; } function lookFork ( board , c , i , j ) { let x = [ - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 ] ; let y = [ - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 ] ; for ( let k = 0 ; k < 8 ; k ++ ) { let m = i + x [ k ] ; let n = j + y [ k ] ; if ( inBounds ( m , n ) && board [ m ] [ n ] == c ) return true ; } return false ; } function lookForq ( board , c , i , j ) { if ( lookForb ( board , c , i , j ) || lookForr ( board , c , i , j ) ) return true ; return false ; } function lookForb ( board , c , i , j ) { let k = 0 ; while ( inBounds ( i + ++ k , j + k ) ) { if ( board [ i + k ] [ j + k ] == c ) return true ; if ( board [ i + k ] [ j + k ] != ' ' ) break ; } k = 0 ; while ( inBounds ( i + ++ k , j - k ) ) { if ( board [ i + k ] [ j - k ] == c ) return true ; if ( board [ i + k ] [ j - k ] != ' ' ) break ; } k = 0 ; while ( inBounds ( i - ++ k , j + k ) ) { if ( board [ i - k ] [ j + k ] == c ) return true ; if ( board [ i - k ] [ j + k ] != ' ' ) break ; } k = 0 ; while ( inBounds ( i - ++ k , j - k ) ) { if ( board [ i - k ] [ j - k ] == c ) return true ; if ( board [ i - k ] [ j - k ] != ' ' ) break ; } return false ; } function lookForr ( board , c , i , j ) { let k = 0 ; while ( inBounds ( i + ++ k , j ) ) { if ( board [ i + k ] [ j ] == c ) return true ; if ( board [ i + k ] [ j ] != ' ' ) break ; } k = 0 ; while ( inBounds ( i + -- k , j ) ) { if ( board [ i + k ] [ j ] == c ) return true ; if ( board [ i + k ] [ j ] != ' ' ) break ; } k = 0 ; while ( inBounds ( i , j + ++ k ) ) { if ( board [ i ] [ j + k ] == c ) return true ; if ( board [ i ] [ j + k ] != ' ' ) break ; } k = 0 ; while ( inBounds ( i , j + -- k ) ) { if ( board [ i ] [ j + k ] == c ) return true ; if ( board [ i ] [ j + k ] != ' ' ) break ; } return false ; } function lookForn ( board , c , i , j ) { let x = [ 2 , 2 , - 2 , - 2 , 1 , 1 , - 1 , - 1 ] ; let y = [ 1 , - 1 , 1 , - 1 , 2 , - 2 , 2 , - 2 ] ; for ( let k = 0 ; k < 8 ; k ++ ) { let m = i + x [ k ] ; let n = j + y [ k ] ; if ( inBounds ( m , n ) && board [ m ] [ n ] == c ) return true ; } return false ; } function lookForp ( board , c , i , j ) { let lookFor ; if ( c . charCodeAt ( ) >= 65 && c . charCodeAt ( ) <= 90 ) { lookFor = ' ' ; if ( inBounds ( i + 1 , j - 1 ) && board [ i + 1 ] [ j - 1 ] == lookFor ) return true ; if ( inBounds ( i + 1 , j + 1 ) && board [ i + 1 ] [ j + 1 ] == lookFor ) return true ; } else { lookFor = ' ' ; if ( inBounds ( i - 1 , j - 1 ) && board [ i - 1 ] [ j - 1 ] == lookFor ) return true ; if ( inBounds ( i - 1 , j + 1 ) && board [ i - 1 ] [ j + 1 ] == lookFor ) return true ; } return false ; } function inBounds ( i , j ) { return i >= 0 && i < 8 && j >= 0 && j < 8 ; } let board = [ [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] ] ; if ( checkBoard ( board ) == 0 ) document . write ( " " ) ; else if ( checkBoard ( board ) == 1 ) document . write ( " " ) ; else document . write ( " " ) ;