I am trying to understand the algorithms behind determining valid moves for each chess piece. The specific issue I am having is determining when a piece cannot move past a certain point because it is being blocked by a piece of its own colour or is able to take a piece of the opposite colour but cannot move past that point.
The simple algorithms I have for each piece are:
Valid King move, if the piece moves from (X1, Y1) to (X2, Y2), the move is valid if and only if |X2-X1|<=1 and |Y2-Y1|<=1.
Valid Bishop move, if the piece moves from (X1, Y1) to (X2, Y2), the move is valid if and only if |X2-X1|=|Y2-Y1|.
Valid Rook move, if the piece moves from (X1, Y1) to (X2, Y2), the move is valid if and only if X2=X1 or Y2=Y1.
Valid Queen move, a queen's move is valid if it is either a valid bishop or rook move.
Valid Knight move, if the piece moves from (X1, Y1) to (X2, Y2), the move is valid if and only if (|X2-X1|=1 and |Y2-Y1|=2) or (|X2-X1|=2 and |Y2-Y1|=1).
Valid Pawn move, if the piece moves from (X1, Y1) to (X2, Y2), the move is valid if and only if X2=X1 and Y2-Y1=1 (only for a white pawn).
Any advice would be appreciated.
What is an Illegal Move in Chess? In Chess, an illegal move is when a piece moves outside of the boundaries of its defined abilities. Illegal moves include ones that are otherwise legal but expose that player's King to check. It is also an illegal chess move to leave your king in check.
A Legal Move is a pseudo-legal move which does not leave its own king in check. If not in check, most programs delay the legality test to the child node, after incremental updates attack and defend maps or an explicit square attacked test direct after make move.
A king can move one square horizontally, vertically, or diagonally unless the square is already occupied by a friendly piece or the move would place the king in check. If the square is occupied by an undefended enemy piece, the king may capture it, removing it from play.
There are 9,132,484 distinct positions or 120,921,506 total positions after 6 moves (three moves for White and three moves for Black). The total number of chess positions after 7 moves is 3,284,294,545. The total number of chess positions is about 2x10 to the 46 power.
You need to take the board state into account for that. I think the common way to do it would be checking if each cell on the path is empty or not.
public enum PieceColor { Black, White }
public interface IBoard
{
bool IsEmpty(int x, int y);
PieceColor GetPieceColor(int x, int y);
}
IBoard board;
bool BishopCanMove(PieceColor bishopColor, int fromX, int fromY, int toX, int toY)
{
int pathLength = Mathf.Abs(toX - fromX);
if (pathLength != Mathf.Abs(toY - fromY)) return false; // Not diagonal
// Also validate if the coordinates are in the 0-7 range
// Check all cells before the target
for (int i = 1; i < pathLength; i++)
{
int x = fromX + i;
int y = fromY + i;
if(board.IsEmpty(x, y)) continue; // No obstacles here: keep going
else return false; // Obstacle found before reaching target: the move is invalid
}
// Check target cell
if (board.IsEmpty(toX, toY)) return true; // No piece: move is valid
// There's a piece here: the move is valid only if we can capture
return board.GetPieceColor(toX, toY) == bishopColor;
}
The IBoard
interface is there just to show the point. You should have a board state exposing those informations in some way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With