Programming Languages‎ > ‎Java‎ > ‎

Java Programming Assignment 3

Requirement

Problem: Modify Prog.HW#2 in the following way: Write a class called MineGrid in which you declare a 2-dim. array of boolean (I'm calling it grid) as a private instance variable. Also declare static final variables for MIN_DIM (5) and MAX_DIM (20).


Solution


public class HW3_MineGrid 
{
private boolean [][] grid;
public  static final int MIN_DIMENSION = 5;
public static final int MAX_DIMENSION = 20;
// constructor with an int for the first dimension, and an int for the second dimension, and changing each parameter to MIN_DIM (if it's < MIN_DIM) or to MAX_DIM (if it's > MAX_DIM), checking each one separately. Allocate memory for a 2-dim. array of boolean, using the parameter dimensions and assigning to the instance variable.
public HW3_MineGrid(int rowDim, int colDim)
{
rowDim = (rowDim > 0)? rowDim : 1; // if value <=0 than assign it to 1
colDim = (colDim > 0)? colDim : 1;
rowDim = (rowDim < MIN_DIMENSION)? MIN_DIMENSION : rowDim; // Min and Max check as it is asked in hw requirement
rowDim = (rowDim > MAX_DIMENSION)? MAX_DIMENSION : rowDim;
colDim = (colDim < MIN_DIMENSION)? MIN_DIMENSION : colDim;
colDim = (colDim > MAX_DIMENSION)? MAX_DIMENSION : colDim;
grid = new boolean[rowDim][colDim];
}
// To Do: another constructor with a 2-dim. array of boolean as its parameter (assign to the instance variable if parameter isn't null AND if each row has the same length as the other rows) and each dimension >= MIN_DIM and <= MAX_DIM, otherwise, assign a MIN_DIM X MIN_DIM 2-dim. array of boolean to the instance variable.
public HW3_MineGrid(boolean [][] mineGrid)
{
if(mineGrid != null)
{
for(int i=0; i < (mineGrid.length-1); i++) // Go through all row to check equal width
{
if(mineGrid[i].length != mineGrid[i+1].length) // if each row has the same length as the other rows
{
grid = new boolean [MIN_DIMENSION][MIN_DIMENSION];
return;
}
}
if(mineGrid.length >= MIN_DIMENSION && mineGrid.length <= MAX_DIMENSION && mineGrid[0].length >= MIN_DIMENSION && mineGrid[0].length <= MAX_DIMENSION)
{
grid = new boolean [mineGrid.length][mineGrid[0].length];
grid = mineGrid;
}
else
{
grid = new boolean [MIN_DIMENSION][MIN_DIMENSION];
}
}
else
{
grid = new boolean [MIN_DIMENSION][MIN_DIMENSION];
}
}
// 1. public instance method to return the first dimension (how many "rows") (NOT an instance variable in this class, but use the length of grid!)
public int getRowDimension()
{
return grid.length;
}
// 2. public instance method to return the second dimension (how many "columns" in one row) (NOT an instance variable in this class, but use the 2nd dim. length of grid!)
public int getColumnDimension()
{
return grid[0].length;
}
// 3. public instance method to set randomly selected spots in the instance variable (int parameter for number of spots to set is the ONLY parameter) that first calls initGrid (described below), then assigns the number of spots (randomly chosen) to true if it's not already true. (this is similar to setRandomValues in #2, but assign directly to the instance 2-dim. array variable)
public void setRandomValues(int numSpots)
{
initGrid(); // Set default values to false
for (int i = 0; i < numSpots; i++)
{
// formula to get random number: (int)(Math.random() * (MAX-MIN+1) ) + MIN;
int i_rowIndex = (int)(Math.random() * ((grid.length - 1) - 0 + 1) ) + 0;
int i_colIndex = (int)(Math.random() * ((grid[0].length - 1) - 0 + 1) ) + 0;
if (grid[i_rowIndex][i_colIndex] == true)
{
i--; // loop one more time if value is already true
continue; // not necessary
}
else
{
grid[i_rowIndex][i_colIndex] = true;
}
}
}

// 4. public instance method to initialize all the elements of the instance 2-dim. array variable to false (NO PARAMETERS)
public void initGrid()
{
for (int i = 0; i < grid.length; i++)
{
for (int j = 0; j < grid[i].length; j++)
{
grid[i][j] = false;
}
}
}

// 5. public instance method to set one spot in the instance 2-dim. array variable to true, with 2 int parameters (one for the row index, one for the column index). You MUST check if the row and column are within the bounds of the 2-dim. array (return true if the parameters are OK, false if not)
public boolean SetSpotInGrid(int rowDim, int colDim)
{ // this method returns true if set value is successful
if(rowDim >=0 && rowDim < grid.length && colDim >=0 && colDim < grid[0].length)
{
grid[rowDim][colDim] = true;
return true;
}
else
{
return false;
}
}
// 6. public instance method to return one element of this' 2-dim. array instance variable, passing row & column indices (make sure the row & column are in the array bounds)
public boolean GetSpotInGrid(int rowDim, int colDim)
{
if(rowDim >=0 && rowDim < grid.length && colDim >=0 && colDim < grid[0].length)
{
return grid[rowDim][colDim];
}
else
{
return false; // To Do: how to handle this case
}
}
// 7. public instance method to compare 2 MineGrid objects that has ONLY ONE PARAMETER for a MineGrid object to compare to and return a new MineGrid (you'll need a local 2-dim. array to store the result of COMPARING and pass to the 2nd constructor). Make sure you check if the dimensions of the parameter are the same as this' instance 2-dim. array (if not, return a new MineGrid calling the first constructor passing MIN_DIM, MIN_DIM).
public HW3_MineGrid compareGrids(HW3_MineGrid tempMineGrid)
{
if(grid.length != tempMineGrid.grid.length || grid[0].length != tempMineGrid.grid[0].length)
return new HW3_MineGrid(MIN_DIMENSION, MIN_DIMENSION);
boolean [][] tmpResultMatching = new boolean [grid.length][grid[0].length];
for(int i = 0; i < grid.length; i++)
{
for(int j = 0; j < grid[0].length; j++)
{
tmpResultMatching[i][j] = (grid[i][j] == true && tempMineGrid.grid[i][j] == true) ? true : false;
}
}
return new HW3_MineGrid(tmpResultMatching);
}

// 8. public instance method to display the instance 2-dim. array, one char parameter, no other parameters) that display the char parameter where ever the grid element is true (space, otherwise)
public void displayGrid(char c_arrayType)
{
if(grid.length == 0 || grid[0].length == 0) // To Do: throw an exception
return;
char printChar;
System.out.println("|" + String.format(String.format("%%0%dd", grid[0].length), 0).replace("0","---|"));
for(boolean [] displayRow: grid)
{
System.out.print("|");
for(boolean valueEachRow:displayRow)
{
printChar = (valueEachRow)? c_arrayType : ' '; // print either array specific character or a blank space
System.out.print("-" + printChar + "-|"); // 'G' OR '*' OR 'M'
}
System.out.println("");
}
System.out.println("|" + String.format(String.format("%%0%dd", grid[0].length), 0).replace("0","---|"));
}

}


HW3_Program3Main


/*
Name of program: Simplified version of Minesweeper Game
Programmer's name: ABC
Current Date: dd/mm/yyyy
Computer system and compiler you are using: Win 8, JDK 1.7
Brief description of the program (1-5 sentences): 
Modify Prog.HW#2 in the following way: Write a class called MineGrid
in which you declare a 2-dim. array of boolean (I'm calling it grid) as a private
instance variable. Also declare static final variables for MIN_DIM (5) and MAX_DIM (20).
Variable names (if any) and descriptions of what they represent: numSpots->average of the rowDim and columnDim
*/
import java.util.Scanner;
public class HW3_Program3Main 
{
static Scanner oScanner = new Scanner(System.in);
public static void main(String[] args) 
{
HW3_MineGrid userGrid, gameGrid, resultsGrid;
int i_rowDim = 0, i_columnDim = 0;
int i_numSpots = 0; // average of the rowDim and columnDim
// Get the first and second dimensions the same way as in Prog. HW#2 (using the getDimension method called 2 times)
i_rowDim = InputMethods();
i_columnDim = InputMethods();
// Instantiate a MineGrid passing the first and second dimensions you just assigned and assign it to userGrid
userGrid = new HW3_MineGrid(i_rowDim, i_columnDim);
// Instantiate a MineGrid passing the same dimensions and assign it to gameGrid
gameGrid = new HW3_MineGrid(i_rowDim, i_columnDim);
// Calculate the average of the first dimension and second dimension, assign to a main int variable for number of spots.
i_numSpots = (i_rowDim + i_columnDim) / 2;

boolean b_IsGameContinue = true; // determine if user want to repeat
while(b_IsGameContinue)
{
// Call the instance method on the gameGrid to assign to numSpots elements "randomly"
gameGrid.setRandomValues(i_numSpots);
// Call a static method similar to setInputValues in #2, but instead of passing a 2-dim. array, pass the userGrid MineGrid object (details in A. below)
setInputValues(userGrid, i_numSpots);
// Call the instance method to compare MineGrids, calling it on the gameGrid, passing the userGrid, assigning the return value to resultsGrid
resultsGrid = userGrid.compareGrids(gameGrid);
// Display result
System.out.println("Your Guesses:");
userGrid.displayGrid('G');
System.out.println("Game Mines:");
gameGrid.displayGrid('*');
System.out.println("Your Matches:");
resultsGrid.displayGrid('M');
b_IsGameContinue = isGameContinue(); // Ask if user wants to repeat

}
}
public static void setInputValues(HW3_MineGrid tempUserGuess, int numSpots)
{
tempUserGuess.initGrid(); // Method 4: reset all values to false
for (int i = 0; i < numSpots; i++)
{
System.out.println("For mine " + i);
int i_rowIndex = readRowIndex(tempUserGuess.getRowDimension());
int i_colIndex = readColumnIndex(tempUserGuess.getColumnDimension());
if (tempUserGuess.GetSpotInGrid(i_rowIndex, i_colIndex) == true) // Method 6
{
i--; // loop one more time if value is already true
continue; // not necessary
}
else
{
if(!tempUserGuess.SetSpotInGrid(i_rowIndex, i_colIndex)) // Method 5
{
i--; // loop one more time if set function return false
continue; // not necessary

}
}
}
}
public static int readRowIndex(int rowDim)
{
int rowIndex = -1;
while(rowIndex < 0 || rowIndex >= rowDim) // check if the row index is >=0 and < parameter (for the row dimension)
{
System.out.print("\tEnter row index:");
rowIndex = oScanner.nextInt();
}
return rowIndex;
}
public static int readColumnIndex(int columnDim)
{
int columnIndex = -1;
while(columnIndex < 0 || columnIndex >= columnDim) // check if the row index is >=0 and < parameter (for the row dimension)
{
System.out.print("\tEnter column index:");
columnIndex = oScanner.nextInt();
}
return columnIndex;
}

// Get row and column dimension from user within range 5-20 --> (Copied this method from HW2)
public static int InputMethods()
{
int i = 0;
while (i < HW3_MineGrid.MIN_DIMENSION || i > HW3_MineGrid.MAX_DIMENSION)
{
System.out.print("Enter a dimension of the grid (5-20): ");
i = oScanner.nextInt();
}
return i;
}
// Ask if user wants to repeat  --> Copied this method from HW2
public static boolean isGameContinue()
{
boolean isGameContinue;
System.out.print("Play a new game? (y for yes): ");
String s_UserInput = oScanner.next();
if(s_UserInput != null && ! s_UserInput.isEmpty())
{
System.out.println(s_UserInput.substring(0, 0).toUpperCase());
isGameContinue = (s_UserInput.toUpperCase().charAt(0) == 'Y')? true : false;
}
else
isGameContinue = false;
return isGameContinue;
}
}

/*
// Output:
Enter a dimension of the grid (5-20): 4
Enter a dimension of the grid (5-20): 30
Enter a dimension of the grid (5-20): 5
Enter a dimension of the grid (5-20): 6
For mine 0
Enter row index:2
Enter column index:3
For mine 1
Enter row index:0
Enter column index:0
For mine 2
Enter row index:-3
Enter row index:100
Enter row index:2
Enter column index:1
For mine 3
Enter row index:4
Enter column index:3
For mine 4
Enter row index:2
Enter column index:4
Your Guesses:
|---|---|---|---|---|---|
|-G-|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|-G-|- -|-G-|-G-|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|-G-|- -|- -|
|---|---|---|---|---|---|
Game Mines:
|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|-*-|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|-*-|- -|- -|
|-*-|- -|- -|-*-|-*-|- -|
|---|---|---|---|---|---|
Your Matches:
|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|-M-|- -|- -|
|---|---|---|---|---|---|
Play a new game? (y for yes): y

For mine 0
Enter row index:1
Enter column index:1
For mine 1
Enter row index:1
Enter column index:1
For mine 1
Enter row index:2
Enter column index:2
For mine 2
Enter row index:4
Enter column index:5
For mine 3
Enter row index:3
Enter column index:4
For mine 4
Enter row index:3
Enter column index:4
For mine 4
Enter row index:1
Enter column index:5
Your Guesses:
|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|
|- -|-G-|- -|- -|- -|-G-|
|- -|- -|-G-|- -|- -|- -|
|- -|- -|- -|- -|-G-|- -|
|- -|- -|- -|- -|- -|-G-|
|---|---|---|---|---|---|
Game Mines:
|---|---|---|---|---|---|
|- -|- -|-*-|- -|-*-|-*-|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|-*-|- -|-*-|- -|- -|
|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|
Your Matches:
|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|
Play a new game? (y for yes): yes

For mine 0
Enter row index:3
Enter column index:4
For mine 1
Enter row index:2
Enter column index:3
For mine 2
Enter row index:5
Enter row index:4
Enter column index:3
For mine 3
Enter row index:1
Enter column index:2
For mine 4
Enter row index:4
Enter column index:2
Your Guesses:
|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|
|- -|- -|-G-|- -|- -|- -|
|- -|- -|- -|-G-|- -|- -|
|- -|- -|- -|- -|-G-|- -|
|- -|- -|-G-|-G-|- -|- -|
|---|---|---|---|---|---|
Game Mines:
|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|-*-|
|- -|- -|- -|-*-|-*-|- -|
|- -|- -|- -|-*-|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|-*-|- -|- -|- -|
|---|---|---|---|---|---|
Your Matches:
|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|-M-|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|-M-|- -|- -|- -|
|---|---|---|---|---|---|
Play a new game? (y for yes): no
*/

/*
// Program Output 2:
Enter a dimension of the grid (5-20): 7
Enter a dimension of the grid (5-20): 8
For mine 0
Enter row index:1
Enter column index:2
For mine 1
Enter row index:3
Enter column index:4
For mine 2
Enter row index:5
Enter column index:6
For mine 3
Enter row index:4
Enter column index:2
For mine 4
Enter row index:7
Enter row index:6
Enter column index:0
For mine 5
Enter row index:0
Enter column index:0
For mine 6
Enter row index:0
Enter column index:0
For mine 6
Enter row index:10
Enter row index:-10
Enter row index:
6
Enter column index:6
Your Guesses:
|---|---|---|---|---|---|---|---|
|-G-|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|-G-|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|-G-|- -|- -|- -|
|- -|- -|-G-|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|-G-|- -|
|-G-|- -|- -|- -|- -|- -|-G-|- -|
|---|---|---|---|---|---|---|---|
Game Mines:
|---|---|---|---|---|---|---|---|
|- -|- -|- -|- -|-*-|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|-*-|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|-*-|- -|- -|- -|- -|- -|-*-|- -|
|- -|- -|- -|- -|- -|-*-|- -|-*-|
|- -|-*-|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|---|---|
Your Matches:
|---|---|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|---|---|
Play a new game? (y for yes): YES

For mine 0
Enter row index:6
Enter column index:6
For mine 1
Enter row index:5
Enter column index:5
For mine 2
Enter row index:4
4 Enter column index:
For mine 3
Enter row index:3
Enter column index:3
For mine 4
Enter row index:2
Enter column index:2
For mine 5
Enter row index:1
Enter column index:1
For mine 6
Enter row index:4
Enter column index:3
Your Guesses:
|---|---|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|-G-|- -|- -|- -|- -|- -|- -|
|- -|- -|-G-|- -|- -|- -|- -|- -|
|- -|- -|- -|-G-|- -|- -|- -|- -|
|- -|- -|- -|-G-|-G-|- -|- -|- -|
|- -|- -|- -|- -|- -|-G-|- -|- -|
|- -|- -|- -|- -|- -|- -|-G-|- -|
|---|---|---|---|---|---|---|---|
Game Mines:
|---|---|---|---|---|---|---|---|
|- -|-*-|-*-|-*-|- -|- -|- -|- -|
|- -|- -|- -|-*-|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|-*-|
|-*-|- -|- -|- -|- -|- -|-*-|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|---|---|
Your Matches:
|---|---|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|---|---|
Play a new game? (y for yes): y

For mine 0
Enter row index:2
Enter column index:3
For mine 1
Enter row index:4
Enter column index:3
For mine 2
Enter row index:2
Enter column index:1
For mine 3
Enter row index:2
Enter column index:3
For mine 3
Enter row index:7
Enter row index:8
Enter row index:6
Enter column index:7
For mine 4
Enter row index:4
Enter column index:3
For mine 4
Enter row index:5
Enter column index:3
For mine 5
Enter row index:2
Enter column index:4
For mine 6
Enter row index:1
Enter column index:1
Your Guesses:
|---|---|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|-G-|- -|- -|- -|- -|- -|- -|
|- -|-G-|- -|-G-|-G-|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|-G-|- -|- -|- -|- -|
|- -|- -|- -|-G-|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|-G-|
|---|---|---|---|---|---|---|---|
Game Mines:
|---|---|---|---|---|---|---|---|
|- -|- -|- -|- -|-*-|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|-*-|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|-*-|- -|
|- -|- -|- -|- -|-*-|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|-*-|- -|-*-|- -|- -|-*-|- -|- -|
|---|---|---|---|---|---|---|---|
Your Matches:
|---|---|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|-M-|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|---|---|
Play a new game? (y for yes): y

For mine 0
Enter row index:3
Enter column index:4
For mine 1
Enter row index:2
Enter column index:2
For mine 2
Enter row index:5
Enter column index:4
For mine 3
Enter row index:2
Enter column index:5
For mine 4
Enter row index:2
Enter column index:6
For mine 5
Enter row index:7
Enter row index:3
Enter column index:2
For mine 6
Enter row index:3
Enter column index:4
For mine 6
Enter row index:5
Enter column index:2
Your Guesses:
|---|---|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|-G-|- -|- -|-G-|-G-|- -|
|- -|- -|-G-|- -|-G-|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|-G-|- -|-G-|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|---|---|
Game Mines:
|---|---|---|---|---|---|---|---|
|- -|- -|-*-|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|-*-|- -|- -|
|- -|-*-|- -|- -|- -|-*-|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|-*-|- -|
|- -|- -|-*-|-*-|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|---|---|
Your Matches:
|---|---|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|-M-|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|-M-|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|---|---|
Play a new game? (y for yes): n

*/


Comments