Iterating over the Sprites

In order to write out the Sprites, you need some way to enumerate all of them. You'll find an example of that in the step() method of class Sprites.

The method step() has to deal withg the possibility of seeing a Sprite more than once because it moves them (by calling act()). So it uses a HashSet to keep track of Sprites that have moved to avoid moving the same Sprite twice in a turn.

In your method to save all of the Sprites, you can ignore the risk that they are moving; it is OK if you simply run through all of the grid locations and save all of the agents you find.

Note that this is not a perfect solution, because it is possible that step() may run while you are writing out the Sprites. That is an interesting problem: if it happens, you may fail to save a Sprite (if it moves from a position you have not yet looked at to one that you already have) or you might duplicate a Sprite (if it moves after you save it to a position that you process again). Solving this well is a bit beyond the scope of this semester's class, and it isn't the point of this lab. In this case, we'll be accept the imperfection.


Cary Gray
Last modified: Mon Dec 2 11:48:39 CST 2013