CSci 151: Foundations of computer science II
Home Syllabus Assignments Tests

Assignment 1: Fire!

Due: 5:00pm, Friday, September 5. Value: 30 pts. Submit your solution (consisting of FireSim.java only) using the Sauron Submission System. [instructions]

Researching how to fight forest fires is tricky but important business: Obviously you can't just start forest fires willy-nilly for the sake of testing methods for fighting them. For this reason, fire researchers have invented techniques for simulating them in a computer. In this assignment, we'll build a simplified version of one established technique for simulating forest fires.

In our simplified version, we regard the forest as a grid. (You'll want to use two-dimensional arrays for this assignment. I'll be happy to discuss them with you if you're not familiar with them, or if you'd just like a review.) Each square of the grid will be in one of three states.

EMPTYthe grid square contains no trees
HAPPYthe grid square contains trees, which are not burning (yet)
BURNINGthe grid square contains trees, which are burning

At the outset, all the grid's boundary squares will be EMPTY, the grid's center square will be BURNING, and the rest are either EMPTY or HAPPY, chosen randomly and independently according to a probability configured by the user (emptyProb). If the grid has an even number of rows or columns, then there isn't an exact center, and so the BURNING square just has to be close to the center.

Time proceeds in discrete steps. With each step, any HAPPY square for whom one or more of the four neighbors is BURNING will itself change to BURNING with another probability chosen by the user (catchProb). A square isn't more likely to catch fire if it has multiple burning neighbors. Also, a BURNING square will change to EMPTY after it has been burning for a number of steps as configured by the user (burnLife).

Your program will consist of two classes, GuiInterface and FireSim. I am providing you with GuiInterface, and your assignment is to write FireSim. You should not alter GuiInterface or create any other classes.

The GuiInterface class manages the display of the simulation on the computer screen. The interface uses yellow-green for EMPTY squares, pine green for HAPPY squares, and blaze orange for BURNING squares. (It also uses red to indicate squares in an invalid state, but if your program works, you should never see red.) The drawing includes a feature that you may find counterintuitive at first: When a grid square's state changes between steps, it paints the center of the square with its previous state and the boundary with the square's new state. This is to help with visualizing how the simulation transitions from one state to the next.

FireSim will compute the forest fire simulation according to the above rules. It should define three integer constants, EMPTY (0), HAPPY (1), and BURNING (2). And it should provide the following constructor and instance methods.

FireSim(int rows, int cols, double emptyProb, double catchProb, int burnLife)

(Constructor) Constructs a fire simulation in its initial configuration. The parameters are as follows.

cols:the number of columns in the grid, including the boundary
rows:the number of rows in the grid, including the boundary.
emptyProb:the probability (between 0 and 1) that a non-boundary, non-center squares starts out as EMPTY.
catchProb:the probability (between 0 and 1) that for each time step a HAPPY square becomes BURNING if it has one or more neighboring squares that are BURNING.
burnLife:the number of time steps that a square is BURNING before it changes to EMPTY.
int getCell(int row, int col)

Returns EMPTY, HAPPY, or BURNING corresponding to the cell.

void step()

Advances the simulation one time step.

I encourage you to use Eclipse for developing your solution. Eclipse is a widely used professional-grade development environment for writing Java programs. It's definitely worth learning. Please feel free to stop by my office if you want me to show you how to get started with Eclipse.