On this page:
Problem 1: Minesweeper
9.1 Introduction
9.2 Prerequisites
9.3 Part 1
9.4 Part 2
Extra credit
8.5

Assignment 9: Minesweeper

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

This assignment is due in two parts.

Part 1: Due March 28th, at 9pm

Part 2: Due April 4th, at 9pm

Problem 1: Minesweeper

9.1 Introduction

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.)

9.2 Prerequisites

You will be using the Impworld library, as in Lab 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.*;

9.3 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:

9.4 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.

Extra credit

If you would like to elaborate on this game for extra credit, there are several possibilities, of varying degrees of difficulty: