Assignment 4: APIsolutely About Zombies¶
This assignment is the first step in a sequence of assignments that will simulate zombies! In this part you will prepare the simulation data and display the entities being simulated (the zombies and non-zombies/humans). In the next part (Assignment 5) we will actually simulate the interaction between the two.
There are a few reasons for this choice of subject matter:
These assignments show a real-world application of computing. The structure we will create is used in many simulations and games, and computer scientists often do similar work to visualize data and understand natural phenomena.
Games and simulations often have a loop that simulates time steps. The typical flow of this loop is:
Check if the game/simulation should continue (part of Assignment 5)
Update all the items being simulated for the current time step (part of Assignment 5)
Show or record any progress (we’ll do this here in Assignment 4)
Simulating biological systems can be fascinating but complex. Using zombies allows us to simplify the rules of the system, which means shorter assignments more focused on the CS concepts we want you to learn.
In order to simulate how a zombie infection can spread, we will simulate:
Zombies, which have a 2D location. The x and y values can range from 0.0 to 1.0.
Non-Zombies, which also have a 2D location with values ranging from 0.0 to 1.0.
In other words, for every entity (zombie or non-zombie), we will want to keep track of the entity’s current coordinates and whether the entity is a zombie. We can use a boolean for the entity type, with true to indicate a zombie and false to indicate a non-zombie.
There are several ways to manage information like this. For this assignment, we’ll use an approach sometimes called parallel arrays. The basic idea is that we will have three arrays to keep track of
An array of length
Nthat contains whether or not each entity is a zombie. Call this the
An array of length
Nthat contains the x coordinates of all the entities. Call this the
Each thing being simulated will be associated with an index. For example,
ys represent the type and coordinates of a single entity in our simulation. In a sense you can think about
ys as being columns in a table. Each individual row of the table represents a specific thing being simulated. The three arrays are considered “in parallel” since the i-th items in each array represent different aspects of a single composite thing.
The initial locations of zombies and non-zombies will be provided in a file. The file will have a very specific format:
The first line of the file will indicate how many entities are in the file (
Nlines will each contain information for a single entity:
Each line will start with a
"Zombie"indicates a zombie and
"Nonzombie"indicates a non-zombie.
Second will be a real number (
double) indicating the initial x position of the entity.
Third will be another real number (
double) indicating the initial y position of the entity.
We’ve included several example files. When you run the program, you will see a file dialog box that will allow you to pick a
.sim file. This is the file that your code will read from (click to enlargen examples below).
A Note About Strings¶
Although the files will contain a String for each entity type, we will want to represent the type as a boolean value in our areZombies array.
Unlike primitive data types (
boolean, etc.), comparing the value of two Strings requires using
.equals() instead of
== on Strings checks if the two Strings are the exact same object while
.equals() checks if the Strings contain the same characters. Here is an example of the difference between the two approaches:
This assignment will utilize two different APIs:
Scanner: This allows us to read data from a file. We’ll get the type and location of all entities from a file. This is slightly different from how we have used Scanner in the past, as we will not be prompting the user for input values.
StdDraw: This will allow us to display the location of the zombies and non-zombies as our simulation progresses.
Using Scanner to read from a file¶
When Scanner is connected to a file,
nextDouble(), will get the next value in the selected file if it’s a double. If the next value is not a double, it will ask the user for a double instead.
Each time you call
nextDouble(), Scanner will process that value, and the next call to
nextDouble()will return the next double in the file, whether it’s on the same line or the next.
Make sure you use the method that asks for the data type that corresponds to the next value in the file. For example, if the next value in the file is a double, make sure to call
Your work needs to:
Read all the information from the selected file and store it in parallel arrays. Use
Scannermethods as described above.
Display all the elements in the simulation using Sedgewick’s
For full credit you must read in all entities and store them in arrays before displaying anything. (This structure will make future assignments easier.)
Zombies should be displayed with filled red circles with a radius of
Non-Zombies should be displayed with filled black circles with a radius of
StdDrawto display the ratio of Non-Zombies to total entities in the corner of the window (choose whatever corner you like). For example, in a simulation that has 3 Non-Zombies and 4 Zombies, this would look like “
3/7”. Make sure that this display is readable.
assignment4package. One line has already been done for you. The remaining work is summarized with
Use code to read in the first line of the file (the
Create arrays of appropriate size (for the “parallel data”).
Read in each additional piece of data. Remember the file format:
Display all the entities using
Display the ratio of Non-Zombies to total entities in a corner of the canvas using
When running the program:
Select the file you wish to read from the file dialog box.
Submitting your work¶
To submit your work come to office hours or class on an “Assignment day” and sign up for a demo via wustl-cse.help.