Recently I came across some article on Chess OOPS design.Following is some snippet from it:
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 move(CellPosition fromPosition, CellPositionb toPosition);
public List<CellPosition> possibleMoves(CellPosition fromPosition);
public boolean validate(CellPosition fromPosition, CellPositionb toPosition);
}
public class Knight extends Piece {
public boolean move(CellPosition fromPosition, CellPositionb toPosition);
public List<CellPosition> possibleMoves(CellPosition fromPosition);
public boolean validate(CellPosition fromPosition, CellPositionb toPosition);
}
I pretty much liked the way classes are constructed but there is something that is confusing me.Now the code here is self explanatory. As per the author, the way it is designed,the "possibleMoves" function in the Piece class will give the list of possible moves and these moves are shown to user and user can select one of the move,which makes sense to me.
Now my question is, let us say we get possible moves by calling possibleMoves function from Piece class. Now since while actually making a move, there is a possibility that a piece can cross another piece in its way, which is not allowed except knight. SO where will we check that?I questioned the designed and the author is saying it should be done in chess class or in some Rule Engine, but my suggestion is to pass Board as a parameter and let the piece decide it as it knows how it will move.Otherwise in chess class or in rule engine we have to put a logic for each piece to check if while making a move it does not cross any other piece and for that we need to replicate the logic of how piece move in Piece class and in Chess/Rule Engine class. What can be the correct way here?