Minesweeper (Due 17 August 2014)

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 he opens a tile containing a mine (see Figure 2)

If you are unfamiliar with the game here are some references that will get you started:

This assignment was created by Jeff Lehman in the Mathematics and Computer Science Department at Huntington College and posted on the Nifty Assignments website. It has been modified for use for CS 312.

Figure 1: Winning Game           Figure 2: Losing Game
winning 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 2 1 1 9 2 1 1 1
4 2 9 1 1 2 9 1 0 0
5 2 2 2 0 1 1 1 0 0
6 1 9 1 0 0 0 0 0 0
7 1 2 2 1 0 0 0 0 0
8 0 1 9 1 0 0 0 0 0

Tiles Array
A second two-dimensional array can be used to represent the status of the tiles for a minesweeper game. Integer values are used to represent a tile that is opened, closed, flagged as a mine, or set as question mark.

Value Represents
0 Open tile
1 Closed tile
2 Question mark
3 Mine

Figure 4 shows a sample tiles array for Figure 3. All tiles in the first row (row 0) have been set to open (value 0). All tiles in the second row (row 1) have been set as a question mark (value 2). All tiles in the third row (row 2) have been flagged as mines (value 3). All tiles in the remaining rows (rows 3 to 8) have been set to closed (value 1).

Figure 4: Tiles Array for Figure 1

  0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 2 2 2 2 2 2 2 2 2
2 3 3 3 3 3 3 3 3 3
3 1 1 1 1 1 1 1 1 1
4 1 1 1 1 1 1 1 1 1
5 1 1 1 1 1 1 1 1 1
6 1 1 1 1 1 1 1 1 1
7 1 1 1 1 1 1 1 1 1
8 1 1 1 1 1 1 1 1 1

Board
The mines and tiles arrays are used to determine what the player will see as they play the minesweeper game. The characters 'X', ' ', '?', 'F', and '*' can be used to create a rough text representation of minesweeper game board. When the game is "won" all mines should be seen as 'F'. When the game is lost the mine that loses the game is shown as '!', the remaining mines are shown as '*', and any mines that were incorrectly flagged will be displayed as '-'.

Value Represents
'X' Closed tile
' ' Open tile, zero adjacent mines
'1' ... '8' Open tile, number indicates number of adjacent mines
'?' Closed tile, set as question mark
'F' Closed tile, flagged as mine
'*' Mine (after game lost)
'!' Mine that loses game (after game lost)
'-' Flagged mine that was incorrect (after game lost)
'F' All mines (after game won)

Figure 5 shows the values the player would see for the mines and tiles arrays from Figure 3 and Figure 4 if the game was lost. At this point the status of the game is "lose". The mine that lost the game was opened at position (0, 8).

Figure 5: Board values for Figure 1

  0 1 2 3 4 5 6 7 8
0             1 2 !
1 ? ? ? ? ? ? ? * ?
2 - - - - - - - - *
3 * X X X * X X X X
4 X * X X X * X X X
5 X X X X X X X X X
6 X * X X X X X X X
7 X X X X X X X X X
8 X X X X X X X X X

The following table summarizes the board values for the tiles array, mines array, and current game status.

Tiles Value Mines Value Game Status Board Character
1 NA play 'X'
2 NA play '?'
3 NA play 'F'
0 0 play ' '
0 1 ... 8 play '1' ... '8'
0 9 lose '!'
1, 2, 3 9 lose '*'
3 0 ... 8 lose '-'
NA 9 win 'F'




Assignment Statement

For this assignment you will implement a minesweeper class to support a complete working version of a Minesweeper game. While you are not creating a GUI interface in this assignment, you will be creating attributes and methods that will support a GUI interface. You may add additional attributes or methods if necessary.

You must use recursion for the method markTile() to open all blanks and clues when a tile is opened that covers a blank.

A sample test program TestMineSweeper.java is provided. Modify the test program to demonstrate each of the methods in your minesweeper class.

For this assignment you may work with a partner. Both of you must read the paper on Pair Programming before you get started. Your programs must have a header of the following form:

/*
  File: TestMineSweeper.java

  Description:

  Student Name:

  Student UT EID:

  Partner's Name:

  Partner's UT EID:

  Course Name: CS 312

  Unique Numbers: 

  Date Created:

  Date Last Modified:

*/

You will follow the standard Java Coding Conventions. You can either view the HTML page or download the PDF or Postscript and print it out. There is a modification that I would like to make to the standard coding conventions. Please align the opening and closing braces vertically so that you can easily make out the blocks of code. For example:

Do this:
if ( x > 5 )
{
  a = b + c;
}

Not this:
if ( x > 5 ) {
  a = b + c;
}

Use the turnin program to submit your TestMineSweeper.java file. We should receive your work by 11 PM on Sunday, 17 August 2014. There will be substantial penalties if you do not adhere to the guidelines. There are no extensions on the due date.




GUI Test
After testing each of your methods using text output, you must test your Minesweeper class using the GUI test program. The .class and image files for the GUI test program are provided.

Step 1 - Copy each of the following supporting GUI class and image files into the same directory as the TestMineSweeper.class file

Step 2 - Run the GUI test program using your minesweeper.class

java minesweeperGUI



Minesweeper Demo

A minesweeper demo program is provided as an executable .jar file. (minesweeperDemo.jar)

Step 1 - Copy the following jar file

minesweeperDemo.jar

Step 2 - Run the GUI demo

Type the following from the command line

java -jar minesweeperDemo.jar




Outline of class minesweeper

class minesweeper 
{
  // Attributes
  public int[][] mines; //mines and clue values
  public int[][] tiles; //tiles covering mines and clues

  private String status; //game status - play, win, lose

  //Constructors
  public minesweeper() // default constructor 9 by 9 board
  public minesweeper(int newRows, int newCols) // non-default constructor

  // Public Methods
  public String getStatus() //current game status - play, win, lose

  public int getRows() //number of game board rows
  public int getCols() //number of game board columns

  public int getMines(int r, int c) //mine array value at position r,c
  public int getTiles(int r, int c) //mine array value at position r,c
  public char getBoard(int r, int c) //board value for position r,c

  public void markTile(int r, int c, int t) //change tile status

  public String toStringMines() //mines array as String
  public String toStringTiles() //tiles array as String
  public String toStringBoard() //game board as String

  private void initGame(int newRows, int newCols) //set-up game
  private void resetTiles() //set all tiles closed
  private void placeMines() //place random mines
  private void calculateClues() //calculate clue values

  private boolean validIndex(int r, int c) //verify index

  private boolean gameWon() //determine if game is won

}



Outline of class TestMineSweeper

public class TestMineSweeper 
{ 
  public static void main (String[] args) 
  {
    // create new minesweeper instance 2 rows by 5 columns
    minesweeper game = new minesweeper(2, 5); 

    // display mines
    System.out.println ( game.toStringMines() );

    // display tiles
    System.out.println ( game.toStringTiles() );

    // display board
    System.out.println ( game.toStringBoard() );

    // mark tile at (0, 0) as Open
    game.markTile (0, 0, 0);

    // mark tile at (0, 1) as Question Mark
    game.markTile (0, 1, 2);

    // mark tile at (0, 0) as Mine
    game.markTile (0, 2, 3);

    // display tiles 
    System.out.println ( game.toStringTiles() );

    // display board
    System.out.println ( game.toStringBoard() );
 } 
}



Sample Output #1 for TestMineSweeper.java

92100
29100

11111
11111

XXXXX
XXXXX

02311
11111

!XXXX
X*XXX




Sample Output #2 for TestMineSweeper.java

29100
92100

11111
11111

XXXXX
XXXXX

02311
11111

2?FXX
XXXXX