gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:55:01 25 4
gpt4 key购买 nike

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

最佳答案

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

关于java - 找不到 getColor() 方法?国际象棋游戏帮助 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23975156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com