Java扫雷游戏问题。游戏开始时隐藏地雷?

发布于 2024-10-07 18:49:37 字数 938 浏览 9 评论 0原文

当游戏开始时,我已经能够成功地启动雷区,并在场地上随机散布 10 个地雷。然而,当游戏开始时,我在向用户隐藏这些地雷时遇到了问题。如您所知,扫雷游戏的目的是在看不见的情况下找到地雷的位置。我需要帮助弄清楚如何隐藏地雷。

下面是我为 Grid 类编写的代码,它启动网格并包含一个用地雷填充网格的方法。我怎样才能隐藏这些地雷并仅在点击它们时才显示它们?感谢您的帮助!

public class Grid {

 private int[][] grid;
 private boolean isHidden;
 private  int rows;
 private final int columns;
 private final int mines;


 public Grid() {
  this.rows = 8;
  this.columns = 8;
  this.mines = 10;
  this.grid = new int[rows][columns];

 }

 public int[][] getGrid() {
  return grid;
 }

 public int getRows() {
  return rows;
 }

 public int getColumns() {
  return columns;
  }

 public void fillGrid() {
  Random ranGen = new Random();

  for(int i = 0; i < this.mines; ) {
   int row = ranGen.nextInt(this.rows - 1);
   int column = ranGen.nextInt(this.columns - 1);

   if(grid[row][column] != MinesweeperGUI.MINE) {
    grid[row][column] = MinesweeperGUI.MINE;
    i++;
   }
  }
    }

I've been able to successfully initiate a minefield when the game begins with 10 mines randomly scattered over the field. However, I'm having problems hiding these mines from the user when the game begins. As you know, the point of minesweeper is to find where the mines are WITHOUT being able to see them. I need help figuring out how to hide the mines.

Below is the code I have written for my Grid class which initiates the grid and includes a method which fills it with mines. How can I hide these mines and only reveal them when they are clicked on? Thanks for the help!

public class Grid {

 private int[][] grid;
 private boolean isHidden;
 private  int rows;
 private final int columns;
 private final int mines;


 public Grid() {
  this.rows = 8;
  this.columns = 8;
  this.mines = 10;
  this.grid = new int[rows][columns];

 }

 public int[][] getGrid() {
  return grid;
 }

 public int getRows() {
  return rows;
 }

 public int getColumns() {
  return columns;
  }

 public void fillGrid() {
  Random ranGen = new Random();

  for(int i = 0; i < this.mines; ) {
   int row = ranGen.nextInt(this.rows - 1);
   int column = ranGen.nextInt(this.columns - 1);

   if(grid[row][column] != MinesweeperGUI.MINE) {
    grid[row][column] = MinesweeperGUI.MINE;
    i++;
   }
  }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睫毛上残留的泪 2024-10-14 18:49:37

方法很多。这里最容易实现的是一个二维布尔数组:已打开和未打开。如果未打开,则显示一个封闭的正方形。否则,显示那里有什么。

另一种方法是使其更加面向对象,并创建一个 Tile 类:它包含一个用于矿井的布尔值,以及一个用于打开/关闭/标记/问号的枚举。

Lots of methods. Easiest to implement here would be a 2d array of booleans: Opened and unopened. If not opened, show a closed square. Else, show what's there.

An alternative method would be to turn this more object oriented, and make a Tile class: it holds a boolean for a mine, and a enum for open/closed/flagged/question mark.

娜些时光,永不杰束 2024-10-14 18:49:37

您应该有两个矩阵:一个用于地雷 (MineMatrix),另一个用于跟踪用户点击 (UserClickMatrix)。

您应该只向最终用户显示 UserClickMatrix 并在游戏开始时使用 NotClicked 初始化所有单元格。每当用户单击某个单元格时,将该单元格的状态更改为Clicked。然后,从 UserClickMatrix 中获取单元格索引并检查 MineMatrix (MineMatrix[clickedRow][clickedColumn] == MINE)。如果条件评估为 TRUE,游戏结束。

You should have two matrices: One for the mines (MineMatrix) and one to track the user clicks (UserClickMatrix).

You should only show the end user UserClickMatrix and initialize all the cells with NotClicked at the start of the game. Whenever the user clicks on a cell, change the status of the cell to Clicked. Then, grab the cell indices from UserClickMatrix and check against MineMatrix (MineMatrix[clickedRow][clickedColumn] == MINE). If the condition evaluates to TRUE, game ends.

北城孤痞 2024-10-14 18:49:37

为什么您的网格矩阵不包含 GridElement 类的元素?该 GridElement 类可以具有属性“bool visibile”。整数网格矩阵不太好。多思考OO。

Why doesn't your grid matrix contains elements of the class GridElement? This GridElement class could have an property "bool visibile". An grid matrix of int's isn't so good. Think more OO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文