int[][][] play = new int[15][15][2]; //[][][0]代表黑棋,[][][1]代表白,基于这个数组用java判断白棋黑棋的胜利
时间: 2023-10-16 15:08:26 浏览: 16
判断黑棋或白棋是否胜利,可以分别遍历 play 数组,检查每个棋子的连续情况。具体实现如下:
```java
/**
* 判断黑棋是否胜利
*/
public boolean isBlackWin() {
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 11; j++) {
if (play[i][j][0] == 1 && play[i][j+1][0] == 1 && play[i][j+2][0] == 1 && play[i][j+3][0] == 1 && play[i][j+4][0] == 1) {
return true;
}
}
}
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 15; j++) {
if (play[i][j][0] == 1 && play[i+1][j][0] == 1 && play[i+2][j][0] == 1 && play[i+3][j][0] == 1 && play[i+4][j][0] == 1) {
return true;
}
}
}
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (play[i][j][0] == 1 && play[i+1][j+1][0] == 1 && play[i+2][j+2][0] == 1 && play[i+3][j+3][0] == 1 && play[i+4][j+4][0] == 1) {
return true;
}
}
}
for (int i = 4; i < 15; i++) {
for (int j = 0; j < 11; j++) {
if (play[i][j][0] == 1 && play[i-1][j+1][0] == 1 && play[i-2][j+2][0] == 1 && play[i-3][j+3][0] == 1 && play[i-4][j+4][0] == 1) {
return true;
}
}
}
return false;
}
/**
* 判断白棋是否胜利
*/
public boolean isWhiteWin() {
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 11; j++) {
if (play[i][j][1] == 1 && play[i][j+1][1] == 1 && play[i][j+2][1] == 1 && play[i][j+3][1] == 1 && play[i][j+4][1] == 1) {
return true;
}
}
}
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 15; j++) {
if (play[i][j][1] == 1 && play[i+1][j][1] == 1 && play[i+2][j][1] == 1 && play[i+3][j][1] == 1 && play[i+4][j][1] == 1) {
return true;
}
}
}
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (play[i][j][1] == 1 && play[i+1][j+1][1] == 1 && play[i+2][j+2][1] == 1 && play[i+3][j+3][1] == 1 && play[i+4][j+4][1] == 1) {
return true;
}
}
}
for (int i = 4; i < 15; i++) {
for (int j = 0; j < 11; j++) {
if (play[i][j][1] == 1 && play[i-1][j+1][1] == 1 && play[i-2][j+2][1] == 1 && play[i-3][j+3][1] == 1 && play[i-4][j+4][1] == 1) {
return true;
}
}
}
return false;
}
```
其中,第一个 for 循环和第二个 for 循环分别遍历行和列,检查是否存在连续的五个棋子。第三个和第四个 for 循环则分别检查左上到右下和右上到左下的对角线上是否存在连续的五个棋子。如果检查到了连续的五个棋子,就返回 true,否则返回 false。