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

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

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

问题

以下是您要翻译的代码部分:

  1. 程序下面要求用户在场地上看到多少个地雷然后显示带有地雷的场地
  2. 在下一步中我需要计算每个空单元周围有多少个地雷我知道我
  3. 需要检查8个单元格如果单元格在中间如果单元格在侧面则需要检查5
  4. 单元格如果单元格在角落则需要检查3个单元格如果单元格周围有18个地雷
  5. 我需要输出地雷的数量而不是表示空单元格的符号
  6. import java.util.Scanner;
  7. import java.util.Random;
  8. public class Minesweeper {
  9. char[][] minesweeper = new char[9][9];
  10. Random randNum = new Random();
  11. Scanner sc = new Scanner(System.in);
  12. public Minesweeper() {
  13. for (int i = 0; i < 9; i++) {
  14. for (int j = 0; j < 9; j++) {
  15. minesweeper[i][j] = '*';
  16. }
  17. }
  18. }
  19. public void printMinesweeper() {
  20. for (int i = 0; i < 9; i++) {
  21. for (int j = 0; j < 9; j++) {
  22. System.out.print(minesweeper[i][j]);
  23. }
  24. System.out.println();
  25. }
  26. }
  27. public void randomX() {
  28. System.out.print("How many mines do you want on the field?: ");
  29. int numberOfMines = sc.nextInt();
  30. int i = 0;
  31. while (i < numberOfMines) {
  32. int x = randNum.nextInt(9);
  33. int y = randNum.nextInt(9);
  34. if (minesweeper[x][y] == '*') {
  35. minesweeper[x][y] = 'X';
  36. i++;
  37. }
  38. }
  39. printMinesweeper();
  40. }
  41. }

希望这可以帮助您。

英文:

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.

  1. import java.util.Scanner;
  2. import java.util.Random;
  3. public class Minesweeper {
  4. char[][] minesweeper = new char[9][9];
  5. Random randNum = new Random();
  6. Scanner sc = new Scanner(System.in);
  7. public Minesweeper() {
  8. for (int i = 0; i &lt; 9; i++) {
  9. for (int j = 0; j &lt; 9; j++) {
  10. minesweeper[i][j] = &#39;*&#39;;
  11. }
  12. }
  13. }
  14. public void printMinesweeper() {
  15. for (int i = 0; i &lt; 9; i++) {
  16. for (int j = 0; j &lt; 9; j++) {
  17. System.out.print(minesweeper[i][j]);
  18. }
  19. System.out.println();
  20. }
  21. }
  22. public void randomX() {
  23. System.out.print(&quot;How many mines do you want on the field?: &quot;);
  24. int numberOfMines = sc.nextInt();
  25. int i = 0;
  26. while (i &lt; numberOfMines) {
  27. int x = randNum.nextInt(9);
  28. int y = randNum.nextInt(9);
  29. if (minesweeper[x][y] == &#39;*&#39;) {
  30. minesweeper[x][y] = &#39;X&#39;;
  31. i++;
  32. }
  33. }
  34. printMinesweeper();
  35. }
  36. }

答案1

得分: 1

你可以这样做:

  1. import java.util.Random;
  2. import java.util.Scanner;
  3. public class Minesweeper {
  4. public static void main(String[] args) {
  5. Minesweeper minesweeper = new Minesweeper();
  6. minesweeper.randomX();
  7. minesweeper.printMinesweeper();
  8. }
  9. char[][] minesweeper = new char[9][9];
  10. Random randNum = new Random();
  11. Scanner sc = new Scanner(System.in);
  12. public Minesweeper() {
  13. for (int i = 0; i < 9; i++) {
  14. for (int j = 0; j < 9; j++) {
  15. minesweeper[i][j] = '*';
  16. }
  17. }
  18. }
  19. public void printMinesweeper() {
  20. for (int i = 0; i < 9; i++) {
  21. for (int j = 0; j < 9; j++) {
  22. System.out.print(getCharAt(i, j));
  23. }
  24. System.out.println();
  25. }
  26. }
  27. private String getCharAt(int i, int j) {
  28. if (mineAt(i, j)) {
  29. return "X";
  30. }
  31. int minesNear = countMinesNear(i, j);
  32. return Integer.toString(minesNear);
  33. }
  34. private boolean mineAt(int i, int j) {
  35. return minesweeper[i][j] == 'X';
  36. }
  37. private int countMinesNear(int i, int j) {
  38. int mines = 0;
  39. for (int x = -1; x <= 1; x++) {//near fields in x direction
  40. for (int y = -1; y <= 1; y++) {//near fields in y direction
  41. if (x + i >= 0 && x + i < minesweeper.length && y + j >= 0 && y + j < minesweeper.length) {//check whether the field exists
  42. if (minesweeper[x+i][y+j] == 'X') {//check whether the field is a mine
  43. mines++;
  44. }
  45. }
  46. }
  47. }
  48. return mines;
  49. }
  50. public void randomX() {
  51. System.out.print("How many mines do you want on the field?: ");
  52. int numberOfMines = sc.nextInt();
  53. int i = 0;
  54. while (i < numberOfMines) {
  55. int x = randNum.nextInt(9);
  56. int y = randNum.nextInt(9);
  57. if (minesweeper[x][y] == '*') {
  58. minesweeper[x][y] = 'X';
  59. i++;
  60. }
  61. }
  62. printMinesweeper();
  63. }
  64. }
英文:

You can do it like this:

  1. import java.util.Random;
  2. import java.util.Scanner;
  3. public class Minesweeper {
  4. public static void main(String[] args) {
  5. Minesweeper minesweeper = new Minesweeper();
  6. minesweeper.randomX();
  7. minesweeper.printMinesweeper();
  8. }
  9. char[][] minesweeper = new char[9][9];
  10. Random randNum = new Random();
  11. Scanner sc = new Scanner(System.in);
  12. public Minesweeper() {
  13. for (int i = 0; i &lt; 9; i++) {
  14. for (int j = 0; j &lt; 9; j++) {
  15. minesweeper[i][j] = &#39;*&#39;;
  16. }
  17. }
  18. }
  19. public void printMinesweeper() {
  20. for (int i = 0; i &lt; 9; i++) {
  21. for (int j = 0; j &lt; 9; j++) {
  22. System.out.print(getCharAt(i, j));
  23. }
  24. System.out.println();
  25. }
  26. }
  27. private String getCharAt(int i, int j) {
  28. if (mineAt(i, j)) {
  29. return &quot;X&quot;;
  30. }
  31. int minesNear = countMinesNear(i, j);
  32. return Integer.toString(minesNear);
  33. }
  34. private boolean mineAt(int i, int j) {
  35. return minesweeper[i][j] == &#39;X&#39;;
  36. }
  37. private int countMinesNear(int i, int j) {
  38. int mines = 0;
  39. for (int x = -1; x &lt;= 1; x++) {//near fields in x direction
  40. for (int y = -1; y &lt;= 1; y++) {//near fields in y direction
  41. 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
  42. if (minesweeper[x+i][y+j] == &#39;X&#39;) {//check whether the field is a mine
  43. mines++;
  44. }
  45. }
  46. }
  47. }
  48. return mines;
  49. }
  50. public void randomX() {
  51. System.out.print(&quot;How many mines do you want on the field?: &quot;);
  52. int numberOfMines = sc.nextInt();
  53. int i = 0;
  54. while (i &lt; numberOfMines) {
  55. int x = randNum.nextInt(9);
  56. int y = randNum.nextInt(9);
  57. if (minesweeper[x][y] == &#39;*&#39;) {
  58. minesweeper[x][y] = &#39;X&#39;;
  59. i++;
  60. }
  61. }
  62. printMinesweeper();
  63. }
  64. }

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:

确定