Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class FillTheBoard {
- public static void main(String[] args) {
- int row = 16;
- int col = 16;
- int numberOfMines = 49;
- int[][] board = new int[row][col];
- placeMines(board, numberOfMines, row, col);
- countMines(board);
- printBoard(board);
- }
- public static void placeMines(int[][] mineField, int mines, int row, int col) {
- Random random = new Random();
- int minesPlaced = 0;
- while(minesPlaced < mines) {
- int x = random.nextInt(row); // a number between 0 and mWidth - 1
- int y = random.nextInt(col);
- // make sure we don't place a mine on top of another
- if(mineField[y][x] != 9) {
- mineField[y][x] = 9;
- minesPlaced ++;
- }
- }
- }
- public static void countMines(int[][] board) {
- int mines = 0;
- for (int i = 0; i < board.length; i++) {
- for (int j = 0; j < board[i].length; j++) {
- if (board[i][j] == 9)
- mines++;
- }
- }
- System.out.println(mines);
- }
- public static void printBoard(int[][] board) {
- for (int i = 0; i < board.length; i++) {
- for (int j = 0; j < board[i].length; j++) {
- System.out.print(board[i][j] + " ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement