The goals of this assignment are:
Requirements:
- Every method you created should be tested in the class’s main method.
- Every java file should have a header. However, DO NOT PUT YOUR NAME IN YOUR HEADERS. On gradescope we grade your assignments anonymously so including your name in your Java file will defeat that purpose. However, please include your name in your README.txt
- Every method should contain a javdoc that briefly describes the method, lists the params, and the return type/value. Use the notation that we used in lectures.
WARNING: In this assignment you are NOT ALLOWED to import any packages or classes besides for
java.util.Scanner
,java.io.File
, andjava.io.FileNotFoundException
. For example, you cannot use the builtinArrays
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.
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.
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:
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.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
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).
Your main()
method should use the methods below to do the bulk of the work.
The general outline of the program is as follows:
Print out a welcome message: “Welcome to Minesweeper”. You can do this in main, or define a function for it.
Welcome to Minesweeper
. . . .
. . . .
. . . .
. . . .
In the next steps, the user will “uncover” cells by inputting a row and column.
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.
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.
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.
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!!”
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
In a text file called README.txt
answer the following questions:
Submit the following files to the assignment called HW08
on Gradescope:
Board.java
Minesweeper.java
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