I\'m creating a minesweeper game in Java, this piece of code counts the number o
ID: 3805833 • Letter: I
Question
I'm creating a minesweeper game in Java, this piece of code counts the number of mines adjacent to a square. How do I count the mines adjacent to a square that are in the border of the grid without having an out of array error? If you can answer whith the little piece of code I gave you.
Explanation / Answer
Start 'for' loop of 'x' and 'y' for 0. Like
for(x=0; x<TOTAL_COLUMNS;x++)
{
for(y=0; y<TOTAL_ROWS;y++)
{
//Add this if condition
if(y>=0 && y<TOTAL_ROWS && x>=0 && x<TOTAL_COLUMNS && bombArray[x][y]==1)
{
numcount++;
}
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.