2016-05-05 91 views
0

好吧,所以我正在開發一個學校項目,我們將C++轉換爲java,而且我已經相當瞭解了。該項目是一個國際象棋遊戲。下面的代碼:無法實例化類型Chessboard.Type

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。有誰知道如何解決這一問題?

+0

謝謝,我之前讀過,但是我們不知道m_type類型是什麼。這就是爲什麼我們不能調用特定的一個。另外下面我已經說明了當你試圖表明一個特定的類型時會發生什麼。 – Destry

回答

2

停止嘗試實例化枚舉類型。

例如,您可以編寫初始化是這樣的:

private Type m_type = Type.BLANK; 
+0

這並沒有解決任何問題,它改變拒絕插入()來完成表達式,當你這樣做時它拒絕再次改變Type Chess()然後將它切換回Type(); – Destry

+1

@DestryAmiott你是什麼意思?這個改變使得代碼[編譯](http://melpon.org/wandbox/permlink/91JMt41dYrMtuz5l),我看不到代碼中的「insert()」。 – MikeCAT

+0

對不起,我還是有新的Type;在那裏,謝謝你的快速回答!對上一篇文章感到抱歉 – Destry

1

您不能實例化Enum類型的變量。

您應該改爲向類型添加UNKNOWN,然後用默認值Type.UNKNOWN初始化m_type,然後在構造函數中將其設置爲適當的值。

+0

代碼中有一條評論說BLANK是空置空間。 UNKNOWN,你知道應該有一塊,但它沒有被分配一個適當的類型。 – CConard96

+0

你是對的 - 趕上! –

0

Type是一個枚舉,而不是你可以用它來實例化對象的類。閱讀更多關於枚舉here的信息。