views:

219

answers:

4

If I were keeping an array in C representing a chess board, I might fill it with enumed items that roughly look like:

enum CHESSPIECE {
  none  = 0,
  pawn, knight, bishop, rook, queen, king,
  type_mask = 7,
  white = 8,
  white_pawn, white_knight, white_bishop, white_rook, white_queen, white_king,
  black = 16,
  black_pawn, black_kight, black_bishop, black_rook, black_queen, black_king,
  color_mask = 24
} chessPiece;

Thus allowing logic that looks like:

if (board[i][j] & color_mask == currentColor)
{
  impossibleMove = true; // or some-such
}

In Java, I'd just find that bitwise & operations on enums are not supported, and that further, the fairly awesome EnumSet doesn't easily apply since a piece can't be black AND white AND a rook AND a king.

So what I'm thinking looks like:

public enum ChessPieceId {
    None            (null, null),
    Pawn            (null, null),
    Knight          (null, null),
    Bishop          (null, null),
    Rook            (null, null),
    Queen           (null, null),
    King            (null, null),
    Type_Mask       (null, null),
    White           (null, null),
    White_Pawn      (ChessPieceId.White, ChessPieceId.Pawn),
    White_Knight    (ChessPieceId.White, ChessPieceId.Knight),
    White_Bishop    (ChessPieceId.White, ChessPieceId.Bishop),
    White_Rook      (ChessPieceId.White, ChessPieceId.Rook),
    White_Queen     (ChessPieceId.White, ChessPieceId.Queen),
    White_King      (ChessPieceId.White, ChessPieceId.King),
    SkipA           (null, null),
    Black           (null, null),
    Black_Pawn      (ChessPieceId.Black, ChessPieceId.Pawn),
    Black_Knight    (ChessPieceId.Black, ChessPieceId.Knight),
    Black_Bishop    (ChessPieceId.Black, ChessPieceId.Bishop),
    Black_Rook      (ChessPieceId.Black, ChessPieceId.Rook),
    Black_Queen     (ChessPieceId.Black, ChessPieceId.Queen),
    Black_King      (ChessPieceId.Black, ChessPieceId.King),
    SkipB           (null, null),
    Color_Mask      (null, null);

    private final ChessPieceId color;
    private final ChessPieceId type;

    ChessPieceId(ChessPieceId pColor, ChessPieceId pType){
        this.color = pColor;
        this.type = pType;
    }

    ChessPieceId color() { return color; }
    ChessPieceId type() { return type; }

    // & operator should be built in. I considered an EnumSet but...
    ChessPieceId and(ChessPieceId pSecond) {
        switch(ChessPieceId.this.ordinal() & pSecond.ordinal()) {
        case 0: //None.ordinal() etc. [if only Java were smarter]
            return None;
        case 1:  return Pawn;
        case 2:  return Knight;
        case 3:  return Bishop;
        case 4:  return Rook;
        case 5:  return Queen;
        case 6:  return King;
        case 7:  return Type_Mask;
        case 8:  return White;
        case 9:  return White_Pawn;
        case 10: return White_Knight;
        case 11: return White_Rook;
        case 12: return White_Bishop;
        case 13: return White_Queen;
        case 14: return White_King;
        //case 15: return SkipA;
        case 16: return Black;
        case 17: return Black_Pawn;
        case 18: return Black_Knight;
        case 19: return Black_Rook;
        case 20: return Black_Bishop;
        case 21: return Black_Queen;
        case 22: return Black_King;
        //case 23: return SkipB;
        case 24: return Color_Mask;
        default:
            return None;
        }
    }
}

Clearly I only need one or the other (the and operation, or the initialized values). Also it sure would be great if I could use an enum type in it's own definition, but I can't. So lines like:

    Bishop          (null, ChessPieceId.Bishop),
    ...
    White           (ChessPieceId.White, null),

Are out.

What was my question? Is there a better way that I'm missing. Also can I parse an int of the ordinal into an enum defined value, avoiding that whole case statement?

+3  A: 

In the Card example in the Sun tutorial on Enums, they represent a deck of cards as two Enums - Rank and Suit. This is isomorphic to your problem. See http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html.

Note that you can augment an Enum with any extra values you like by defining an appropriate constructor, so you are not bound to ordinal values, either.

danben
That is a class with two enums; yeah it's great. I was literally trying to stay analogous to representing a board as essentially an array of ints. It sure is the first better way to do it that springs to mind though.
dlamblin
+2  A: 

Wouldn't it make sense to define a class for the chess piece?

Then you can stick two enums in it. Imagine that.

Anon.
Or the piece can still be an enum while keeping a field for color and a field for type.
Michael Myers
I almost thought this was a duplicate of my answer, but then I saw that you had added a sarcastic comment which mine clearly lacks. +1.
danben
Sure my imagination went: I like C.
dlamblin
Imagine a beowulf cluster of those!
Thorbjørn Ravn Andersen
+1  A: 

Why do you insist on the usage of Enums? It's clearly not the proper tool.

I would have a Token interface, and have it implemented in a class with it's color and type (the type and color being two different enums). You may even have multiple implementations for the different type of pieces so that you can put right into the class itself it's movement behavior....

Johnco
Because the chess pieces don't do anything actually. I just need a representation of a board state; I'm never even going to move them, this isn't actually chess, just a captured game state.
dlamblin
Ok, so go for a simple class with 2 attributes (enums) for type and color. Should work really easily and be much clearer.
Johnco
+1  A: 

Just a modeling suggestion...

I'd probably start out with a ChessPiece object that had an abstract canMoveTo() method. It would be overridden by pawn, bishop, king, ... The White/Black is an attribute of the piece.

So from your code, it always follows the same pattern, piece.canMoveTo(x1,y1); or piece.move(x1,y1);

Always think in terms of asking your object to do something--not operating on attributes of your object from outside (from another object).

As for the enum problem, I wouldn't worry about optimizing the solution until you had a great general purpose solution coded and you find that it isn't fast/small/cool enough to meet your requirements. It may turn out that using enums doesn't even fit into your final model. I'd even leave bitwise operations out for a bit.

EDIT:

I've heard some pretty strong arguments stating that going sub-int for Java slows everything down quite a bit. For instance, using byte arrays or bitfields. If you absolutely need them for space, fine, but don't use them because you think they are going to make your code faster--they just mess up caching and optimizing routines.

For instance, declaring a variable "byte b;" still takes up an entire int in ram, but forces an extra masking operation and additional validations every time you access it. I'm not sure if the same thing applies to bit operations, but what you are trying to do really feels like premature optimization to me--one of the biggest programming evils.

Bill K