gpt4 book ai didi

java - 无法实例化类型 Chessboard.Type

转载 作者:行者123 更新时间:2023-12-01 21:34:46 26 4
gpt4 key购买 nike

好吧,我正在开展一个学校项目,我们将 C++ 转换为 Java,并且我已经取得了相当大的进展。该项目是一个国际象棋游戏。代码如下:

package Chess;

import java.util.Enumeration;

public class ChessBoard{
/* This enum represents values for each of the possible "pieces" that can
* occupy a space on the chess board, including "blank" for a vacant space.
*/


public enum Type
{
BLANK,
PAWN,
BISHOP,
KNIGHT,
ROOK,
QUEEN,
KING;

public int getValue()
{
return this.ordinal();
}

public static Type forValue(int value)
{
return values()[value];
}
}

public class Coord
{
public short m_X;
public short m_Y;
}

public class GlobalMembers
{
/* These const values are used for identifying whether the "piece" occupying
* a chess board space is black, white, or in the case of a blank spot,
* "clear."
*/

public static final short BLACK = 0;

public static final short WHITE = 1;

public static final short CLEAR = 2;

public static final short CHECK_MOVE = 1; // this is a piece move "mode" designating an AI board evaluation

public static final short REAL_MOVE = 2; // this is a piece move "mode" designating an actual AI move
}


public class ChessPiece
{

/* The ctor for ChessPiece takes a piece type and color.
*/

public ChessPiece(Type type, short color)
{
this.m_type = type;
this.m_color = color;
this.m_moves = 0;
}
public void dispose()
{
}

/* This member function returns the chess piece type.
*/

public final Type getType()
{
return m_type;
}

/* This member function returns the chess piece color.
*/

public final short getColor()
{
return m_color;
}

/* This function is used to record a new movement of a piece. Movement tracking
* is necessary for evaluating validity of special piece moves like en passant
* capture and castling.
*/

public final void incrementMoves()
{
++m_moves;
}

/* Reduces the number of times a piece has been moved. Used for adjustment when
* a function incidentally increments the movement of a piece in a case where
* a board evaluation is taking place, and is not intended as an actual move.
*/

public final void decrementMoves()
{
--m_moves;
}

/* Returns the number of times a piece has been moved.
*/

public final int getMoves()
{
return m_moves;
}
private Type m_type = new Type(); // this variable holds the type of piece this object represents

private short m_color; // this variable holds the color of the piece, white or black
private int m_moves; // this variable holds the number of moves the piece has made
}

}

问题是当我开始

private Type m_type = new Type();

它指出“无法实例化类型 Chessboard.Type”

现在我已经到处看了,包括here

正如您所看到的,该程序与此程序非常不同,并且每当您在程序中引用 Type 时,都会抛出此错误。有谁知道如何解决这一问题?

最佳答案

停止尝试实例化枚举类型。

例如,您可以将初始化编写为如下所示:

private Type m_type = Type.BLANK;

关于java - 无法实例化类型 Chessboard.Type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37040894/

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