java 随机五子棋怎么判断是否有五子连续



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();
        }
    }
}

我的结果是这样的:

img


希望帮忙指点指点!谢谢!

假如当前点是 {9,0 } 他会有好几种情形 为 0,继而退出循环

img

其实规则已经是明确的,就是怎么实现的问题
所谓连续5子,就是在棋盘位描述中(假设为一个平面坐标系),
存在下面的情况

  1. P x=x1,x1+1,..,x1+4,y=y0 是同色
  2. P x=x0,y=y1,y1+1,...y1+4 是同色
  3. p(x0,y0),p(x0+1,y0+1)...p(x0+4,y0+4)是同色
  4. p(x0,y0),p(x0+1,y0-1)...p(x0+4,y0-4) 是同色

这四种情况之一

我也解决了,在我原来的基础上,要考虑无效点位,因为无效点位比如(0,0)会使a[0]一直为0,从而使b[0]一直++,导致只要出现在边缘的棋子while及停止。