fork download
  1. public class Minesweeper {
  2. public static void main(String[] args) {
  3. int m = Integer.parseInt(args[0]);
  4. int n = Integer.parseInt(args[1]);
  5. double p = Double.parseDouble(args[2]);
  6.  
  7. // game grid is [1..m][1..n], border is used to handle boundary cases
  8. boolean[][] bombs = new boolean[m+2][n+2];
  9. for (int i = 1; i <= m; i++)
  10. for (int j = 1; j <= n; j++)
  11. bombs[i][j] = (Math.random() < p);
  12.  
  13. // print game
  14. for (int i = 1; i <= m; i++) {
  15. for (int j = 1; j <= n; j++)
  16. if (bombs[i][j]) System.out.print("* ");
  17. else System.out.print(". ");
  18. System.out.println();
  19. }
  20.  
  21. // sol[i][j] = # bombs adjacent to cell (i, j)
  22. int[][] sol = new int[m+2][n+2];
  23. for (int i = 1; i <= m; i++)
  24. for (int j = 1; j <= n; j++)
  25. // (ii, jj) indexes neighboring cells
  26. for (int ii = i - 1; ii <= i + 1; ii++)
  27. for (int jj = j - 1; jj <= j + 1; jj++)
  28. if (bombs[ii][jj]) sol[i][j]++;
  29.  
  30. // print solution
  31. System.out.println();
  32. for (int i = 1; i <= m; i++) {
  33. for (int j = 1; j <= n; j++) {
  34. if (bombs[i][j]) System.out.print("* ");
  35. else System.out.print(sol[i][j] + " ");
  36. }
  37. System.out.println();
  38. }
  39.  
  40. }
  41. }
  42.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:1: error: class Minesweeper is public, should be declared in a file named Minesweeper.java
public class Minesweeper { 
       ^
1 error
stdout
Standard output is empty