java五子棋 五子棋 java 五子棋代码直接和电脑下对战
Public class FiveChessAI {
public int data_a[][] = new int[5][3];// 用于储存进攻值
public int data_d[][] = new int[5][3];// 用于储存防守值
FiveChessAI() {
// 进攻值的初始化
data_a[1][1] = 2;
data_a[1][2] = 3;
data_a[2][1] = 10;
data_a[2][2] = 110;
data_a[3][1] = 2500;
data_a[3][2] = 3000;
data_a[4][1] = 99999;
data_a[4][2] = 99999;
// 防守值的初始化
data_d[1][1] = 1;
data_d[1][2] = 2;
data_d[2][1] = 1;
data_d[2][2] = 100;
data_d[3][1] = 100;
data_d[3][2] = 500;
data_d[4][1] = 20000;
data_d[4][2] = 50000;
}
public FiveChessMap g1 = new FiveChessMap();
public int x, y;
void find()// 查找最大值
{
int max = 0;
for (int i = 0; i < 15; ++i) {
for (int j = 0; j < 15; ++j) {
if (max < g1.data[i][j]) {
max = g1.data[i][j];
}
}
}
for (int i = 0; i < 15; ++i) {
for (int j = 0; j < 15; ++j) {
if (max == g1.data[i][j]) {
x = i;
y = j;
return;
}
}
}
}
int getx()// 返回x坐标值
{
return x;
}
int gety()// 返回y坐标值
{
return y;
}
boolean judge_result(int x, int y, int who, FiveChessMap gm)// 判断胜负
{
int m, n, i, lianzi = 0;
for (m = -1; m <= 1; m++)
for (n = -1; n <= 1; n++) {
if (m != 0 || n != 0) {
for (i = 1; i <= 4; i++) {
if (x + i * m >= 0 && x + i * m < 15 && y + i * n >= 0
&& y + i * n < 15
&& gm.data[x + i * m][y + i * n] == who) {
lianzi++;
} else {
break;
}
}
for (i = -1; i >= -4; i--) {
if (x + i * m >= 0 && x + i * m < 15 && y + i * n >= 0
&& y + i * n < 15
&& gm.data[x + i * m][y + i * n] == who) {
lianzi++;
} else {
break;
}
}
if (lianzi >= 4)
{
return true;
} else {
lianzi = 0;
}
}
}
return false;
}
1