Android game development-my little game 1-five-game 4-computer chess and man-machine combat Algorithms

Source: Internet
Author: User

The most difficult part of chess games is the human-computer combat with computer AI.AlgorithmThe quality of algorithm design will seriously affect the fun of the game. There are also many discussions about wuziqi algorithms. The approach we use in this computer AI algorithm is that when the computer starts playing chess, it will calculate the possibility of winning each position under the opponent, then, we can compare the position of the pawn by the possibility. The size of the possibility is represented by the weight. How can we calculate the weight? The calculation method of the weight value is relatively easy, that is, to find a connection in four directions: top, bottom, left oblique, Right oblique. If there is one connection, you can assign a weight value of 100, increasing in turn, if there are four connected sub-databases, the maximum weight should be 10000. It is worth noting that the weight value includes not only one direction, for example, "Double three" (two or three players, all of whom know how to play five games) or "dash 4" (that is, you can win immediately). When a piece has multiple sub-accounts in different directions, you should add the weights in all directions.

 

Due to my recent busy schedule, I don't have time to seriously study algorithms. I will try again later. I will refer to an online AI Algorithm for wuziqi, after a while, I will take a good look at it and reorganize it:

Public class suanfa {// defines an array to assign values to the current Board private int [] [] curchess = new int [12] [12]; // copy the current private void copychessboard (INT [] [] chess) {for (INT I = 0; I <12; I ++) {for (Int J = 0; j <12; j ++) {curchess [I] [J] = chess [I] [J] ;}} // record the current position of the child. Private int CI, CJ; private void copychesslocation (int I, Int J) {CI = I; Cj = J ;} // record the idprivate int nameid of the player; Public void loadinfo (INT [] [] chess, int I, Int J) {copychessboard (ChEss); copyches Slocation (I, j); nameid = 2;} // define the weight constant // black and white empty private final int q2o = 50; // empty private final int q2 = 100; // empty private final int q3o = 500; // empty private final int Q3 = 1000; // white/empty private final int q4o = 5000; // white/empty private final int Q4 = 10000; // white/White private final int Q5 = 100000; // After the weight value is defined, it can be used to calculate the weight of each vertex on the board. After analysis, the weight of a vertex on the board consists of four parts, // The weights of this point in the horizontal direction, the weights of this point in the vertical direction, the weights of this point in the 45 degree direction, and the weights of this point in the 135 degree direction., // Therefore, you must first find the four directions And then add them to get the value of this point. Private int getquan (int I, Int J) // I, j record the currently assumed destination vertex {// calculate the weight of the current location int q = 0; q + = getquan0 (I, j); // q + = getquan90 (I, j); // q + = getquan45 (I, j); // q + = getquan135 (I, j); Return Q;} private int getquan0 (int I, int J) {// horizontal weight int samechessnums = 0; // The number of identical pawns int samechessnumf = 0; // used to block the game, number of identical chess pieces of the inverse: int blancnums = 0; int blancnumf = 0; int q = 0, Qs = 0, QF = 0; int [] [] ij0 = new int [2] [2]; // ij0 [0 used for Weight Calculation] [0] = ij0 [0] [1] = I; ij0 [1] [0] = ij0 [1] [1] = J; samechessnums = getsamechessnum0 (ij0, 1); // obtain the white sub number if (ij0 [1] [0]> = 0) if (curchess [ij0 [0] [1] [ij0 [1] [0] = 0) blanknums ++; if (ij0 [1] [1] <12) // 12 is the number of lines. If (curchess [ij0 [0] [1] [ij0 [1] [1] = 0) blanknums ++; qs = getquanpart (samechessnums, blanknums); // obtain the accesskey value ij0 [0] [0] = ij0 [0] [1] = I; ij0 [1] [0] = ij0 [1] [1] = J; samechessnumf = getsamechessnum0 (ij0, 2 ); // obtain the number of sunspots if (ij0 [1] [0]> = 0) if (curchess [ij0 [0] [0] [ij0 [1] [0] = 0) blanknumf ++; if (ij0 [1] [1]> = 0) if (curchess [ij0 [0] [1] [ij0 [1] [1] = 0) blancnumf ++; QF = getquanpart (samechessnumf, blancnumf); // obtain the blocked enemy weight q = qs + QF; return Q ;} private int getsamechessnum0 (INT [] [] qij, int chessid) {// obtain the same sub-number in the horizontal direction int num = 1; // store the same number of sub-pieces, the current vertex meets the condition qij [1] [0] --; while (qij [1] [0]> = 0 & num <5) {// to the left, we will only explore the four neighboring points, be sure not to exit the boundary. If (curchess [qij [0] [0] [qij [1] [0]! = Chessid) break; num ++; qij [1] [0] --;} qij [1] [1] ++; while (qij [1] [1] <12 & num <5) {// right detection if (curchess [qij [0] [1] [qij [1] [1]! = Chessid) break; num ++; qij [1] [1] ++;} return num;} private int getquanpart (INT samechessnum, int blanknum) {// obtain the General weight of a certain party. If (samechessnum = 2 & balancer num = 1) return q2o; If (samechessnum = 2 & balancer num = 2) return Q2; else if (samechessnum = 3 & balancer num = 1) return q3o; else if (samechessnum = 3 & balancer num = 2) return Q3; else if (samechessnum = 4 & blancnum = 1) return q4o; else if (samechessnum = 4 & blancnum = 2) return Q4; else if (samechessnum = 5) return Q5; else return 0 ;}}

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.