I wrote a program that examines threats of tools on a chessboard on other tools.
I want to write the movements that a knight can perform. I thought about checking the distance in Math abs in two options- two Horizontal blocks and one vertical or two vertical blocks and one Horizontal. I know he has 8 moves on the board, but is there a way to shorten it or is there another way to write his move? I attached the code, but the problem is with the knight:
// examines threats of tools on the chessboard
switch (first)
{
case 'r':
if ((row1 == row2) || (col1 == col2)){
System.out.println("rook threats " + (second == knight ? "knight" : "bishop"));
foundTreat = true;
}
break;
case 'b':
if(Math.abs(row1 - row2) == Math.abs(col1 - col2)){
System.out.println("bishop threats " + (second == rook ? "rook" : "knight"));
foundTreat = true;
}
break;
case 'k':
if((Math.abs(row1 + 1) == row2) && (Math.abs(col1 + 2) == col2)){
System.out.println("knight threats " + (second == rook ? "rook" : "bishop"));
foundTreat = true;
}
break;
}
switch (second)
{
case 'r':
if ((row1 == row2) || (col1 == col2)){
System.out.println("rook threats " + (first == knight ? "knight" : "bishop"));
foundTreat = true;
}
break;
case 'b':
if(Math.abs(row1 - row2) == Math.abs(col1 - col2)){
System.out.println("bishop threats " + (first == rook ? "rook" : "knight"));
foundTreat = true;
}
break;
case 'k':
if((Math.abs(row1 + 1) == row2) && (Math.abs(col1 + 2) == col2)){
System.out.println("knight threats " + (first == rook ? "rook" : "bishop"));
foundTreat = true;
}
break;
}
But I can't write the code correctly, so I would love to get help.
By the way, I'm a beginner programmer in Java, and I can write the code only with: if, else, Switch and without: while, loop.
thanks.