MineSweeper
题目:
实现一个扫雷的constructor功能。
解题思路:
这道题就是先把雷部署好,然后弄好数字。
代码:
实现一个扫雷的constructor功能。
解题思路:
这道题就是先把雷部署好,然后弄好数字。
代码:
public class MineSweeper { private int width; private int height; private int minesCount; int numMarked; int numUnknown; boolean[][] mines; int[][] board; public void Board(int width, int height, int minesCount) { // Initialise instance variables. Note the use of 'this' when parameters // have the same name. this.width = width; this.height = height; this.minesCount = minesCount; this.numMarked = 0; this.numUnknown = width * height; // Allocate storage for game board and mines mines = new boolean[width][height]; board = new int[width][height]; // Clear the board for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { mines[i][j] = false; board[i][j] = -1; } } // Randomly allocate mines. The loop runs until numMines mines have been // placed on the board. The the purposes of this operation we treat the // board as a width*height linear array of cells, and simply try again if // the chosen cell already contains a mine. int cells = width * height; int temp = 0; Random rand = new Random(); while (temp < minesCount) { int cell = rand.nextInt(); cell = (cell < 0 ? -cell : cell)%cells; if (!mines[cell%width][cell/width]) { mines[cell%width][cell/width] = true; temp++; } } } }

Comments
Post a Comment