I am implementing a chess engine so i've constructed the following function to test possible moves(without actually making one) for the AI part of the engine.
The function takes as parameters the side(black or white),an object of type board that contains an arraylist of all the pieces of the chessboard as well as some other information and the source and destination squares of the move as strings.
My problem is that the moves aren't "made" properly.What i mean is that in the board object that is returned after this function is finished the pieces have no changed row or col even though if the function changes them.The board only changes if a piece is removed from it.
public Board doTestMove(int side,Board b, String srcSq, String destSq) {
//check source square notation for validity
if (checkSqValidity(srcSq)) {
//check destination square notation for validity
if (checkSqValidity(destSq)) {
//find the piece based on the source square notation.
Piece piece =new Piece(b.notationToPiece(srcSq));
if(piece==null){
System.out.println("The piece is null");
}
//make sure the piece is owned by the player
if (piece.getColor()==side) {
//get all movements that are allowed for the selected piece
ArrayList<ArrayList<Integer>> legalMoves=new ArrayList<ArrayList<Integer>>();
legalMoves = possiblePieceMoves(piece, false);
//array coordinates for new destination
Index newLoc = new Index(b.notationToIndex(destSq).getX(),b.notationToIndex(destSq).getY());
//find out if destination location is included in the legal moves list
ArrayList<Integer> x = legalMoves.get(0); //list of row numbers
ArrayList<Integer> y = legalMoves.get(1); //list of column numbers
ListIterator<Integer> xList = x.listIterator(); //row iterator
ListIterator<Integer> yList = y.listIterator(); //column iterator
int xL, yL;
while (xList.hasNext() && yList.hasNext()) { //while lists have coordinates
//listiterator next() method doesn't work inside if statement -> assign to variables
xL = xList.next();
yL = yList.next();
if (newLoc.getX()==xL && newLoc.getY()==yL) { //legal move
b.removePiece(newLoc.getX(), newLoc.getY()); //remove captured piece from the board
piece.setRow(newLoc.getX()); //change piece row
piece.setCol(newLoc.getY()); //change piece column
b.updateGameState(); //populate the board with new location of pieces.
//place source and destination square to history of moves
if (side==0) { //if white
getHistoryOfMoves().addWhiteMove(srcSq, destSq); //add white piece move to history
} else if (side==1) { //if black
getHistoryOfMoves().addBlackMove(srcSq, destSq); //add black piece move to history
}
//promote pawns to queens if they reach enemy's end
b.promotePawnsToQueen(side);
return new Board(b); //move successful
}
}
}
} else {
System.out.println("Not a valid destination square. ");
}
} else {
System.out.println("Not a valid source notation.");
}
return null;
}
As you can see the object piece is defined as following:
Piece piece =new Piece(b.notationToPiece(srcSq));
I have concluded that i must change it to:
Piece piece =b.notationToPiece(srcSq);
After thinking that the only thing that happens correctly is the removal of a piece.Thus changing something directly to the board that is to be returned works.
So i decided that the changes the function makes must affect the piece that is part of the arraylist of the object b and not some new object.
However when i try to do this i get the error:
java.lang.NullPointerException
At the line:
if (piece.getColor()==side) {
That is quite strange since this function for testing moves is almost identical to the function i use for making moves(that works fine) which is the following:
public boolean doMove(Player player, String srcSq, String destSq) {
//check source square notation for validity
if (checkSqValidity(srcSq)) {
//check destination square notation for validity
if (checkSqValidity(destSq)) {
//find the piece based on the source square notation.
Piece piece = getBoard().notationToPiece(srcSq);
//make sure the piece is owned by the player
if (piece.getColor()==player.getSide()) {
//get all movements that are allowed for the selected piece
ArrayList<ArrayList<Integer>> legalMoves = possiblePieceMoves(piece, false);
//array coordinates for new destination
Index newLoc = getBoard().notationToIndex(destSq);
//find out if destination location is included in the legal moves list
ArrayList<Integer> x = legalMoves.get(0); //list of row numbers
ArrayList<Integer> y = legalMoves.get(1); //list of column numbers
ListIterator<Integer> xList = x.listIterator(); //row iterator
ListIterator<Integer> yList = y.listIterator(); //column iterator
int xL, yL;
while (xList.hasNext() && yList.hasNext()) { //while lists have coordinates
xL = xList.next();
yL = yList.next();
if (newLoc.getX()==xL && newLoc.getY()==yL) { //legal move
getBoard().removePiece(newLoc.getX(), newLoc.getY()); //remove captured piece from the board
piece.setRow(newLoc.getX()); //change piece row
piece.setCol(newLoc.getY()); //change piece column
//place source and destination square to history of moves
if (player.getSide()==0) { //if white
getHistoryOfMoves().addWhiteMove(srcSq, destSq); //add white piece move to history
} else if (player.getSide()==1) { //if black
getHistoryOfMoves().addBlackMove(srcSq, destSq); //add black piece move to history
}
//promote pawns to queens if they reach enemy's end
getBoard().promotePawnsToQueen(player.getSide());
return true; //move successful
}
}
}
} else {
System.out.println("Not a valid destination square. ");
}
} else {
System.out.println("Not a valid source notation.");
}
return false; //move failed, not own piece
}
I can't find the source of the problem.