gpt4 book ai didi

java - 我如何引用枚举的分组?基本的国际象棋游戏,想要有效地引用所有黑/白棋子

转载 作者:行者123 更新时间:2023-12-01 09:15:56 25 4
gpt4 key购买 nike

所以我将不同的棋子表示为一组名为 Chessmen 的枚举。 (这是我这样设置它们的要求)。我认为将这些Chessmen分组为WHITE组和BLACK组是一个好主意,因此对于某些情况,我可以引用全白棋子或全黑棋子。

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;
}
}

因此,例如在下面的代码中,我不必像下面所做的那样引用所有单独的黑色 block 或白色 block (此代码 100% 按预期工作):

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++;
}
}

我根据自己的搜索尝试了一些建议,但不太正确。您可以看到 if 语句的查看和维护都相当费力。因此,下面是我希望我的代码是什么样子的示例,但如上所述,不太确定如何正确实现它:

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++;
}
}

非常感谢您的帮助。如果需要更多背景信息,请告诉我。

最佳答案

您不希望 chessboard[preMoveX][preMoveY] 等于所有黑色棋子。那不可能。单件不能等于多件。您希望它成为黑人(或白人)群体的一部分。 IE。您希望其 group 字段等于 BLACK(或 WHITE)。

所以你只是想要

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

当然,您需要向 Chessmen 添加 getGroup() 方法,否则无法从枚举外部知道其组,从而使 group 字段无用。

关于java - 我如何引用枚举的分组?基本的国际象棋游戏,想要有效地引用所有黑/白棋子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40548459/

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