CIT 594 Assignment 11: Wa-Tor
Spring 2014, David Matuszek
"Wa-Tor" or "Wator" (a deliberate misspelling of "water") is a simple predator-prey simulation. The simulation was apparently devised by Alexander Dewdney, but there have been many versions since.
In this assignment we will follow the "traditional" rules for Wa-Tor, as explained in this Wikipedia article.
Name your project
Wator, your package
wator, and your main class
Wator. You will probably want classes
Shark. Since these classes have a lot in common, you may want a superclass or interface; if so,
Pisces would be a reasonable name. Unit tests should go in a separate
The ocean will be represented by a two-dimensional array, 500x500 or larger. I suggest blue for fish, red for sharks, but it's up to you.
Concurrency is used mainly to speed up programs. Although there is no need for this (because a sequential version will run fast enough), each fish and each shark will have its own Thread, taken from a Thread pool. The reason for this is, of course, pedagogical--to familiarize you with the use of thread pools.
The GUI provided in the starter code for the Interpreter assignment, logo.java, is almost exactly what you need. Change the name of the class from
Wator, delete any widgets and menu items you don't need, add any you do need, and it should be ready for use.
All simulation parameters should be settable, not hard-wired into the program. The simplest way to do this is by using an input dialog (
JOptionPane.showInputDialog) for each parameter. A better way would be to add text fields for these to the main display (where we previously displayed a Logo program). Your choice.
Use the Model-View-Controller design pattern, in which the GUI acts as the View and Controller, and all computation is done in the Model. (If you are not familiar with this design pattern, see the article in Wikipedia and my slides from CIT 591.) The key point is that the Model, which performs the simulation, should be unaware of the existence of the GUI; it could be run from a text-only main program, or from a web interface.
For this program, each fish and each shark will run in its own Thread. Since threads are expensive to create, and there will be a lot of fish and sharks, we will use a smaller number of threads, and recycle them. Java provides a thread pool for this purpose. Here's a basic outline of how to use a thread pool:
ExecutorService pool = Executors.newFixedThreadPool(poolSize);
Runnableobjects (objects that implement
public void run())
void shutdown()stops accepting new tasks.
List<Runnable> shutdownNow()stops all tasks and returns a list of unfinished tasks.
java.util.concurrent for more details.