Assignment 6: Minesweeper

Due Thursday, October 20, before midnight

The goals of this assignment are:

  • Design and implement programs containing multiple functions

  • Apply top-down design principles to building a larger program

  • Work with loops, conditionals, and variables

Minesweeper

minesweeper

In minesweeper, players try to uncover each cell of a NxM grid without hitting any bombs. N represents the number of rows in the grid and M represents the number of columns.

When the game starts, the program will generate a board and show it to the user with all cells hidden. When the user selects a cell, the game will reveal the contents of the cell. If the cell has bombs in any of the 8 adjacent cells, the cell will show a count of the number of neighboring bombs. If no bombs are adjacent, the cell is empty. If the cell contains a bomb, the game is over. If the user uncovers all cells, the user wins!

We will build up our programs in two parts.

Board

Write a program, Board.java, that generates and displays a NxM minesweeper board. The user should specify the following parameters as command line arguments.

  • the number of rows (integer)

  • the number of cols (integer)

  • the probability that a cell contains a bomb (double in range [0,1))

$ javac Board.java
$ java Board 5 8 0.1
0 0 2 X 3 2 X 1
0 0 2 X X 2 2 2
0 1 2 3 3 2 2 X
0 1 X 1 1 X 2 1
0 1 1 1 1 1 1 0

$ java Board 5 8 0.9
X X X X X X X 3
X X X X X X X X
X X 8 X X X X X
X X X X X X 7 X
X X X X X 4 X X

Requirements/Hints:

  • You must use command-line arguments

  • You should use a 2D array to store the board. We recommend using a 2D array of integers, where each cell contains the count of neighboring bombs. If the cell contains a bomb, you can use a special value, such as -1, to denote it.

  • To decide whether a cell includes a bomb, use Math.random and test whether the value is less than the given probability, e.g. for each cell, generate a random number. if the the value is less than p, place a bomb in that cell.

We recommend that you organize your code to use functions for generating and displaying the board. You will re-use these functions in the next question.

class Board {
    public static void display(int[][] board) {
        // todo: display board to console
    }

    public static int[][] generate(int rows, int cols, double prob) {
        // todo: generate board with dimensions rows x cols
        // todo: fill board with prob chance of containing a bomb
    }

    public static void main(String[] args) {
        // call generate and display
    }
}

Game

Write a program, Minesweeper.java, that implements the game of minesweeper. Re-use your code from the previous question. You should also re-use your validation functions from last week to check for valid user input.

Below are several examples showing how the game should play:

Your output doesn’t need to look exactly the same but it should be clean and easy to read.

Your program should use command line arguments!

Main Function

Your main() function should use the functions below to do the bulk of the work. The general outline of the program is as follows:

  1. Print out a welcome message. You can do this in main, or define a function for it.

  2. Set up initial variables before entering the game loop. You will need at least two boards: one with the generated bombs and one that keeps track of which cells the user has uncovered already.

  3. Set up your game loop (hint: use a while loop). Each time through the loop, the program will ask for a cell to uncover and update the board.

  4. Print the output of the game. If the user selects a cell with the bomb, they lose. If the user uncovers all non-bomb cells, they win.

Design

Think about how to subdivide the features of the game into functions. Each function should implement a single, well-defined task. For example, we don’t recommend you put your entire game loop into a function! But we do recommend that you use functions to help abstract sub-tasks, for example,

  • to validate input

  • to check whether the user has uncovered all non-bomb cells

  • to display the current state of the board

  • printing text with banners

You can define a 2D array of booleans to keep track of which cells the user has selected already.

Extra challenge

Implement your own unique minesweeper game in MyMinesweeper.java. Be sure to point out to us your original ideas and features in your readme. Below are ideas

  • Use StdDraw to display the board with graphics

  • If the user selects an empty square, uncover all adjacent cells until you reach a non-empty cells.

What to hand-in

  1. The programs, Board.java and Minesweeper.java, and if applicable, MyMinesweeper.java

  2. Make sure your program has a header containing your name, date, and purpose of the program

  3. A brief write-up containing your name, assignment number, and a few sentences about how long you spent on the assignment and any interesting bugs you solved. Be sure to highlight any customizations you made for your game!!

How to hand-in

  1. Copy your files to your dropbox, into the folder called A06.