LiXie LiXie - 28 days ago 5
Java Question

How do I refer to the grouping of my enums? Basic chess game, wanting to refer to all black/white pieces efficiently

So I have represented different chess pieces as a set of enum called

Chessmen
. (It is a requirement that I set them out like this). I thought it would be a good idea to group these
Chessmen
into a
WHITE
group and a
BLACK
group so for certain situations, I can refer to all white chess pieces or all black pieces.

public enum Chessmen {


WHITE_KING (Group.WHITE),
WHITE_QUEEN (Group.WHITE),
WHITE_ROOK (Group.WHITE),
WHITE_BISHOP (Group.WHITE),
WHITE_KNIGHT (Group.WHITE),
WHITE_PAWN (Group.WHITE),
BLACK_KING (Group.BLACK),
BLACK_QUEEN (Group.BLACK),
BLACK_ROOK (Group.BLACK),
BLACK_BISHOP (Group.BLACK),
BLACK_KNIGHT (Group.BLACK),
BLACK_PAWN (Group.BLACK),
EMPTY (Group.NEUTRAL);

private Group group;

Chessmen(Group group){
this.group = group;
}

public enum Group {
WHITE,
BLACK,
NEUTRAL;
}
}


So for example in the code below I wouldn't have to refer to all of the individual black pieces or white pieces like I have done below (this code 100% works as intended):

if (isWhiteMove){
//Moving the wrong colour piece
if ((chessboard[preMoveX][preMoveY] == Chessmen.BLACK_KING) || (chessboard[preMoveX][preMoveY] == Chessmen.BLACK_ROOK) ||
(chessboard[preMoveX][preMoveY] == Chessmen.BLACK_BISHOP) || (chessboard[preMoveX][preMoveY] == Chessmen.BLACK_QUEEN) ||
(chessboard[preMoveX][preMoveY] == Chessmen.BLACK_KNIGHT) || (chessboard[preMoveX][preMoveY] == Chessmen.BLACK_PAWN)){
//System.out.println("It is white's move, not black's.");
LOGGER.setLevel(Level.SEVERE);
LOGGER.severe("It is white's move, not black's.");
invalidCounter++;
}

//Moving onto one of your own pieces
if ((chessboard[postMoveX][postMoveY] == Chessmen.WHITE_KING) || (chessboard[postMoveX][postMoveY] == Chessmen.WHITE_ROOK) ||
(chessboard[postMoveX][postMoveY] == Chessmen.WHITE_BISHOP) || (chessboard[postMoveX][postMoveY] == Chessmen.WHITE_QUEEN) ||
(chessboard[postMoveX][postMoveY] == Chessmen.WHITE_KNIGHT) || (chessboard[postMoveX][postMoveY] == Chessmen.WHITE_PAWN)){
//System.out.println("You cannot land on one of your own pieces.");
LOGGER.setLevel(Level.SEVERE);
LOGGER.severe("You cannot land on one of your own pieces.");
invalidCounter++;
}
}


I have tried a few suggestions based on my own searches, but I can't quite get it right. You can see the
if
statement is quite laborious to look at and to maintain. So below is an example of how I would like my code to look like, but as mentioned, not quite sure how to properly implement it:

if (isWhiteMove){
//Moving the wrong colour piece
if ((chessboard[preMoveX][preMoveY] == **ALL BLACK PIECES IN MY GROUP**){
//System.out.println("It is white's move, not black's.");
LOGGER.setLevel(Level.SEVERE);
LOGGER.severe("It is white's move, not black's.");
invalidCounter++;
}

//Moving onto one of your own pieces
if ((chessboard[postMoveX][postMoveY] == **ALL WHITE PIECES IN MY GROUP**)){
//System.out.println("You cannot land on one of your own pieces.");
LOGGER.setLevel(Level.SEVERE);
LOGGER.severe("You cannot land on one of your own pieces.");
invalidCounter++;
}
}


Any help really appreciated thank you. If any more context is needed, please tell me.

Answer

You don't want chessboard[preMoveX][preMoveY] to be equal to all the black pieces. That not possible. A single piece can't be equal to several pieces. You want it to be part of the BLACK (or WHITE) group. I.e. you want its group field to be equal to BLACK (or WHITE).

So you just want

if (chessboard[preMoveX][preMoveY].getGroup() == Chessmen.Group.BLACK)

Of course you'll need to add the getGroup() method to Chessmen, otherwise there is no way to know its group from outside of the enum, making the group field useless.