我可以读取每个空单元周围的地雷数量。游戏扫雷。

huangapple go评论69阅读模式
英文:

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

希望这可以帮助您。

英文:

The program below ask the user how many mines he wants to see on the field and then display the field with mines.
In next step I need to calculate how many mines are around each empty cell. And I know that I
need to check 8 cells if the cell is in the middle, 5 cells if the cell is in the side, and 3
cells if the cell is in the corner. If there are from 1 to 8 mines around the cell, I need to
output the number of mines instead of the symbol representing an empty cell.

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 &lt; 9; i++) {
for (int j = 0; j &lt; 9; j++) {
minesweeper[i][j] = &#39;*&#39;;
}
}
}
public void printMinesweeper() {
for (int i = 0; i &lt; 9; i++) {
for (int j = 0; j &lt; 9; j++) {
System.out.print(minesweeper[i][j]);
}
System.out.println();
}
}
public void randomX() {
System.out.print(&quot;How many mines do you want on the field?: &quot;);
int numberOfMines = sc.nextInt();
int i = 0;
while (i &lt; numberOfMines) {
int x = randNum.nextInt(9);
int y = randNum.nextInt(9);
if (minesweeper[x][y] == &#39;*&#39;) {
minesweeper[x][y] = &#39;X&#39;;
i++;
}
}
printMinesweeper();
}
}

答案1

得分: 1

你可以这样做:

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

You can do it like this:

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 &lt; 9; i++) {
for (int j = 0; j &lt; 9; j++) {
minesweeper[i][j] = &#39;*&#39;;
}
}
}
public void printMinesweeper() {
for (int i = 0; i &lt; 9; i++) {
for (int j = 0; j &lt; 9; j++) {
System.out.print(getCharAt(i, j));
}
System.out.println();
}
}
private String getCharAt(int i, int j) {
if (mineAt(i, j)) {
return &quot;X&quot;;
}
int minesNear = countMinesNear(i, j);
return Integer.toString(minesNear);
}
private boolean mineAt(int i, int j) {
return minesweeper[i][j] == &#39;X&#39;;
}
private int countMinesNear(int i, int j) {
int mines = 0;
for (int x = -1; x &lt;= 1; x++) {//near fields in x direction
for (int y = -1; y &lt;= 1; y++) {//near fields in y direction
if (x + i &gt;= 0 &amp;&amp; x + i &lt; minesweeper.length &amp;&amp; y + j &gt;= 0 &amp;&amp; y + j &lt; minesweeper.length) {//check whether the field exists
if (minesweeper[x+i][y+j] == &#39;X&#39;) {//check whether the field is a mine
mines++;
}
}
}
}
return mines;
}
public void randomX() {
System.out.print(&quot;How many mines do you want on the field?: &quot;);
int numberOfMines = sc.nextInt();
int i = 0;
while (i &lt; numberOfMines) {
int x = randNum.nextInt(9);
int y = randNum.nextInt(9);
if (minesweeper[x][y] == &#39;*&#39;) {
minesweeper[x][y] = &#39;X&#39;;
i++;
}
}
printMinesweeper();
}
}

The countMinesNear(int, int) method check whether the field near exists (to prevent index errors on the edges) and counts the mines if the fields exist.

huangapple
  • 本文由 发表于 2020年9月11日 14:20:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63841738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定