i'm making a chess game in java, and pawns are giving me a hell of a problem. I honestly don't know why, as their movement is the simplest. Here's the Piece declaration, and the Pawn (extends Piece) declaration. The movement class defines a simple x;y object that i use to mark possible movement targets on the board.. i have a GameHandler class that later depures all impossible movements based on game rules later. But Pawn's moves array seems to be clean; all other pieces' movements work like wonders! Thanks in advance!
Piece class
public abstract class Piece{
protected int x, y;
protected boolean isWhite;
protected ArrayList<Movement> moves;
public Piece(int x, int y, boolean isWhite) {
this.x=x;
this.y=y;
this.isWhite = isWhite;
moves = new ArrayList<Movement>();
}
public abstract ArrayList<Movement> getMoves();
//obvious methods
public int getX()
public int getY()
public boolean isWhite()
//end obvious methods
public void setCoordinates(int x, int y){
this.x=x;
this.y=y;
}
}
Pawn class
public class Pawn extends Piece{
public Pawn(int x, int y, boolean isWhite){
super(x,y,isWhite);
}
public ArrayList<Movement> getMoves() {
moves.clear();
if(isWhite){
if(y>0) moves.add(new Movement(x, y-1));
if(y==7) moves.add(new Movement(x, y-2));
}else{
if(y<7) moves.add(new Movement(x, y+1));
if(y==0) moves.add(new Movement(x, y+2));
}
return moves;
}
}
EDIT: Adding King class as reference
public class King extends Piece{
public King(int x, int y, boolean isWhite){
super(x,y,isWhite);
}
public ArrayList<Movement> getMoves() {
moves.clear();
if(y-1>=0 && x-1>=0) moves.add(new Movement (x-1, y-1));
if(y-1>=0 && x+1<8) moves.add(new Movement (x+1, y-1));
if(y+1<8 && x+1<8) moves.add(new Movement (x+1, y+1));
if(y+1<8 && x-1>=0) moves.add(new Movement (x-1, y+1));
if(y-1>=0) moves.add(new Movement (x, y-1));
if(y+1<8) moves.add(new Movement (x, y+1));
if(x+1<8) moves.add(new Movement (x+1, y));
if(x-1>=0) moves.add(new Movement (x-1, y));
return moves;
}
}

y == 6andy == 1instead of 7 and 0, since pawns start ahead of the other pieces. - Greg Hewgill