<script>
function
checkBoard(board)
{
for
(let i = 0; i < 8; i++) {
for
(let j = 0; j < 8; j++) {
if
(board[i][j] ==
'k'
) {
if
(lookForn(board,
'N'
, i, j))
return
1;
if
(lookForp(board,
'P'
, i, j))
return
1;
if
(lookForr(board,
'R'
, i, j))
return
1;
if
(lookForb(board,
'B'
, i, j))
return
1;
if
(lookForq(board,
'Q'
, i, j))
return
1;
if
(lookFork(board,
'K'
, i, j))
return
1;
}
if
(board[i][j] ==
'K'
) {
if
(lookForn(board,
'n'
, i, j))
return
2;
if
(lookForp(board,
'p'
, i, j))
return
2;
if
(lookForr(board,
'r'
, i, j))
return
2;
if
(lookForb(board,
'b'
, i, j))
return
2;
if
(lookForq(board,
'q'
, i, j))
return
2;
if
(lookFork(board,
'k'
, 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;
}
// Check the lower left diagonal
k = 0;
while (inBounds(i + ++k, j - k)) {
if (board[i + k][j - k] == c)
return true;
if (board[i + k][j - k] != '
-
')
break;
}
// Check the upper right diagonal
k = 0;
while (inBounds(i - ++k, j + k)) {
if (board[i - k][j + k] == c)
return true;
if (board[i - k][j + k] != '
-
')
break;
}
// Check the upper left diagonal
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;
}
// Check if
function lookForr(board, c, i, j)
{
// Check downwards
let k = 0;
while (inBounds(i + ++k, j)) {
if (board[i + k][j] == c)
return true;
if (board[i + k][j] != '
-
')
break;
}
// Check upwards
k = 0;
while (inBounds(i + --k, j)) {
if (board[i + k][j] == c)
return true;
if (board[i + k][j] != '
-
')
break;
}
// Check right
k = 0;
while (inBounds(i, j + ++k)) {
if (board[i][j + k] == c)
return true;
if (board[i][j + k] != '
-
')
break;
}
// Check left
k = 0;
while (inBounds(i, j + --k)) {
if (board[i][j + k] == c)
return true;
if (board[i][j + k] != '
-
')
break;
}
return false;
}
// Check if the knight can attack the king
function lookForn(board, c, i, j)
{
// All possible moves of the knight
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++) {
// Incrementing index values
let m = i + x[k];
let n = j + y[k];
// Checking boundary conditions
// and character match
if (inBounds(m, n) && board[m][n] == c)
return true;
}
return false;
}
// Function to check if pawn can attack the king
function lookForp(board, c, i, j)
{
let lookFor;
if (c.charCodeAt() >= 65 && c.charCodeAt() <= 90) {
// Check for white pawn
lookFor = '
P
';
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 {
// Check for black pawn
lookFor = '
p
';
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;
}
// Check if the indices are within
// the matrix or not
function inBounds(i, j)
{
// Checking boundary conditions
return i >= 0 && i < 8 && j >= 0 && j < 8;
}
// Chessboard instance
let board
= [ [ '
-
', '
-
', '
-
', '
k
', '
-
', '
-
', '
-
', '
-
' ],
[ '
p
', '
p
', '
p
', '
-
', '
p
', '
p
', '
p
', '
p
' ],
[ '
-
', '
-
', '
-
', '
-
', '
-
', '
b
', '
-
', '
-
' ],
[ '
-
', '
-
', '
-
', '
R
', '
-
', '
-
', '
-
', '
-
' ],
[ '
-
', '
-
', '
-
', '
-
', '
-
', '
-
', '
-
', '
-
' ],
[ '
-
', '
-
', '
-
', '
-
', '
-
', '
-
', '
-
', '
-
' ],
[ '
P
', '
-
', '
P
', '
P
', '
P
', '
P
', '
P
', '
P
' ],
[ '
K
', '
-
', '
-
', '
-
', '
-
', '
-
', '
-
', '
-' ] ];
if
(checkBoard(board) == 0)
document.write(
"No king in danger"
);
else
if
(checkBoard(board) == 1)
document.write(
"White king in danger"
);
else
document.write(
"Black king in danger"
);
</script>