public class Minefield { // An m x n minefiled is modeled as a 2D array of boole
ID: 3567656 • Letter: P
Question
public class Minefield
{
// An m x n minefiled is modeled as a 2D array of booleans, with "true"
// representing a mine cell and "false" representing an empty cell.
// SUGGESTION: instead of using an m x n array, use an (m + 2) x (n + 2)
// array to represent an m x n minefield. That way, each cell in
// the m x n minefield has eight surrounding neighbors, and can be
// nicely indexed by 1 <= i <= m and 1 <= j <= n.
// NOTE: m and n need not be attributes since the number of rows and
// columns of a 2D array A can be dynamically obtained as A.length and
// A[0].length respectively.
private boolean[][] mines;
// Constructs a minefield of dimention m x n in which each cell is a
// mine with probability p.
public Minefield(int m, int n, double p)
{
/* Your code here */
}
// Prints a layout of the minefield in which a '*' denotes a mine cell
// and a '-' denotes an empty cell.
public void printMinePlacement()
{
/* Your code here */
}
// Prints a layout of the minefield in which a '*' denotes a mine cell
// and an empty cell is marked by a number corresponding to the number
// of surrounding mines.
public void printMineCounts()
{
/* Your code here */
}
}
This is minesweeper:
public class Minesweeper
{
public static void main(String[] args)
{
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
double p = Double.parseDouble(args[2]);
Minefield mf = new Minefield(m, n, p);
System.out.println("Mine placement: ");
mf.printMinePlacement();
System.out.println();
System.out.println("Mine counts: ");
mf.printMineCounts();
}
}
Explanation / Answer
if I inputed:
This code will output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.