所以我正在尝试用 Java 编写一个国际象棋程序,但我的 getColor() 方法遇到了一些问题。我依赖于 Gridworld 的一些代码。我为每件作品创建了一个类。我希望这件作品像来自 Gridworld 的小动物一样工作,因为我有一种方法可以创建一个 ArrayList,其中包含在移动时可供选择的可能位置。这是我遇到问题的地方。我试图创建一个 getColor() 方法,但由于某种原因它不起作用。我向老师求助,但他和我一样困惑。我尝试调试它,但我看不出它有什么问题。我得到的确切错误是:
“找不到符号 - 方法 getColor()”
这是我的所有代码,我使用 BlueJ 作为记录:
import java.util.ArrayList;
import java.awt.Color;
public interface Piece
{
public enum PieceType {pawn, rook, knight, bishop, queen, king}
}
接下来是 ChessPiece 抽象类。我还没有研究过 selectMoveLocation 方法:
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;
}
}
最后,给我编译器错误的代码。这只是我的 King 部分的代码,尽管它给了我尝试实现它的每一部分的错误:
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));
}
}
}
}