我试图了解如何以面向对象的方式进行设计和思考,并希望从社区得到一些关于这个主题的反馈。下面是我希望以面向对象方式设计的一个国际象棋游戏示例。这是一个非常广泛的设计,我在这个阶段的重点只是确定谁负责什么消息以及对象如何相互作用来模拟游戏。请指出是否存在糟糕的设计元素(高耦合、不良内聚等) ,以及如何改进它们。
国际象棋游戏有以下类别
The Board is made up of squares and so Board can be made responsible for creating and managing Square objects. Each piece also is on a square so each piece also has a reference to the square it is on. (Does this make sense?). Each piece then is responsible to move itself from one square to another. Player class holds references to all pieces he owns and is also responsible for their creation (Should player create Pieces?) . Player has a method takeTurn which in turn calls a method movePiece which belongs to the piece Class which changes the location of the piece from its current location to another location. Now I am confused on what exactly the Board class must be responsible for. I assumed it was needed to determine the current state of the game and know when the game is over. But when a piece changes it's location how should the board get updated? should it maintain a seperate array of squares on which pieces exist and that gets updates as pieces move?
此外,棋盘游戏最初创建的董事会和玩家对象,谁反过来创建正方形和块分别开始模拟。简而言之,这可能就是象棋游戏中的代码的样子
Player p1 =new Player();
Player p2 = new Player();
Board b = new Board();
while(b.isGameOver())
{
p1.takeTurn(); // calls movePiece on the Piece object
p2.takeTurn();
}
我不清楚董事会的情况将如何更新。作品应该有一个参考板?责任在哪里?谁持有什么推荐信?请帮助我与您的投入和指出问题在这个设计。我故意不关注任何算法或游戏的进一步细节,因为我只对设计方面感兴趣。我希望这个社区能提供有价值的见解。