Minesweeper
Option #1 - Static Methods and Array Processing


Introduction
The game of Minesweeper requires a player to determine the location of “mines” hidden randomly throughout a two dimensional grid i.e. “minefield”.  Each of the grid locations is initially covered by a tile.  The player may open a tile, flag a tile as a mine location, or set a tile as a question mark.  Clues describing the number of adjacent mines to a tile are displayed when the player opens a tile.  A player “wins” by opening all of the non-mine tiles (see Figure 1.). A player loses when they open a tile containing a mine (see Figure 2.)


Figure 1. Winning Game
winning game

Figure 2. Losing Game
losing game

Mines Array
A two-dimensional array can be used to represent the mines and clue values for a minesweeper game. Integer values are used to represent mines and clue values.

Value

Represents

0-8

Clue Value

9

Mine

Figure 3. shows a sample mines array for Figure 1. The array has nine mines (shown in red). Clue values are included for all mines.

Figure 3. Mines Array for Figure 1.

 

0

1

2

3

4

5

6

7

8

0

0

0

0

0

0

0

1

2

9

1

0

0

0

0

0

0

1

9

3

2

1

1

0

1

1

1

1

2

9

3

9

1

1

1

9

1

0

1

1

4

2

9

1

1

2

9

1

0

0

5

2

2

1

1

1

1

1

0

0

6

1

9

1

0

0

0

0

0

0

7

1

2

2

0

0

0

0

0

0

8

0

1

9

0

0

0

0

0

0

 



Assignment

Your assignment is to code four static methods that can be used to partially implement the minesweeper game.  The requirements for each method are described below.  The methods must be stored in a class file called minesweeperLibrary.java.  You may add supporting methods.

 

A shell program minesweeperLibrary.java is provided.  A sample test program minesweeperLibraryTest.java is also provided.

 

 


showMines method

The showMines method accepts a two dimensional array of integers as input.  The method displays the contents of the array to the console in table format.

 

Method call

minesweeperLibrary.showMines( mines ); //display the mines array

 

Sample Output (for 5 by 8 array)

00000000

00000000

00000000

00000000

00000000

 
 


placeMines method

The placeMines method accepts a two dimensional array as input.  The method places mines randomly in the array.  A mine is the integer value 9.  The number of mines must be equal to 1 + (number of columns * number rows) / 10.  Your code must ensure that at least one mine is placed in the array.

 

Bonus if your code ensures two mines can not be placed in the same location

 

Method call

minesweeperLibrary.placeMines( mines ); //place mines in array
minesweeperLibrary.showMines( mines ); //display the mines array

 

Sample Output (for 5 by 8 array)

00009000

99000000

00000000

00000900

09000000

 

 

 


calculateClues method

The calculateClues method accepts a two dimensional array as input.  The method calculates and places the clue values that surround each mine.

 

Method call

minesweeperLibrary.calculateClues( mines ); //place mines in array
minesweeperLibrary.showMines( mines ); //display the mines array

 

Sample Output (for 5 by 8 array)

22119100

99111100

22101110

11101910

19101110

 

 


showStats method

The showStats method accepts a two dimensional array as input.  The method calculates the frequency for each tile value 0 to 9.  The frequency of each tile value is displayed to the console as shown in the sample output.

 

Method call

minesweeperLibrary.calculateClues( mines ); //place mines in array
minesweeperLibrary.showMines( mines ); //display the mines array

 

Sample Output (for 5 by 8 array)

0 tiles = 10, 1 tiles = 21, 2 tiles = 4, 3 tiles = 0, 4 tiles = 0, 5 tiles = 0,

6 tiles = 0, 7 tiles = 0, 8 tiles = 0, 9 tiles = 5

 



Sample Test Program (minesweeperLibraryTest.java)

/*
 * minesweeperLibraryTest.java
 * 
 */
import javax.swing.*;
public class minesweeperLibraryTest {   
    public static void main(String[] args) {      
      //get the number of rows and columns from user
      int maxRow;
      int maxCol;
      String tempString;            
      tempString = JOptionPane.showInputDialog("Enter number of rows: ");
      maxRow = Integer.parseInt( tempString );     
      tempString = JOptionPane.showInputDialog("Enter number of cols: ");
      maxCol = Integer.parseInt( tempString );
      //create an array called mines 
      int[][] mines = new int[maxRow][maxCol];
      //display the mines array
      minesweeperLibrary.showMines( mines );      
      //place mines in the mines array
      minesweeperLibrary.placeMines( mines );
      minesweeperLibrary.showMines( mines ); //display the mines array
      //calculate and store the clue values in the mines array
      minesweeperLibrary.calculateClues( mines );    
      minesweeperLibrary.showMines( mines ); //display the mines array
      //calculate and display the stats for the mines array
      minesweeperLibrary.showStats( mines );                
      System.exit(0);
   }//main             
}//mindsweeperLibraryTest
 


Shell program (minesweeperLibrary.java)

 

/*
 * minesweeperLibrary.java
 *
 */ 
import javax.swing.*;
public class minesweeperLibrary {   
    //displays the array passed in table format
    public static void showMines(int[][] temp)
    {
      //add your code here
    }//showMines
    //place random bombs in mines array
    public static void placeMines(int[][] temp)
    {
	//add your code here 
    }//placeMines   
    //calculate clue values for mines array
    public static void calculateClues(int temp[][])
    { 
      //add your code here  
    }//calculateClues
    //calculate and display statistics for all values in mines array
    public static void showStats(int[][] temp)
    {
      //add your code here 
    }//showStats
    //you may want to add supporting private methods
 }//mindsweeperLibrary

 



Turn-In

Turn in your modified minesweeperLibrary.java code via e-mail and turn in a printed program listing.  Note:  Make sure the case (ie. upper/lower) for each of your files and methods matches the assignment specifications.