java扫雷自动打开递归出问题

package com.sao.lei;

/**

  • 增加 扫雷自动扩大的功能
  • /
    import java.util.Random;
    import java.util.Scanner;

public class minesweeper {

public static String[][] display = new String[9][9]; // 答案位置
public static String[][] hide = new String[9][9]; // 隐藏位置
public static String[] T = { "0", "X", " " };
public static Scanner sca = new Scanner(System.in);

public static void main(String[] args) {
    System.out.println("欢迎来到扫雷游戏,请输入你想要的地雷数量");
    answer();
}

public static void answer() {
    int lei = sca.nextInt();
    if (lei > display.length * display.length) {
        System.out.println("地雷太多了,重新输入");
        answer();
    }

    // 起始化位置
    for (int i = 0; i < display.length; i++) {
        for (int j = 0; j < display.length; j++) {
            display[i][j] = "O";
            hide[i][j] = "*";
        }
    }
    mailei(lei);
    draw(display);
    draw(hide);
}

public static void draw(String[][] qipan) {
    System.out.println("--------------");
    for (int i = 0; i < display.length + 1; i++) {
        System.out.print(i + " ");
    }
    System.out.println();
    for (int i = 0; i < display.length; i++) {
        System.out.print(i + 1 + " ");
        for (int j = 0; j < display.length; j++) {
            System.out.print(qipan[i][j] + " ");
        }
        System.out.println();
    }
    if (qipan == hide) {
        start();
    }
}

// 开始埋地雷
public static void mailei(int lei) {
    // 开始随机埋地雷
    for (int i = 0; i < lei; i++) {
        Random ran = new Random();
        int row = ran.nextInt(9);
        int col = ran.nextInt(9);
        if (display[row][col] != T[1]) {
            display[row][col] = T[1];
        } else {
            i--;
        }
    }

    // 加载数字
    shuzi();
}

// 加载周围雷数字
public static void shuzi() {
    System.out.println("地雷数字开始加载");
    for (int a = 0; a < display.length; a++) {
        for (int b = 0; b < display.length; b++) {
            if (display[a][b] == T[1]) {
                continue;
            } else {

                // 检测周围雷的数量 雷的数量累加
                int sum = 0;
                for (int c = a - 1; c <= a + 1; c++) {
                    for (int d = b - 1; d <= b + 1; d++) {
                        // 检测数组边界
                        if (c < 0 || d < 0 || c >= display.length || d >= display.length) {
                            continue;
                        } else {
                            // 判断
                            if (display[c][d] == T[1]) {
                                sum++;
                            }
                        }
                    }
                }
                display[a][b] = String.valueOf(sum);
                // 把雷的数量赋值给当前坐标
            }
        }
    }
    System.out.println("地雷数字开始加载完毕");
}

// 开始玩游戏破解地雷

public static void start() {
    System.out.println("请输入坐标");
    int row = sca.nextInt() - 1;
    int col = sca.nextInt() - 1;
    // 防止输入数字溢出数组
    if (row > display.length && col > display.length) {
        System.out.println("哎呀,你输入的数字太大了,请重新输入下");
    }
    panduan(row, col);
}

public static void panduan(int row, int col) {
    // 判断是否碰到地雷
    if (display[row][col] == T[1]) {
        System.out.println("哎呀,小心点呀。你碰到地雷了。结束游戏");
        draw(display);
        System.exit(1);
    } else {
        if (display[row][col].equals(T[0])) {
            q(row, col);
        } else {
            hide[row][col] = display[row][col];
        }
        draw(hide);
    }
}

**public static void q(int row, int col) {
    hide[row][col] = display[row][col] = T[3];

    int x,y;
    for(x=row-1;x<= col+1;x++) {
        for (y = col - 1; y <= col+1; y++) {
            // 检测数组边界
            if (x >= 0 && y >= 0 && x < display.length&& y < display.length && x != row && y != col) {
                //判断该位子的字符
                if (display[x][y].equals(T[0])) {
                    q(x, y);
                } else {
                    hide[x][y] = display[x][y];
                }
            }
        }
    }
}**

}

img

数组越界异常了,访问的数组下标超过了定义的长度。