The exploration of Java Chinese chess game procedure [4]--generation effective way __java

Source: Internet
Author: User
Generate effective

Reprint please retain the author information:

Author: 88250

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

MSN & Gmail & qq:dl88250@gmail.com

The formation of the process is to produce all the effective way, so that the computer players in these move to choose the best way, and finally out of this. The only thing you can do to make all the move is to be exhaustive. Chinese chess can have 45 choices in each step. Here's the code:

    /** * generates all valid motions. * @return All valid motion list, if no motion could to be generated, * returns <code>null</code> * @s EE 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; + +) {for (int y = 0; y < 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= "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) (ches

                            Sman, x, y, x, y-1, 0)); } if (Isvalidmove (x, y, x-1, y)) {Ret.add (new Motion (ches

                            Sman, x, y, x-1, y, 0)); } if (Isvalidmove (x, y, x + 1, y)) {ret.add (new Motion) (ches

                            Sman, x, y, x + 1, y, 0);

                                for (int oppjiangy = 7; oppjiangy < oppjiangy++) { if (Isvalidmove (x, y, X, oppjiangy)) {Ret.add (new Motion (Chessman, x, y, X, Oppjiang

                                Y, 0));

                  }          }//</editor-fold> break;

                            Case://<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) (ches

                            Sman, x, y, x, y-1, 0)); } if (Isvalidmove (x, y, x-1, y)) {Ret.add (new Motion (ches

                            Sman, x, y, x-1, y, 0)); } if (Isvalidmove (x, y, x + 1, y)) {ret.add (new Motion) (ches

                            Sman, 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://<editor-fold defaults

                                Tate= "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 Motio N (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://<editor-fold defaultstate= "collapsed" desc= "Xi Ang "> 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 Moti

                            On (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 Motio

                            N (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 Mo

                                tion (Chessman, x, y, X, I,-4));

                                else {//one Chessman block it 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 it ways break;  }//left for (int j = x-1 J >-1; j--) {if (Isvalidmove (x, Y, J, y)) {Ret.add (new Mo

                                tion (Chessman, X, Y, J, Y,-4));

                                else {//one Chessman block it 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://<editor-fold defaults  Tate= "collapsed" desc= "Pao" >//up for (int i = y + 1; i < 10; i++) {if (Isvalidmove (x, y, x, i)) {Ret.add (new Mo

                                tion (Chessman, x, y, X, I, 3));  }//down for (int i = y-1 I >-1; i--) {if (Isvalidmove (x, y, x, i)) {Ret.add (new Mo

                                tion (Chessman, x, y, X, I, 3));



                         }

                            }   Left for (int j = x-1 J >-1; j--) {if (isValid

                                Move (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 Mo

                                tion (Chessman, X, Y, J, Y, 3));

                        }//</editor-fold> break; Case 4:case://<editor-fold defaults Tate= "collapsed" desc= "Bing" > if (isred (Chessman)) {if (

                          Isvalidmove (x, y, x, y + 1)) {          I can front Ret.add (new Motion (Chessman, x, y, X, y

                                + 1, 2));  } if (y >= 5) {//passed the "he Jie", I can The point at//I left and right if (ISVALIDMO

                                    ve (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 (Isvalidmov

                       E (x, y, X, y-1)) {//I can one point in my front             Ret.add (New Motion (Chessman, x, y, X, y-1, 2));  } if (y <= 4) {//passed the "he Jie", I can The point at//I left and right if (ISVALIDMO

                                    ve (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 post your code in the IDE with Javadoc view, and it's clearer:)

Related Article

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.