0

Okay so I am working on a school project where we convert C++ to java, and I have gotten fairly far. The project is for a chess game. Here's the code:

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
}

}

The problem is when I get down to

private Type m_type = new Type();

It is stating "Cannot instantiate the type Chessboard.Type"

Now I have looked everywhere including here

As you can see that program is very different from this one and anytime you reference Type in the program at all this gets thrown. Does anyone know how to fix this?

Community
  • 1
  • 1
Destry
  • 228
  • 1
  • 12
  • Thanks, I read that earlier however we do not know what the m_type Type will be. This is why we cannot call a specific one. Also below I have stated what happens when you try to indicate a specific Type. – Destry May 05 '16 at 02:04

3 Answers3

2

Stop trying to instantiate enum types.

For example, you can write the initialization to something like this:

private Type m_type = Type.BLANK;
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • This does not fix anything, it changes the rejection to insert () to complete expression and when you do that it rejects again saying change to Type Chess() then switches it back to Type(); – Destry May 05 '16 at 02:02
  • 1
    @DestryAmiott What do you mean? Thiss change made the code [compile](http://melpon.org/wandbox/permlink/91JMt41dYrMtuz5l) and I see no `insert ()` in your code. – MikeCAT May 05 '16 at 02:10
  • Oh sorry, I still had new Type; in there, thank you for your quick answer! Sorry about the previous post – Destry May 05 '16 at 02:25
1

You cannot instantiate a variable of an Enum type.

You should instead add UNKNOWN to Type and then initialize m_type with the default value of Type.UNKNOWN, and then set it to a proper value in a constructor.

CConard96
  • 874
  • 1
  • 7
  • 18
0

Type is an enum, not a class you can use to instantiate an object. Read more about enums here.

perryh
  • 1
  • 1