五子棋算法是把棋盘上所有能组合五子的单独算分,机器下在分数最高处,因为是加权,有先加,后加的先后顺序总有先后顺序,这个机器人斜的是先,可以多下横竖偷鸡哈哈。你可以换换四个顺序来调整机器侧重的顺序,还可以加个百位的random来选择下不下来增加机器的变量。
import java.awt.*;
import javax.swing.JFrame;
public class AI {
public static final int CHESSBOARD_SIZE = 15;
private static int[][] score = new int[CHESSBOARD_SIZE][CHESSBOARD_SIZE];
private int x = 0;
private int y = 0;
private int r = 36;
private Color color = null;
private int type = 0;// 棋子类型 1:白棋 2:黑棋
public AI(int x, int y, int type,GamePanel panel) {
this.x =x;
this.y =y;
this.type = type;
}
public void draw(Graphics g) {
Color oColor = g.getColor();
if(type == 1) {//白色
g.drawImage(Pointer.whiteQiziImage, x -r/2, y-r/2,r,r, null);
}else if(type == 2){//黑色
g.drawImage(Pointer.blackQiziImage, x -r/2, y-r/2,r,r,null);
}
if(color != null){// 操作者用完设置回机器使用颜色
g.setColor(oColor);
}
}
static void next(GamePanel gamePanel) {
luozinext(gamePanel);
}
//判断五子连珠
static boolean has5(Pointer pointer,GamePanel gamePanel) {
//ArrayList<Data> datas =new ArrayList<Data>();
Pointer[][] points = gamePanel.points;
int i = pointer.getI();
int j = pointer.getJ();
boolean flag = false;
int count = 1;//用来保存共有多少相同颜色多少棋子相连,初始值为1
int k1 = i,k2=j,k3=i,k4=j;
int i2=1,j2=1,i3=1,j3=1;
for(;k1<15;) {
if(k1==0 || k2==0) {
i2=k1;
j2=k2;
break;
}
k1--;
k2--;
}
for(;k3<15;) {
if(k3==0 || k4==14) {
i3=k3;
j3=k4;
break;
}
k3--;
k4++;
}
Pointer tempPointer;
Pointer tempPointer2;
Pointer tempPointer3;
Pointer tempPointer4;
for(int i1=0;i1<15;i1++) {
tempPointer=points[i1][j];
if(tempPointer.getQizi()==pointer.getQizi()) {
count++;
if(count>5) {
flag=true;
break;
}
}else {
count=1;
}
}
for(int j1=0;j1<15;j1++) {
tempPointer2=points[i][j1];
if(tempPointer2.getQizi()==pointer.getQizi()) {
count++;
if(count>5) {
flag=true;
break;
}
}else {
count=1;
}
}
for(;j2<15 && i2<15;i2++,j2++) {
tempPointer3=points[i2][j2];
if(tempPointer3.getQizi()==pointer.getQizi()) {
count++;
if(count>5) {
flag=true;
break;
}
}else {
count=1;
}
}
for(int k5=0;i3<15 && j3<15 && k5<15;i3++,j3--,k5++) {
tempPointer4=points[i3][j3];
if(tempPointer4.getQizi()==pointer.getQizi()) {
count++;
if(count>5) {
flag=true;
break;
}
}
if(j3==0 || i3==15) {
break;
}
}
return flag;
}
//AI进行下一步
static boolean luozinext(GamePanel gamePanel) {
Pointer pointer = getRandomPoiner(gamePanel);
luozi(pointer,gamePanel,1);
return true;
}
//获得随机下的棋子
static Pointer getRandomPoiner(GamePanel gamePanel) {
//每次棋子落下后重置棋盘score数组
for(int a=0;a<15;a++) {
for(int b=0;b<15;b++) {
score[a][b]=0;
}
}
int blackNum=0; //玩家棋子数量
int writeNum=0;//机器棋子数量
int tupleScoreTemp=0;//临时数组加量
int goalX=0;
int goalY=0;
int maxScore = -1;
//加上所有横向棋子加量
for(int a = 0 ; a < 15 ; a++ ) {
for(int b = 0 ; b<11 ; b++ ) {
for(int c = 0 ; c < 5 ; c++ ) {
if(gamePanel.points[ b + c ][a].getQizi() == 1)writeNum++;
else if(gamePanel.points[ b + c][a].getQizi() == 2)blackNum++;
}
tupleScoreTemp=tupleScore(blackNum,writeNum);
//为该五元组的每个位置添加分数
for(int c1=0;c1<5;c1++) {
score[ b + c1 ][a]+=tupleScoreTemp;
}
//临时数值归零
blackNum= 0 ;
writeNum= 0 ;
tupleScoreTemp = 0;
}
}
//加上所有纵向棋子加量
for(int a = 0 ; a < 15; a++ ) {
for(int b = 0 ; b < 11 ; b++ ) {
for(int c = 0 ; c < 5 ; c++ ) {
if(gamePanel.points[ a ][ b + c ].getQizi() == 1) writeNum++;
else if(gamePanel.points[a][ b + c ].getQizi() == 2) blackNum++;
}
//为该五元组每个位置添加分数
tupleScoreTemp = tupleSc