我正在开发巴洛克国际象棋游戏的爱好项目.对于那些没有玩过它的人,它具有与国际象棋相同的基本规则,但移动和捕捉的方法是不同的.
当然,我创建的标准类游戏:GameState,Board,Square,和指定,每件从一个继承类BasePiece.
每件作品都有2个主要的虚拟方法,GetPossibleMoves(Board board)和GetCapturedSquares(Board board, Square toSquare).
现在,其中一件作品,模仿者,通过"模仿"它捕获的作品来捕捉碎片.例如,Long Leaper可以通过跳过它们捕获碎片.这意味着模仿者可以跳过敌人的Long Leapers捕捉它们(但不能跳过任何其他东西).
我完成了GetCapturedSquares()除模仿者之外的所有部分的功能(这绝对是最狡猾的部分).
我对模仿者的基本算法是:
由于我已经为其他部分的动作编写了代码,我想我会实例化新的部分,并GetCapturedSquares()根据敌人的哪种类型使用他们的方法.为此,我设置了一个Dictionary如您所见,将a映射System.Type到所述类型的实例化对象:
var typeToPiece = new Dictionary<Type, BasePiece>() 
{
    {typeof(Pincer), new Pincer() { Color = this.Color, CurrentSquare = this.CurrentSquare}},
    {typeof(Withdrawer), new Withdrawer() { Color = this.Color, CurrentSquare = this.CurrentSquare }},
    {typeof(Coordinator), new Coordinator() { Color = this.Color, CurrentSquare = this.CurrentSquare }},
    {typeof(LongLeaper), new LongLeaper() { Color = this.Color, CurrentSquare = this.CurrentSquare }},
    {typeof(King), new King() { Color = this.Color, CurrentSquare = this.CurrentSquare }},
};
//...
var possibleMoves = typeToPiece[enemySquare.Occupant.GetType()].GetPossibleMoves(board, toSquare);
这样做会让我内心感到肮脏.创建一个enum或string表示作为字典键的片段类型更合适,还是真的无关紧要?有没有不同的方法来处理这个?我认为它的方式很好,但我有兴趣听听你的想法.
我认为你应该添加一个abstract方法来BasePiece"克隆"当前的片段并返回模拟片段.
你override在每种作品类型中使用这种方法.要在它们之间共享代码,可以protected向基类添加一个方法,将共享属性复制到传递给它的实例:
abstract class BasePiece {
   protected BasePiece(BasePiece pieceToClone) {
      this.Color = pieceToClone.Color;
      this.CurrentSquare = pieceToClone.CurrentSquare;
   }
   public abstract BasePiece GetSimulatedClone();
}
class King : BasePiece {
   protected King(King pieceToClone) : base(pieceToClone) { }
   public King() { }
   public override BasePiece GetSimulatedClone() {
       return new King(this);
   }
}
一般来说,每当你根据变量的类型进行切换时,你应该三思而后行,看看你是否可以改变多态性.