简约设计の艺术

讨论软件制造过程中的艺术与工程,软件哲学

用户操作
[即时聊天] [发私信] [加为好友]
88250
88250的公告






最近评论
youjianbo_han_87:可惜了些资料啊,虽然说不一定每一本都会看,但是要用到的时候可以参考下,这才是书真正的价值,呵呵
DL88250:哎。。。。1年多以前,我格了我的硬盘。。。。
youjianbo_han_87:要不给你寄个大容量U盘,拷好寄回来? :)
youjianbo_han_87:终于浏览了一遍,感觉有点乱,有些事示例代码,一些是书。
分english和chinese是不是英文版和中文版的意思?如果想要的话,楼主是发到邮箱里面呢?怎么多,怎么发啊,哪里有得下,给个地址
DL88250:厄。。。。应该是有用的,只是间隔要调整好,不然很占CPU的。
文章分类
收藏
    相册
    Beyond
    壁纸收集
    动漫Kiss图图
    我的珍藏
    我的桌面
    CSDN专家Blog
    孟岩的专栏
    袁萌的专栏
    Ubuntu/Linux相关
    ChinaUnix
    Compiz Themes
    Compiz-Fusion
    deviantART Search
    GetDeb
    Gnome-Look
    KDE-Look
    LinuxToy
    Linux桌面中文网
    Ubuntu中文官方论坛
    Ubuntu桌面中文网
    博友
    老李的Blog
    代码示例
    C++代码示例
    HTML代码示例
    Java Code examples
    技术站点
    Apache Software
    CSDN
    Eclipse.org
    Extreme Programming
    Facelets DevDoc
    hibernate.org
    IBM软件技术
    JavaFX Home
    JavaFX Script Reference
    JavaWorld@TW
    Java开源大全
    JBoss.org
    LEX & YACC Page
    NetBeans中文社区
    Open Source Initiative
    PHP 官方
    Ruby on Rails
    Ruby中文社区论坛
    SOURCEFORGE.NET
    Springframework.org
    Struts Framework
    Sun中国技术社区
    UML官方
    图书下载
    CSDN下载频道
    e 书时空
    IT e Book
    中华电脑书库
    中国 E 书网
    中国 IT 认证实验室
    中文电子书网
    偶要雷锋 - 分享社区
    我爱 e 书
    网络中国 - E 书
    我的偶像 :-)
    Alan Turing
    Bjarne Stroustrup's Homepage
    Don Knuth's Home Page
    Martin Fowler
    Richard Stallman's Home Page
    Uncle Bob (Robert C. Martin)
    我的朋友
    Eleven的专栏
    Eric.Gao的空间
    Meteor的专栏
    mmchsusan的主页
    solonote的专栏
    Vanessa的小窝
    ZhiBaoDeng的专栏
    zyofprogrammer的学习历程
    先知罗庄的专栏
    光光的Blog~
    师傅dorainm的Blog
    皮皮的空间
    秋歌的专栏
    金秋风采
    阿明的专栏
    存档
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 Java中国象棋博弈程序探秘[3]——着法合法性判断收藏

    新一篇: NetBeans 时事通讯(刊号 # 11 - Jun 11, 2008) | 旧一篇: Java中国象棋博弈程序探秘[2]——棋盘的表示

    着法合法性判断


    转载请保留作者信息:

    作者:88250

    Bloghttp:/blog.csdn.net/DL88250

    MSN & Gmail & QQDL88250@gmail.com

     

    本次,我们要着手于除了棋盘外——最基本着法合法性判断代码了。这些代码其实描述的是中国象棋的着法规则,玩过象棋的人应该很熟悉了,虽然比较繁琐,但是一定要写。而且,这一段代码对于性能的影响极大,是后面要介绍的着法生成与局面搜索的基础。


    好了,不多说了,直接贴代码了,里面注释很清楚了:

        /**
         * This method be used to determine the specified move is valid or not.
         * Firstly, let's glance at the <em>Chinese chess move rules</em>:
         * <p>
         * <h2>Jiang</h2>
         * <ol>
         *  <li>
         *      it can only move straight, the distance of one
         *      motion must equals to 1
         *  </li>
         *  <li>
         *      it can only move within the "Base".
         *  </li>
         *  <li>
         *      it can eat opponent's Jiang while nothing between them,
         *      and the "eatways" must be a line.
         *  </li>
         * </ol>
         * </p>
         * <p>
         * <h2>Shi</h2>
         * <ol>
         *  <li>
         *      it can only move to catercorner point of a square,
         *      that square's width equals to 1
         *  </li>
         *  <li>
         *      it can only move within the "Base". Refers to
         *      {@link Jiang#isValid(java.awt.Point)}
         *  </li>
         * </ol>
         * </p>
         * <p>
         * <h2>Xiang</h2>
         * <ol>
         *  <li>
         *      it can only move to catercorner point of a rectangle,
         *      that square's width equals to 2
         *  </li>
         *  <li>
         *      the aspect of this motion must has no chessman on the center of
         *      the square, no matter ours nor opponent's。The following example
         *      is invalid:<br>
         *      #---------<br>
         *      |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
         *      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br>
         *      ----<b>&nbsp;&nbsp;X&nbsp;----</b><br>
         *      |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
         *      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br>
         *      ---------Xiang
         *  </li>
         *  <li>
         *      a xiang can't move beyond our "He Jie"
         *  </li>
         * </ol>
         * </p>
         * <p>
         * <h2>Ma</h2>
         * <ol>
         *  <li>
         *      it can only move to catercorner point of a rectangle,
         *      that rectangle's width equals to 1, and height equals to
         *      2
         *  </li>
         *  <li>
         *      the aspect(the longger distance direction) of this motion
         *      must has no chessman , no matter ours nor opponent's。The
         *      following example is invalid:<br>
         *      #----<br>
         *      |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br>
         *      -----<b>X</b><br>
         *      |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br>
         *      -----Ma
         *  </li>
         * </ol>
         * </p>
         * <p>
         * <h2>Che</h2>
         * <ol>
         *  <li>it can only move straight</li>
         * </ol>
         * </p>
         * <p>
         * <h2>Pao</h2>
         * <ol>
         *  <li>
         *      it can only move straight
         *  </li>
         *  <li>
         *      it can eat opponent chessman between a chessman along a line,
         *      like this:<br>
         *      Pao----#------OpponentChessMan<br>
         *      <b>NOTE:</b> the motion only occur when eating
         *  </li>
         * </ol>
         * </p>
         * <p>
         * <h2>Bing</h2>
         * <ol>
         *  <li>it can move forward, no backward</li>
         *  <li>only when it passed the "Chu He Hang Jie",
         *       it can turn left/right forward</li>
         *  <li>motion distance equals 1</li>
         * </ol>
         * </p>
         * @param fromX x coordinate of a motion's start
         * @param fromY y coordinate of a motion's start
         * @param toX x coordinate of a motion's destination
         * @param toY y coordinate of a motion's destination
         * @return if is valid, returns <code>true</code>, otherwise,
         * returns <code>false</code>
         */
        final public boolean isValidMove(int fromX, int fromY, int toX, int toY) {
            if (basicMoveIsValid(fromX, fromY, toX, toY)) {
                int moveId = chessboard[fromX][fromY];
                int targetId = chessboard[toX][toY];

                switch (moveId) {
                    case 7:
                        //<editor-fold defaultstate="collapsed" desc="Hong Jiang">
                        if (toX < 3 || toX > 5) {
                            // violate the first rule
                            return false;
                        }

                        if (targetId == 14) {
                            int pivotCount = calcChessmanCountAlongLine(fromX, fromY,
                                                                        toX, toY);
                            if (pivotCount == 0) {
                                // the thrid rule
                                return true;
                            }
                        }

                        if (toY > 2) {
                            // violate the first rule
                            return false;
                        }

                        if (distanceSq(fromX, fromY, toX, toY) != 1) {
                            // violate the first rule
                            return false;
                        }
                        //</editor-fold>
                        break;
                    case 14:
                        //<editor-fold defaultstate="collapsed" desc="Hei Jiang">
                        if (toX < 3 || toX > 5) {
                            // violate the first rule
                            return false;
                        }

                        if (targetId == 7) {
                            int pivotCount = calcChessmanCountAlongLine(fromX, fromY,
                                                                        toX, toY);
                            if (pivotCount == 0) {
                                // the thrid rule
                                return true;
                            }
                        }

                        if (toY < 7) {
                            // violate the first rule
                            return false;
                        }

                        if (distanceSq(fromX, fromY, toX, toY) != 1) {
                            // violate the first rule
                            return false;
                        }
                        //</editor-fold>
                        break;
                    case 6:
                        //<editor-fold defaultstate="collapsed" desc="Hong Shi">
                        if (toX < 3 || toX > 5 || toY > 3) {
                            // violate the first rule
                            return false;
                        }

                        if (distanceSq(fromX, fromY, toX, toY) != 2) {
                            // violate the first rule
                            return false;
                        }
                        //</editor-fold>
                        break;
                    case 13:
                        //<editor-fold defaultstate="collapsed" desc="Hei Shi">
                        if (toX < 3 || toX > 5 || toY < 8) {
                            // violate the first rule
                            return false;
                        }

                        if (distanceSq(fromX, fromY, toX, toY) != 2) {
                            // violate the first rule
                            return false;
                        }



                        //</editor-fold>
                        break;
                    case 5:
                        //<editor-fold defaultstate="collapsed" desc="Hong Xiang">
                        if (toY > 4) {
                            return false;
                        }

                        if (distanceSq(fromX, fromY, toX, toY) != 8) {
                            // violate the first rule
                            return false;
                        }

                        if (chessboard[(fromX + toX) / 2][(fromY + toY) / 2] != 0) {
                            // can't move widthways, violates the second rule
                            return false;
                        }
                        //</editor-fold>
                        break;
                    case 12:
                        //<editor-fold defaultstate="collapsed" desc=" Hei Xiang">
                        if (toY < 6) {
                            return false;
                        }

                        if (distanceSq(fromX, fromY, toX, toY) != 8) {
                            // violate the first rule
                            return false;
                        }

                        if (chessboard[(fromX + toX) / 2][(fromY + toY) / 2] != 0) {
                            // can't move widthways, violates the second rule
                            return false;
                        }
                        //</editor-fold>
                        break;
                    case 2:
                    case 9:
                        //<editor-fold defaultstate="collapsed" desc="Ma">
                        if (distanceSq(fromX, fromY, toX, toY) != 5) {
                            // violate the first rule
                            return false;
                        }

                        if (chessboard[(fromX + toX) / 2][fromY] != 0 &&
                                abs(fromY - toY) == 1) {
                            // can't move widthways, violates the second rule
                            return false;
                        }

                        if (chessboard[fromX][(fromY + toY) / 2] != 0 &&
                                abs(fromX - toX) == 1) {
                            // can't move lengthways violates the second rule
                            return false;
                        }
                        //</editor-fold>
                        break;
                    case 1:
                    case 8:
                        //<editor-fold defaultstate="collapsed" desc="Che">
                        if (fromX != toX && fromY != toY) {
                            // must move straight
                            return false;
                        }

                        int pivotCount = calcChessmanCountAlongLine(fromX, fromY,
                                                                    toX, toY);
                        // move eat motion!
                        if (pivotCount != 0) {
                            return false;
                        }
                        //</editor-fold>
                        break;
                    case 3:
                    case 10:
                        //<editor-fold defaultstate="collapsed" desc="Pao">
                        if (fromX != toX && fromY != toY) {
                            // must move straight, violates the first rule
                            return false;
                        }

                        pivotCount = calcChessmanCountAlongLine(fromX, fromY,
                                                                toX, toY);

                        if (targetId != 0) {
                            // move eat motion!
                            if (pivotCount != 1) {
                                return false;
                            }
                        } else {
                            if (pivotCount != 0) {
                                return false;
                            }
                        }
                        //</editor-fold>
                        break;
                    case 4:
                    case 11:
                        //<editor-fold defaultstate="collapsed" desc="Bing">
                        if (fromX != toX && fromY != toY) {
                            // must move straight
                            return false;
                        }

                        if (distanceSq(fromX, fromY, toX, toY) != 1) {
                            // move forware ONLY ONE step
                            return false;
                        }

                        if (isBlack(moveId)) {
                            if (fromY > 4 && fromX != toX) {
                                // has nor yet passed the "He Jie"
                                // cannot turn left/right
                                return false;
                            }
                            if (fromY < toY) {
                                // "Bing" cannot backward!
                                return false;
                            }
                        } else {
                            if (fromY < 5 && fromX != toX) {
                                // has nor yet passed the "He Jie"
                                // cannot turn left/right
                                return false;
                            }
                            if (fromY > toY && fromX == toX) {
                                // "Bing" cannot backward!
                                return false;
                            }
                        }
                        //</editor-fold>
                        break;
                }
            } else {
                return false;
            }

            return true;
        }

    可以把代码贴到带Javadoc查看的IDE里看一下,那样比较清晰 : )

    发表于 @ 2008年06月07日 17:50:00|评论(loading...)|收藏

    新一篇: NetBeans 时事通讯(刊号 # 11 - Jun 11, 2008) | 旧一篇: Java中国象棋博弈程序探秘[2]——棋盘的表示

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © 88250