-1

I have all my cells stored in an ArrayList and I want to check how many mines they are surrounded by (mines are cells with a not null mine png). I thought of checking the positions -1, +1, -9, +9, -10, +10, -11, +11 relative to each cell and add 1 to a counter inside the cell object. Problem is I get out of bounds and don´t know how to avoid it.

for (Cell cell: cells){
        if ((cells.get(cells.indexOf(cell) - 1).mine != null)&&((cells.indexOf(cell) - 1) >= 0)) {
            cell.setMine_number(cell.getMine_number() + 1);
        }
        if ((cells.get(cells.indexOf(cell) + 1).mine != null)&&((cells.indexOf(cell) + 1) < 100)) {
            cell.setMine_number(cell.getMine_number() + 1);
        }
        if ((cells.get(cells.indexOf(cell) - 10).mine != null)&&((cells.indexOf(cell) - 10) >= 0)) {
            cell.setMine_number(cell.getMine_number() + 1);
        }
        if ((cells.get(cells.indexOf(cell) + 10).mine != null)&&((cells.indexOf(cell) + 10) < 100)) {
            cell.setMine_number(cell.getMine_number() + 1);
        }
        if ((cells.get(cells.indexOf(cell) - 11).mine != null)&&((cells.indexOf(cell) - 11) >= 0)) {
            cell.setMine_number(cell.getMine_number() + 1);
        }
        if ((cells.get(cells.indexOf(cell) + 11).mine != null)&&((cells.indexOf(cell) + 11) < 100)) {
            cell.setMine_number(cell.getMine_number() + 1);
        }
        if ((cells.get(cells.indexOf(cell) - 9).mine != null)&&((cells.indexOf(cell) - 9) >= 0)) {
            cell.setMine_number(cell.getMine_number() + 1);
        }
        if ((cells.get(cells.indexOf(cell) + 9).mine != null)&&((cells.indexOf(cell) + 9) < 100)) {
            cell.setMine_number(cell.getMine_number() + 1);
        }
    }

Ignore the spaghetti code I always refactor when things work.

Jds Wolf
  • 13
  • 3
  • *Ignore the spaghetti code* ... that is a very bad take when asking a question here. You want others to spend their time to help you with your problem, so you spend *all the time* that is needed to come up with easy to read code. You see, in order to help you, we kinda have to read/understand your input. – GhostCat May 12 '21 at 06:44
  • And unrelated: read about java naming conventions. You dont use "_" in method or variable names. Rather call the method `setMineCount()` / `getMineCount()` for example. Also note that you should create **helpful** abstractions. Why not simple have a method `increaseMineCount()`, instead of doing that "set( get() + 1)` a zillion times? – GhostCat May 12 '21 at 06:47
  • @GhostCat I didn't know I couldn't snake case methods always had the doubt though – Jds Wolf May 12 '21 at 06:59
  • You *can* do a lot of things. Many of them: you should not do. – GhostCat May 12 '21 at 07:33

1 Answers1

0

I thought of checking the positions -1, ...

That thought doesn't work unfortunately.

First of all, you "hardcode" the dimension of your "supposedly 2 dim" list into these numbers. What if you change the grid to 20x20. Then -10 is meaningless.

Then: it is kinda obvious that for plenty of slots, -10 or +10 wont work.

You could create a simple checker method like:

boolean isValidIndex(int cellIndex, int offset) {
  // not doing your homework for you, but rest assured
  // this method is easy to implement

that you then use like:

if (isValidIndex(cells.indexOf(cell), 9))

for example.

GhostCat
  • 137,827
  • 25
  • 176
  • 248