Advertisement
salron3

MM

Jun 25th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class FillTheBoard {
  4.  
  5.     public static void main(String[] args) {
  6.         int row = 16;
  7.         int col = 16;
  8.         int numberOfMines = 49;
  9.         int[][] board = new int[row][col];
  10.  
  11.  
  12.         placeMines(board, numberOfMines, row, col);
  13.        
  14.         countMines(board);
  15.        
  16.         printBoard(board);
  17.  
  18.     }
  19.    
  20.     public static void placeMines(int[][] mineField, int mines, int row, int col) {
  21.         Random random = new Random();
  22.         int minesPlaced = 0;
  23.        
  24.         while(minesPlaced < mines) {
  25.           int x = random.nextInt(row); // a number between 0 and mWidth - 1
  26.           int y = random.nextInt(col);
  27.           // make sure we don't place a mine on top of another
  28.           if(mineField[y][x] != 9) {
  29.               mineField[y][x] = 9;
  30.             minesPlaced ++;
  31.           }
  32.         }
  33.     }
  34.  
  35.     public static void countMines(int[][] board) {
  36.         int mines = 0;
  37.         for (int i = 0; i < board.length; i++) {
  38.             for (int j = 0; j < board[i].length; j++) {
  39.                 if (board[i][j] == 9)
  40.                     mines++;
  41.             }
  42.         }
  43.        
  44.         System.out.println(mines);
  45.     }
  46.  
  47.     public static void printBoard(int[][] board) {
  48.         for (int i = 0; i < board.length; i++) {
  49.             for (int j = 0; j < board[i].length; j++) {
  50.                 System.out.print(board[i][j] + " ");
  51.             }
  52.             System.out.println();
  53.         }
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement