so I'm trying to code a Chess Program in Java but I'm having some trouble with my getColor() method. I'm relying on some of the code from Gridworld. I've created a class for each piece. I want the piece to work like a critter from Gridworld in the sense that I have a method that creates an ArrayList of possible locations to choose from when moving. This is where I've hit a problem. I've tried to create a getColor() method, but for some reason it is not working. I asked my teacher for help but he was as puzzled as I was. I tried debugging it but I don't see anything wrong with it. The exact error I get is this:
"Cannot find symbol - method getColor()"
Here's all my code, I'm using BlueJ for the record:
import java.util.ArrayList;
import java.awt.Color;
public interface Piece
{
public enum PieceType {pawn, rook, knight, bishop, queen, king}
}
Next is the ChessPiece Abstract Class. I haven't worked on the selectMoveLocation method yet though:
import java.util.ArrayList;
import java.awt.Color;
import info.gridworld.grid.Location;
import info.gridworld.grid.BoundedGrid;
public abstract class ChessPiece implements Piece
{
Color colorOfPiece;
PieceType typeOfPiece;
public BoundedGrid<Object> board;
public Location location;
public ArrayList moveLocations;
public ChessPiece( Color whiteOrBlack, PieceType selectedType)
{
if (whiteOrBlack == Color.BLACK || whiteOrBlack == Color.WHITE)
{
if ((selectedType == PieceType.pawn || selectedType == PieceType.rook || selectedType == PieceType.knight || selectedType == PieceType.bishop ||selectedType == PieceType.queen || selectedType == PieceType.king))
{
colorOfPiece = whiteOrBlack;
typeOfPiece = selectedType;
location = null;
}
}
}
public Color getColor()
{
return colorOfPiece;
}
public void makeMove(Location newLocation)
{
if (board == null)
throw new IllegalStateException("This actor is not in a board.");
if (board.get(location) != this)
throw new IllegalStateException(
"The board contains a different actor at location "
+ location + ".");
if (!board.isValid(newLocation))
throw new IllegalArgumentException("Location " + newLocation
+ " is not valid.");
if (newLocation.equals(location))
return;
board.remove(location);
location = newLocation;
board.put(location, this);
}
public Location getLocation()
{
return location;
}
public BoundedGrid<Object> getBoard()
{
return board;
}
public Location selectMoveLocation(ArrayList<Location> moveLocations)
{
Location selection;
selection = null;
//mouse stuff
return selection;
}
}
And finally, the code that gives me the compiler error. This is just the code for my King piece, though it gives me the error for every piece where I try and implement it:
import java.awt.Color;
import java.util.ArrayList;
import info.gridworld.grid.Location;
import info.gridworld.grid.BoundedGrid;
//must fix problem with getColor()
public class King extends ChessPiece
{
ArrayList<Location> moveLocations;
// private Color colorOfPiece;
public King( Color whiteOrBlack )
{
super(whiteOrBlack, PieceType.king);
//colorOfPiece = whiteOrBlack;
}
public void getMoveLocations()
{
moveLocations.clear();
for (int i = 0; i < 360; i += 45)
{
if ((getBoard().isValid(getLocation().getAdjacentLocation(i))) && (((getBoard().get(getLocation().getAdjacentLocation(i))) == null) || (((getBoard().get(getLocation().getAdjacentLocation(i)))).getColor() == colorOfPiece)))
{
moveLocations.add(getLocation().getAdjacentLocation(i));
}
}
}
}
getBoard().get(..) returns a template type E according to the doc I found online, and in your case, E is type Object (since your board data member in ChessPiece is a collection of Objects.) Object has no getColor() method. You'll want to cast (((getBoard().get(getLocation().getAdjacentLocation(i)))) to a class that has a getColor method. (Or maybe change your board to a collection of ChessPieces)