当前位置:首页 > 全部子站 > IT > 思科认证

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

来源:长理培训发布时间:2017-12-19 16:25:25

 百度广告

  着法生成就是要产生所有有效的着法,让电脑棋手在这些着法中选择最好的着法,最后走出这一着。要生成所有着法只能用穷举了。中国象棋大约每一步可以有45个着法选择。下面是代码

  view plaincopy to clipboardprint?

   /**

  * Generates all valid motions.

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

  * returns null

  * @see cn.edu.ynu.sei.chinesechess.common.Motion

  */

  @SuppressWarnings("unchecked")

  final public List generatePossibleMoves() {

  List ret = new ArrayList();

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

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

  int chessman = chessboard[x][y];

  if (chessman != 0) {

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

  continue;

  }

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

  continue;

  }

  switch (chessman) {

  case 7:

  //

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

  ret.add(new Motion(chessman, x, y, x, y + 1, 0));

  }

  if (isValidMove(x, y, x, y - 1)) {

  ret.add(new Motion(chessman, x, y, x, y - 1, 0));

  }

  if (isValidMove(x, y, x - 1, y)) {

  ret.add(new Motion(chessman, x, y, x - 1, y, 0));

  }

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

  ret.add(new Motion(chessman, x, y, x + 1, y, 0));

  }

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

  if (isValidMove(x, y, x, oppJiangY)) {

  ret.add(new Motion(chessman, x, y, x, oppJiangY, 0));

  }

  }

  //

  break;

  case 14:

  //

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

  ret.add(new Motion(chessman, x, y, x, y + 1, 0));

  }

  if (isValidMove(x, y, x, y - 1)) {

  ret.add(new Motion(chessman, x, y, x, y - 1, 0));

  }|||   if (isValidMove(x, y, x - 1, y)) {

  ret.add(new Motion(chessman, x, y, x - 1, y, 0));

  }

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

  ret.add(new Motion(chessman, x, y, x + 1, y, 0));

  }

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

  if (isValidMove(x, y, x, oppJiangY)) {

  ret.add(new Motion(chessman, x, y, x, oppJiangY, 04));

  }

  }

  //

  break;

  case 6:

  case 13:

  //

  if (isValidMove(x, y, x - 1, y + 1)) {

  ret.add(new Motion(chessman, x, y, x - 1, y + 1, 1));

  }

  if (isValidMove(x, y, x - 1, y - 1)) {

  ret.add(new Motion(chessman, x, y, x - 1, y - 1, 1));

  }

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

  ret.add(new Motion(chessman, x, y, x + 1, y + 1, 1));

  }

  if (isValidMove(x, y, x + 1, y - 1)) {

  ret.add(new Motion(chessman, x, y, x + 1, y - 1, 1));

  }

  //

  break;

  case 5:

  case 12:

  //

  if (isValidMove(x, y, x - 2, y + 2)) {

  ret.add(new Motion(chessman, x, y, x - 2, y + 2, 1));

  }

  if (isValidMove(x, y, x - 2, y - 2)) {

  ret.add(new Motion(chessman, x, y, x - 2, y - 2, 1));

  }

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

  ret.add(new Motion(chessman, x, y, x + 2, y + 2, 1));

  }

  if (isValidMove(x, y, x + 2, y - 2)) {

  ret.add(new Motion(chessman, x, y, x + 2, y - 2, 1));

  }

  //

  break;

  case 2:

  case 9:

  //

  if (isValidMove(x, y, x - 1, y + 2)) {

  ret.add(new Motion(chessman, x, y, x - 1, y + 2, 3));

  }

  if (isValidMove(x, y, x - 1, y - 2)) {

  ret.add(new Motion(chessman, x, y, x - 1, y - 2, 3));

  }

  if (isValidMove(x, y, x - 2, y + 1)) {

  ret.add(new Motion(chessman, x, y, x - 2, y + 1, 3));

  }

  if (isValidMove(x, y, x - 2, y - 1)) {

  ret.add(new Motion(chessman, x, y, x - 2, y - 1, 3));

  }

  if (isValidMove(x, y, x + 1, y + 2)) {

  ret.add(new Motion(chessman, x, y, x + 1, y + 2, 3));

  }

  if (isValidMove(x, y, x + 1, y - 2)) {

  ret.add(new Motion(chessman, x, y, x + 1, y - 2, 3));

  }|||   if (isValidMove(x, y, x + 2, y + 1)) {

  ret.add(new Motion(chessman, x, y, x + 2, y + 1, 3));

  }

  if (isValidMove(x, y, x + 2, y - 1)) {

  ret.add(new Motion(chessman, x, y, x + 2, y - 1, 3));

  }

  //

  break;

  case 1:

  case 8:

  //

  // up

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

  if (isValidMove(x, y, x, i)) {

  ret.add(new Motion(chessman, x, y, x, i, -4));

  } else {

  // one chessman block its ways

  break;

  }

  }

  // down

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

  if (isValidMove(x, y, x, i)) {

  ret.add(new Motion(chessman, x, y, x, i, -4));

  } else {

  // one chessman block its ways

  break;

  }

  }

  // left

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

  if (isValidMove(x, y, j, y)) {

  ret.add(new Motion(chessman, x, y, j, y, -4));

  } else {

  // one chessman block its ways

  break;

  }

  }

  // right

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

  if (isValidMove(x, y, j, y)) {

  ret.add(new Motion(chessman, x, y, j, y, -4));

  } else {

  // one chessman block its ways

  break;

  }

  }

  //

  break;

  case 3:

  case 10:

  //

  // up

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

  if (isValidMove(x, y, x, i)) {

  ret.add(new Motion(chessman, x, y, x, i, 3));

  }

  }

  // down

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

  if (isValidMove(x, y, x, i)) {

  ret.add(new Motion(chessman, x, y, x, i, 3));

  }

  }

  // left|||   for (int j = x - 1; j > -1; j--) {

  if (isValidMove(x, y, j, y)) {

  ret.add(new Motion(chessman, x, y, j, y, 3));

  }

  }

  // right

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

  if (isValidMove(x, y, j, y)) {

  ret.add(new Motion(chessman, x, y, j, y, 3));

  }

  }

  //

  break;

  case 4:

  case 11:

  //

  if (isRed(chessman)) {

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

  // I can see one point at my front

  ret.add(new Motion(chessman, x, y, x, y + 1, 2));

  }

  if (y >= 5) {

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

  // my left and right

  if (isValidMove(x, y, x - 1, y)) {

  ret.add(new Motion(chessman, x, y, x - 1, y, 2));

  }

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

  ret.add(new Motion(chessman, x, y, x + 1, y, 2));

  }

  }

  } else {

  if (isValidMove(x, y, x, y - 1)) {

  // I can see one point at my front

  ret.add(new Motion(chessman, x, y, x, y - 1, 2));

  }

  if (y

责编:罗莉

发表评论(共0条评论)
请自觉遵守互联网相关政策法规,评论内容只代表网友观点,发表审核后显示!

国家电网校园招聘考试直播课程通关班

  • 讲师:刘萍萍 / 谢楠
  • 课时:160h
  • 价格 4580

特色双名师解密新课程高频考点,送国家电网教材讲义,助力一次通关

配套通关班送国网在线题库一套

课程专业名称
讲师
课时
查看课程

国家电网招聘考试录播视频课程

  • 讲师:崔莹莹 / 刘萍萍
  • 课时:180h
  • 价格 3580

特色解密新课程高频考点,免费学习,助力一次通关

配套全套国网视频课程免费学习

课程专业名称
讲师
课时
查看课程
在线题库
面授课程更多>>
图书商城更多>>
在线报名
  • 报考专业:
    *(必填)
  • 姓名:
    *(必填)
  • 手机号码:
    *(必填)
返回顶部