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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 42

Thread: Lagnton Ant

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Lagnton Ant

    Hi I am a begginner in java and have an assignment to simulate a Langton Ant. Would like some help where to start.

    package simulation;
     
    /**
     * 
     * Simulation class
     * 
     *
     */
    public class Simulation {
     
    	public int height ;
    	public int width;
    	public int antStartI;
    	public int antStartJ;
    	public String originalDirection;
     
    	 /* @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) {
     
    		height = height;
    		width = width;
    		antStartI = antStartI;
    		antStartJ = antStartJ;
    		originalDirection = originalDirection;
     
    	}
     
    	/**
    	 * 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() {
    		switch (direction) {
    		case NORTH:
    			;
    			break;
    		case SOUTH:
    			;
    			break;
    		case EAST:
    			;
    			break;
    		case WEST:
    			;
    			break;
    		}
    	}
     
    	/**
    	 * 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
    	}
    }

    I tried to write execute step but have know idea of hw to set the ant's next position regarding its direction. Any ideas?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Lagnton Ant

    hw to set the ant's next position regarding its direction. Any ideas?
    Can you explain? What is its position: x,y?
    What is its direction: north, east, south, west?
    For example: If the ant is at x,y and it is facing east, what should happen?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Lagnton Ant

    Sorry for the lack of information
    The original position is 5,5
    Direction is
     
    package core;
     
    public enum Direction {
    	NORTH, SOUTH, EAST, WEST;
    }


    The rules are:
    1 - At each cell, swap the colour from black to white or white to black
    2 - If the new colour is white then turn left; else turn right
    3 - Move forward one space
    4 - If you reach a boundary, exit the grid and stop.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Lagnton Ant

    Ok, now what should the program do?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Lagnton Ant

    This is the program

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

    and I have to fullfill the simulation and grid regarding the rules on post 3

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Lagnton Ant

    What are your problems with the posted code? Do you have any specific questions?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Lagnton Ant

    I am starting with simulation on post 1, trying to do it by parts because I am really confused. I tried to write execute step, but I can't figure out how to make the ant move one step. I put switch inside it, but I don't know if it is the best approach and still I have no idea of how to program the ant to move one step regarding its direction.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Lagnton Ant

    how to make the ant move one step.
    Is the ant in a 2 dim array of sqaures?
    Would a move be to an adjacent square?
    Would there be 4 possible squares to move to (barring a current location at an edge which would reduce the possible next squares)?
    What rules or conditions determine which of the 4 adjacent squares the ant can move to?

    An example move from location 3,4 in the North(up) direction would be to location 3,3 (y-- to move up)
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Lagnton Ant

    The idea is that the ant is in a 2 dim array, but no array is declared

    I don't understand this question:

    Would a move be to an adjacent square?
    Would there be 4 possible squares to move to (barring a current location at an edge which would reduce the possible next squares)?

    The rule that determine which cell the ant will go to is the direction. There are other rules like changing the color of the cell the ant is in and when it arrives next cell, if this cell is white it should turn left, else turn right. But I am trying first to write execute step on post 1. I just don't know how to refer to the ant next position since there are only the variables antstarti and antstarj. I don't understand which variables determine the next position of the ant..

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Lagnton Ant

    I assume the ant is located in a square at a row and column in an array.
    I was asking what squares are adjacent to a given square? Just the 4 that are above, right, below and left of the current square.

    how to refer to the ant next position
    Assuming that the ant is not at an edge of the array and its location is row, col then change the row or col values this way
    move up: row--
    move right: col++
    move down: row++
    move left: col--

    If the ant is at an edge, the move in that direction would take the ant out of the array.

    With the variables: antstarti and antstarj
    I don't know which is the row and which is the column.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Lagnton Ant

    I have to fullfill with code a package called simulation with 3 classes. The first is in post 1

    Tha seconde is

     
    package simulation;
     
    /**
     * 
     * Ant class
     * 
     *
     */
    public class Ant {
     
    	 int iPos;
         int jPos;
         String direction;
     
     
    	public Ant(int iPos, int jPos, Direction direction){
     
    		iPos=iPos;
    		jPos=jPos;
    	    direction = direction;
     
    	}
     
    	public int getIPos() {
    		iPos = 
    		return iPos;
    	}
    	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
    	}
     
    }

    And the last one:

    package simulation;
     
    /**
     * 
     * Grid class
     * 
     * 
     *
     */
    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
    	 */
     
    	int height;
    	int width;
     
    	public Grid(int height, int width) {
     
    	}
     
    	public int getHeight() {
    		height = height;
    	return height;	
    	}
     
    	public int getWidth() {
    		width=width;
    		return width;
     
    	}
     
    	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
    	}
    }


    Any advice where to start?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Lagnton Ant

    What is the current problem you are working on? Ask a specific question.
    Have you done any analysis of the problem and written a list of the steps the program needs to do to solve the problem? What items on the list are coded and solved?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Lagnton Ant

    It could go Right, Left, Below and Up.

    I know how to change rows and columns values, I just don't understan which variables refer to column and row in this code since I am supposed to follow what is already written. I just see variables to the ants original position. AntStartI is the row and AntStartJ is the column the ant is.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Lagnton Ant

    AntStartI is the row and AntStartJ is the column the ant is.
    Those variables name have "Start" in them which could mean that they are the initial values and would not be used to keep track of the ant's current location which could be kept in variables: antRow and antCol for the rest of the code.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Lagnton Ant

    Thanks.

    But how to set this variables to the ants new position.

    to execute step i wrote:

    	public void executeStep() {
    		switch (originalDirection) {
    		case NORTH:
    			int antrow=antStartI+1;
    			break;
    		case SOUTH:
    			antrow = antStartI-1;
    			break;
    		case EAST:
    			antcolumn=antStartJ+1;
    			break;
    		case WEST:
    			antcolumn = antStartJ-1;
    			break;
    		}
    	}

    But how to declare antrow ant antcolumn are the position of the ant after its original positions?
    Last edited by Norm; April 19th, 2013 at 08:25 PM. Reason: fixed code tag changed [ to ]

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Lagnton Ant

    You can't always use the start position to determine the next position. You need to use the current position.
    	int antrow=antStartI+1;
    The current position variables need to be defined at the class level, not inside of the case statements.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Lagnton Ant

    Now I defined it at the class level:
    	public void executeStep() {
    		switch (originalDirection) {
    		case NORTH:
    			antrow=antcurrentI+1;
    			break;
    		case SOUTH:
    			antrow = antcurrentI-1;
    			break;
    		case EAST:
    			antcolumn = antcurrentJ+1;
    			break;
    		case WEST:
    			antcolumn = antcurrentJ-1;
    			break;
    		}
    	}
    But how do I initialise antcurrentI and antcurrentJ? I mean, how do I define what they are?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Lagnton Ant

    The case statements should update the current position. What's coded in the case statements doesn't change the current row. The moves should change the current position by 1 in the appropriate direction.
    For example:
    row--; // move east

    The initial value of row would come from the value of the antStartI variable, if i is the index for a row.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Lagnton Ant

    public void executeStep() {
    		switch (originalDirection) {
    		case NORTH:
    			antcurrentI++;
    			break;
    		case SOUTH:
    			antcurrentJ++;
    			break;
    		case EAST:
    			antcurrentJ++;
    			break;
    		case WEST:
    			antcurrentJ++;
    			break;
    		}
    	}

    Where is the correct place to initialise and declare:

    antcurrentI=antStartI;
    antcurrentJ=antStartJ;

    ?

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Lagnton Ant

    Where is the correct place to initialise and declare:
    Do it where the execution starts, like in the constructor.

    Do you live south of the equator and orient your maps with the South pole at the top?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Lagnton Ant

    In:
    public void executeStep() {
    		switch (originalDirection) {
    		case NORTH:
    			antcurrentI++;
    			break;
    		case SOUTH:
    			antcurrentJ++;
    			break;
    		case EAST:
    			antcurrentJ++;
    			break;
    		case WEST:
    			antcurrentJ++;
    			break;
    		}
    	}

    eclipse keeps showing error: North, South, East an West cannot be resolved in variables.

    North, South, East and West are in another package in a class called Direction. Does it mean i cannot use switch in this case?

    Sorry, I did not understand this question:
    Do you live south of the equator and orient your maps with the South pole at the top?

  22. #22
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Lagnton Ant

    No need to answer my last question as I already imported Direction. I am trying to stub methods just to have the program running first. But I am having a problem.

    package simulation;
     
    import core.Direction;
     
     
    /**
     * 
     * Ant class
     * 
     *
     */
    public class Ant {
     
    	public int iP;
         public int jP;
         public Direction d;
     
     
     
    	public Ant(int iPos, int jPos, Direction direction){
     
    		iP=iPos;
    		jP=jPos;
    	    d = direction;
     
    	}
     
    	public int getIPos() {
    		return 0;
    	}
    	protected void setIPos(int iPos) {
     
    	}
    	public int getJPos() {
    		return 10;
    	}
    	protected void setJPos(int jPos) {
    		// TODO fill in this method
    	}
    	public Direction getDirection() {
    		return EAST;
    	}
    	protected void setDirection(Direction direction) {
     
    	}
     
    }

    return EAST; keeps showing error even thought I have already imported Direction. It says:
    The member enum EAST can only be defined inside a top-level class or interface
    Syntax error, insert "EnumBody" to complete BlockStatements


    How can I fix it?

    --- Update ---


  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Lagnton Ant

    If EAST worked in post#21, what is different in the code in post#22.

    What should the getDirection() method return? Why always EAST?

    Suggestion: add a main() method for testing the constructor and methods. Create an instance of the Ant class, call its methods and print out the results as appropriate.

    Sorry, I did not understand this question:
    Do you live south of the equator and orient your maps with the South pole at the top?
    On my map North is towards the top. To go north, the row would be decremented. The posted code increments it. See post#10
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Lagnton Ant

    21 is in another class.

    Here is the one the is not working:
    package simulation;
     
    import core.Direction;
     
     
    /**
     * 
     * Ant class
     * 
     *
     */
    public class Ant {
     
    	public int iP;
         public int jP;
         public Direction d;
     
     
     
    	public Ant(int iPos, int jPos, Direction direction){
     
    		iP=iPos;
    		jP=jPos;
    	    d = direction;
     
    	}
     
    	public int getIPos() {
    		return 0;
    	}
    	protected void setIPos(int iPos) {
     
    	}
    	public int getJPos() {
    		return 10;
    	}
    	protected void setJPos(int jPos) {
    		// TODO fill in this method
    	}
    	public Direction getDirection() {
    		return EAST;
    	}
    	protected void setDirection(Direction direction) {
     
    	}
     
    }

    I don't understand because I have imported Direction.

    It is not always East. I just stud it, I am trying to run it before so I can write and then run again to see what the code is printing so it is easier to code what the assignment requires. So I am just returning the type required in each method.(returning anything)

    The main method is constructed already (post#5)

    You are right, I will correct it.

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Lagnton Ant

    The question was, how did code in post#21 work without an error when it referenced EAST.
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Error in ant files
    By workforsiva in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 17th, 2013, 08:38 AM
  2. Java Ant
    By dsavatar in forum Java Theory & Questions
    Replies: 2
    Last Post: December 6th, 2012, 09:29 AM
  3. Ant Colony Simulator
    By KevinWorkman in forum The Cafe
    Replies: 4
    Last Post: December 14th, 2011, 01:39 PM
  4. Ant Executes and Stops After a Certain Task
    By DanielPros in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 9th, 2011, 10:46 AM
  5. Reading ant output when exec the command
    By cipm66 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: October 23rd, 2010, 01:48 PM