天天看点

win8 开发之旅(5) --五子棋游戏开发

    上节说道了对这个游戏  面向对象的分析,各位读者 ,有什么不懂,尽情给我留言把!!!!!!

闲话少说,这节我们对 游戏的实体类,先进行伪代码分析,然后进行源代码的分析。

           我们先看这些类的整体的架构如下图所示:

win8 开发之旅(5) --五子棋游戏开发

            piece类  代表相应的棋子的类, 他应该有 位置这个属性,而位置的最好的体现是通过纵坐标横坐标来体现;相应形状这个属性代表是黑色用户控件还是白棋的用户控件。

             类型属性代表代表这是黑棋, 还是白棋;

           索引属性  插入相应的索引的位置。  相应源代码如下:

          private usercontrol _shape;

         private position _position;

       ///

      /// gets or sets the x.

     ///

    /// the x.

     public int x

 {

    get { return _position.x; }

     set { _position.x = value; }

  }

   ///

   /// gets or sets the y.

  /// the y.

   public int y

  {

   get { return _position.y; }

   set { _position.y = value; }

  ///

  /// gets or sets the position.

  /// the position.

  public position position

get { return _position; }

set { _position = value; }

}

  public piecetype type { get; set; }

  public int index { get; set; }

 /// initializes a new instance of the class.

 //

  public piece()

  _position = new position(0,0);

  /// gets the shape.

  /// the shape.

  public usercontrol shape

 get

 if (_shape == null)

 if (type == piecetype.black)

 _shape = new blackpiececomponent();

 if (type == piecetype.white)

 _shape = new whitepiececomponent();

 }

 return _shape;

   pieceboard(棋盘) 这个类 是用于对棋盘进行面向对象的处理后的类,   他有那些属性和方法了

    首先,考虑整块游戏只需要一块棋盘,因此我们是不是用单例模式来产生这快棋盘。 说道单例模式的话,有一个 相应类事例和产生单独一个对象的方法。

  棋盘棋盘本身就是盛放棋子的容器,因此我们这里需要一个盛放棋子的数组,为什么是数组啊? 因为棋盘是15*15方格子。

    相应的源代码如下

    public class pieceboard

   {

    static pieceboard _instance { get; set; }

    public piece[,] pieces { get; set; }  

   private pieceboard()

  init();

  private void init()

   pieces = new piece[appconfig.boardwidth, appconfig.boardwidth];

    }

    public static pieceboard getpieces()

  lock (_instance)

  if (_instance==null)

   _instance = new pieceboard();

   }

  return _instance;

   怎么知道谁赢了,当然是有一套复杂判断连五子的方法,显然,我们需要一套复杂,的算法。这节我们不说他,说一说储存胜利结果的类。

   winningresult 他有哪些属性,谁取胜的一个枚举的属性,一个那种棋子属性,一个棋子的泛型数组。可能读者就纳闷了,前两个属性需要

  还好理解,而一个棋子的泛型数组  是干嘛的,首先他不是吃饭的。他是把每个胜利结果下的棋子储存起来,以做以后判断使用。

    相应的源代码如下:

   public class winningresult

  public winningtype wtype { get; set; }

  public piecetype ptype { get; set; }

  public list piecs;

  public winningresult()

  piecs = new list();

  这里说他两个枚举类型,源代码就不用多贴了,piecetype棋子的类型分别 代表的   白色、黑色类型。  winningtype 胜利的类型,        有水平,垂直,斜起方向。

你会问我为什么要枚举,简而言之,就是避免**数字, 更好的见名之义。 增强源代码的可读性。      是1更好理解, 还是black更好的理解,你说了

我这节说的够多了,就此休笔把!!!      欲知后事如何,且听下回分解。