Results 1 to 3 of 3

Thread: Langton ant

  1. #1
    d a r i u s is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Langton ant

    Can anyone help me out? I'm not to sure where to start.

    Java Code:
    package Core;
    
    public enum Direction {
    	NORTH, SOUTH, EAST, WEST;
    }
    Java Code:
    package Langton;
    
    import Simulation.Ant;
    import Simulation.Grid;
    import Simulation.Simulation;
    import Core.Direction;
    
    public class LangtonAnt {
    	/**
    	 * Main method.
    	 * 
    	 * You do not need to modify this method. It will not be part of the
    	 * automated or hand marking
    	 * 
    	 */
    	public static void main(String[] args) {
    		// create a simulation with given parameters
    		Simulation sim = new Simulation(10, 10, 5, 5, Direction.NORTH, 10);
    		// while the simulation is not finished
    		while (!sim.isCompleted()) {
    			// get current board
    			Grid board = sim.cloneCurrentGrid();
    			// get current ant
    			Ant ant = sim.cloneCurrentAnt();
    
    			// print out the ant and board
    			System.out.print("+");
    			for (int j = 0; j < board.getWidth(); ++j) {
    				System.out.print("-");
    			}
    			System.out.println("+");
    
    			for (int i = 0; i < board.getHeight(); ++i) {
    				System.out.print("|");
    				for (int j = 0; j < board.getWidth(); ++j) {
    					if (i == ant.getIPos() && j == ant.getJPos()) {
    						switch (ant.getDirection()) {
    						case NORTH:
    							System.out.print("^");
    							break;
    						case SOUTH:
    							System.out.print("V");
    							break;
    						case EAST:
    							System.out.print(">");
    							break;
    						case WEST:
    							System.out.print("<");
    							break;
    						}
    					} else {
    						if (board.isWhite(i, j)) {
    							System.out.print(" ");
    						} else {
    							System.out.print("*");
    						}
    					}
    				}
    				System.out.println("|");
    			}
    
    			System.out.print("+");
    			for (int j = 0; j < board.getWidth(); ++j) {
    				System.out.print("-");
    			}
    			System.out.println("+");
    
    			/*
    			 * make the computer wait 150 ms to make it easier to view the ant's
    			 * progression
    			 */
    			try {
    				Thread.sleep(150);
    			} catch (InterruptedException e) {
    				// do nothing
    			}
    
    			// execute a step
    			sim.executeStep();
    			// buffer to make it easier to read
    			System.out.println();
    		}
    	}
    }
    Java Code:
    package Simulation;
    
    /**
     * 
     * Ant class
     * 
     * @author _________
     *
     */
    public class Ant {
    	
    	public Ant(int iPos, int jPos, Direction direction){
    		// TODO fill in this constructor
    	}
    	
    	public int getIPos() {
    		// TODO fill in this method
    	}
    	protected void setIPos(int iPos) {
    		// TODO fill in this method
    	}
    	public int getJPos() {
    		// TODO fill in this method
    	}
    	protected void setJPos(int jPos) {
    		// TODO fill in this method
    	}
    	public Direction getDirection() {
    		// TODO fill in this method
    	}
    	protected void setDirection(Direction direction) {
    		// TODO fill in this method
    	}
    	
    }
    Java Code:
    package Simulation;
    
    /**
     * 
     * Grid class
     * 
     * @author _________
     *
     */
    public class Grid {
    
    	boolean[][] grid;
    
    	/**
    	 * Constructor for the grid.
    	 * 
    	 * Initial state is that the every cell in the grid is white
    	 * 
    	 * @param height - height of the grid
    	 * @param width - width of the grid
    	 */
    	public Grid(int height, int width) {
    		// TODO fill in this constructor
    	}
    
    	public int getHeight() {
    		// TODO fill in this method
    	}
    
    	public int getWidth() {
    		// TODO fill in this method
    
    	}
    
    	public boolean isWhite(int i, int j) {
    		// TODO fill in this method
    	}
    
    	protected void setWhite(int i, int j) {
    		// TODO fill in this method
    	}
    
    	protected void setBlack(int i, int j) {
    		// TODO fill in this method
    	}
    }
    Java Code:
    package Simulation;
    
    /**
     * 
     * Simulation class
     * 
     * @author _________
     *
     */
    public class Simulation {
    
    	/**
    	 * Initialize instance variables in this constructor
    	 * 
    	 * @param height - the height of the grid
    	 * @param width - the width of the grid
    	 * @param antStartI - the original I coordinate of the ant
    	 * @param antStartJ - the original J coordinate of the ant
    	 * @param originalDirection - the original direction the ant is facing
    	 */
    	public Simulation(int height, int width, int antStartI, int antStartJ,
    			Direction originalDirection, int maxTimeSteps) {
    		// TODO fill in this constructor
    	}
    
    	/**
    	 * Execute a time step for the simulation.
    	 * 
    	 * The ant must:
    	 * 		* move forward 1 space
    	 * 			- if this movement would cause it to move off the grid, 
    	 * 				the simulation is completed.
    	 * 		* rotate depending on the state of the cell the ant is occupying
    	 * 			- if the cell is white, rotate left
    	 * 			- otherwise, rotate right
    	 * 		* change the state of the cell the ant is currently occupying
    	 * 			- if the cell is white, it becomes black
    	 * 			- otherwise, it becomes white
    	 * 
    	 * NOTE: this method should do nothing if the simulation is completed.
    	 */
    	public void executeStep() {
    		// TODO fill in this method
    	}
    
    	/**
    	 * Method to check if the simulation is completed.
    	 * 
    	 * The simulation is completed if and only if:
    	 * 		* it has reached the maximum time steps allowed
    	 * 		* the ant has moved off the grid
    	 * 
    	 * @return true - the simulation is completed
    	 * @return false - the simulation is not completed
    	 */
    	public boolean isCompleted() {
    		// TODO fill in this method
    	}
    
    	/**
    	 * Method to return a copy of the current grid.
    	 * 
    	 * You should always return a copy of an object if you do not
    	 * want your base object to be changed by any code calling this method.
    	 * 
    	 * @return a clone of the grid.
    	 */
    	public Grid cloneCurrentGrid() {
    		// TODO fill in this method
    	}
    
    	/**
    	 * Method to return a copy of the current ant.
    	 * 
    	 * You should always return a copy of an object if you do not
    	 * want your base object to be changed by any code calling this method.
    	 * 
    	 * NOTE: Do not canche this value, return a new object for every call.
    	 * 
    	 * @return a clone of the ant.
    	 */
    	public Ant cloneCurrentAnt() {
    		// TODO fill in this method
    	}
    }
    Last edited by d a r i u s; 04-21-2013 at 01:09 PM.

  2. #2
    d a r i u s is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: Langton ant

    bump

  3. #3
    d a r i u s is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •