flex 聯(lián)機游戲開發(fā) - 中國象棋游戲:(一)核心邏輯
在開發(fā)四國軍棋的游戲中,通過 flex聯(lián)機游戲開發(fā)- 四國軍棋游戲(五)-提煉棋類開發(fā)api,我們提煉出了第一個關于棋類游戲開發(fā)的api-FlexChessAPI,這個api設計的方針就是基于狀態(tài)機與事件驅(qū)動的flex主要機制,使開發(fā)工作簡潔易行。現(xiàn)在,我們第一次使用這個api來開發(fā)一款中國象棋游戲,對一個成熟的開發(fā)工作者來說,我相信,你大概只需要半天時間就可以讓這個象棋游戲運作起來。
現(xiàn)在,我們將中國象棋的邏輯出來。
1)中國象棋由9*10個方格,與四國軍棋不同,棋子本身并無行棋路線,
也就是說我們只需要簡單擴展ipostion 形成posiitonVO類即可
2)中國象棋的棋子本身無大小,但每個子有自己的行棋路線,吃子的方式是目標子正好處于該子的行棋路線上,另外,其它的子的行為會對目標子的行棋路線造成影響。
也就是說,不論是移動,吃子等,我們都無法立即獲得該子的可移動路線,只有等對方行完棋后才能確定行棋路線,所以我們選擇的計算行棋路線的方式應該定位在 選中源棋子的時候。
下面,我設計一個ChinaChessHelp類,類中的所有方法與變量都是靜態(tài)類。
2.1 列出所有棋子,(注意,紅方的相與黑方的象不但顯示名稱不同,行棋路線也不同,這樣,我就將他們處理成兩上不同的棋子),至于棋子的值,并不重要,只是一個區(qū)別作用。
//棋子
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 設計棋子的移動路線,這是中國象棋最核心的邏輯。請參與這個圖,我設計出每個子的行棋路線,對于一些行棋路線有限而且透明的棋子,我直接索引出值(如士,象,師),對一些行棋路線不確定的棋子,我們計算出他的行棋路線,如車,馬,炮等。所有的索引請參照下面這個圖像(棋盤的繪制我在下一節(jié)將講到)。
2.2.1 函數(shù) 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 函數(shù) 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中當數(shù)組只有一個元素時,最好不要直接用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
車,馬,炮屬于自由移動棋子,對紅黑雙方是對等存在,所以可以使用公用函數(shù)進行處理。
馬的游戲邏輯主要就是判斷目標點位是否在棋盤中以及是不是有蹩腳點位存在。
/**
* 馬的行棋路線
* 每個馬有八個行棋位置,有四個蹩腳點位,
* @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++)
{
主站蜘蛛池模板:
中文字幕乱码在线播放|
亚洲熟妇熟女久久精品一区|
国产一区二区三区导航|
久久精品国产久精国产一老狼|
国产精品免费AⅤ片在线观看|
精品国产成人a在线观看|
亚洲人妻精品中文字幕|
国产日韩精品一区二区三区在线|
四虎国产精品永久地址99|
国产肥妇一区二区熟女精品|
亚洲精品国产福利一区二区|
无套内谢少妇高清毛片|
日韩乱码卡一卡2卡三卡四|
国产精品中文字幕第一页|
最近中文字幕免费手机版|
亚洲熟女精品一区二区|
国产精品日韩av在线播放|
成人免费av在线观看|
日韩国产中文字幕精品|
国产精品无遮挡猛进猛出|
少妇无套内射中出视频|
人妻丰满熟妇无码区免费|
精品午夜福利无人区乱码|
午夜福利国产区在线观看|
久久天天躁夜夜躁狠狠85|
国产免费无遮挡吸奶头视频|
国内熟妇与亚洲洲熟妇妇|
国产精品亚洲А∨天堂免下载|
国产精品成人中文字幕|
东京热高清无码精品|
色综合亚洲一区二区小说|
无码日韩精品一区二区三区免费|
日本视频一两二两三区|
A级日本乱理伦片免费入口|
集贤县|
亚洲精品综合网二三区|
精品国偷自产在线视频99|
影音先锋亚洲成aⅴ人在|
日本高清视频色wwwwww色|
男女性高爱潮免费网站|
久久综合综合久久综合|