您的位置:知识库 » 其他分类

象棋编程游戏——悔棋算法

作者: 夏小冰  来源: 博客园  发布时间: 2009-11-03 09:57  阅读: 1178 次  推荐: 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
标签:算法

其他分类热门文章

    其他分类最新文章

      最新新闻

        热门新闻