Need help on these three methods in a minesweeper program in Java, I typed what
ID: 3712161 • Letter: N
Question
Need help on these three methods in a minesweeper program in Java, I typed what I need done below each method. Thank -you for your help, I am pretty confused with these instructions
/** Sets all tiles to 1 - closed
*/
private void resetTiles() {
// add your code here
}
/** places mines randomly on grid
* integer value 9 represents a mine<br>
* number of mines = (1 + number of columns * number rows) / 10<br>
* minimum number of mines = 1<br>
*/
private void placeMines() {
// add your code here
}
/** calculates clue values and updates
* clue values in mines array<br>
* integer value 9 represents a mine<br>
* clue values will be 0 ... 8<br>
*/
private void calculateClues() {
// add your code here
}
/** determines if x,y is valid position
* @param x row index
* @param y column index
* @return true if valid position on board,
* false if not valid board position
*/
Explanation / Answer
Below are your methods
private void calculateClues(){
// Loop through the board
for (int i = 0; i < getRows(); i++){
for (int j = 0; j < getCols(); j++){
// Check if the spot is not a mine
if (mines[i][j] != 9){
// Call method
mines[i][j] = spotChecker(i, j);
}
}
}
}
// Checks surrounding of the spot if it is a valid index and a mineprivate
int spotChecker(int row, int column){
int counter = 0;
for (int i = row - 1; i <= row + 1; i++){
for (int j = column - 1; j <= column + 1; j++){
if (validIndex(i, j) && mines[i][j] == 9)
{
counter++;
}
}
}
return counter;
}
private void placeMines(){
// Initialise mines to 0
int numOfMines = 0;
// While mines value does not hit the target value
while (numOfMines != (1 + getRows() * getCols()) / 10){
// Randomly gets a spot to put mine using math.random
int row = (int) (Math.random() * getRows());
int col = (int) (Math.random() * getCols());
// Check if the spot is not already a mine
if (mines[row][col] != 9){
// Set spot to mine and increment mine counter
mines[row][col] = 9;
numOfMines++;
}
}
}
/**
* Sets all tiles to 1 - closed
*/
private void resetTiles() {
for (int i = 0; i < tiles.length; i++) {
for (int j = 0; j < tiles[i].length; j++) {
tiles[i][j] = 1;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.