Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Langton's Ant. Can anybody complete it?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Langton's Ant. Can anybody complete it?

    package langton;

    public class LangtonAnt{

    import simulation.Ant;
    import simulation.Grid;
    import simulation.Simulation;
    import core.Direction;


    /**
    * 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();
    }
    }
    }

    }


  2. #2
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    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
    }
    }

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    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
    }
    }

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    package core;

    public enum Direction {
    NORTH, SOUTH, EAST, WEST;
    }

    --- Update ---

    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
    }

    }

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    here is the additional information


    package core;

    public enum Direction {
    NORTH, SOUTH, EAST, WEST;
    }

    --- Update ---

    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
    }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    Please ask some specific questions about the code and the problems you are having.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Where are the packages and classes referenced in the import statements?

    --- Update ---

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    <package langton;
     
    public class LangtonAnt{
     
    import simulation.Ant;
    import simulation.Grid;
    import simulation.Simulation;
    import core.Direction;
     
     
    /**
    * 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();
    }
    }
    }
     
    } >


    --- Update ---

    <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
    }
    } >

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    Please ask some specific questions about the code and the problems you are having.

    The posted code needs formatting. Nested statements should be indented 3-4 spaces.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    <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
    }
    } >

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    Please ask some specific questions about the code and the problems you are having.

    The posted code in post#7 needs formatting. Nested statements should be indented 3-4 spaces.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    Basically i have been given a template for the code that i need to complete, in the end it should out put this:

    Description: Write a program to simulate an “ant” moving one step at a time on a grid of coloured cells, with rules to govern at each cell which direction to turn and what colour-change to make to the cell. The rules are:
    1 At each cell, swap the colour from L to R or R to L;
    2 If the new colour is L then turn left; else turn right;
    3 Move forward one space
    4 If you reach a boundary, exit the grid
    and stop.

    --- Update ---

    sorry, im really new at this so im not sure how to format my code according to convention :S

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    Make a list of the steps the code must take to solve the problem. Start with the first one.
    Code it, compile it, fix the errors, execute it, fix the errors.
    When it executes without errors move to the next step one the list.

    how to format my code according to convention
    Code Conventions for the Java Programming Language: Contents

    Or look at many of the other threads with posted code.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    <package langton;
     
    public class LangtonAnt{
     
    	import simulation.Ant;
    	import simulation.Grid;
    	import simulation.Simulation;
    	import core.Direction;
     
     
    		/**
    		 * 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();
    			}
    		}
    	}
     
    }>

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    That looks a lot better. One small problem is the excessive spaces for the indentions. 3-4 is enough.
    That probably because your IDE uses tabs instead of spaces and the tabs on the forum are bigger.


    Now post the list of steps you made that describes what the program needs to do to solve the problem, mark which ones you have finished
    and state what the next step is that you are working on and ask any questions that you have about coding that step.

    I'm done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    do you know how to write the section of code under : public void executeStep() ????

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    What have you tried?
    Define what the method is supposed to do. Make a list of steps it needs to do and try coding them one at a time.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    i have been trying that for about the last 4 hours, i really have no idea how to do it. I honest dont understand the logic a code follows. Can you PLEASE help????
    Basically
    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.
    */

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    What step are you working on? What problems are you having with that step?
    Take the first step: move forward 1 space
    That means there must be a direction for "forward" and a location.
    To move in any of the 4 directions, change either the row or column value of the current location. For example to move up, subtract 1 from the row.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    I know i have to do that but i dont know how to write it out so that i dont get any errors

    --- Update ---

    can you please provide an example as to what my code should look like?

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Langton's Ant. Can anybody complete it?

    Post the code with the errors and the error messages.

    if direction=east
    col++
    else if direction=south
    row++
    else if direction=west
    col--
    else if direction=north
    row--
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Lagnton Ant
    By dianac in forum Object Oriented Programming
    Replies: 41
    Last Post: April 21st, 2013, 08:01 AM
  2. Error in ant files
    By workforsiva in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 17th, 2013, 08:38 AM
  3. Replies: 4
    Last Post: April 16th, 2013, 08:06 AM
  4. Java Ant
    By dsavatar in forum Java Theory & Questions
    Replies: 2
    Last Post: December 6th, 2012, 09:29 AM
  5. Ant Colony Simulator
    By KevinWorkman in forum The Cafe
    Replies: 4
    Last Post: December 14th, 2011, 01:39 PM

Tags for this Thread