I am creating a minesweeper like application and have some code that aims to check around a non mined space how many mines are around it. I have created something to this effect
int count = 0;
if(model.get(i-1, j-1) == MinerGridCo.UNTURNED_MINE){ count++;}
if(model.get(i, j-1) == MinerGridCo.UNTURNED_MINE){ count++;}
if(model.get(i-1, j) == MinerGridCo.UNTURNED_MINE){ count++;}
if(model.get(i+1, j) == MinerGridCo.UNTURNED_MINE){ count++;}
if(model.get(i, j+1) == MinerGridCo.UNTURNED_MINE){ count++;}
if(model.get(i-1, j+1) == MinerGridCo.UNTURNED_MINE){ count++;}
if(model.get(i+1, j-1) == MinerGridCo.UNTURNED_MINE){ count++;}
if(model.get(i+1, j+1) == MinerGridCo.UNTURNED_MINE){ count++;}
String mineNum = String.valueOf(count);
cell[i][j].setText(mineNum);
however, this produces errors when aim to get the mine number around the edges of the board. Any useful methods to avoid this?
After trying suggestions below. I am still getting the out of bounds errors. Anyone have any advice, here is the repo if anyone wants to compile it themselves https://github.com/phillolivercomp/MineSweeper.git
model?List<Cell> getNeighbors(Cell cell)that, given a cell, returns a list of valid neighbors to be examined. Then examine only those neighbors.for (int x=-1; x <= 1; x++) { for (int y=-1; y <= 1; y++) { if( model.get( i+x, j+y )...You get the idea.