Advertisement
Guest User

babababab

a guest
Jun 25th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class asd {
  5.     static int row = 0;
  6.     static int col = 0;
  7.     static int numberOfMines = 0;
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         Scanner sc = new Scanner(System.in);
  12.         row = sc.nextInt();
  13.         col = sc.nextInt();
  14.         numberOfMines = sc.nextInt();
  15.         sc.close();
  16.  
  17.         int[][] mineField = new int[row][col];
  18.  
  19.         placeMines(mineField);
  20.        
  21.         calcNumbers(mineField);
  22.        
  23.         countMines(mineField);
  24.  
  25.         printBoard(mineField);
  26.  
  27.     }
  28.  
  29.     public static void calcNumbers(int[][] mineFiled) {
  30.         for (int i = 0; i < mineFiled.length; i++) {
  31.             for (int j = 0; j < mineFiled[i].length; j++) {
  32.                 if (mineFiled[i][j] != 9) {
  33.                     mineFiled[i][j] = minesNear(mineFiled, i, j);
  34.                 }
  35.             }
  36.         }
  37.     }
  38.  
  39.     public static int minesNear(int[][] mineFiled, int x, int y) {
  40.         int mines = 0;
  41.         // check mines in all directions
  42.         mines += mineChecker(mineFiled, x - 1, y - 1); // NW
  43.         mines += mineChecker(mineFiled, x - 1, y); // N
  44.         mines += mineChecker(mineFiled, x - 1, y + 1); // NE
  45.         mines += mineChecker(mineFiled, x, y - 1); // W
  46.         mines += mineChecker(mineFiled, x, y + 1); // E
  47.         mines += mineChecker(mineFiled, x + 1, y - 1); // SW
  48.         mines += mineChecker(mineFiled, x + 1, y); // S
  49.         mines += mineChecker(mineFiled, x + 1, y + 1); // SE
  50.         if (mines > 0) {
  51.             // we're changing an int to a char
  52.             // why?!
  53.             // http://www.asciitable.com/
  54.             // 48 is ASCII code for '0'
  55.             return mines;
  56.         } else {
  57.             return 0;
  58.         }
  59.     }
  60.  
  61.     public static int mineChecker(int[][] mineField, int x, int y) {
  62.         if ((x >= 0 && x < row) && y >= 0 && y < col && mineField[x][y] == 9) {
  63.             return 1;
  64.         } else {
  65.             return 0;
  66.         }
  67.     }
  68.  
  69.     public static void placeMines(int[][] mineField) {
  70.         Random random = new Random();
  71.         int minesPlaced = 0;
  72.  
  73.         while (minesPlaced < numberOfMines) {
  74.             int x = random.nextInt(row);
  75.             int y = random.nextInt(col);
  76.             // make sure we don't place a mine on top of another
  77.             if (mineField[y][x] != 9) {
  78.                 mineField[y][x] = 9;
  79.                 minesPlaced++;
  80.             }
  81.         }
  82.     }
  83.  
  84.     public static void countMines(int[][] mineFiled) {
  85.         int mines = 0;
  86.         for (int i = 0; i < mineFiled.length; i++) {
  87.             for (int j = 0; j < mineFiled[i].length; j++) {
  88.                 if (mineFiled[i][j] == 9)
  89.                     mines++;
  90.             }
  91.         }
  92.  
  93.         System.out.println(mines);
  94.     }
  95.  
  96.     public static void printBoard(int[][] mineFiled) {
  97.         for (int i = 0; i < mineFiled.length; i++) {
  98.             for (int j = 0; j < mineFiled[i].length; j++) {
  99.                 System.out.print(mineFiled[i][j] + " ");
  100.             }
  101.             System.out.println();
  102.         }
  103.     }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement