熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java中國象棋博弈程序探秘之生成有效著法

2022-06-13   來源: Java核心技術 

  著法生成就是要產生所有有效的著法讓電腦棋手在這些著法中選擇最好的著法最後走出這一著要生成所有著法只能用窮舉了中國象棋大約每一步可以有個著法選擇下面是代碼

  view plaincopy to clipboardprint?
    <PRE class=java name=code>    /**

  * Generates all valid motions

  * @return all valid motion list if no motion could be generated

  * returns <code>null</code>

  * @see cneduynumonMotion

  */

  @SuppressWarnings(unchecked)

  final public List<Motion> generatePossibleMoves() {

  List<Motion> ret = new ArrayList<Motion>();

  for (int x = ; x < ; x++) {

  for (int y = ; y < ; y++) {

  int chessman = chessboard[x][y];

  if (chessman != ) {

  if (!isRedGo && isRed(chessman)) {

  continue;

  }

  if (isRedGo && !isRed(chessman)) {

  continue;

  }

  switch (chessman) {

  case :

  //<editorfold defaultstate=collapsed desc=Hong Jiang>

  if (isValidMove(x y x y + )) {

  retadd(new Motion(chessman x y x y + ));

  }

  if (isValidMove(x y x y )) {

  retadd(new Motion(chessman x y x y ));

  }

  if (isValidMove(x y x y)) {

  retadd(new Motion(chessman x y x y ));

  }

  if (isValidMove(x y x + y)) {

  retadd(new Motion(chessman x y x + y ));

  }

  for (int oppJiangY = ; oppJiangY < ; oppJiangY++) {

  if (isValidMove(x y x oppJiangY)) {

  retadd(new Motion(chessman x y x oppJiangY ));

  }

  }

  //</editorfold>

  break;

  case :

  //<editorfold defaultstate=collapsed desc=Hei Jiang>

  if (isValidMove(x y x y + )) {

  retadd(new Motion(chessman x y x y + ));

  }

  if (isValidMove(x y x y )) {

  retadd(new Motion(chessman x y x y ));

  }

  if (isValidMove(x y x y)) {

  retadd(new Motion(chessman x y x y ));

  }

  if (isValidMove(x y x + y)) {

  retadd(new Motion(chessman x y x + y ));

  }

  for (int oppJiangY = ; oppJiangY < ; oppJiangY++) {

  if (isValidMove(x y x oppJiangY)) {

  retadd(new Motion(chessman x y x oppJiangY ));

  }

  }

  //</editorfold>

  break;

  case :

  case :

  //<editorfold defaultstate=collapsed desc=Shi>

  if (isValidMove(x y x y + )) {

  retadd(new Motion(chessman x y x y + ));

  }

  if (isValidMove(x y x y )) {

  retadd(new Motion(chessman x y x y ));

  }

  if (isValidMove(x y x + y + )) {

  retadd(new Motion(chessman x y x + y + ));

  }

  if (isValidMove(x y x + y )) {

  retadd(new Motion(chessman x y x + y ));

  }

  //</editorfold>

  break;

  case :

  case :

  //<editorfold defaultstate=collapsed desc=Xiang>

  if (isValidMove(x y x y + )) {

  retadd(new Motion(chessman x y x y + ));

  }

  if (isValidMove(x y x y )) {

  retadd(new Motion(chessman x y x y ));

  }

  if (isValidMove(x y x + y + )) {

  retadd(new Motion(chessman x y x + y + ));

  }

  if (isValidMove(x y x + y )) {

  retadd(new Motion(chessman x y x + y ));

  }

  //</editorfold>

  break;

  case :

  case :

  //<editorfold defaultstate=collapsed desc=Ma>

  if (isValidMove(x y x y + )) {

  retadd(new Motion(chessman x y x y + ));

  }

  if (isValidMove(x y x y )) {

  retadd(new Motion(chessman x y x y ));

  }

  if (isValidMove(x y x y + )) {

  retadd(new Motion(chessman x y x y + ));

  }

  if (isValidMove(x y x y )) {

  retadd(new Motion(chessman x y x y ));

  }

  if (isValidMove(x y x + y + )) {

  retadd(new Motion(chessman x y x + y + ));

  }

  if (isValidMove(x y x + y )) {

  retadd(new Motion(chessman x y x + y ));

  }

  if (isValidMove(x y x + y + )) {

  retadd(new Motion(chessman x y x + y + ));

  }

  if (isValidMove(x y x + y )) {

  retadd(new Motion(chessman x y x + y ));

  }

  //</editorfold>

  break;

  case :

  case :

  //<editorfold defaultstate=collapsed desc=Che>

  // up

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

  if (isValidMove(x y x i)) {

  retadd(new Motion(chessman x y x i ));

  } else {

  // one chessman block its ways

  break;

  }

  }

  // down

  for (int i = y ; i > ; i) {

  if (isValidMove(x y x i)) {

  retadd(new Motion(chessman x y x i ));

  } else {

  // one chessman block its ways

  break;

  }

  }

  // left

  for (int j = x ; j > ; j) {

  if (isValidMove(x y j y)) {

  retadd(new Motion(chessman x y j y ));

  } else {

  // one chessman block its ways

  break;

  }

  }

  // right

  for (int j = x + ; j < ; j++) {

  if (isValidMove(x y j y)) {

  retadd(new Motion(chessman x y j y ));

  } else {

  // one chessman block its ways

  break;

  }

  }

  //</editorfold>

  break;

  case :

  case :

  //<editorfold defaultstate=collapsed desc=Pao>

  // up

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

  if (isValidMove(x y x i)) {

  retadd(new Motion(chessman x y x i ));

  }

  }

  // down

  for (int i = y ; i > ; i) {

  if (isValidMove(x y x i)) {

  retadd(new Motion(chessman x y x i ));

  }

  }

  // left

  for (int j = x ; j > ; j) {

  if (isValidMove(x y j y)) {

  retadd(new Motion(chessman x y j y ));

  }

  }

  // right

  for (int j = x + ; j < ; j++) {

  if (isValidMove(x y j y)) {

  retadd(new Motion(chessman x y j y ));

  }

  }

  //</editorfold>

  break;

  case :

  case :

  //<editorfold defaultstate=collapsed desc=Bing>

  if (isRed(chessman)) {

  if (isValidMove(x y x y + )) {

  // I can see one point at my front

  retadd(new Motion(chessman x y x y + ));

  }

  if (y >= ) {

  // passed the He Jie I can see the point at

  // my left and right

  if (isValidMove(x y x y)) {

  retadd(new Motion(chessman x y x y ));

  }

  if (isValidMove(x y x + y)) {

  retadd(new Motion(chessman x y x + y ));

  }

  }

  } else {

  if (isValidMove(x y x y )) {

  // I can see one point at my front

  retadd(new Motion(chessman x y x y ));

  }

  if (y <= ) {

  // passed the He Jie I can see the point at

  // my left and right

  if (isValidMove(x y x y)) {

  retadd(new Motion(chessman x y x y ));

  }

  if (isValidMove(x y x + y)) {

  retadd(new Motion(chessman x y x + y ));

  }

  }

  }

  //</editorfold>

  break;

  }

  }

  }

  }

  // alphabeta or its variations are all moveordering related!

  Collectionssort(ret);

  return retisEmpty() ? null : ret;

  }

  </PRE>
           轉載請保留作者信息

  作者

  Blog

  MSN & Gmail & QQ


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25876.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.