我正在编写一个Java棋类游戏,试图解决设计问题。以下是我目前的类:
注意,在我的Player类中,我有像canMovePiece(Square start,Square end,Board board),movePiece(Square start,Square end,Board board)等方法:
我应该将这个方法移动到Board类中,这样我就可以访问板对象,而不必从方块到板吗?
非常感谢任何想法,bclayman
游戏:由两个玩家对象和一个棋盘对象组成
玩家:
棋盘:由一个2D数组(8 x 8)的方格对象组成,每个方格上可能有/可能没有棋子
国王、皇后、车、象、马、兵:
棋子:以上棋子的超类,共享功能位于此处
这里是我不喜欢的地方:我的Square类中有一个名为“board”的字段,以便我可以引用给定方格所属的棋盘。我不喜欢这种设计,因为它会导致克隆问题。
因此,我想重新编写一些棋子类的功能,而不进行这种回溯。让棋盘能够访问其每个方格很好,但我不想为Square类设置getBoard()函数。
以下是需要重写的一个函数示例:
public Square getSquareOneMoveAway(Square start, int heightChange, int widthChange) {
Square candidateSquare = start.getBoard().getNearbySquare(start, heightChange, widthChange);
if (candidateSquare != null) {
if (candidateSquare.isEmpty()) {
return candidateSquare;
} else if (! candidateSquare.getPiece().getColor().equals(start.getPiece().getColor())) {
return candidateSquare;
}
else {
return null;
}
}
else {
return null;
}
}
注意,在我的Player类中,我有像canMovePiece(Square start,Square end,Board board),movePiece(Square start,Square end,Board board)等方法:
我应该将这个方法移动到Board类中,这样我就可以访问板对象,而不必从方块到板吗?
非常感谢任何想法,bclayman