On this page:
Instructions
Minesweeper
Prerequisites
1 Part 1
2 Part 2
8.10

Assignment 7/8: Minesweeper

Goals: Design a game with mutable world state, ArrayLists, mutable linked data structures, and loops.

Instructions

As always, be very careful with your naming conventions.

You will submit this assignment in two parts:

Due Dates:
  • Minesweeper Part 1 – Friday, December 8th at 10:00pm

  • Minesweeper Part 2 – Monday, December 15th at 10:00pm

Minesweeper

For this project, you will be implementing the famous time-wasting game known since the 1960s, and popularized by Microsoft Windows 3.1, Minesweeper. The game consists of a two-dimensional grid of cells, with each cell possibly containing a hidden mine. The player can reveal what’s in a cell by clicking on it; there are three possibilities. First and most importantly, if it contains a mine, the player loses. Second, if the cell is empty but adjacent to some mines, the revealed cell shows a number indicating how many of its neighboring cells contain a mine.

Finally, if the revealed cell is both empty and has no neighbors with a mine, then instead of showing a zero, all of its neighbors are revealed, as well. This should cause a cascading “flood-fill” effect where an entire region of connected cells without mines is uncovered (play the game at the above link to see this in action). The flood-fill should stop when it reaches a cell with a non-zero neighboring mine count.

Obviously in the real game, you wouldn’t draw mines on tiles that haven’t been revealed, since that would defeat the whole game! This image is just for demonstration purposes only. In these images, “hidden” cells are drawn light-blue, and “revealed” cells are drawn in gray.

For example, say the grid has mines in the following positions:

If the player clicks the cell in the top-right corner, the cell shows the number of adjacent mines (in this case, 1):

On the other hand, the cell in the lower-right corner has no adjacent mines. If the player clicked that cell instead, this would trigger the flood-fill behaivor, continuously spreading out and revealing all neighboring cels, stopping at cells that have adjacent mines:

In that screenshot, it’s obvious that there must be two mines in the middle of the bottom row. A player can indicate the location of a suspected mine by right-clicking a cell to place a flag on it (here shown as an orange triangle on the right-most cell):

The player’s goal is to identify where all of the mines are by uncovering all of the cells except the ones that contain mines. (The mines do not need to be flagged; they’re only for the player’s convenience.)

Prerequisites

You will be using the Impworld library, as in Recitation 10 make sure that at the top of your file, you include
import java.util.ArrayList;
import tester.*;
import javalib.impworld.*;
import java.awt.Color;
import javalib.worldimages.*;

1 Part 1

For the first part of the assignment, design the representation of the game and the initial drawing functionality. At a high level, you should:

2 Part 2

For this part of the assignment, finish implementing the game. That means:

Note: To implement the flood-fill behavior, you will very much want the ability to iterate over the neighbors of a cell, without having to mess about with their coordinates. If you did not get this working in Part 1, fix it now.

Note: To have enough information to handle left and right clicks, you will need to override the two-argument form of onMouseClicked, that takes a Posn for the mouse position, and a String describing which button ("LeftButton", "MiddleButton", "RightButton" or "UnknownButton") was clicked.