我如何读取每个空 cell.Game 扫雷器周围有多少地雷

How I can read how many mines are around each empty cell.Game minesweeper

下面的程序询问用户他想在场地上看到多少地雷,然后显示有地雷的场地。 在下一步中,我需要计算每个空单元格周围有多少地雷。我知道我 如果单元格在中间,则需要检查 8 个单元格;如果单元格在侧面,则需要检查 5 个单元格;如果单元格在侧面,则需要检查 3 个 如果单元格在角落里。如果小区周围有 1 到 8 个地雷,我需要 输出地雷的数量而不是表示空单元格的符号。

import java.util.Scanner;
import java.util.Random;

public class Minesweeper {

    char[][] minesweeper = new char[9][9];
    Random randNum = new Random();
    Scanner sc = new Scanner(System.in);

    public Minesweeper() {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                minesweeper[i][j] = '*';
            }
        }
    }

    public void printMinesweeper() {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                System.out.print(minesweeper[i][j]);
            }
            System.out.println();
        }
    }

    public void randomX() {
        System.out.print("How many mines do you want on the field?: ");
        int numberOfMines = sc.nextInt();
        int i = 0;
        while (i < numberOfMines) {
            int x = randNum.nextInt(9);
            int y = randNum.nextInt(9);
            if (minesweeper[x][y] == '*') {
                minesweeper[x][y] = 'X';
                i++;
            }
        }
        printMinesweeper();

    }
}

你可以这样做:

import java.util.Random;
import java.util.Scanner;

public class Minesweeper {
    
    public static void main(String[] args) {
        Minesweeper minesweeper = new Minesweeper();
        minesweeper.randomX();
        minesweeper.printMinesweeper();
    }
    
    char[][] minesweeper = new char[9][9];
    Random randNum = new Random();
    Scanner sc = new Scanner(System.in);
    
    public Minesweeper() {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                minesweeper[i][j] = '*';
            }
        }
    }
    
    public void printMinesweeper() {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                System.out.print(getCharAt(i, j));
            }
            System.out.println();
        }
    }
    
    private String getCharAt(int i, int j) {
        if (mineAt(i, j)) {
            return "X";
        }
        
        int minesNear = countMinesNear(i, j);
        return Integer.toString(minesNear);
    }
    
    private boolean mineAt(int i, int j) {
        return minesweeper[i][j] == 'X';
    }
    
    private int countMinesNear(int i, int j) {
        int mines = 0;
        for (int x = -1; x <= 1; x++) {//near fields in x direction
            for (int y = -1; y <= 1; y++) {//near fields in y direction
                if (x + i >= 0 && x + i < minesweeper.length && y + j >= 0 && y + j < minesweeper.length) {//check whether the field exists
                    if (minesweeper[x+i][y+j] == 'X') {//check whether the field is a mine
                        mines++;
                    }
                }
            }
        }
        return mines;
    }

    public void randomX() {
        System.out.print("How many mines do you want on the field?: ");
        int numberOfMines = sc.nextInt();
        int i = 0;
        while (i < numberOfMines) {
            int x = randNum.nextInt(9);
            int y = randNum.nextInt(9);
            if (minesweeper[x][y] == '*') {
                minesweeper[x][y] = 'X';
                i++;
            }
        }
        printMinesweeper();
        
    }
}

countMinesNear(int, int)方法检查near字段是否存在(防止边缘索引错误),如果字段存在则计算地雷。