Hello, I am a student taking APCS and I am getting very frustrated with this program. Usually I can get everything figured out but I can't figure this out. It compiles but when I run it, it seems to give the wrong number for the number of bombs in adjacent squares. I'm fairly sure my logic is correct but I may be wrong. I provided sample runs to illustrate my problem.
import java.util.Scanner;
public class Minesweeper
{
public static void main (String [] args)
{
int size = welcomeAndAskForSizeOfField();
int [] [] minesfield = new int [size] [size];
char [] []outputField = new char [size][size];
init (minesfield, outputField);
displayOutput(outputField);
boolean gameOver = false;
while (gameOver != true)
{
gameOver = playGame(minesfield, outputField);
displayOutput(outputField);
}
} // main
//********************************************* welcomeAndAskForSizeOfField
public static int welcomeAndAskForSizeOfField()
{
Scanner keybd = new Scanner(System.in);
System.out.println("Welcome to the game of minesweeper! If you would like to play on a 5x5 grid press 5");
System.out.println("If you want to play on a 10x10 grid press 10");
int choice = keybd.nextInt();//input by user
if (choice==5 || choice ==10)
{
return choice;
}
else//if not 5 or 10
{
System.out.println("Error, please try again");
choice=welcomeAndAskForSizeOfField();//a loop until correct grid size is chosen
}
return choice;
}
//********************************************* init
public static void init (int [] [] mines, char [] [] output)
{
for (int r = 0; r < output.length; r++)
{
for (int c = 0; c < output[0].length; c++)
output[r][c] = ' ';
}
// generate a random mine field
for (int r = 0; r < mines.length; r++)
{
for (int c = 0; c < mines[0].length; c++)
mines[r][c] = (int)(Math.random() * 2);
}
} // init
//********************************************* displayOutput
public static void displayOutput (char [] [] output)
{
for (int r = 0; r < output.length; r++)
{
for (int c = 0; c < output[0].length; c++)
System.out.print ("[" + (char)output[r][c] + "]");
System.out.println();
}
}
//********************************************* countMines
public static int countMines (int [] [] mines, int r, int c)
{
int rMin = r-1;
int rMax = r+1;
int cMin = c-1;
int cMax = c+1;
if (r == 0)
{rMin = 0;}
if (r == mines.length-1)
{rMax = mines.length-1;}
if (c == 0)
{cMin = 0;}
if (c == mines.length-1)
{cMax = mines.length-1;}
int count = 0;
for(int i = rMin; i<= rMax; i++)//rows
{
for(int j = cMin; j<= cMax; j++)//columns
{
if(mines[i][j] == 1 && i != r && j !=c)//checks around square for bombs
{
count++;
}
}
}
return count;
} // countMines
//********************************************* checkStatus
private static boolean checkStatus(int [][] mines)
{
int count = 0;
int target = mines.length*mines.length;//total area of grid
for (int r = 0; r<mines.length; r++)
{
for(int c = 0; c<mines.length;c++)
{
if(mines[r][c]==1 || mines[r][c]==-1)//counts bombs and used spaces
{
count++;
}
if (count==target)//if count is equal to the area, the game is over because there are no more blank spaces
{
return true;
}
}
}
return false;//grid not complete
}//checkStatus
//********************************************* playGame
public static boolean playGame (int [][] mines, char [] [] outputBoard)
{
Scanner keybd = new Scanner(System.in);
System.out.println("Enter row and column of the cell you want to open[row col]:");
int row = keybd.nextInt();//row #
int col = keybd.nextInt();//column #
if (mines[row][col] == 1)//if there is a bomb a that coordinate
{
System.out.println("You stepped on a mine and killed all of your troops, you lose.");
outputBoard[row][col]='X';
return true;//game over
}
else
{
outputBoard[row][col]=(countMines(mines,row,col)+"").charAt(0);//display number of bombs surrounding space
mines[row][col]=-1;//mark space as used
if (checkStatus(mines)==true)//The user wins
{
System.out.println("Congratulations! You saved your troops and made it across the minefield.");
return true;
}
return false;//game continues
}
} // playGame
} // Minesweeper
/*
Welcome to the game of minesweeper! If you would like to play on a 5x5 grid press 5
If you want to play on a 10x10 grid press 10
[DrJava Input Box]: 5
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
Enter row and column of the cell you want to open[row col]:
[DrJava Input Box]: 0 0
[0][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
Enter row and column of the cell you want to open[row col]:
[DrJava Input Box]: 0 1
[0][1][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
Enter row and column of the cell you want to open[row col]:
[DrJava Input Box]: 1 0
You stepped on a mine and killed all of your troops, you lose.
[0][1][ ][ ][ ]
[X][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
end game 1
Welcome to the game of minesweeper! If you would like to play on a 5x5 grid press 5
If you want to play on a 10x10 grid press 10
[DrJava Input Box]: 5
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
Enter row and column of the cell you want to open[row col]:
[DrJava Input Box]: 2 2
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][0][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
Enter row and column of the cell you want to open[row col]:
[DrJava Input Box]: 2 1
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][2][0][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
Enter row and column of the cell you want to open[row col]:
[DrJava Input Box]: 2 3
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][2][0][2][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
Enter row and column of the cell you want to open[row col]:
[DrJava Input Box]: 1 2
You stepped on a mine and killed all of your troops, you lose.
[ ][ ][ ][ ][ ]
[ ][ ][X][ ][ ]
[ ][2][0][2][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
end game 2
*/