Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package it.samsung;
- import java.util.Arrays;
- public class Main {
- public static int hasBomb(int[][] field, int i, int j) {
- // проверяет, находится ли поле с координатами i,j
- // внутри массива, если да, то проверяет, есть ли
- // мина на этом поле
- if (i >= 0 && i <= field.length-1 &&
- j >= 0 && j <= field[0].length-1)
- if (field[i][j] == -1)
- return 1;
- else return 0;
- // ячейка массива с миной содержит -1
- else return 0;
- }
- public static void printArray(int [][] arr) {
- for (int i = 0; i < arr.length; i++) {
- for (int j = 0; j < arr[0].length; j++) {
- System.out.printf("%d ", arr[i][j]);
- }
- System.out.println();
- }
- }
- public static void main(String[] args) {
- int[][] pole = new int[][]{{0,-1,0}, {-1,0,0} };
- for (int i = 0; i < pole.length; i++) {
- for (int j = 0; j < pole[0].length; j++) {
- if (pole[i][j] == -1)
- continue;
- pole[i][j] =
- hasBomb(pole, i-1,j) +
- hasBomb(pole, i+1,j) +
- hasBomb(pole, i,j-1) +
- hasBomb(pole, i,j+1) +
- hasBomb(pole, i-1,j-1) +
- hasBomb(pole, i+1,j+1) +
- hasBomb(pole, i-1,j+1) +
- hasBomb(pole, i+1,j-1);
- }
- }
- printArray(pole);
- }
- }
Add Comment
Please, Sign In to add comment