Skip to main content

Homework 6: Minesweeper

The goals of this assignment are:


WARNING: In this assignment you are NOT ALLOWED to import any packages or classes besides for java.util.Scanner,, and For example, you cannot use the builtin Arrays java class to print out an array. Doing so will result in earning ZERO POINTS for a method that uses it.



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.

1. Board

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

Update: 03/13 (8:23PM): board1.txt, board2.txt, board3.txt, and board4.txt are all examples boards that we provide. We will test your program with other boards. For example, we might have a file name called test.txt that looks like

- - -
X - -

You can assume that the board files will be in the same directory as and

$ javac
$ java Board board3.txt
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 board4.txt
X X X X X X X 3
X X 8 X X X X X
X X X X X X 7 X
X X X X X 4 X X


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.

We have provided method stubs in for you to complete.

2. Minesweeper

Write a program,, that implements the game of minesweeper. Re-use your code from the previous question. You should also write a method to validate 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 to determine the file name that contains the board!

Update (02/13 5:45pm): If no filename is passed in as a command line argument, the program should print a message to the user saying so and then terminate. If a filename is passed in as a command line argument and the file does not exist, the program should print a message to the user saying so and then terminate.

We have released 4 example boards: board1, board2, board3, board4

Main Method

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


Think about how to subdivide the features of the game into methods. Each method should implement a single, well-defined task. For example, do not put your entire game loop into a single method! Use methods to help abstract sub-tasks, for example,

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

3. Extra Credit

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


In a text file called README.txt answer the following questions:

  1. How much time did you spend on the homework
  2. What did you learn from this homework
  3. optional: What did you struggle with during this homework
  4. optional: any other feedback you would like to share

Dont forget: make sure to fill in the header in all of your java files.


Submit the following files to the assignment called HW06 on Gradescope:

  4. README.txt

Make sure to name these files exactly what we specify here and name the methods exactly what we specify too. Otherwise, our autograders might not work and we might have to take points off.