Java Chinese chess game program exploring [4] -- generating valid methods

Source: Internet
Author: User
Generate valid methods

Reprinted by the author:

By: 88250

Blog: http:/blog.csdn.net/dl88250

MSN & Gmail & QQ: DL88250@gmail.com

 

Method generation is to generate all valid methods, so that computer players can choose the best method in these methods, and finally come out of this. To generate all methods, you can only use the exhaustive method. About 45 Chinese chess methods can be selected at each step. The following code is used:

    /**     * Generates all valid motions.     * @return all valid motion list, if no motion could be generated,     * returns <code>null</code>     * @see cn.edu.ynu.sei.chinesechess.common.Motion     */    @SuppressWarnings("unchecked")    final public List<Motion> generatePossibleMoves() {        List<Motion> ret = new ArrayList<Motion>();        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:                            //<editor-fold defaultstate="collapsed" desc="Hong Jiang">                            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));                                }                            }                            //</editor-fold>                            break;                        case 14:                            //<editor-fold defaultstate="collapsed" desc="Hei Jiang">                            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));                                }                            }                            //</editor-fold>                            break;                        case 6:                        case 13:                            //<editor-fold defaultstate="collapsed" desc="Shi">                            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));                            }                            //</editor-fold>                            break;                        case 5:                        case 12:                            //<editor-fold defaultstate="collapsed" desc="Xiang">                            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));                            }                            //</editor-fold>                            break;                        case 2:                        case 9:                            //<editor-fold defaultstate="collapsed" desc="Ma">                            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));                            }                            //</editor-fold>                            break;                        case 1:                        case 8:                            //<editor-fold defaultstate="collapsed" desc="Che">                            // 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;                                }                            }                            //</editor-fold>                            break;                        case 3:                        case 10:                            //<editor-fold defaultstate="collapsed" desc="Pao">                            // 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));                                }                            }                            //</editor-fold>                            break;                        case 4:                        case 11:                            //<editor-fold defaultstate="collapsed" desc="Bing">                            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 <= 4) {                                    // 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));                                    }                                }                            }                            //</editor-fold>                            break;                    }                }            }        }        // alpha-beta or its variations are all move-ordering related!        Collections.sort(ret);        return ret.isEmpty() ? null : ret;    }

You can paste the code to the IDE with javadoc to view the code, which is clear :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.