Reputation: 1
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.
Upvotes: 0
Views: 1874
Reputation: 1856
From the code you posted I assume you have a 2D board representation. A more clean way of doing this would be to loop through all possible moves a knight can make from its current position, which would be e.g. a list of list of ints. A knight has 8 possible moves, each being some combination of two squares away horizontally and one square vertically, or two squares vertically and one square horizontally.
I am not super familiar with java, so here is some pseudo/C# code.
knightMoves = [ [1, 2], [1, -2], ..., [-2, -1]]; // All 8 possible moves in all dircetions
foreach(List<int> move in knightMoves)
{
newPos = knightCurrentPosition + move;
// Check if knight is on board after move which means that the knights
// new position should be within 0-7.
if(newPos[0] < 0 || newPos[0] > 7 || newPos[1] < 0 || newPos[1] > 7) continue;
// Check if piece is not own piece (can't capture own piece)
if(board[newPos] == ownPiece) continue;
// If the criterias are fulfilled, add to the possible moves
possibleMoves.Add(newPos);
}
Then you will also have to check if king is left in check, this can be done either in the generate moves function itself, or at some later stage depending on your implementation.
Upvotes: 0