gobang.java

来自「java编写的五子棋源代码具有很好的学习价值,对与五子棋的游戏开发」· Java 代码 · 共 1,264 行 · 第 1/3 页

JAVA
1,264
字号
blackStateRow = sum[1];

sum = sumCol(row, col, 1);
blackSumCol = sum[0];
blackStateCol = sum[1];

sum = sumLr(row, col, 1);
blackSumLr = sum[0];
blackStateLr = sum[1];

sum = sumRl(row, col, 1);
blackSumRl = sum[0];
blackStateRl = sum[1];

//落子后即获胜
if (whiteSumRow >= 5 || whiteSumCol >= 5 || whiteSumLr >= 5 || whiteSumRl >= 5) {
g += 1398101;
return g;
}

//落子后可防止对方获胜
if (blackSumRow >= 5 || blackSumCol >= 5 || blackSumLr >= 5 || blackSumRl >= 5) {
g += 349525;
return g;
}

//落子后形成活四
if ((whiteSumRow == 4 && whiteStateRow == 0) || (whiteSumCol == 4 && whiteStateCol == 0)
|| (whiteSumLr == 4 && whiteStateLr == 0) || (whiteSumRl == 4 && whiteStateRl == 0)) {
g += 87381;
return g;
}

//落子后可防止对方形成活四
if ((blackSumRow == 4 && blackStateRow == 0) || (blackSumCol == 4 && blackStateCol == 0)
|| (blackSumLr == 4 && blackStateLr == 0) || (blackSumRl == 4 && blackStateRl == 0)) {
g += 21845;
return g;
}

//落子后可形成活三
int count3 = 0;
if (whiteSumRow == 3 && whiteStateRow == 0)
count3 += 85;
if (whiteSumCol == 3 && whiteStateCol == 0)
count3 += 85;
if (whiteSumLr == 3 && whiteStateLr == 0)
count3 += 85;
if (whiteSumRl == 3 && whiteStateRl == 0)
count3 += 85;
g += count3;

//落子后可防止对方形成活三
int count33 = 0;
if (blackSumRow == 3 && blackStateRow == 0)
count33 += 21;
if (blackSumCol == 3 && blackStateCol == 0)
count33 += 21;
if (blackSumLr == 3 && blackStateLr == 0)
count33 += 21;
if (blackSumRl == 3 && blackStateRl == 0)
count33 += 21;
g += count33;

//落子后可形成冲四
int count = 0; //用来统计形成冲四的次数
if (isFourRow(row, col, -1))
count++;
if (isFourCol(row, col, -1))
count++;
if (isFourLr(row, col, -1))
count++;
if (isFourRl(row, col, -1))
count++;
if (count == 1) {
if (count3 == 0) {
g += 25;
} else {
g += 342;
}
}
if (count > 1)
g += 342;

//落子后可防止对方形成冲四
count = 0;
if (isFourRow(row, col, 1))
count++;
if (isFourCol(row, col, 1))
count++;
if (isFourLr(row, col, 1))
count++;
if (isFourRl(row, col, 1))
count++;
if (count == 1) {
if (count33 == 0) {
g += 24;
} else {
g += 341;
}
}
if (count > 1)
g += 341;

//落子后形成活二
if (whiteSumRow == 2 && whiteStateRow == 0)
g += 5;
if (whiteSumCol == 2 && whiteStateCol == 0)
g += 5;
if (whiteSumLr == 2 && whiteStateLr == 0)
g += 5;
if (whiteSumRl == 2 && whiteStateRl == 0)
g += 5;

//落子后可防止对方形成活二
if (blackSumRow == 2 && blackStateRow == 0)
g += 1;
if (blackSumCol == 2 && blackStateCol == 0)
g += 1;
if (blackSumLr == 2 && blackStateLr == 0)
g += 1;
if (blackSumRl == 2 && blackStateRl == 0)
g += 1;

return g;
}

/********************************************
函数功能:返回水平方向上的连子数和自由状态
输入参数:下棋点的行号和列号,下棋点的颜色值
输出参数:连子数(整型),自由状态(整型)
*******************************************/
private static int[] sumRow(int row, int col, int color) {
int l = col, r = col;
for (int h = col - 1; h >= 0; h--) {
if (t[row][h] == color) {
l = h;
} else {
break;
}
}
for (int h = col + 1; h < 15; h++) {
if (t[row][h] == color) {
r = h;
} else {
break;
}
}

int result[] = new int[2];
result[0] = r - l + 1;
int leftState = 0, rightState = 0; //左右状态值
if (l == 0 || t[row][l - 1] == -color)
leftState = 1;
if (r == 14 || t[row][r + 1] == -color)
rightState = 1;
result[1] = leftState + rightState;
return result;
}

/********************************************
函数功能:返回垂直方向上的连子数和自由状态
输入参数:下棋点的行号和列号,下棋点的颜色值
输出参数:连子数(整型),自由状态(整型)
*******************************************/
private static int[] sumCol(int row, int col, int color) {
int l = row, r = row;
for (int h = row - 1; h >= 0; h--) {
if (t[h][col] == color) {
l = h;
} else {
break;
}
}
for (int h = row + 1; h < 15; h++) {
if (t[h][col] == color) {
r = h;
} else {
break;
}
}

int result[] = new int[2];
result[0] = r - l + 1;
int topState = 0, downState = 0; //左右状态值
if (l == 0 || t[l - 1][col] == -color)
topState = 1;
if (r == 14 || t[r + 1][col] == -color)
downState = 1;
result[1] = topState + downState;
return result;
}

/********************************************
函数功能:返回左上角到右下角方向上的连子数和自由状态
输入参数:下棋点的行号和列号,下棋点的颜色值
输出参数:连子数(整型),自由状态(整型)
*******************************************/
private static int[] sumLr(int row, int col, int color) {
int l = row, r = row, l2 = col, r2 = col;
for (int h = row - 1, h2 = col - 1; h >= 0; h--, h2--) {
if (h2 < 0)
break;
if (t[h][h2] == color) {
l = h;
l2 = h2;
} else {
break;
}
}
for (int h = row + 1, h2 = col + 1; h < 15; h++, h2++) {
if (h2 >= 15)
break;
if (t[h][h2] == color) {
r = h;
r2 = h2;
} else {
break;
}
}

int result[] = new int[2];
result[0] = r - l + 1;
int leftState = 0, rightState = 0; //左右状态值
if (l == 0 || l2 == 0 || t[l - 1][l2 - 1] == -color)
leftState = 1;
if (r == 14 || r2 == 14 || t[r + 1][r2 + 1] == -color)
rightState = 1;
result[1] = leftState + rightState;
return result;
}

/********************************************
函数功能:返回右上角到左下角方向上的连子数和自由状态
输入参数:下棋点的行号和列号,下棋点的颜色值
输出参数:连子数(整型),自由状态(整型)
*******************************************/
private static int[] sumRl(int row, int col, int color) {
int l = row, r = row, l2 = col, r2 = col;
for (int h = row - 1, h2 = col + 1; h >= 0; h--, h2++) {
if (h2 >= 15)
break;
if (t[h][h2] == color) {
l = h;
l2 = h2;
} else {
break;
}
}
for (int h = row + 1, h2 = col - 1; h < 15; h++, h2--) {
if (h2 < 0)
break;
if (t[h][h2] == color) {
r = h;
r2 = h2;
} else {
break;
}
}

int result[] = new int[2];
result[0] = r - l + 1;
int leftState = 0, rightState = 0; //左右状态值
if (l == 0 || l2 == 14 || t[l - 1][l2 + 1] == -color)
leftState = 1;
if (r == 14 || r2 == 0 || t[r + 1][r2 - 1] == -color)
rightState = 1;
result[1] = leftState + rightState;
return result;
}

/********************************************
函数功能:检查水平方向是否是冲4
输入参数:下棋点的行号和列号,下棋点的颜色值
输出参数:布尔值
*******************************************/
private static boolean isFourRow(int row, int col, int color) {
int[] sum = sumRow(row, col, color);
int count = sum[0];
if (count > 3) {
return sum[1] < 2;
} else {
int countLeft = 0, countRight = 0;
for (int h = col - 1; h >= 0; h--) {
if (t[row][h] == -color) {
break;
} else if (t[row][h] == 0) {
if (h > 0 && t[row][h - 1] == color)
countLeft = sumRow(row, h - 1, color)[0];
break;
}
}
for (int h = col + 1; h < 15; h++) {
if (t[row][h] == -color) {
break;
} else if (t[row][h] == 0) {
if (h < 14 && t[row][h + 1] == color)
countRight = sumRow(row, h + 1, color)[0];
break;
}
}
count += countLeft > countRight ? countLeft : countRight;
return count > 3;
}
}

/********************************************
函数功能:检查垂直方向是否是冲4
输入参数:下棋点的行号和列号,下棋点的颜色值
输出参数:布尔值
*******************************************/
private static boolean isFourCol(int row, int col, int color) {
int[] sum = sumCol(row, col, color);
int count = sum[0];
if (count > 3) {
return sum[1] < 2;
} else {
int countTop = 0, countDown = 0;
for (int h = row - 1; h >= 0; h--) {
if (t[h][col] == -color) {
break;
} else if (t[h][col] == 0) {
if (h > 0 && t[h - 1][col] == color)
countTop = sumCol(h - 1, col, color)[0];
break;
}
}
for (int h = row + 1; h < 15; h++) {
if (t[h][col] == -color) {
break;
} else if (t[h][col] == 0) {
if (h < 14 && t[h + 1][col] == color)
countDown = sumCol(h + 1, col, color)[0];
break;
}
}
count += countTop > countDown ? countTop : countDown;
return count > 3;
}
}

/********************************************
函数功能:检查左上角到右下角方向是否是冲4
输入参数:下棋点的行号和列号,下棋点的颜色值
输出参数:布尔值
*******************************************/
private static boolean isFourLr(int row, int col, int color) {
int[] sum = sumLr(row, col, color);
int count = sum[0];
if (count > 3) {
return sum[1] < 2;
} else {
int countLeftTop = 0, countRightDown = 0;
for (int h = row - 1, h2 = col - 1; h >= 0; h--, h2--) {
if (h2 < 0)
break;
if (t[h][h2] == -color) {
break;
} else if (t[h][h2] == 0) {
if (h > 0 && h2 > 0 && t[h - 1][h2 - 1] == color)
countLeftTop = sumLr(h - 1, h2 - 1, color)[0];
break;
}
}
for (int h = row + 1, h2 = col + 1; h < 15; h++, h2++) {
if (h2 >= 15)
break;
if (t[h][h2] == -color) {
break;
} else if (t[h][h2] == 0) {
if (h < 14 && h2 < 14 && t[h + 1][h2 + 1] == color)
countRightDown = sumLr(h + 1, h2 + 1, color)[0];
break;
}
}
count += countLeftTop > countRightDown ? countLeftTop : countRightDown;
return count > 3;
}
}

/********************************************
函数功能:检查右上角到左下角方向是否是冲4
输入参数:下棋点的行号和列号,下棋点的颜色值
输出参数:布尔值
*******************************************/
private static boolean isFourRl(int row, int col, int color) {
int[] sum = sumRl(row, col, color);
int count = sum[0];
if (count > 3) {
return sum[1] < 2;
} else {
int countRightTop = 0, countLeftDown = 0;
for (int h = row - 1, h2 = col + 1; h >= 0; h--, h2++) {
if (h2 >= 15)
break;
if (t[h][h2] == -color) {
break;
} else if (t[h][h2] == 0) {
if (h > 0 && h2 < 14 && t[h - 1][h2 + 1] == color)
countRightTop = sumRl(h - 1, h2 + 1, color)[0];
break;
}
}
for (int h = row + 1, h2 = col - 1; h < 15; h++, h2--) {
if (h2 < 0)
break;
if (t[h][h2] == -color) {
break;
} else if (t[h][h2] == 0) {
if (h < 14 && h2 > 0 && t[h + 1][h2 - 1] == color)
countLeftDown = sumRl(h + 1, h2 - 1, color)[0];
break;
}
}
count += countRightTop > countLeftDown ? countRightTop : countLeftDown;
return count > 3;
}
}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?