Introduction
Download the zip file LangtonAnts.zip. This is a JavaFX project that simulates the running of a popular cellular automata simulation called Langton’s Ant.
The program works mostly. What you will be doing is adding your own improvements to my program as listed below. This will give you some practice in working with an already written code as this a common task for software engineers.
What the program does
When you first run the program, you will see a grid of squares about 20 by 20. Each square is a ‘cell’ in the simulation. A cell has a ‘state’ which is shown as a different color. In the original Langton Ant simulation there were only 2 states but this program can handle from 2 to 10 states. It is currently set to 6 states.
If you left click on a square, an ‘ant’ will appear on it. The original Langton Ant simulation had one ant but this simulation can have as many ants as you want. But keep in mind the more ants, the slower the simulation will run as it needs to move every ant at each step.
When the simulation is running, an ant will move to an adjacent square (up, down, left or right). To determine which square to move to, the ant first turns 90 degrees to the left (L) or right (R) according to a rule based on the state the cell is in. For example, if the cell is in state 3, the program will look up state 3 in a list of rules. The list is stored as a string of ‘L’ or ‘R’ characters. The rule in the string is the index that matches the state number. If the character is ‘L’, then the ant turns 90 to the left (adds 90 to it’s angle). If a ‘R’, then the ant turns 90 to the right (subtracts 90 from it’s angle). It then moves to the adjacent cell in the direction the ant is heading.
What You Need To Do
The following improvements are needed to be done on the program.
- Settings Dialog Pane: Right now the number of rows, the number of states and the rulestring (set of ‘L’ and ‘R’ commands as a string) are all fixed in the code. Write a dialog pane that allows the user to at least change the number of rows(from 2 to 100), number of states(from 2 to 10) and the rulestring. Make sure to check for user input being in the right ranges and print an error message to the window (not the console) if they are not valid. Also, make sure the rulestring has the same length as the number of states and that it only consists of ‘L’ and ‘R’ characters.
Then add the settings pane to the MainPane. You might need to make span more than one row or column so use the GridPane add method which allows you to input how many rows/columns to span. - Draw a more interesting ant Right now the ant is just a black Ellipse. Make it either more interesting or more artistic. An idea would be to use a ImageView instead of using an Ellipse (or a Polygon object that looks more like an ant). At least do this for a cell size of 10 pixels or larger. You can keep the Ellipse for smaller cell sizes as the images get a bit too small to even see at 10 pixels or less.
- Add some more interesting animation Right now all that happens is the ants disappear from one cell and appear on another and cells change colors. Add some interesting animation to the ant’s movement (or possibly to the cell’s changing color). You can use maybe PathTransition to gradually move the ant or FadeTransition to fade out the ant from one cell and fade in the ant to the other. OR you can try some other idea for animation as long as it uses a subclass of Animation and makes sense for the program.
- Add some text to the window to show the number of steps completed The step field is in the MainPane class. You can update this text in the run() method.
- Buttons to speed up or slow down the simulation. Add these to the buttonPanel.
- Add a button to turn off or on the extra animation you added This is to allow the simulation to run faster as animation tends to slow down the processing of each step.
- A better action for the right click on a cell Note that I have created my own custom event called CellClickedEvent for the CellPane class’s use. I needed this to create or delete an ant in the Model class which resides in the MainPane class. So when a mouse clicks on a CellPane on the board, I fire an event (which goes to all panes) so that the MainPane can get the information needed to create or delete an ant.
Right now, the right mouse button will delete an ant but I don’t find this action very useful. So come up with something more useful to do with the right mouse button (like turning an ant on the cell 90 degrees or changing the state of the cell underneath).
You will need to change the CellPane’s mouse handler and maybe add some extra fields to the CellClickedEvent class that you can set by changing the constructor for this class. It doesn’t necessarily have to be the MainPane that picks up the CellClickedEvent event but it does have to be a class that subclasses a JavaFX Parent class (so Model and Ant wont work here).