class Neigbors{ int COL_SIZE = 4; int ROW_SIZE = 4; for (int currentBombIndex = 1; currentBombIndex < ((COL_SIZE * ROW_SIZE) + 1); currentBombIndex++) { int currentBombCol = ((currentBombIndex - 1) % COL_SIZE) + 1; int currentBombRow = ((currentBombIndex - 1) / COL_SIZE) + 1; System.out.println("Neighbors of " + currentBombIndex + " (" + currentBombRow + ", " + currentBombCol + ")"); for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { if (x == 0 && y == 0) { continue; // Current bomb index } int neighborCol = currentBombCol + y; int neighborRow = currentBombRow + x; if (neighborCol > 0 && neighborRow > 0 && neighborCol <= COL_SIZE && neighborRow <= ROW_SIZE ) { int computedNeighborIndex = neighborCol + ((neighborRow - 1) * COL_SIZE); } } } } } }
Standard input is empty
Neighbors of 1 (1, 1) 2 5 6 Neighbors of 2 (1, 2) 1 3 5 6 7 Neighbors of 3 (1, 3) 2 4 6 7 8 Neighbors of 4 (1, 4) 3 7 8 Neighbors of 5 (2, 1) 1 2 6 9 10 Neighbors of 6 (2, 2) 1 2 3 5 7 9 10 11 Neighbors of 7 (2, 3) 2 3 4 6 8 10 11 12 Neighbors of 8 (2, 4) 3 4 7 11 12 Neighbors of 9 (3, 1) 5 6 10 13 14 Neighbors of 10 (3, 2) 5 6 7 9 11 13 14 15 Neighbors of 11 (3, 3) 6 7 8 10 12 14 15 16 Neighbors of 12 (3, 4) 7 8 11 15 16 Neighbors of 13 (4, 1) 9 10 14 Neighbors of 14 (4, 2) 9 10 11 13 15 Neighbors of 15 (4, 3) 10 11 12 14 16 Neighbors of 16 (4, 4) 11 12 15