I have implemented a simulation which is called "Langton's Ant" with Java. Here is a short summary of basic rules:
A ant is placed in a 2D matrix and looks to north, west, east or south. First, all cells are colored white.
If the ant is on a white cell, the cell is colored black and the ant turns 90 degrees right and moves to the next cell in this direction.
If the ant is on a black cell, the cell is colored white and the ant turns 90 degrees left and moves to the next cell in this direction.
A white cell is represented by a 0 in array and a black cell is represented by a 1 in array.
Ant:
public class Ant { private Direction direction; private int positionX, positionY; private World world = new World(); private final int steps = 10000; public Ant(int positionX, int positionY, Direction direction) { this.positionX = positionX; this.positionY = positionY; this.direction = direction; } public int getSteps() { return steps; } public int getPositionX() { return this.positionX; } public int getPositionY() { return this.positionY; } public void setDirection(Direction direction) { this.direction = direction; } public Direction getDirection() { return this.direction; } public boolean inWorld(Ant a) { if(a.getPositionX()<=world.getWorldSize()-2 && a.getPositionY()<=world.getWorldSize()-2 && a.getPositionX()>=1 && a.getPositionY()>=1) { return true; } return false; } public boolean nextStep(int w[][],Ant a) { if(inWorld(a) == true) { if(w[a.getPositionX()][a.getPositionY()]==0 && a.getDirection()==Direction.North) { a.setDirection(Direction.East); w[a.getPositionX()][a.getPositionY()]=1; a.positionY--; } if(w[a.getPositionX()][a.getPositionY()]==0 && a.getDirection()==Direction.East) { a.setDirection(Direction.South); w[a.getPositionX()][a.getPositionY()]=1; a.positionX++; } if(w[a.getPositionX()][a.getPositionY()]==0 && a.getDirection()==Direction.South) { a.setDirection(Direction.West); w[a.getPositionX()][a.getPositionY()]=1; a.positionY++; } if(w[a.getPositionX()][a.getPositionY()]==0 && a.getDirection()==Direction.West) { a.setDirection(Direction.North); w[a.getPositionX()][a.getPositionY()]=1; a.positionX--; } if(w[a.getPositionX()][a.getPositionY()]==1 && a.getDirection()==Direction.North) { a.setDirection(Direction.West); w[a.getPositionX()][a.getPositionY()]=0; a.positionY++; } if(w[a.getPositionX()][a.getPositionY()]==1 && a.getDirection()==Direction.East) { a.setDirection(Direction.North); w[a.getPositionX()][a.getPositionY()]=0; a.positionX--; } if(w[a.getPositionX()][a.getPositionY()]==1 && a.getDirection()==Direction.South) { a.setDirection(Direction.East); w[a.getPositionX()][a.getPositionY()]=0; a.positionY--; } if(w[a.getPositionX()][a.getPositionY()]==1 && a.getDirection()==Direction.West) { a.setDirection(Direction.South); w[a.getPositionX()][a.getPositionY()]=0; a.positionX++; } return true; } return false; } }
Direction:
public enum Direction { North, East, South, West }
World:
public class World { private final int worldSize =400; private int world[][] = new int[worldSize][worldSize]; public int[][] getWorld() { return world; } public int getWorldSize() { return worldSize; } }
AntFrame:
public class AntFrame { private Ant a = new Ant(50, 50, Direction.South); private World w = new World(); private int world[][] = w.getWorld(); private int steps = a.getSteps(); private Timer timer = new Timer(); private JFrame f = new JFrame(); private int worldSize = w.getWorldSize(); private AntPanel antPanel = new AntPanel(); public AntFrame() { f.setSize(600, 600); f.setTitle("Langtons Ant"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new FlowLayout()); f.add(antPanel); f.setVisible(true); world[50][50] =1; a.setDirection(Direction.South); timer.schedule(new SimulationThread(), 400, 5); } class SimulationThread extends TimerTask { private int tmp =0; @Override public void run() { if(a.nextStep(world,a)==true) { a.nextStep(world, a); antPanel.repaint(); tmp = tmp+1; } else { timer.cancel(); } if(tmp==steps) { timer.cancel(); } } } class AntPanel extends JPanel { public AntPanel() { setPreferredSize(new Dimension(600, 600)); setBackground(Color.WHITE); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); for (int i = 0; i < world.length; i++) { for (int j = 0; j < world.length; j++) { if (world[i][j] == 1) { g.setColor(Color.BLACK); g.fillRect(i * 3, j * 3, 3, 3); } } } } } public static void main(String[] args) { new AntFrame(); } }
It would be really nice if someone could give me feedback.
Ant.nextStep()
,if(w[a.getPositionX()][a.getPositionY()]==0 && a.getDirection()==Direction.North) a.setDirection(Direction.West);
, says that if the ant is on a white cell, it turns left. This is in contradiction to your specification.\$\endgroup\$