Computer Science 132
Introduction To Computing II

Dickinson College
Fall Semester 2001
Grant Braught

Lab #7 - Minesweeper

Introduction

Minesweeper is a strategy game. The game is played on a grid of cells as shown in figure 1. The exact size of the grid is determined at the start of the game.
Total Mines : 5 Flagged Mines : 0 Remaining Mines : 5 | 1 2 3 4 5 6 7 8 --+----------------- A | * * * * * * * * B | * * * * * * * * C | * * * * * * * * D | * * * * * * * * E | * * * * * * * * F | * * * * * * * * G | * * * * * * * * Figure 1: The playing grid for a Minesweeper game.


Explosive mines are hidden behind some number of the cells. The objective is for the player to identify all of the cells that contain mines. During each turn the player may uncover one of the cells. If the uncovered cell contains a mine the game is over. If the uncovered cell does not contain a mine a value indicating the number of adjacent cells that contain mines is displayed. A partially completed game of Minesweeper is shown in figure 2:
Total Mines : 5 Flagged Mines : 0 Remaining Mines : 5 | 1 2 3 4 5 6 7 8 --+----------------- A | * 1 * * * * * * B | 1 1 * * * * * * C | * * * * * * * * D | * * * * * * * * E | * * * 2 * 0 1 1 F | * * * * * 1 0 0 G | * * * * * * * * Figure 2: A Minesweeper game in progress.


In figure 2 the cells A2, B1 and B2 all indicate that one adjacent cell contains a mine. Therefore, the cell A1 must contain a mine! This is the type of reasoning that a player must use to determine the location of all of the mines. When the location of a mine has been determined the player may indicate that location by placing a flag in that cell. Figure 3 shows a continuation of the game in figure 2 with several flags indicating the known locations of mines.
Total Mines : 5 Flagged Mines : 3 Remaining Mines : 2 | 1 2 3 4 5 6 7 8 --+----------------- A | F 1 * * * * * * B | 1 1 * * * * * * C | * * * * * * * * D | * * * * * * * F E | * * * 2 * 0 1 1 F | * * * * * 1 0 0 G | * * * * F * * * Figure 3: A Minesweeper game with several flagged mines.


To win the game the user must successfully place flags in all of the cells that contain mines and all cells not containing mines must be uncovered.

For each turn the user will enter their command to Uncover or Flag a cell. Some examples of moves are:

U B3 - Uncovers cell B3. F C7 - Places a flag in cell C7. F C7 - Removes the flag from cell C7. - Placing a flag in a cell containing a flag removes the flag.

Assignment

Implement the Minesweeper game. Your program should play one complete game of Minesweeper each time it is run.

I strongly suggest using an incremental approach to developing this game. Start small by just getting the program to create and display the board. Get that part of the program running and then add additional features one by one (Uncover a cell, flag a cell etc...) Compile and test your program as you add each feature. By developing in this way you will be sure that you always have a program that runs. Also, if an error appears it is most likely due to the feature you just added. This will make it easier to debug your program.

Bonus: Enhance the method that updates the user's view of the board so that when the user uncovers a cell with no adjacent mines, the 8 adjacent cells are uncovered automatically.

Super Bonus: Further enhance the method that updates the user's view of the board so that when a cell with no adjacent mines is uncovered, cells are uncovered in all directions until cells with adjacent mines are found. In other words, make your game behave like the real game.

Programming Hints

Parsing the Command Input

Read the user's commands as a string using readLine(). Then parse that string to determine the command and the cell on which that command acts. You can use the charAt(...) method of the String class to find the character in the first location and determine if the command is Uncover or Flag. Your program should be case insensitive.

To find the row on which the command should operate you will need to find the third character in the string and convert it to an integer so that:

A or a = 1 B or b = 2 ... Z or z = 26 For a hint on how to accomplish this consider the following code: char c1 = 'A'; char c2 = 'B'; int x1 = (int)c1 - (int)'A'; int x2 = (int)c2 - (int)'A'; System.out.println(x1 + "," + x2); The output of this code is 0,1.

To find the column on which the command should operate you will need to convert the rest of the string into an integer. To obtain the rest of the string you can use the substring(...) method of the String class. The following code prints a string containing the column number from the command:

String cmd = "U B15"; String col; col = cmd.substring(3,cmd.length()); System.out.println(col); Now given a string that contains just the column number you will need to convert that number into an integer. The following code shows how to use the parseInt(...) method of the Integer class to convert a string containing an integer into an int: String strInt = "1234"; int x; x = Integer.parseInt(strInt);

After these instructions the variable x will contain the integer 1234.

Handing in the Lab