-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.

4
  • 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. Commented May 12, 2021 at 6: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? Commented May 12, 2021 at 6:47
  • @GhostCat I didn't know I couldn't snake case methods always had the doubt though Commented May 12, 2021 at 6:59
  • You can do a lot of things. Many of them: you should not do. Commented May 12, 2021 at 7:33

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.