flex 联机游戏开发 - 中国象棋游戏:(一)核心逻辑

在开发四国军棋的游戏中,通过 flex联机游戏开发- 四国军棋游戏(五)-提炼棋类开发api,我们提炼出了第一个关于棋类游戏开发的api-FlexChessAPI,这个api设计的方针就是基于状态机与事件驱动的flex主要机制,使开发工作简洁易行。现在,我们第一次使用这个api来开发一款中国象棋游戏,对一个成熟的开发工作者来说,我相信,你大概只需要半天时间就可以让这个象棋游戏运作起来。

现在,我们将中国象棋的逻辑出来。

1)中国象棋由9*10个方格,与四国军棋不同,棋子本身并无行棋路线,

也就是说我们只需要简单扩展ipostion 形成posiitonVO类即可

2)中国象棋的棋子本身无大小,但每个子有自己的行棋路线,吃子的方式是目标子正好处于该子的行棋路线上,另外,其它的子的行为会对目标子的行棋路线造成影响。

也就是说,不论是移动,吃子等,我们都无法立即获得该子的可移动路线,只有等对方行完棋后才能确定行棋路线,所以我们选择的计算行棋路线的方式应该定位在 选中源棋子的时候。

下面,我设计一个ChinaChessHelp类,类中的所有方法与变量都是静态类。

2.1 列出所有棋子,(注意,红方的相与黑方的象不但显示名称不同,行棋路线也不同,这样,我就将他们处理成两上不同的棋子),至于棋子的值,并不重要,只是一个区别作用。

  //棋子
  public static const PIECE_RED_CHE:int=56;
  public static const QIZHI_RED_MA:int=57;
  public static const QIZHI_RED_XIAN:int=58;
  public static const QIZHI_RED_SHI:int=59;
  public static const QIZHI_RED_JIAN:int=60;
  public static const QIZHI_RED_BING:int=55;
  public static const QIZHI_RED_PAO:int=54;
  
  public static const PIECE_BLACK_CHE:int=156;
  public static const QIZHI_BLACK_MA:int=157;
  public static const QIZHI_BLACK_XIAN:int=158;
  public static const QIZHI_BLACK_SHI:int=159;
  public static const QIZHI_BLACK_SUAN:int=160;
  public static const QIZHI_BLACK_ZHU:int=155;
  public static const QIZHI_BLACK_PAO:int=154;

2.2 设计棋子的移动路线,这是中国象棋最核心的逻辑。请参与这个图,我设计出每个子的行棋路线,对于一些行棋路线有限而且透明的棋子,我直接索引出值(如士,象,师),对一些行棋路线不确定的棋子,我们计算出他的行棋路线,如车,马,炮等。所有的索引请参照下面这个图像(棋盘的绘制我在下一节将讲到)。

2.2.1 函数 initMoveRedJian():ArrayCollection

将的行棋路线 用switch case来描述就是如下

/**
   * 紅方將的行棋路線
   * @param posindex
   * @return
   *
   */  
  public static function initMoveRedJian(posindex:int):ArrayCollection{
   var temp:ArrayCollection;
   switch(posindex){
    case 3:
     temp=new ArrayCollection([4,12]);
     break;
    case 4:
     temp=new ArrayCollection([3,5,13]);
     break;
    case 5:
     temp=new ArrayCollection([4,14]);
     break;
    case 12:
     temp=new ArrayCollection([3,13,21]);
     break;
    case 13:
     temp=new ArrayCollection([4,12,14,22]);
     break;
    case 14:
     temp=new ArrayCollection([5,13,23]);
     break;
    case 21:
     temp=new ArrayCollection([12,22]);
     break;
    case 22:
     temp=new ArrayCollection([13,21,23]);
     break;
    case 23:
     temp=new ArrayCollection([14,22]);
     break;
    default:
     break;
   }
   return temp;
  }

 

2.2.2 函数 initMoveRedShi():ArrayCollection

士的行棋路线 用switch case来描述就是如下

/**
   * 紅方士的行棋路線
   * @param posindex
   * @return
   *
   */  
  public static function initMoveRedShi(posindex:int):ArrayCollection{
   var temp:ArrayCollection;
   switch(posindex){
    case 3:
     temp=new ArrayCollection([13]);
     break;
    case 5:
     temp=new ArrayCollection([13]);
     break;
    case 13:
     temp=new ArrayCollection([3,5,21,23]);
     break;
    case 21:
     temp=new ArrayCollection([13]);
     break;
    case 23:
     temp=new ArrayCollection([13]);
     break;
    default:
     break;
   }
   return temp;
  }

逻辑是这样,但上面的写法是错误的,flex中当数组只有一个元素时,最好不要直接用arraycollection进行初始化。您可以修改一下:)

2.2.3 红方象的行棋路线 initMoveRedXian

/**
   * 紅方象的行棋路線
   * 注意蹩脚路线。
   * @param posindex
   * @return
   *
   */ 


  public static function initMoveRedXian(posindex:int,pieceMap:HashMap):ArrayCollection{
   var temp:ArrayCollection=new ArrayCollection();
   switch(posindex){
    case 2:
     if ((pieceMap.get(10) as PieceVO).deskpos==-1)
      temp.addItem(18);
     if ((pieceMap.get(12) as PieceVO).deskpos==-1)
      temp.addItem(22);
     break;
    case 6:
     if ((pieceMap.get(14) as PieceVO).deskpos==-1)
      temp.addItem(22);
     if ((pieceMap.get(16) as PieceVO).deskpos==-1)
      temp.addItem(26);
     break;
    case 18:
     if ((pieceMap.get(10) as PieceVO).deskpos==-1)
      temp.addItem(2);
     if ((pieceMap.get(28) as PieceVO).deskpos==-1)
      temp.addItem(38);
     break;
    case 22:
     if ((pieceMap.get(12) as PieceVO).deskpos==-1)
      temp.addItem(2);
     if ((pieceMap.get(14) as PieceVO).deskpos==-1)
      temp.addItem(6);
     if ((pieceMap.get(30) as PieceVO).deskpos==-1)
      temp.addItem(38);
     if ((pieceMap.get(32) as PieceVO).deskpos==-1)
      temp.addItem(42);
     break;
    case 26:
     if ((pieceMap.get(16) as PieceVO).deskpos==-1)
      temp.addItem(6);
     if ((pieceMap.get(34) as PieceVO).deskpos==-1)
      temp.addItem(42);
     
     break;
    case 38:
     if ((pieceMap.get(28) as PieceVO).deskpos==-1)
      temp.addItem(18);
     if ((pieceMap.get(30) as PieceVO).deskpos==-1)
      temp.addItem(22);
     break;
    case 42:
     if ((pieceMap.get(32) as PieceVO).deskpos==-1)
      temp.addItem(22);
     if ((pieceMap.get(34) as PieceVO).deskpos==-1)
      temp.addItem(26);
     break;
    default:
     break;
   }
   return temp;
  }

2.2.4 红方兵的行棋路线 public static function initMoveRedBing():ArrayCollection

注意的是,并没有过河时,只能向前走,过河后则可以有三个位置可以走。

利用posindex的值进行判断

/**
   * 紅方并的行棋路線
   * @param posindex
   * @return
   *
   */ 
  public static function initMoveRedBing(posindex:int,pieceMap:HashMap):ArrayCollection{
   var temp:ArrayCollection=new ArrayCollection();
   if (posindex%9-1>=0&&posindex>=45) temp.addItem(posindex-1);
   if (posindex%9+1<9&&posindex>=45) temp.addItem(posindex+1);
   if (posindex+9<90) temp.addItem(posindex+9);
   return temp;
  }

2.2.5 马的行棋路线 public static function initMoveRedBing():ArrayCollection

车,马,炮属于自由移动棋子,对红黑双方是对等存在,所以可以使用公用函数进行处理。

马的游戏逻辑主要就是判断目标点位是否在棋盘中以及是不是有蹩脚点位存在。

 /**
   * 马的行棋路線
   * 每个马有八个行棋位置,有四个蹩脚点位,
   * @param posindex
   * @return
   *
   */  
  public static function initMoveMa(posindex:int,pieceMap:HashMap):ArrayCollection{
   var temp:ArrayCollection=new ArrayCollection();
   
   //左方向
   if (posindex%9-2>=0) {
     if ((pieceMap.get(posindex-1) as PieceVO).deskpos==-1)
     {
      if ((posindex-11)>=0)
      {
       temp.addItem(posindex-11);
      }
      if ((posindex+7)<89)
      {
       temp.addItem(posindex+7);
      }
      
     }
   }
   //右方向
   if (posindex%9+2<9) {
    
    if ((pieceMap.get(posindex+1) as PieceVO).deskpos==-1)
    {
     if ((posindex-7)>=0)
     {
      temp.addItem(posindex-7);
     }
     if ((posindex+11)<89)
     {
      temp.addItem(posindex+11);
     }
     
    }
   }
   //上方向
   if (posindex-18>=0) {
    if ((pieceMap.get(posindex-9) as PieceVO).deskpos==-1)
    {
     if ((posindex-19)%9<posindex%9)
     {
      temp.addItem(posindex-19);
     }
     if ((posindex-17)%9>posindex%9)
     {
      temp.addItem(posindex-17);
     }
     
    }
   }
   //下方向
   if (posindex+18<90) {
    if ((posindex+19)%9>posindex%9)
    {
     temp.addItem(posindex+19);
    }
    if ((posindex+17)%9<posindex%9)
    {
     temp.addItem(posindex+17);
    }
   }
   return temp;
  }

 

2.2.6 车的行棋路线

车的行棋逻辑就是查看四个方向是否有空的位置,如果找到一个棋子,则结束寻找,将当前找到的棋子也加入索引。

 /**
   * 车的行棋路線
   * @param posindex
   * @return
   *
   */  
  public static function initMoveChe(posindex:int,pieceMap:HashMap):ArrayCollection{
   var temp:ArrayCollection=new ArrayCollection();
   //模方向
   for (var xr:int=posindex%9+1;xr<9;xr++)
   {
    if ((pieceMap.get(posindex+xr-posindex%9) as PieceVO).deskpos==-1)
    {
     temp.addItem(posindex+xr-posindex%9);
    }
    else
    {
     //将找到最后一子的位置也加入
     temp.addItem(posindex+xr-posindex%9);
     break;
    }
   }
   for (var xl:int=posindex%9-1;xl>=0;xl--)
   {
    if ((pieceMap.get(posindex+xl-posindex%9) as PieceVO).deskpos==-1)
    {
     temp.addItem(posindex+xl-posindex%9);
    }
    else
    {
     temp.addItem(posindex+xl-posindex%9);
     break;
    }
   }
   //竖方向
   for (var yb:int=Math.floor(posindex/9)+1;yb<10;yb++)
   {
    if ((pieceMap.get(posindex+9*(yb-Math.floor(posindex/9))) as PieceVO).deskpos==-1)
    {
     temp.addItem(posindex+9*(yb-Math.floor(posindex/9)));
    }
    else
    {
     temp.addItem(posindex+9*(yb-Math.floor(posindex/9)));
     break;
    }
   }
   for (var yt:int=Math.floor(posindex/9)-1;yt>=0;yt--)
   {
    if ((pieceMap.get(posindex+9*(yt-Math.floor(posindex/9))) as PieceVO).deskpos==-1)
    {
     temp.addItem(posindex+9*(yt-Math.floor(posindex/9)));
    }
    else
    {
     temp.addItem(posindex+9*(yt-Math.floor(posindex/9)));
     break;
    }
   }
   return temp;
  }

 

2.2.7 炮的行棋路线

炮的行棋路线相对复杂些,不过也很好处理,基本情况与车相同,只不过当你在某一方向找到第一个棋子后,设置一个判断,判断为找到一个棋子后,对空余的位置不再添加,则再找第二个棋子,如果找到第二个棋子,将第二个棋子加入索引

/**
   * 炮的行棋路線
   * @param posindex
   * @return
   *
   */  
  public static function initMovePao(posindex:int,pieceMap:HashMap):ArrayCollection{
   var temp:ArrayCollection=new ArrayCollection();
   
   //模方向
   var findOne:Boolean=false;
   for (var xr:int=posindex%9+1;xr<9;xr++)
   {
    if ((pieceMap.get(posindex+xr-posindex%9) as PieceVO).deskpos==-1)
    {
     if (!findOne)
      temp.addItem(posindex+xr-posindex%9);
    }
    else
    {
     if (!findOne)
     {
      findOne=true;
      continue;
     }
     else
     {
      //将找到子的下一子的位置也加入
      temp.addItem(posindex+xr-posindex%9);
      break;
     }
     
    }
   }
   findOne=false;
   for (var xl:int=posindex%9-1;xl>=0;xl--)
   {
    if ((pieceMap.get(posindex+xl-posindex%9) as PieceVO).deskpos==-1)
    {
     if (!findOne)
      temp.addItem(posindex+xl-posindex%9);
    }
    else
    {
     if (!findOne)
     {
      findOne=true;
      continue;
     }
     else
     {
      //将找到子的下一子的位置也加入
      temp.addItem(posindex+xl-posindex%9);
      break;
     }
     
    }
   }
   //竖方向
   findOne=false;
   for (var yb:int=Math.floor(posindex/9)+1;yb<10;yb++)
   {
    if ((pieceMap.get(posindex+9*(yb-Math.floor(posindex/9))) as PieceVO).deskpos==-1)
    {
     if (!findOne)
      temp.addItem(posindex+9*(yb-Math.floor(posindex/9)));
    }
    else
    {
     if (!findOne)
     {
      findOne=true;
      continue;
     }
     else
     {
      //将找到子的下一子的位置也加入
      temp.addItem(posindex+9*(yb-Math.floor(posindex/9)));
      break;
     }
     
    }
   }
   findOne=false;
   for (var yt:int=Math.floor(posindex/9)-1;yt>=0;yt--)
   {
    if ((pieceMap.get(posindex+9*(yt-Math.floor(posindex/9))) as PieceVO).deskpos==-1)
    {
     if (!findOne)
      temp.addItem(posindex+9*(yt-Math.floor(posindex/9)));
    }
    else
    {
     if (!findOne)
     {
      findOne=true;
      continue;
     }
     else
     {
      //将找到子的下一子的位置也加入
      temp.addItem(posindex+9*(yt-Math.floor(posindex/9)));
      break;
     }
     
    }
   }
   return temp;
  }

3)中国象棋的游戏控制引擎与四国军棋并无太大差异,我们在扩展 BaseGameEngine 后,只需要覆写 CheckWin()方法,就基本可以完成大量的工作。当然,别忘了棋盘不家个初始布局,我们只需要设计一个静态布局与一个初始化方法即可。

 //默认的棋盘布局
  private const DEFAULT_LAYOUT:Array=[56,57,58,59,60,59,58,57,56,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,54,-1,-1,-1,-1,-1,54,-1,55,-1,55,-1,55,-1,55,-1,55,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,155,-1,155,-1,155,-1,155,-1,155,-1,154,-1,-1,-1,-1,-1,154,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,156,157,158,159,160,159,158,157,156];

 

public function initLayout():void{
   for (var i:int=0;i<this.DEFAULT_LAYOUT.length;i++)
   {
    var pieceVO:PieceVO=pieceMap.get(i) as PieceVO;
    pieceVO.value=DEFAULT_LAYOUT[i];
    if (int(DEFAULT_LAYOUT[i])>0&&int(DEFAULT_LAYOUT[i])<100)
    {
     pieceVO.deskpos=getNextPlayer(1).deskpos;
    }
    else if (int(DEFAULT_LAYOUT[i])>=100)
    {
     pieceVO.deskpos=currentDeskpos;
    }
   }
  }

在完成以上这些逻辑工作后,得到的结果就是下面这个,这个是点击炮后我们可以清楚地看到行棋路线索引。

 

 

核心逻辑基本就是这些,在这个寒冷的冬天里,我打算利用这些时间开发与优化一些有趣的棋牌类游戏,我的下一个目标是扎金花游戏,这是一个牌类游戏,我打算接着写一个flexcardapi,当然,本文的下一节我将提供中国象棋的扩展类的设计与显示设计。如果时间允许的话,会很快,当然,如果您是一名flex开发者,您也可以直接给我发消息,让您来完成下面的开发,我会把已经完成的设计工作教给你,您也可以学会如何使用这个api,我相信,您会找到乐趣的,您也可以开发一些更简单或复杂的游戏,如乖三棋,动物棋,或者跳棋。

posted @ 2011-01-06 11:58  博弈居  阅读(3129)  评论(7编辑  收藏  举报

我的微博