Android Gobang (2)-man-Machine vs.

Source: Internet
Author: User

Man-machine battle, is nothing more than an algorithm to implement AI, traverse the Gobang chessboard, return a value of x, Y, and then to draw the Board on the line.

Not much to say, the following directly attached code, man-machine algorithm, with this algorithm, in a custom view to use the line

Package Com.dcx.bean;

Import Android.util.Log;

public class Smartaiplayer {

Private final static int FIVE = 10000;
Private final static int live_four = 4500;
Private final static int four = 2000;
Private final static int live_three = 900;
Private final static int three = 400;
Private final static int live_two = 150;
Private final static int 70;
Private final static int live_one = 30;
Private final static int one = 15;
Private final static int DEAD = 1;

Private Chessbean Chessbean;

Public Point Findbestpoint () {
Smart AI find the right spot
LOG.D ("Smart AI", "This is a Smart AI");
Optimal point position
Point point = new Point ();
Record the current point position highest score
int scores = 0;
Comprehensive scoring for each blank spot
Chessbean = Chessbean.getinstance ();
int[][] data = Chessbean.dataarray;
for (int i=0;i<data.length;i++) {
for (int j=0;j<data[i].length;j++) {
if (data[i][j] = = staticutils.no_chess) {
Analog AI plays chess at this position and calculates the chess potential score
int aiscore = Getscorehorizontal (i, J, Staticutils.chess_black)
+getscorevertical (i, J, Staticutils.chess_black)
+getscorelefttop (i, J, Staticutils.chess_black)
+getscorerighttop (i, J, Staticutils.chess_black);
Simulate the opponent playing chess at this position and calculate the chess potential score
int playerscore = Getscorehorizontal (i, J, Staticutils.chess_white)
+getscorevertical (i, J, Staticutils.chess_white)
+getscorelefttop (i, J, Staticutils.chess_white)
+getscorerighttop (i, J, Staticutils.chess_white);
Calculate Total Score
if (Aiscore+playerscore > scores) {
scores = Aiscore+playerscore;
Point.setx (i);
Point.sety (j);
LOG.D ("Scores", i+ "," +j+ "" +scores ");
}
}
}
}
return point;
}

/**
* Horizontal Score
*/
private int getscorehorizontal (int x,int y,int chesstype) {
int[][] data = Chessbean.dataarray;
Record whether the start end is blocked
Boolean isstartdead = false;
Whether the end of the record is blocked
Boolean isenddead = false;
Record the number of consecutive occurrences of a pawn
int total = 1;
if (x==0
|| Data[x-1][y]!=chesstype && data[x-1][y]!=staticutils.no_chess) {
Isstartdead = true;
}
if (x==staticutils.array_x_length-1
|| Data[x+1][y]!=chesstype && data[x+1][y]!=staticutils.no_chess) {
Isenddead = true;
}
Previous Search
for (int i=x-1;i>=0;i--) {
if (data[i][y]! = Chesstype) {//If the end is different from the type of pawn
Break
}
total++;
}
After search
for (int i=x+1;i<staticutils.array_x_length;i++) {
if (data[i][y]! = Chesstype) {//If the end is different from the type of pawn
Break
}
total++;
}
Return Getscore (Total,isstartdead,isenddead);
}

private int Getscore (int total, Boolean isstartdead, Boolean isenddead) {
TODO auto-generated Method Stub
if (total >= 5) {
return FIVE;
}
if (Isstartdead && isenddead) {
return DEAD;
}
if (Isstartdead | | isenddead) {
Switch (total) {
Case 4:
return four;
Case 3:
return three;
Case 2:
return;
Case 1:
return one;
}
}
There's no stopping at either end.
Switch (total) {
Case 4:
return live_four;
Case 3:
return live_three;
Case 2:
return live_two;
Case 1:
return live_one;
Default
return 0;
}

}
/**
* Longitudinal score
*/
private int getscorevertical (int x,int y,int chesstype) {
int[][] data = Chessbean.dataarray;
Record whether the start end is blocked
Boolean isstartdead = false;
Whether the end of the record is blocked
Boolean isenddead = false;
Record the number of consecutive occurrences of a pawn
int total = 1;
if (y==0
|| Data[x][y-1]!=chesstype && data[x][y-1]!=staticutils.no_chess) {
Isstartdead = true;
}
if (y==staticutils.array_y_length-1
|| Data[x][y+1]!=chesstype && data[x][y+1]!=staticutils.no_chess) {
Isenddead = true;
}
Search on
for (int i=y-1;i>=0;i--) {
if (data[x][i]! = Chesstype) {//If the end is different from the type of pawn
Break
}
total++;
}
Next Search
for (int i=y+1;i<staticutils.array_y_length;i++) {
if (data[x][i]! = Chesstype) {//If the end is different from the type of pawn
Break
}
total++;
}
Return Getscore (Total,isstartdead,isenddead);
}


/**
* Top left to right bottom score
*/
private int getscorelefttop (int x,int y,int chesstype) {
int[][] data = Chessbean.dataarray;
Record whether the start end is blocked
Boolean isstartdead = false;
Whether the end of the record is blocked
Boolean isenddead = false;
Record the number of consecutive occurrences of a pawn
int total = 1;
if (x==0| | y==0)
|| Data[x-1][y-1]!=chesstype && data[x-1][y-1]!=staticutils.no_chess) {
Isstartdead = true;
}
if (x==staticutils.array_x_length-1| | Y==STATICUTILS.ARRAY_Y_LENGTH-1)
|| Data[x][y+1]!=chesstype && data[x][y+1]!=staticutils.no_chess) {
Isenddead = true;
}
Search on top left
for (int i=x-1,j=y-1;i>=0&&j>=0;i--, j--) {
if (data[i][j]! = Chesstype) {//If the end is different from the type of pawn
Break
}
total++;
}
Lower Right Search
for (int i=x+1,j=y+1;i<staticutils.array_x_length&&j<staticutils.array_y_length;i++,j++) {
if (data[i][j]! = Chesstype) {//If the end is different from the type of pawn
Break
}
total++;
}
Return Getscore (Total,isstartdead,isenddead);
}

/**
* Top right to bottom left score
*/
private int getscorerighttop (int x,int y,int chesstype) {
int[][] data = Chessbean.dataarray;
Record whether the start end is blocked
Boolean isstartdead = false;
Whether the end of the record is blocked
Boolean isenddead = false;
Record the number of consecutive occurrences of a pawn
int total = 1;
if (x==staticutils.array_x_length-1| | y==0)
|| Data[x+1][y-1]!=chesstype && data[x+1][y-1]!=staticutils.no_chess) {
Isstartdead = true;
}
if (x==0| | Y==STATICUTILS.ARRAY_Y_LENGTH-1)
|| Data[x-1][y+1]!=chesstype && data[x-1][y+1]!=staticutils.no_chess) {
Isenddead = true;
}
Search on top Right
for (int i=x+1,j=y-1;i<staticutils.array_x_length-1&&j>=0;i++,j--) {
if (data[i][j]! = Chesstype) {//If the end is different from the type of pawn
Break
}
total++;
}
Lower left Search
for (int i=x-1,j=y+1;i>=0&&j<staticutils.array_y_length;i--, J + +) {
if (data[i][j]! = Chesstype) {//If the end is different from the type of pawn
Break
}
total++;
}
Return Getscore (Total,isstartdead,isenddead);
}
}

The following is a constant class, the data used in the algorithm, such as the length of the board

Package Com.dcx.bean;

public class Staticutils {

Public final static int array_x_length = 12;
Public final static int array_y_length = 12;
Public final static int chess_black = 1;
Public final static int chess_white = 2;
Public final static int no_chess = 0;

Win players
Public final static int ai_win = 0x10;
Public final static int play_win = 0x11;
Public final static int chess_full = 0x12;

}

Android Gobang (2)-man-Machine vs.

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.