Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 00 Mine Counter! Objective: Write a program that creates a 10x10 multi-dimen

ID: 2246612 • Letter: L

Question

Lab 00 Mine Counter! Objective: Write a program that creates a 10x10 multi-dimensional array, places 10 mines in random locations on the board, and finally counts the number of mines an empty space is neighboring 8 squares. For reference this works similarly to functionality in the game "Minesweeper". Then finally demonstrate that the program can create an empty board, populate it with mines, and finally count mines by using its The example above shows the center squares value is based on the number of mines "*") that surrounds its 8 neighbors. Tips and Hints: import java.util.Random; Make sure exactly 10 mines are placed in the board. If a mine already exists in that space then randomly pick another space until there are 10. This may require a loop. g each of the functionalities required into multiple methods may be extremely beneficial . A multi-dimensional array of enums, Strings, or even ints can solve this problem.

Explanation / Answer

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//package chegg;
import java.util.*;


class Mine
{
public static void main(String[] args)
{
char[][] a = new char[10][10];
System.out.println("Creating empty board");
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
a[i][j] = '_';
System.out.print(a[i][j]); // Create empty board
}
System.out.println();
}
int mines = 10;
Random r = new Random();

while (mines > 0)
{
int i = r.nextInt(10); // Get random indices
int j = r.nextInt(10);
if (a[i][j] != '*')
{
a[i][j] = '*'; // Fill with mines
mines--;
}
}
System.out.println("Placing mines");
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
System.out.print(a[i][j]);
}
System.out.println();
}
  
System.out.println("Performing mine count");
int[][] mineCount = new int[10][10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (a[i][j] == '_') // Get all cases
{
int mine = 0;
if (i + 1 < 10 && a[i + 1][j] == '*')
mine++;
  
if (j + 1 < 10 && a[i][j + 1] == '*')
mine++;
  
if (j - 1 >= 0 && a[i][j - 1] == '*')
mine++;
  
if (i - 1 >= 0 && a[i - 1][j] == '*')
mine++;
  
if (j + 1 < 10 && i + 1 < 10 && a[i + 1][j + 1] == '*')
mine++;
  
if (j + 1 < 10 && i - 1 >= 0 && a[i - 1][j + 1] == '*')
mine++;
  
if (i + 1 < 10 && j - 1 >= 0 && a[i + 1][j - 1] == '*')
mine++;
  
if (j - 1 >= 0 && i - 1 >= 0 && a[i - 1][j - 1] == '*')
mine++;
  
a[i][j] = Integer.toString(mine).charAt(0);
}
}
}
  
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
System.out.print(a[i][j]);
}
System.out.println();
}
}   
}

The output of above program I'm getting is:
debug:
Creating empty board
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
Placing mines
_____*__*_
________*_
_________*
_*______*_
__________
______*___
___*______
____**____
__________
__________
Performing mine count
00001*12*2
00001112*3
111000023*
1*100001*2
1110011211
001111*100
001*332100
0012**1000
0001221000
0000000000
BUILD SUCCESSFUL (total time: 0 seconds)

Lab
1) It is not possible to change size of standard array
2) It is not possible for standard arrays. You can change for arrayLists.