I am making a chess game in Gridworld. My pawn method is not working. For example, when I use getMoveLocations() on a pawn the first time, it brings up both valid moves. Once I move it two spaces and use getMoveLocations() again, it brings up the space behind the pawn. When I move it there, and then I use getMoveLocations(), it just brings up an empty ArrayList. Any help would be appreciated.
Pawn classimport info.gridworld.actor.Actor; import info.gridworld.grid.*; import java.util.ArrayList; import java.awt.Color; /** * @author * A class that extends Piece to represent a Pawn */ public class WhitePawn extends Piece { /** * A Piece that represents a Pawn * @param Location l- the Location of the Pawn * @param Color c- the Color of the Pawn */ public WhitePawn(Location l,Color c) { super(l,c); } /** * A method that returns an ArrayList of possible move Locations * @return ArrayList moves- an ArrayList of moves */ public ArrayList<Location> getMoveLocations() { ArrayList<Location> moves = new ArrayList<Location>(); int locX = currentLocation.getRow(); int locY = currentLocation.getCol(); Location l1 = new Location(locX - 1,locY); Location l2 = new Location(locX - 2,locY); if(getGrid().isValid(l1)) { moves.add(l1); if(getGrid().get(l1) instanceof Piece) { moves.remove(l1); } if(getGrid().isValid(l2) && numTimesMoved == 0) { moves.add(l2); if(getGrid().get(l2) instanceof Piece) { moves.remove(l2); } } } return moves; } /** * A method that returns an ArrayList of locations that can be attacked * @return ArrayList moves- an ArrayList of moves */ public ArrayList<Location> getAttackLocations() { ArrayList moves = new ArrayList<Location>(); ArrayList occupiedSpaces = getGrid().getOccupiedLocations(); int locX = currentLocation.getRow(); int locY = currentLocation.getCol(); Location l1 = new Location(locX - 1, locY - 1); Location l2 = new Location(locX - 1, locY + 1); if(occupiedSpaces.contains(l1)) { moves.add(l1); } if(occupiedSpaces.contains(l2)) { moves.add(l2); } return moves; } }
And this is the Piece class that Pawn is extendingimport java.util.ArrayList; import info.gridworld.grid.Location; import java.awt.Color; import info.gridworld.actor.Actor; import javax.swing.JOptionPane; import javax.swing.JFrame; /** * @author * Piece * An Abstract class that represents a piece in a game of chess */ public abstract class Piece extends Actor { Color color; Location currentLocation; int numTimesMoved; public Piece(Location l,Color c) { super(); color = c; currentLocation = l; } public abstract ArrayList<Location> getMoveLocations(); public abstract ArrayList<Location> getAttackLocations(); public void moveTo() { String location = JOptionPane.showInputDialog("Where would you like to move? (use chess notation)"); String row = location.substring(1); String column = location.substring(0,1); int r; int c; if(column.equalsIgnoreCase("A")) c = 0; else if(column.equalsIgnoreCase("B")) c = 1; else if(column.equalsIgnoreCase("C")) c = 2; else if(column.equalsIgnoreCase("D")) c = 3; else if(column.equalsIgnoreCase("E")) c = 4; else if(column.equalsIgnoreCase("F")) c = 5; else if(column.equalsIgnoreCase("G")) c = 6; else c = 7; r = 8 - (Integer.parseInt(row)); Location newLocation = new Location(r,c); ArrayList<Location> possibleMoves = getMoveLocations(); ArrayList<Location> attackLocations = getAttackLocations(); if(attackLocations.contains(newLocation)) { getGrid().get(newLocation).removeSelfFromGrid(); super.moveTo(newLocation); numTimesMoved++; } else if(possibleMoves.contains(newLocation)) { super.moveTo(newLocation); numTimesMoved++; } else { JFrame frame = new JFrame("That is not a valid move"); JOptionPane.showMessageDialog(frame, "That is not a valid move"); } } public boolean isOppositeColor(Actor p) { boolean b = false; if(p.getColor().equals(Color.WHITE) && color.equals(color.black)) b = true; if(p.getColor().equals(Color.black) && color.equals(color.WHITE)) b = true; return b; } }