package test;
import java.util.Random;
public class text1 {
public static void main(String[] args) {
char[][] str = new char[15][15];
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
str[i][j] = '0';
}
}//创建一个二维数组,并赋初值为0
Random ne = new Random();
int flag = 1;
int x, y, count = 0, j, k;
j = ne.nextInt();
k = ne.nextInt();
y = Math.abs(k % 15);
x = Math.abs(j % 15);
while (true) {//沦落下棋,下棋位置为随机数,在下棋前需判断该位置是否是空位。有连续五子或下满为止。
if (count < 225 && str[x][y] == '0') {
if (flag == 1) {
str[x][y] = '*';
} else {
str[x][y] = '1';
}
int[] a = {0, 0, 0, 0, 0, 0, 0, 0};//以下是判断是否连成五子
int[] b = {1, 1, 1, 1};
for (int n = 1; n <= 4; n++) { //以刚下的棋子为中心,画一个米字,及看作八条射线方向,思路是在这个方向上碰到一个不相同的棋子就停止在该方向延申。
for (int m = 1; m <= 8; m++) { //每个点周围有八个点
if (x - n >= 0 && str[x][y] != str[x - n][y] && a[0] == 0) {
a[0] = 1;//x-n是保证不越界。若满足条件,及不相同,则a[0]=1,这个方向终止。
} else if (x - n >= 0 && y + n < 15 && a[1] == 0 && str[x - n][y + n] != str[x][y]) {
a[1] = 1;
} else if (y + n < 15 && a[2] == 0 && str[x][y + n] != str[x][y]) {
a[2] = 1;
} else if (x + n < 15 && y + n < 15 && a[3] == 0 && str[x + n][y + n] != str[x][y]) {
a[3] = 1;
} else if (x + n < 15 && a[4] == 0 && str[x + n][y] != str[x][y]) {
a[4] = 1;
} else if (x + n < 15 && y - n >= 0 && a[5] == 0 && str[x + n][y - n] != str[x][y]) {
a[5] = 1;
} else if (y - n >= 0 && a[6] == 0 && str[x][y - n] != str[x][y]) {
a[6] = 1;
} else if (x - n >= 0 && y - n >= 0 && a[7] == 0 && str[x - n][y - n] != str[x][y]) {
a[7] = 1;
}
}
for (int i = 0; i < 8; i++) {
if (a[i] == 0) {
b[i % 4]++;
}
}
}
if (b[0] >= 5 || b[1] >= 5 || b[2] >= 5 || b[3] >= 5) {
break;
}
j = ne.nextInt();
k = ne.nextInt();
y = Math.abs(k % 15);
x = Math.abs(j % 15);
flag = -flag;
count++;
} else if (count < 225 && str[x][y] != '0') {
j = ne.nextInt();
k = ne.nextInt();
y = Math.abs(k % 15);
x = Math.abs(j % 15);
} else if (count == 225) {
break;
}
}
for (int m = 0; m < 15; m++) {//输出棋盘
for (int n = 0; n < 15; n++) {
if (str[m][n] == '*') {
System.out.print("●");
} else if (str[m][n] == '1') {
System.out.print("○");
} else {
System.out.print("╫");
}
}
System.out.println();
}
}
}
我的结果是这样的:
希望帮忙指点指点!谢谢!