面向对象的象棋游戏设计 [英] Object Oriented Design for a Chess game

查看:269
本文介绍了面向对象的象棋游戏设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以面向对象的方式感受如何设计和思考,并希望从社区获得有关此主题的一些反馈。以下是我想以OO方式设计的象棋游戏的例子。这是一个非常广泛的设计,我在这个阶段的重点只是确定谁负责什么消息,以及对象如何相互交互来模拟游戏。请指出,如果有不良设计(高耦合,不良内聚等)的元素以及如何改进它们。

I am trying to get a feel of how to design and think in an Object Oriented manner and want to get some feedback from the community on this topic. The following is an example of a chess game that I wish to design in an OO manner. This is a very broad design and my focus at this stage is just to identify who is responsible for what messages and how the objects interact each other to simulate the game. Please point out if there are elements of bad design (high coupling, bad cohesion etc.) and how to improve on them.

国际象棋游戏有以下课程

The Chess game has the following classes


  • Board

  • 玩家

  • Piece

  • Square

  • ChessGame

  • Board
  • Player
  • Piece
  • Square
  • ChessGame

董事会由广场组成,因此董事会可以负责创建和管理Square对象。每一块也在一个正方形上,所以每一块也有一个参考它的正方形。 (这是否有意义?)。每一件事都有责任将自己从一个广场移到另一个方块。
玩家类持有对自己拥有的所有作品的引用,并且还负责他们的创作(玩家是否创建片段?)。玩家有一个方法takeTurn,它依次调用属于该棋子的方法movePiece,该棋子将棋子的位置从当前位置更改为另一个位置。现在我对于董事会班级必须负责什么感到困惑。我认为需要确定游戏的当前状态,并知道游戏结束。但是当一件事改变它的位置时,董事会应该如何更新?它应该保持一个独立的广场阵列,哪些片段存在,并且随着片段移动而获得更新?

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?

此外,ChessGame还创建了板和玩家对象,然后分别创建正方形和碎片,并开始模拟。简单来说,这可能是ChessGame中的代码可能看起来像

Also, ChessGame intially creates the Board and player objects who in turn create squares and pieces respectively and start the simulation. Briefly, this might be what the code in ChessGame may look like

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();

}

我不清楚董事会的状态如何更新。板块有参考板吗?哪里应该是责任谎言?谁持有什么参考?请帮助我,并指出这个设计中的问题。我故意不专注于游戏的任何算法或进一步细节,因为我只对设计方面感兴趣。我希望这个社区能够提供宝贵的见解。

I am unclear on how the state of the board will get updated. Should piece have a reference to board? Where should be the responsibility lie? Who holds what references? Please help me with your inputs and point out problems in this design. I am deliberately not focusing on any algorithms or further details of game play as I am only interested in the design aspect. I hope this community can provide valuable insights.

推荐答案

我实际上只是写了一个完整的C#执行棋盘,棋子,规则等这大概是我如何建模的(实际的实现被删除了,因为我不想把所有的从你的编码的乐趣):

I actually just wrote a full C# implementation of a chess board, pieces, rules, etc. Here's roughly how I modeled it (actual implementation removed since I don't want to take all the fun out of your coding):

public enum PieceType {
    None, Pawn, Knight, Bishop, Rook, Queen, King
}

public enum PieceColor {
    White, Black
}

public struct Piece {
    public PieceType Type { get; set; }
    public PieceColor Color { get; set; }
}

public struct Square {
    public int X { get; set; }
    public int Y { get; set; }

    public static implicit operator Square(string str) {
        // Parses strings like "a1" so you can write "a1" in code instead
        // of new Square(0, 0)
    }
}

public class Board {
    private Piece[,] board;

    public Piece this[Square square] { get; set; }

    public Board Clone() { ... }
}

public class Move {
    public Square From { get; }
    public Square To { get; }
    public Piece PieceMoved { get; }
    public Piece PieceCaptured { get; }
    public PieceType Promotion { get; }
    public string AlgebraicNotation { get; }
}

public class Game {
    public Board Board { get; }
    public IList<Move> Movelist { get; }
    public PieceType Turn { get; set; }
    public Square? DoublePawnPush { get; set; } // Used for tracking valid en passant captures
    public int Halfmoves { get; set; }

    public bool CanWhiteCastleA { get; set; }
    public bool CanWhiteCastleH { get; set; }
    public bool CanBlackCastleA { get; set; }
    public bool CanBlackCastleH { get; set; }
}

public interface IGameRules {
    // ....
}

基本思想是Game / Board / etc只是存储游戏的状态。你可以将它们操纵到例如设置一个位置,如果这是你想要的。我有一个类实现我的IGameRules接口,负责:

The basic idea is that Game/Board/etc simply store the state of the game. You can manipulate them to e.g. set up a position, if that's what you want. I have a class that implements my IGameRules interface that is responsible for:


  • 确定哪些动作有效,包括castling和en passant。 li>
  • 确定具体行动是否有效。

  • 确定玩家何时进行支票/支票/停滞。

  • 执行动作

将规则与游戏/棋盘类分开也意味着您可以相对轻松地实施变体。规则界面的所有方法都需要一个游戏对象,他们可以检查它们以确定哪些移动是有效的。

Separating the rules from the game/board classes also means you can implement variants relatively easily. All methods of the rules interface take a Game object which they can inspect to determine which moves are valid.

注意我不会在游戏之间存储玩家信息。我有一个单独的课程,负责存储游戏元数据,如玩游戏,游戏发生时等。

Note that I do not store player information on Game. I have a separate class Table that is responsible for storing game metadata such as who was playing, when the game took place, etc.

编辑:请注意,此答案的目的不是真正给您可以填写的模板代码 - 我的代码实际上有更多的信息存储在每个项目上,更多方法等。目的是指导您实现您要实现的目标。

Note that the purpose of this answer isn't really to give you template code you can fill out -- my code actually has a bit more information stored on each item, more methods, etc. The purpose is to guide you towards the goal you're trying to achieve.

这篇关于面向对象的象棋游戏设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆