象棋編程游戲——悔棋算法

作者: 夏小冰  來源: 博客園  發布時間: 2009-11-03 09:57  閱讀: 1070 次  推薦: 0   原文鏈接   [收藏]  

“觀棋不語真君子,落子無悔大丈夫”這是小時候就知道的一句話。但是進入象棋世界不久的我,還沒有形成自己獨有的下棋風格;也沒有刻意步步為營,落一步子,就考慮好以后很多步應該怎么走。因此,在做幾乎所有單機象棋版本都會有的功能——悔棋算法的時候,對人生也進行了一些思考。

人生的每一步棋,我們不可能都是走對的,但是我們沒有悔棋功能,為了以后少走爛著.少犧牲一些本來就不多的棋子,還是要每走一步,停下來思考一下。

 

public class RegretBack
{
public int[] fromIndex=new int[200]; //記錄每走一步棋,所動的棋子
public int[] toIndex = new int[200]; //記錄每走一步棋,所動的棋子
public Point[] fromPoint=new Point[200];
public Point[] toPoint=new Point[200];
public int[] fromPointIndex=new int[200]; //記錄當前棋子位置
public int[] toPointIndex=new int[200];
public string[] Chess_Text=new string[200]; //存放每一步棋的說明文字
public int[] signRight=new int[200]; //保存哪方走棋標志
public bool[] start=new bool[200]; //悔棋操作,要恢復的標志位
public string[] wrongString=new string[200]; //記錄排斥事件的標志
public bool[] whichFangQianZou = new bool[200];//悔棋操作,要恢復的標志位
public RegretBack[] b = new RegretBack[200];
public int activeIndex; //起流動指針作用
public int tailIndex; //始終指向尾部(剛走完的最后一步)的索引
public RegretBack()
{
this.Initialize();
}
public void Initialize()
{
//初始化各數組
for(int i=0;i<200;i++)
{
fromIndex[i]=-1; toIndex[i]=-1;
fromPoint[i].X=-1; fromPoint[i].Y=-1;
toPoint[i].X=-1; toPoint[i].Y=-1;
fromPointIndex[i]=-1; toPointIndex[i]=-1;
}
for(int i=0;i<200;i++)
{
this.Chess_Text[i]="";
this.signRight[i]=0;
this.start[i]=false;
this.wrongString[i]="right";
this.whichFangQianZou[i]=false;
this.b[i] = new RegretBack();
this.b[i].Initialize();
}
//初始化索引 activeIndex=-1;
tailIndex=-1;
}
public void Initialize(int i)
{
//初始化一個數組
fromIndex[i]=-1; toIndex[i]=-1;
fromPoint[i].X=-1; fromPoint[i].Y=-1;
toPoint[i].X=-1; toPoint[i].Y=-1;
fromPointIndex[i]=-1; toPointIndex[i]=-1;
this.Chess_Text[i]="";
this.signRight[i]=0;
this.start[i]=false;
this.wrongString[i]="right";
this.whichFangQianZou[i]=false;
this.b[i].Initialize();
}
public void SubLastItem()
{
if(this.activeIndex
{
for(int i=this.activeIndex+1;i<=this.tailIndex;i++) 
this.Initialize(i);
return; 
}
this.Initialize(this.tailIndex); 

this.tailIndex--; 
this.activeIndex=this.tailIndex; 
} 
public void SaveNewItem(int fromIndex, int toIndex, Point fromPoint, Point toPoint, string Chess_Text, int signRight, bool start, string wrongString, bool whichFangQianZou, RegretBack b) 
{
if(this.activeIndex>this.tailIndex) 
{
this.activeIndex=this.tailIndex;
} 
this.activeIndex++;
if(this.activeIndex==200) 
{
MessageBox.Show("悔棋超出邊界范圍!"); 
return; 
} 
this.fromIndex[this.activeIndex]=fromIndex; 
this.toIndex[this.activeIndex]=toIndex; 
this.fromPoint[this.activeIndex]=fromPoint; 
this.toPoint[this.activeIndex]=toPoint; 
this.Chess_Text[this.activeIndex]=Chess_Text; 
this.signRight[this.activeIndex]=signRight; 
this.start[this.activeIndex]=start; 
this.wrongString[this.activeIndex]=wrongString; 
this.whichFangQianZou[this.activeIndex]=whichFangQianZou; 
RegretBack tempRegretBack = new RegretBack(); 
tempRegretBack.Initialize(); 
for(int i=0;i<90;i++) 
{
tempRegretBack.have[i] = b.have[i]; 
tempRegretBack.who[i] = b.who[i]; 
tempRegretBack.str[i] = b.str[i]; 
tempRegretBack.allPoint[i] = b.allPoint[i]; 
if(i<32) 
tempRegretBack.partPoint[i] = b.partPoint[i]; 
tempRegretBack.whichPicture[i] = b.whichPicture[i]; 
} 
tempRegretBack.rednum = b.rednum; 
tempRegretBack.blacknum = b.blacknum; 
tempRegretBack.index = b.index; 
tempRegretBack.first_X = b.first_X; 
tempRegretBack.first_Y = b.first_Y; 
tempRegretBack.height = b.height; 
tempRegretBack.width = b.width; 
this.b[this.activeIndex] = tempRegretBack; 
this.tailIndex=this.activeIndex;
} 
public int FallBack(ref int fromIndex, ref int toIndex, ref Point fromPoint, ref Point toPoint, ref string Chess_Text, ref int signRight, ref bool start, ref string wrongString, ref bool whichFangQianZou, ref RegretBack b) 
{
if(this.activeIndex<0) 
return 0; 
if(this.activeIndex>=this.tailIndex) 
this.activeIndex=this.tailIndex-1; 
if(this.activeIndex<=-1) //防止一開始就點后退
return 0; 

fromIndex=this.fromIndex[this.activeIndex]; 
toIndex=this.toIndex[this.activeIndex];
fromPoint=this.fromPoint[this.activeIndex]; 
toPoint=this.toPoint[this.activeIndex]; 
Chess_Text=this.Chess_Text[this.activeIndex]; 
signRight=this.signRight[this.activeIndex]; 
start=this.start[this.activeIndex]; 
wrongString=this.wrongString[this.activeIndex]; 
whichFangQianZou=this.whichFangQianZou[this.activeIndex]; 
b.Initialize(); 
b.Initialize(this.b[this.activeIndex]);
this.activeIndex--; 
return 1; 
} 
public int GoAhead(ref int fromIndex, ref int toIndex, ref Point fromPoint, ref Point toPo
int, ref string Chess_Text, ref int signRight, ref bool start, ref string wrongString, ref bo
ol whichFangQianZou, ref RegretBack b) 
{
if(this.activeIndex>=this.tailIndex-1) 
return 0; //只能前進到最后一步旗
this.activeIndex++;
if(this.activeIndex<0) 
this.activeIndex=0; 
fromIndex=this.fromIndex[this.activeIndex]; 
toIndex=this.toIndex[this.activeIndex]; 
fromPoint=this.fromPoint[this.activeIndex]; 
toPoint=this.toPoint[this.activeIndex]; 
Chess_Text=this.Chess_Text[this.activeIndex+1]; 
signRight=this.signRight[this.activeIndex+1]; 
start=this.start[this.activeIndex+1]; 
wrongString=this.wrongString[this.activeIndex+1]; 
whichFangQianZou=this.whichFangQianZou[this.activeIndex+1]; 
b.Initialize(); 
b.Initialize(this.b[this.activeIndex+1]); 
return 1; 
} 
} 

 

 

很感激我在博客園里的好朋友,特別要謝謝你們給我的鼓勵和建議。這段時間比較忙,很少寫技術文章,但是每次來這里,在這塊技術的凈土,都能感受到大家對技術的執著追求,和分享技術的無私。

0
0
 
標簽:算法
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()