Skip to main content

Homework 08: Minesweeper

The goals of this assignment are:

Requirements:

WARNING: In this assignment you are NOT ALLOWED to import any packages or classes besides for java.util.Scanner, java.io.File, and java.io.FileNotFoundException. 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.

IF YOUR CODE DOES NOT COMPILE, YOU WILL RECEIVE 0 POINTS.

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.

1. Board

Write a program, Board.java, that generates and displays a NxM minesweeper board.

We have provided method stubs in Board.java You will download the file and fill in the template: wget https://raw.githubusercontent.com/BMC-CS-113/BMC-CS-113.github.io/main/hws/hw08/Board.java. It includes methods for generating and displaying the board. Do not modify the method stubs. You will re-use these functions in the next question.

You will also download test files: wget https://raw.githubusercontent.com/BMC-CS-113/BMC-CS-113.github.io/main/hws/hw08/board1.txt
wget https://raw.githubusercontent.com/BMC-CS-113/BMC-CS-113.github.io/main/hws/hw08/board2.txt
wget https://raw.githubusercontent.com/BMC-CS-113/BMC-CS-113.github.io/main/hws/hw08/board3.txt
wget https://raw.githubusercontent.com/BMC-CS-113/BMC-CS-113.github.io/main/hws/hw08/board4.txt

The user should specify the following parameter as a command line argument.

Example boards: 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 Board.java and Minesweeper.java.

An X represents a bomb is in that location.

Your Board program should:

  1. Use generate to read in the file and create a 2D array which represents the board. Each element in the array should hold the number of bombs it is touching.
  2. Use display to print the array as shown below.

In your main, call generate and display to show the following output:

$ javac Board.java
$ 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 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:

2. Minesweeper

Write a program, Minesweeper.java, that implements the game of minesweeper. Re-use your code from the previous question (meaning that you should call the static methods from the Board class).

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:

  1. Print out a welcome message: “Welcome to Minesweeper”. You can do this in main, or define a function for it.

  2. Print a “covered” version of the board which doesn’t display any information on where the bombs are: java Minesweeper board2.txt
    Welcome to Minesweeper
    . . . .  
    . . . .  
    . . . .  
    . . . .  
    

    In the next steps, the user will “uncover” cells by inputting a row and column.

  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. Prompt the user for a row by printing “Enter a row: “. Use a Scanner to read the input. If the input row is not an integer, print “Please enter an integer!” and prompt them to enter a row again. If the input row is outside the bounds of the board, print “Enter an integer in the range [0,X)” where X is the height of the board. Then, prompt them to enter a row again.

  5. Prompt the user for a column by printing “Enter a col: “. Use a Scanner. Ensure that the input is an integer in the bounds of the board. If it is not, print the err messages (as in the previous step) and reprompt for a column.

  6. Continue until the user either (a) The user selects a cell with a bomb and they lose. Print “You hit a BOMB! You lose!”. or (b) The user uncovers all non-bomb cells and they win. Print “You WIN!!”

  7. After the game is over, print “Game Over” and display the uncovered board to the user.

Below are some examples showing how the game should play:

https://github.com/BMC-CS-113/BMC-CS-113.github.io/blob/main/hws/hw08/minesweeper-output1.txt https://github.com/BMC-CS-113/BMC-CS-113.github.io/blob/main/hws/hw08/minesweeper-output2.txt

README.txt

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

Submitting

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

  1. Board.java
  2. Minesweeper.java
  3. 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.

DO NOT SUBMIT .class files