Kosheen

09 October

Oct 9th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. package it.samsung;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Main {
  6. public static int hasBomb(int[][] field, int i, int j) {
  7. // проверяет, находится ли поле с координатами i,j
  8. // внутри массива, если да, то проверяет, есть ли
  9. // мина на этом поле
  10. if (i >= 0 && i <= field.length-1 &&
  11. j >= 0 && j <= field[0].length-1)
  12. if (field[i][j] == -1)
  13. return 1;
  14. else return 0;
  15. // ячейка массива с миной содержит -1
  16. else return 0;
  17.  
  18. }
  19. public static void printArray(int [][] arr) {
  20. for (int i = 0; i < arr.length; i++) {
  21. for (int j = 0; j < arr[0].length; j++) {
  22. System.out.printf("%d ", arr[i][j]);
  23. }
  24. System.out.println();
  25. }
  26.  
  27. }
  28.  
  29. public static void main(String[] args) {
  30. int[][] pole = new int[][]{{0,-1,0}, {-1,0,0} };
  31. for (int i = 0; i < pole.length; i++) {
  32.  
  33. for (int j = 0; j < pole[0].length; j++) {
  34. if (pole[i][j] == -1)
  35. continue;
  36. pole[i][j] =
  37. hasBomb(pole, i-1,j) +
  38. hasBomb(pole, i+1,j) +
  39. hasBomb(pole, i,j-1) +
  40. hasBomb(pole, i,j+1) +
  41.  
  42. hasBomb(pole, i-1,j-1) +
  43. hasBomb(pole, i+1,j+1) +
  44. hasBomb(pole, i-1,j+1) +
  45. hasBomb(pole, i+1,j-1);
  46. }
  47.  
  48. }
  49.  
  50. printArray(pole);
  51. }
  52.  
  53.  
  54.  
  55. }
Add Comment
Please, Sign In to add comment