I need help with the isValidMove Method for my pawn piece. How do I code pawns first move of 2 squares then 1 sqaure after that .
/**
* A player of a chess game.
*
* @author Zachary Kurmas
*/
public enum Player {
BLACK, WHITE;
/**
* Return the {@code Player} whose turn is next.
*
* @return the {@code Player} whose turn is next
*/
public Player next() {
return this == BLACK ? WHITE : BLACK;
}
}
public class Move {
public int fromRow, fromColumn, toRow, toColumn;
public Move(int fromRow, int fromColumn, int toRow, int toColumn) {
this.fromRow = fromRow;
this.fromColumn = fromColumn;
this.toRow = toRow;
this.toColumn = toColumn;
}
}
public class Pawn extends ChessPiece {
protected Pawn(Player player) {
super(player);
}
public String type(){
return "Pawn";
}
public boolean isValidMove(Move move, IChessPiece[][] board){
if(super.isValidMove(move, board) == false){
return false;
}
if (move.toRow != move.fromRow+1){
return false;
}
}
}
public abstract class ChessPiece implements IChessPiece {
/** */
private Player owner;
/**
* Return the player that owns this piece.
*
* @return the player that owns this piece.
*/
protected ChessPiece(Player player) {
this.owner = player;
}
// public abstract String type()
/* (non-Javadoc)
* @see chess.IChessPiece#player()
*/
@Override
public Player player() {
if (owner == Player.BLACK) {
return owner;
}
if (owner == Player.WHITE) {
return owner;
} else
return null;
}
/*
* (non-Javadoc)
*
* @see chess.IChessPiece#isValidMove(chess.Move, chess.IChessPiece[][])
*/
@Override
public boolean isValidMove(Move move, IChessPiece[][] board) {
if (move.toRow == move.fromRow && move.fromColumn == move.toColumn) {
return false;
}
IChessPiece firstPiece = board[move.toRow][move.toColumn];
IChessPiece secondPiece = board[move.fromRow][move.fromColumn];
if( firstPiece != null && firstPiece == secondPiece ){
return false;
}
else
return board[move.fromRow][move.fromColumn].equals(this.owner);
}
public abstract String type();
}
public interface IChessPiece {
/**
* Return the player that owns this piece.
*
* @return the player that owns this piece.
*/
Player player();
/**
* Return the type of this piece ("King", "Queen", "Rook", etc.). Note: In this case "type" refers to the game
* of chess, not the type of the Java class.
*
* @return the type of this piece
*/
String type();
/**
* Returns whether the piece at location {@code [move.fromRow, move.fromColumn]} is allowed to move to location
* {@code [move.fromRow, move.fromColumn]}.
*
* Note: Pieces don't store their own location (because doing so would be redundant). Therefore,
* the {@code [move.fromRow, move.fromColumn]} component of {@code move} is necessary.
* {@code this} object must be the piece at location {@code [move.fromRow, move.fromColumn]}.
* (This method makes no sense otherwise.)
*
* @param move a {@link Move} object describing the move to be made.
* @param board the {@link ChessBoard} in which this piece resides.
* @return {@code true} if the proposed move is valid, {@code false} otherwise.
* @throws IndexOutOfBoundsException if either {@code [move.fromRow, move.fromColumn]} or {@code [move.toRow,
* move.toColumn]} don't represent valid locations on the board.
* @throws IllegalArgumentException if {@code this} object isn't the piece at location {@code [move.fromRow, move.fromColumn]}.
*/
boolean isValidMove(Move move, IChessPiece[][] board);
}