找不到getColor()方法?国际象棋游戏帮助Java

时间:2014-05-31 22:49:37

标签: java colors chess bluej gridworld

所以我试图用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));
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

getBoard()。get(..)根据我在网上找到的文档返回模板类型E,在你的情况下,E是类型Object(因为你在ChessPiece中的棋盘数据成员是对象的集合。)对象没有getColor()方法。你需要将((getBoard()。get(getLocation()。getAdjacentLocation(i))))转换为具有getColor方法的类。(或者将你的棋盘改为ChessPieces的集合)