2

I am implementing a chess game. I am aware of the rules but I have not played it much. I need clarification on a small point.

Suppose a player legally moves a piece from square A to square B. Consider the path this piece followed during this move. Can it be different based on the type of piece?

I am not able to find such a case.

Edit :- I am adding the interface code that I am dealing with to make it more clear as asked by some people to give more context.

public class Chess {

    ChessBoard chessBoard;
    Player[] player;
    Player currentPlayer;
    List<Move> movesList;
    GameStatus gameStatus;

    public boolean playerMove(CellPosition fromPosition, CellPositionb toPosition, Piece piece); 
    public boolean endGame();
    private void changeTurn();

}
public abstract class Piece {

Color color;
public boolean validate(CellPosition fromPosition, CellPositionb toPosition);
}

public class Knight extends Piece {
public boolean validate(CellPosition fromPosition, CellPositionb toPosition);

}

Similarly there will be classes for other pieces as Knight. Now if a player want to move in chess from some position X to another position Y, the validate() method of the Piece will be called by playerMove() of chess,validate() can only check whether the move is valid or not.Like for the given pieces,say knight, it will check that from given location,the destination is one of 8 valid moves.For Rook it can be any one of four directions and so on.

The playerMove() function makes a call the the Piece.validate() function and then it gets to know that it is a valid move.The problem that I am trying to address, now once playerMove function in chess, knows that it is valid move, it does not know that whether there is any piece in between from given location to destination?Because the piece dont have context of Board,it cannot detect it thing and it has to be done in playerMove() function of chess.So i will write a code here to generate the list of possible squares that are involved from source to destination. Will that list of squares will always be unique ?or can it be different for diffeent piece? Because apart from knight ,all moves in straight or diagonal so it should be unique ,so I want to clarify on this .If still question not clear, please let me know.but

4

For queens, bishops and rooks moving from square A to square B there is only one way that move can be made and all the squares in between must be empty for that move to be legal.

For a knight moving from square A to square there are generally two ways but that is irrelevant because knights can jump over pieces in the way.

For castling there is only one way the king can move from A to B and the intervening square must be empty for that move to be legal and the king must be moved first. For castling there is only one way the rook can move from A to B and all the squares in between must be empty apart from the king for that move to be legal.

8
  • so it means, i can safely assume that if there has been a move from position A to position B then there is only one way to do so regardless of piece.The intermediate positions, used in moving from A to B by a piece will be same regardless of piece,say A->X->Y->B, then regardless of piece, the intermediate positions from A to B remain X and Y in sequence? – rahul sharma Dec 5 '20 at 21:59
  • 3
    @rahulsharma If a piece moves A->X->Y->B then that is 3 moves not 1. It is A->X then X->Y then Y->B. If a piece moves from A to B in one move then at the start of the move it is on square A then it is picked up and placed on square B. The piece never occupies any of the squares in between but for pieces other than knights those intervening squares must be empty otherwise the move is blocked and cannot be made. – Brian Towers Dec 5 '20 at 22:12
  • yes i agree, I will give you a little context here. There is one part of my game that will just tell me that move is allowed by piece X from A to B. Now once i got confirmation , i need to make sure that in the path from A to B, there should be no other piece. The problem here is that once i got confirmation, I dont have access to the piece now(the way it is done as of now). So now i will generate path from A to B and check that all intermediate squares should be empty.So i got here like will there be always unique path regardless from A to B if i dont have access to piece that made this move – rahul sharma Dec 5 '20 at 22:33
  • 1
    1/2: @rahulsharma it that case indeed, there is only one way to do this. But I think you might want to clarify your question to explain what exactly you are planning to do, then people can give better answers. "tell me that move is allowed", what tells you that a move is allowed? Presumably some move generator, i.e. a function that returns legal moves? Legality checking should happen in that function. Then for instance you don't check each origin-destination pair independently but many at once. E.g. you check can the queen move one square in this direction? Yes, then that's a legal move. – koedem Dec 6 '20 at 8:30
  • 1
    2/2: Was that first generated move not a capture and is two squares into that direction not a friendly piece? Then that move is legal too, since the first move not being a capture means the previous square was empty. Now was the previous generated move not a capture and the square three into that direction is not a friendly piece? Then this is a legal move too. As you can see you can use the previous generated moves to help generate new ones. (sorry for the not so clear explanation, it's hard in a comment :D ) – koedem Dec 6 '20 at 8:33
3

No. It's a matter of taste how the knight travels from the start square to the end square, but nothing is written on how it gets to the final square. The same for castling, of how the rook arrives at the destination square, since it has to get there by moving past the king that's just made a two square move.

1

The special cases to consider are knight moves, castling, and pawn moves. Knights don't really have a path; you can think of them as teleporting to their destination. Pawns and castling have to follow the same restrictions on moving through occupied squares (they have same squares they "go through" as any other material with the same source and destination squares), but also have additional restrictions. Whenever a king moves other than castling, it doesn't "go through" any squares, so your question doesn't really appy.

Every other move is either a rook move or a bishop move. Whenever a queen moves, it makes either a rook move or a bishop move. When a queen makes a bishop move, it follows the same rules as moving a bishop, and the path it takes is the same as a bishop. When it makes a rook move, it follows the same rules as a rook (other than the aforementioned castling).

0

For pieces which are not the knight, there indeed is only one path to check. For rook, bishop and queen you trace out a "ray", a king only moves one square and the knight does not care about other pieces being in the way so there the path does not matter. So to check you would have to verify that the destination square is not a friendly piece (as you can't capture friendly pieces) and for the ray moving pieces that all squares in between are empty.

The pawn of course is a special case since it captures differently than it moves, I hope you are aware of that. :) (and it has further complications like the double step and en passant rule)

NOTE: For a move to be legal the moving player must not be in check after the move. So you also have to verify that. One possible way to do that is pretend that the king of the moving player is a rook and see if it could capture an enemy rook, then that enemy rook is in fact giving check. (same for the other pieces and of course be careful with the pawns once again)

I will also note that I would implement this a lot differently but I assume it's a learning project, so should be fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.