Chinaunix首页 | 论坛 | 博客
  • 博客访问: 482954
  • 博文数量: 1496
  • 博客积分: 79800
  • 博客等级: 大将
  • 技术积分: 9940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 13:22
文章分类

全部博文(1496)

文章存档

2011年(1)

2008年(1495)

我的朋友

分类:

2008-09-09 17:24:40

     着法生成就是要产生所有有效的着法,让电脑棋手在这些着法中选择最好的着法,最后走出这一着。要生成所有着法只能用穷举了。中国象棋大约每一步可以有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));

                                }

 

 

[1]    

【责编:landy】

--------------------next---------------------

阅读(640) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~