Minesweeper
Option #2 - OOP


 

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

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.  At this point the game status 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

 

Level 1 – Mines, Tiles, & Board
Your assignment is to implement a minesweeper class called minesweeper.javaYour minesweeper class will contain data and methods to partially implement a minesweeper game.  While you are not creating a GUI interface in this assignment, you will be creating the data and methods that will support a GUI interface.

A shell program minesweeper.java is provided containing a partial implementation of the minesweeper class.  Your assignment is to implement the methods listed below.  Your minesweeper class must meet the requirements described in this assignment and the minesweeper class Java Doc.   You may choose to add additional data or methods if necessary.  All data should be private.  Methods may be public or private as is appropriate.  Note: methods and requirements for level 2 are indicated.  You do NOT need to implement the Level 2 methods.

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

Level 2.  Logic and GUI test
Your assignment is to implement the methods needed to support a complete working version of a minesweeper game.  The level 2 requirements for each method are described in this assignment and in the minesweeper class Java Doc.  Add your level 2. changes to the minesweeper class (minesweeper.java) from level 1.  Modify the sample test program minesweeperTest.java from Level 1. to demonstrate each of the methods in your minesweeper.java class.


GUI test program
After testing each of your methods using text output, you can test your minesweeper class using a GUI test program.  The .class and image files for a GUI test program are provided.

Step 1 - Copy each of the following supporting GUI class and image files into the same directory as your minesweeper.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

If you are using a GUI environment you may be able to double-click on the file minesweeperDemo.jar

If this does not work, type the following from the command line

        java -jar minesweeperDemo.jar

 

Bonus
Modify the markTile method to open all adjacent blanks and clues when a tile is opened that covers a blank.  Hint: use recursion.

 


minesweeper.java Class Summary

(*) indicates data and methods for Level 2.

public class minesweeper {

  //Data
  public int[][] mines; //mines and clue values
  public int[][] tiles; //tiles covering mines and clues

  (*)private String status; //game status – play, win, loose

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

  //Public Methods
  (*)public String getStatus()//current game status – play, win, loose

  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 randome mines
  private void calculateClues() //calculate clue values

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

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

}

 


Sample Test Program (minesweeperTest.java)

/*
* 
* minesweeperTest.java
*
*/
public class minesweeperTest { 
 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
  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() );
  System.exit(0);
 } 
}//minesweeperTest

Sample Output #1 for minesweeperTest.java

92100
29100

11111
11111

XXXXX
XXXXX

02311
11111

*?FXX
XXXXX


Sample Output #2 for minesweeperTest.java

29100
92100

11111
11111

XXXXX
XXXXX

02311
11111

2?FXX
XXXXX


Turn-In
Turn in your modified minesweeper.java and minesweeperTest.java files via e-mail and turn in printed program listings.  Note:  Make sure the case (i.e.. upper/lower) for each of your files and methods matches the assignment specifications.