熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

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

2022-06-13   來源: .NET編程 

  

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

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

  悔棋算法

  public class RegretBack

  {

  public int[] fromIndex=new int[]; //記錄每走一步棋所動的棋子public int[] toIndex = new int[]; //記錄每走一步棋所動的棋子

  public Point[] fromPoint=new Point[];

  public Point[] toPoint=new Point[];

  public int[] fromPointIndex=new int[]; //記錄當前棋子位置

  public int[] toPointIndex=new int[];

  public string[] Chess_Text=new string[]; //存放每一步棋的說明文字

  public int[] signRight=new int[]; //保存哪方走棋標志

  public bool[] start=new bool[]; //悔棋操作要恢復的標志位

  public string[] wrongString=new string[]; //記錄排斥事件的標志

  public bool[] whichFangQianZou = new bool[];//悔棋操作要恢復的標志位

  public RegretBack[] b = new RegretBack[];

  public int activeIndex; //起流動指針作用

  public int tailIndex; //始終指向尾部(剛走完的最後一步)的索引

  public RegretBack()

  {

  thisInitialize();

  }

  public void Initialize()

  {

  //初始化各數組

  for(int i=;i<;i++)

  {

  fromIndex[i]=; toIndex[i]=;

  fromPoint[i]X=; fromPoint[i]Y=;

  toPoint[i]X=; toPoint[i]Y=;

  fromPointIndex[i]=; toPointIndex[i]=;

  }

  for(int i=;i<;i++)

  {

  thisChess_Text[i]=;

  thissignRight[i]=;

  thisstart[i]=false;

  thiswrongString[i]=right;

  thiswhichFangQianZou[i]=false;

  thisb[i] = new RegretBack();

  thisb[i]Initialize();

  }

  //初始化索引activeIndex=;

  tailIndex=;

  }

  public void Initialize(int i)

  {

  //初始化一個數組

  fromIndex[i]=; toIndex[i]=;

  fromPoint[i]X=; fromPoint[i]Y=;

  toPoint[i]X=; toPoint[i]Y=;

  fromPointIndex[i]=; toPointIndex[i]=;

  thisChess_Text[i]=;

  thissignRight[i]=;

  thisstart[i]=false;

  thiswrongString[i]=right;

  thiswhichFangQianZou[i]=false;

  thisb[i]Initialize();

  }

  public void SubLastItem()

  {

  if(thisactiveIndex

  {

  for(int i=thisactiveIndex+;i<=thistailIndex;i++) thisInitialize(i);

  return;

  }

  thisInitialize(thistailIndex);

  thistailIndex;

  thisactiveIndex=thistailIndex;

  }

  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(thisactiveIndex>thistailIndex)

  {

  thisactiveIndex=thistailIndex;

  }

  thisactiveIndex++;

  if(thisactiveIndex==)

  {

  MessageBoxShow(悔棋超出邊界范圍!);

  return;

  }

  thisfromIndex[thisactiveIndex]=fromIndex;

  thistoIndex[thisactiveIndex]=toIndex;

  thisfromPoint[thisactiveIndex]=fromPoint;

  thistoPoint[thisactiveIndex]=toPoint;

  thisChess_Text[thisactiveIndex]=Chess_Text;

  thissignRight[thisactiveIndex]=signRight;

  thisstart[thisactiveIndex]=start;

  thiswrongString[thisactiveIndex]=wrongString;

  thiswhichFangQianZou[thisactiveIndex]=whichFangQianZou;

  RegretBack tempRegretBack = new RegretBack();

  tempRegretBackInitialize();

  for(int i=;i<;i++)

  {

  tempRegretBackhave[i] = bhave[i];

  tempRegretBackwho[i] = bwho[i];

  tempRegretBackstr[i] = bstr[i];

  tempRegretBackallPoint[i] = ballPoint[i];

  if(i<)

  tempRegretBackpartPoint[i] = bpartPoint[i];

  tempRegretBackwhichPicture[i] = bwhichPicture[i];

  }

  tempRegretBackrednum = brednum;

  tempRegretBackblacknum = bblacknum;

  tempRegretBackindex = bindex;

  tempRegretBackfirst_X = bfirst_X;

  tempRegretBackfirst_Y = bfirst_Y;

  tempRegretBackheight = bheight;

  tempRegretBackwidth = bwidth;

  thisb[thisactiveIndex] = tempRegretBack;

  thistailIndex=thisactiveIndex;

  }

  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(thisactiveIndex<)

  return ;

  if(thisactiveIndex>=thistailIndex) thisactiveIndex=thistailIndex;

  if(thisactiveIndex<=) //防止一開始就點後退

  return ;

  fromIndex=thisfromIndex[thisactiveIndex];

  toIndex=thistoIndex[thisactiveIndex];

  fromPoint=thisfromPoint[thisactiveIndex];

  toPoint=thistoPoint[thisactiveIndex];

  Chess_Text=thisChess_Text[thisactiveIndex];

  signRight=thissignRight[thisactiveIndex];

  start=thisstart[thisactiveIndex];

  wrongString=thiswrongString[thisactiveIndex];

  whichFangQianZou=thiswhichFangQianZou[thisactiveIndex];

  bInitialize();

  bInitialize(thisb[thisactiveIndex]);

  thisactiveIndex;

  return ;

  }

  public int GoAhead(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(thisactiveIndex>=thistailIndex)

  return ; //只能前進到最後一步旗

  thisactiveIndex++;

  if(thisactiveIndex<) thisactiveIndex=;

  fromIndex=thisfromIndex[thisactiveIndex];

  toIndex=thistoIndex[thisactiveIndex];

  fromPoint=thisfromPoint[thisactiveIndex];

  toPoint=thistoPoint[thisactiveIndex];

  Chess_Text=thisChess_Text[thisactiveIndex+];

  signRight=thissignRight[thisactiveIndex+];

  start=thisstart[thisactiveIndex+];

  wrongString=thiswrongString[thisactiveIndex+];

  whichFangQianZou=thiswhichFangQianZou[thisactiveIndex+];

  bInitialize();

  bInitialize(thisb[thisactiveIndex+]);

  return ;

  }

  }

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


From:http://tw.wingwit.com/Article/program/net/201311/12475.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.