0

# Write a program that, given the coordinates of King and another figure on the chessboard determines whether the K is in check

I have to imput the type of the attacking figure(Q,B,N,R), coordinates of the king and the attacker

1 Answer

+ 3

Not sure how to write it in C++ but the logic should go like this, using coordinates:
if R, check if x or y of attacker and K match;
else if B, check if abs(x of B - x of K) == abs(y of B - y of K);
else if Q, do all the checks above;
else if Knight, check if (abs(x of Knight - x of K)==2 And abs(y of Knight - y of K)==1) Or (abs(x of Knight - x of K)==1 And abs(y of Knight - y of K)==2).
Btw abs(x) returns the absolute i.e. positive value of x. Hope this helps đź‘Ť