JavaFX代写:CS141 Langton's Ant Colony


虽然是JavaFX的Project,但是由于start code已经实现了大部分逻辑功能,因此整体难度不大。整个Project采用MVC设计模式,只要遵循这个设计原则,就能完成大部分的code。


Download the zip file 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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. Buttons to speed up or slow down the simulation. Add these to the buttonPanel.
  6. 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.
  7. 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).

Class Descriptions (For Reference)

LangtonAnts class

This is the main class. It doesn’t do much except set the window size and create a MainPane object as the root to the scene object. You probably won’t do much in this class. Note that the Stage frame is set to not resize as the Board doesn’t resize. If this is a problem for you, then you can take this out (see Extra Credit for suggestions in resizing the Board).

MainPane class

This is the root pane where all other panes are added. It is a subclass of the GridPane class so you can add panes to this one. For the settings dialog box you will be adding, you can put it on the side (right or left side) and make it span two or three rows by using the expanded version of add in the GridPane class that takes a span value.
This is also where the model is created and resides as well as the Board and buttonPanel for putting animation control buttons. It’s technically the ‘controller’ class for the whole program as it holds the animation code for running the simulation.

Board and CellPane classes

The board class is a GridPane where each cell occupies a different row/column. The cells are CellPane objects which is a subclass of StackPane. Each cell has a Rectangle object with a grey outline. The fill color used depends on the state of the cell at this location. Also, there can be one or more ants occupying the cell at one time so there is a list of ant shapes (which are Ellipses). All of these are put one on top of another inside the cell with all the ants in front of the rectangle.
Mouse clicking events are handled by each cell. The left mouse button (PRIMARY button) creates an ant on that cell. The right mouse button (SECONDARY button) deletes the topmost ant from the cell. The mouse click handler sends a custom event (CellClickedEvent) back to the MainPane so that the ant can be added or deleted from the Model’s list of ants.

Model and Ant classes

The model class deals with the nonGUI (nonview) information of the code. This includes the list of ants using the Ant class to store the information on one Ant (it’s location and the angle). The Model class also has a 2 dimensional array of cell states as int values. This is where code that determines where an ant is moving resides. You wont need to touch much code in this class (or the Ant class) unless your doing the first Extra Credit part. However, you will need to call the method changeSettings in this class whenever the user changes settings in the Settings Pane you will be creating.


You must use the given project to add the above features so don’t write your own Ant simulation (or get code from the Internet). Do show me all parts that were changed in comments in the code (or you can do this in a separate text file but go into enough detail so I can see what was changed). Submit your modified project as an exported project file from Netbeans. If you are not using Netbeans, then zip up your files (or load each one individually) and submit them but leave a comment to effect that the zip is not a exported Netbeans project.


Start by examining the code thoroughly. Try putting in System.out.println() at various places to see what is being generated. You don’t need to really learn the Model class unless you need to change it (for extra credit) but the Pane classes should be looked at carefully.
In order to be able to go back to a working copy if your code is too hard to fix, try making a copy of the project at some point where you code works and you are now planning on doing some major changes.
If you need clarification on what the code is doing, email me to talk to me during office hours and I can explain the code to you.

Extra Credit (worth 5 extra project points each)

  1. Make it so that the ants can have their own individual rules. This involves moving the rules into the Ant class. Allow the user to change this rule for each ant on the board. This can be done by adding a dialog box with all ants and their rulestrings and allowing the user to change them. Make sure the rules all have the same number of states or you will have a problem with the program crashing when an ant tries to access a nonexisting rule.
  2. Right now the program has a unresizable window. This is because the board class doesn’t resize cells with the window. Try coming up with a way to resize the board when the window resizes. You can use binding but this might not be very useful (each cell would need to be bind). Also remember to change the cellSize field in the Board class as well as the size field in all CellPane objects.