Advertisement
Guest User

ETTooo

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