Programming Languages‎ > ‎Java‎ > ‎

Java Programming Assignment 2

Requirement

Problem: Write a Java application program in which you play a simplified version of minesweeper. Declare IN MAIN (NOT at class-scope) 3 two-dimensional arrays of booleans, one for the computer's game grid, one for the user to guess and one for the results (matches), an int for the row dimension, and an int for the column dimension. Note that ALL the methods are static!

Main must call the following methods (logic for main is below):
  • Call a method (in a separate class, see 0. below) to prompt and read from the user the row dimension of the grid, assigning the return value to a main int variable. Call it again to get the column dimension, assigning the return value to another main int variable.
  • Call a method (I'm calling it allocateGrid, see 1. below) 3 times, to allocate memory for a 2-dimensional array of boolean (rowDimension X columnDimension) and return it in a return statement. Assign to each of the 3 2-dim. array variables (declared in main, described above).
  • Calculate the average of the rowDim and columnDim, assign to a main int variable for number of spots.
  • **Call a method (I'm calling it setRandomValues, see 2. below) that will assign numSpots values to true in random locations in the 2-dim. array for the game grid (parameter)
    • Call a method (in a separate class, I'm calling it setInputValues, see 3. below) that will assign numSpots values to true in the 2-dim. array for the user guesses (parameter) based on user input.
    • Call a method (I'm calling it compareGrids, see 4. below) that will assign corresponding elements to true in the 2-dim. array for the results (parameter) for each corresponding true element in the game grid (another parameter) that is true in the user guesses 2-dim. array (parameter)
    • Print "Your guesses", then call a method (I'm calling it displayGrid, see 5. below) for the user guesses 2-dim. array, and also pass 'G' for where the user placed a guess (elements are true).
    • Print "Game Mines", then call the displayGrid method for the game grid, and also pass '*' (asterisk) for where the randomly placed "mines" were placed (elements are true)
    • Print "Your matches", then call the displayGrid method for the results grid, and also pass 'M' for where the spots are true.
    • Using another method in the separate class (see 6. below), prompt the user and read (into a String) if the user wants to play another. If the method returns true, repeat to ** .

More on the above-mentioned methods:

0. Put the method that reads and returns one of the dimensions of the Grid in a separate class from main's class (I'm calling it InputMethods). After prompting and reading a dimension, if the input dimension is < 5 or > 20, loop to prompt and read again. If the input dimension is in range, return int.

1. In the allocateGrid method (with 2 int parameters):
    • check if each dimension (parameter) is > 0, if not change the parameter to 1 (check one at a time)
    • return (in a return statement) a rowDim X columnDim, 2-dim. array of boolean(continued)
2. In the setRandomValues method (with a parameter for a 2-dim. array and the number of spots to set), do the following:
    • Call another method (I'm calling initGrid) that will set all the elements of the parameter 2-dim. array to false. This method must have ONLY one parameter (2-dim. array)!
    • In a loop, use the random() method in the Math class in a calculation to get an index for the "row" (1st dim.) between 0 and row length-1 (inclusive) -- (see last page). Also use the random() method again for the "column" (2nd dim.) index (between 0 and column length-1, inclusive). Assign true (means there's a mine there) to the element at the row & column ONLY if it isn't true already. Assign to numSpots elements (no less).
3. In the setInputValues method (with a parameter for a 2-dim. array and the number of spots to set), do the following:
    • call initGrid for the 2-dim. array parameter
    • In a loop, call a method in the InputMethods class to get a row index from the user, then call another InputMethods method to get a column index (see 7. and 8. below). Don't assign true (meaning the user is guessing there's a mine there) to the element if the element at the row & column is already true. Assign to exactly numSpots elements (no less).
4. In the compareGrids method, compare each of the corresponding elements in the game grid (parameter) to the user guesses (parameter). If the corresponding elements are both true, assign true to the corresponding element in the results (parameter). Otherwise, assign false. 

5. In the method that displays a 2-dim. array (parameter), display a space where the element is false, and a char (parameter) where the element is true.

6. In the method that determines if the user wants to repeat (put in the InputMethods class), prompt the user asking is he/she wants to continue and read into a String. If the beginning char of the answer string is 'y' or 'Y', then return true. Otherwise (if the user enters anything else), return false.

7. In the InputMethods method that reads a row index, after prompting the user for the row index, check if the row index is >=0 and < parameter (for the row dimension). If not, prompt and read again (in a loop) until the row index is in range.

8. In the InputMethods method that reads a column index (could be the same method as the input row index method), after prompting the user for the column index, check if the column index is >=0 and < parameter (for the column dimension). If not, prompt and read again (in a loop) until the column index is in range.

  • USE final static VARIABLES FOR 5 AND 20 IN YOUR PROGRAM. DO NOT REALLOCATE MEMORY FOR EACH 2-DIM. ARRAY IN THE PROGRAM (ONLY ALLOCATE ONCE FOR EACH 2-DIM. ARRAY)! DO NOT USE ANY CLASS-SCOPE VARIABLES unless they're static AND final! ALL METHODS ARE static. ALWAYS check the array bounds correctly (both dimensions)!

Input to use for turning in:
  • play at least 3 games in one run (but allow for more)
  • enter out of range dimension (< 5 and > 20)
  • enter out of range (too large and also < 0) row or column index, more than
  • twice for one index
  • enter a row and column already set (try 2 times)
  • match at least 2 mines, AND CHECK YOUR OUTPUT (did it match correctly?)

HOW TO USE get pseudo-randomly generated integers between MIN and MAX

(inclusive): (int)(Math.random() * (MAX-MIN+1) ) + MIN




Solution


/*   This file contain static method used in Minesweeper game (HW2_Main.java)    */

import java.util.Scanner;
public class HW2_Methods 
{
static Scanner oScanner = new Scanner(System.in);
// Constants
public static final int MIN_DIMENSION = 5;
public static final int MAX_DIMENSION = 20;
// Get row and column dimension from user within range 5-20
public static int InputMethods()
{
int i = 0;
while (i < MIN_DIMENSION || i > MAX_DIMENSION)
{
System.out.print("Enter a dimension of the grid (5-20): ");
i = oScanner.nextInt();
}
return i;
}
public static boolean [][] allocateGrid(int rowDim, int colDim)
{
rowDim = (rowDim > 0)? rowDim : 1;
colDim = (colDim > 0)? colDim : 1;
return new boolean[rowDim][colDim];
}
public static void setRandomValues(boolean [][] tempComputerGame, int numSpots)
{
initGrid(tempComputerGame);
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() * ((tempComputerGame.length - 1) - 0 + 1) ) + 0;
int i_colIndex = (int)(Math.random() * ((tempComputerGame[0].length - 1) - 0 + 1) ) + 0;
if (tempComputerGame[i_rowIndex][i_colIndex] == true)
{
i--; // loop one more time if value is already true
continue; // not necessary
}
else
{
tempComputerGame[i_rowIndex][i_colIndex] = true;
}
}
}
public static void setInputValues(boolean [][] tempUserGuess, int numSpots)
{
initGrid(tempUserGuess);
for (int i = 0; i < numSpots; i++)
{
System.out.println("For mine " + i);
int i_rowIndex = readRowIndex(tempUserGuess.length);
int i_colIndex = readColumnIndex(tempUserGuess[0].length);
if (tempUserGuess[i_rowIndex][i_colIndex] == true)
{
i--; // loop one more time if value is already true
continue; // not necessary
}
else
{
tempUserGuess[i_rowIndex][i_colIndex] = true;
}
}
}
public static void compareGrids(boolean [][] tempComputerGame, boolean [][] tempUserGuess, boolean[][] tempResultMatching)
{
// To Do: Check array is not null, all arrays have same number of rows and columns -> array is static and can be used from any class
for(int i = 0; i < tempResultMatching.length; i++)
{
for(int j = 0; j < tempResultMatching[0].length; j++)
{
tempResultMatching[i][j] = (tempComputerGame[i][j] == true && tempUserGuess[i][j] == true) ? true : false;
}
}
}
public static void displayGrid(boolean [][] displayArray, char c_arrayType)
{
if(displayArray.length == 0 || displayArray[0].length == 0) // To Do: throw an exception
return;
char printChar;
System.out.println("|" + String.format(String.format("%%0%dd", displayArray[0].length), 0).replace("0","---|"));
for(boolean [] displayRow: displayArray)
{
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", displayArray[0].length), 0).replace("0","---|"));
}
public static void initGrid(boolean [][] resetArray)
{
for (int i = 0; i < resetArray.length; i++)
{
for (int j = 0; j < resetArray[i].length; j++)
{
resetArray[i][j] = false;
}
}
}
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;
}
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;
}
}


/*
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): Write a Java application program in which you play a simplified version of minesweeper
                            - Get dimension x-y from user withing range
                            - Get user input for the mines and compare it with randomly generate pattern
                            - Display user input, random pattern and Match/No Match result
Variable names (if any) and descriptions of what they represent: numSpots->average of the rowDim and columnDim
*/
public class HW2_Main 
{
public static void main(String[] args) 
{
// Variable declaration
boolean [][] computerGame;
boolean [][] userGuess;
boolean [][] resultMatching;
int i_rowDim, i_columnDim;
i_rowDim = i_columnDim = 0; // Set default value
int i_numSpots; // average of the rowDim and columnDim
// Read row and column dimension of the grid from user
i_rowDim = HW2_Methods.InputMethods();
i_columnDim = HW2_Methods.InputMethods();
// allocate memory for create three arrays of 2-dimensional array of type boolean using received user's input for row and column dimension
computerGame = HW2_Methods.allocateGrid(i_rowDim, i_columnDim);
userGuess = HW2_Methods.allocateGrid(i_rowDim, i_columnDim);
resultMatching = HW2_Methods.allocateGrid(i_rowDim, i_columnDim);
// Calculate the average of the rowDim and columnDim
i_numSpots = (i_rowDim + i_columnDim) / 2;
boolean b_IsGameContinue = true; // determine if user want to repeat
while(b_IsGameContinue)
{
// Randomly generate mines
HW2_Methods.setRandomValues(computerGame, i_numSpots);
// User's Guesses
System.out.println("\nTry to guess where the " + i_numSpots + " mines have been placed.");
HW2_Methods.setInputValues(userGuess, i_numSpots);
// Match previous two grids
HW2_Methods.compareGrids(computerGame, userGuess, resultMatching);
// Display result
System.out.println("Your Guesses:");
HW2_Methods.displayGrid(userGuess, 'G');
System.out.println("Game Mines:");
HW2_Methods.displayGrid(computerGame, '*');
System.out.println("Your Matches:");
HW2_Methods.displayGrid(resultMatching, 'M');
b_IsGameContinue = HW2_Methods.isGameContinue(); // Ask if user wants to repeat
}
}
}

/*--Test Run 1 (Played 3 games with suggested input given in hw2 requirement) --
Enter a dimension of the grid (5-20): 5
Enter a dimension of the grid (5-20): 22
Enter a dimension of the grid (5-20): 4
Enter a dimension of the grid (5-20): 8

Try to guess where the 6 mines have been placed.
For mine 0
Enter row index:1
Enter column index:1
For mine 1
Enter row index:-1
Enter row index:40
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:3
Enter column index:3
For mine 3
Enter row index:4
Enter column index:4
For mine 4
Enter row index:5
Enter row index:4
Enter column index:3
For mine 5
Enter row index:0
Enter column index:4
Your Guesses:
|---|---|---|---|---|---|---|---|
|- -|- -|- -|- -|-G-|- -|- -|- -|
|- -|-G-|- -|- -|- -|- -|- -|- -|
|- -|- -|-G-|- -|- -|- -|- -|- -|
|- -|- -|- -|-G-|- -|- -|- -|- -|
|- -|- -|- -|-G-|-G-|- -|- -|- -|
|---|---|---|---|---|---|---|---|
Game Mines:
|---|---|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|- -|-*-|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|-*-|- -|- -|-*-|- -|
|- -|- -|- -|- -|- -|- -|-*-|- -|
|- -|-*-|- -|- -|- -|- -|- -|-*-|
|---|---|---|---|---|---|---|---|
Your Matches:
|---|---|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|---|---|
Play a new game? (y for yes): y


Try to guess where the 6 mines have been placed.
For mine 0
Enter row index:1
Enter column index:1
For mine 1
Enter row index:0
Enter column index:1
For mine 2
Enter row index:0
Enter column index:2
For mine 3
Enter row index:2
Enter column index:2
For mine 4
Enter row index:3
Enter column index:3
For mine 5
Enter row index:4
Enter column index:2
Your Guesses:
|---|---|---|---|---|---|---|---|
|- -|-G-|-G-|- -|- -|- -|- -|- -|
|- -|-G-|- -|- -|- -|- -|- -|- -|
|- -|- -|-G-|- -|- -|- -|- -|- -|
|- -|- -|- -|-G-|- -|- -|- -|- -|
|- -|- -|-G-|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|---|---|
Game Mines:
|---|---|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|-*-|- -|- -|- -|- -|- -|- -|
|-*-|- -|-*-|- -|- -|- -|- -|- -|
|- -|- -|- -|-*-|- -|- -|- -|- -|
|- -|- -|-*-|-*-|- -|- -|- -|- -|
|---|---|---|---|---|---|---|---|
Your Matches:
|---|---|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|-M-|- -|- -|- -|- -|- -|- -|
|- -|- -|-M-|- -|- -|- -|- -|- -|
|- -|- -|- -|-M-|- -|- -|- -|- -|
|- -|- -|-M-|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|---|---|
Play a new game? (y for yes): y


Try to guess where the 6 mines have been placed.
For mine 0
Enter row index:7
Enter row index:6
Enter row index:5
Enter row index:4
Enter column index:7
For mine 1
Enter row index:3
Enter column index:7
For mine 2
Enter row index:3
Enter column index:7
For mine 2
Enter row index:2
Enter column index:7
For mine 3
Enter row index:1
Enter column index:8
Enter column index:7
For mine 4
Enter row index:0
Enter column index:7
For mine 5
Enter row index:4
Enter column index:5
Your Guesses:
|---|---|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|- -|-G-|
|- -|- -|- -|- -|- -|- -|- -|-G-|
|- -|- -|- -|- -|- -|- -|- -|-G-|
|- -|- -|- -|- -|- -|- -|- -|-G-|
|- -|- -|- -|- -|- -|-G-|- -|-G-|
|---|---|---|---|---|---|---|---|
Game Mines:
|---|---|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|-*-|- -|
|-*-|- -|- -|- -|-*-|- -|- -|- -|
|- -|- -|- -|- -|- -|-*-|- -|- -|
|- -|- -|- -|-*-|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|-*-|- -|
|---|---|---|---|---|---|---|---|
Your Matches:
|---|---|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|---|---|
Play a new game? (y for yes): no

*/

/* -- Test Run 2 (Missed one - entered same row/column index where mine is placed before )
Enter a dimension of the grid (5-20): 9
Enter a dimension of the grid (5-20): -2
Enter a dimension of the grid (5-20): 22
Enter a dimension of the grid (5-20): 6

Try to guess where the 7 mines have been placed.
For mine 0
Enter row index:0
Enter column index:0
For mine 1
Enter row index:0
Enter column index:0
For mine 1
Enter row index:1
Enter column index:2
For mine 2
Enter row index:2
Enter column index:1
For mine 3
Enter row index:5
Enter column index:5
For mine 4
Enter row index:1
Enter column index:2
For mine 4
Enter row index:5
Enter column index:5
For mine 4
Enter row index:5
Enter column index:4
For mine 5
Enter row index:3
Enter column index:1
For mine 6
Enter row index:2
Enter column index:4
Your Guesses:
|---|---|---|---|---|---|
|-G-|- -|- -|- -|- -|- -|
|- -|- -|-G-|- -|- -|- -|
|- -|-G-|- -|- -|-G-|- -|
|- -|-G-|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|-G-|-G-|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|
Game Mines:
|---|---|---|---|---|---|
|- -|- -|- -|-*-|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|-*-|- -|- -|- -|
|-*-|- -|- -|- -|- -|-*-|
|-*-|- -|- -|- -|- -|-*-|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|-*-|- -|- -|
|---|---|---|---|---|---|
Your Matches:
|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|-M-|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|
Play a new game? (y for yes): yes


Try to guess where the 7 mines have been placed.
For mine 0
Enter row index:-4
Enter row index:100
Enter row index:5
Enter column index:5
For mine 1
Enter row index:4
Enter column index:4
For mine 2
Enter row index:3
Enter column index:3
For mine 3
Enter row index:2
Enter column index:2
For mine 4
Enter row index:1
Enter column index:1
For mine 5
Enter row index:0
Enter column index:0
For mine 6
Enter row index:3
Enter column index:4
Your Guesses:
|---|---|---|---|---|---|
|-G-|- -|- -|- -|- -|- -|
|- -|-G-|- -|- -|- -|- -|
|- -|- -|-G-|- -|- -|- -|
|- -|- -|- -|-G-|-G-|- -|
|- -|- -|- -|- -|-G-|- -|
|- -|- -|- -|- -|- -|-G-|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|
Game Mines:
|---|---|---|---|---|---|
|- -|-*-|- -|- -|- -|-*-|
|-*-|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|-*-|- -|- -|- -|
|- -|- -|-*-|- -|-*-|- -|
|- -|- -|- -|- -|- -|-*-|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|
Your Matches:
|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|-M-|- -|
|- -|- -|- -|- -|- -|-M-|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|
Play a new game? (y for yes): YES


Try to guess where the 7 mines have been placed.
For mine 0
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:3
Enter column index:3
For mine 3
Enter row index:4
Enter column index:4
For mine 4
Enter row index:5
Enter column index:5
For mine 5
Enter row index:6
Enter column index:6
Enter column index:5
For mine 6
Enter row index:4
Enter column index:3
Your Guesses:
|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|
|- -|-G-|- -|- -|- -|- -|
|- -|- -|-G-|- -|- -|- -|
|- -|- -|- -|-G-|- -|- -|
|- -|- -|- -|-G-|-G-|- -|
|- -|- -|- -|- -|- -|-G-|
|- -|- -|- -|- -|- -|-G-|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|
Game Mines:
|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|
|- -|-*-|-*-|- -|-*-|- -|
|- -|- -|- -|-*-|- -|- -|
|- -|- -|-*-|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|-*-|
|- -|- -|- -|- -|-*-|- -|
|---|---|---|---|---|---|
Your Matches:
|---|---|---|---|---|---|
|- -|- -|- -|- -|- -|- -|
|- -|-M-|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|- -|- -|- -|- -|- -|- -|
|---|---|---|---|---|---|
Play a new game? (y for yes): NO
*/



Comments