CSE373—Data Structures and Algorithms for Nonmajors

Programming Assignment #0

due: Monday, 10/10/05, 10:00 PM

Programming (60%):

Your first programming assignment is mainly meant to be a warmup, to make sure you are familiar with writing, compiling, and testing Java code and with using the turning server.

We are giving you several skeleton files, though you will only be adding code to one.

·         DrawingPanel.java – simple interface for mostly static graphics (hides AWT/Swing details)

·         MineCell.java – This file defines the legal values of a single cell.

·         MineBoard.java – The state of the board and game is stored and modified in this file.  You will be adding code to it.

·         MineUI.java – main resides here.  This file has the graphics and UI code.  Edit it to change the board size or to print debug info.

The MineBoard code is mostly done, except for eight methods at the bottom.  Read through the MineBoard code and study it.  The other methods will give you insight on how to write the missing methods, and you can, and are encouraged, to invoke other methods while implementing yours.

The MineCell class represents a cell on the board as one of several states: OOB (the cell lies out of bounds), CLEAREDEMPTY (the cell has previously been explored safely), UNEXPLOREDMINE (the cell has not been visited yet, and a mine lies there), UNEXPLOREDEMPTY (the cell has not been visited yet, and it is safe – no mine there), MARKEDMINE (the cell has not been visited, a mine lies there, and it has been marked [correctly] as having a mine), and MARKEDEMPTY (the cell has not been visited, it is safe – no mine, and it has been marked [wrongly] as having a mine). 

You will be writing code for the following eight public methods:

Method

Description

boolean askable(int i, int j)

Returns true if the cell at (i,j) can legally reveal how many mines are neighboring it: legal if (i,j) has already been safely explored (regardless of current gameOver state)

boolean markable(int i, int j)

Returns true if the cell at (i,j) can be legally marked: legal if (I,j) is unexplored and unmarked, and game still in progress

boolean unmarkable(int i, int j)

Returns true if the cell at (i,j) can be legally unmarked: legal if (I,j) is unexplored and marked, and game still in progress

boolean explorable(int i, int j)

Returns true if the cell at (i,j) can be legally explored: legal if (i,j) is unexplored (can be marked or unmarked), and game is still in progress

 

Method

Description

int ask(int i, int j)

 

If not askable, throws an IllegalStateException.

Returns the number of mines in its direct 8-connected neighbors, a number between 0 and 8.

void mark(int i, int j)

If not markable, throws an IllegalStateException.

Changes the MineCell state for the current square to say that it is marked.  Updates the numMarked state variable.

void unmark(int i, int j)

If not unmarkable, throws an IllegalStateException.

Changes the MineCell state for the current square to say that it is unmarked.  Updates the numMarked state variable.

void explore(int i, int j)

If not explorable, throws an IllegalStateException.

Explores the position (i,j).  If position (i,j) is a mine, sets gameLost to true.  Otherwise, changes the MineCell state for the current square to CLEAREDEMPTY and updates numExplored (and numMarked if it was marked).

For ask, because (i,j) cannot be a mine (or else (i,j) wouldn’t be askable), the code can be written concisely with two for-loops (avoiding an 8-fold duplication of code).  To receive full credit, you will need to implement ask as such.  Your error messages for the exceptions should print pertinent debug info for the programmer.

Short Answer Questions (40%):

Put your answers to the following in a file called questions.txt (you can use Notepad for editing).  You will be graded on content, not on spelling or grammar.

1) The populateBoard method performs poorly when the number of mines is more than half the total number of cells (as an extreme example, suppose all but one of the squares were mines).  How can populateBoard be modified to make it perform well in all situations?  The method must still produce a uniformly random distribution of mines.  (A few well-chosen sentences will suffice, or you can provide the rewritten code.)

2) The askable/ask, markable/mark, etc. idiom is common in Java, such as with the hasNextXYZ / nextXYZ of Scanner and hasNext / next of iterators.  Explain why this idiom is a good idea.  Address how exceptions are slow and to be used only to catch the unexpected.  (100-300 words)

3) The MineCell class is much more code than a simple enumeration.  First, explain how it works, with private constructor and static factory methods, and how == is sufficient instead of .equals.  Then explain its advantage for the purpose of defensive programming.  (100-300 words)

4) Explain how, in MineBoard, the choices of public and private methods effectively encapsulate the class, giving the outside users just what they need and making sure they don’t shoot themselves in the foot.  (100-300 words)

Turn in your work electronically from the “assignments” link on the class webpage.