Package microsim.space.turtle
Class Turtle
java.lang.Object
microsim.space.SpacePosition
microsim.space.turtle.AbstractTurtle
microsim.space.turtle.Turtle
- All Implemented Interfaces:
Serializable,Comparable<SpacePosition>
An agent is able to move itself upon an object grid. It has got some specific instruction for movement. Each turtle
has a heading expressed in degrees. It can make steps, turn right or left, measure the distance from another position
on the grid. Each turtle has a color, and it is able to draw itself on a grid drawing layer.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class microsim.space.turtle.AbstractTurtle
AbstractTurtle.Direction, AbstractTurtle.MoveMode -
Field Summary
Fields inherited from class microsim.space.turtle.AbstractTurtle
grid, movingFields inherited from class microsim.space.SpacePosition
x, y -
Constructor Summary
ConstructorsConstructorDescriptionTurtle()Turtle(@NonNull ObjectSpace grid) Create a turtle on the given grid at position (0,0).Turtle(@NonNull ObjectSpace grid, int x, int y) Create a turtle on the given grid at the given position. -
Method Summary
Modifier and TypeMethodDescriptionvoidforward(int steps) Make some steps forward, according the current heading.intReturns the current heading of the turtle.intgetNextX(int steps) Return the newxcoordinate walking the current heading direction for given steps.intgetNextY(int steps) Return the newycoordinate walking the current heading direction for given steps.booleanleap(int steps) Make some steps forward, but moves only if the target position is empty.voidsetCardinalHeading(@NonNull AbstractTurtle.Direction directionType) Set the turtle's heading using a constant for cardinal points.voidsetHeading(int heading) Set the current heading.voidRandomize the turtle's heading.voidsetXY(int x, int y) Set a new position.voidturnCardinalLeft(int steps) voidturnCardinalRight(int steps) voidturnLeft(int degrees) Change the current heading rotating it to the left.voidturnRight(int degrees) Change the current heading rotating it to the right.Methods inherited from class microsim.space.turtle.AbstractTurtle
forward, getDistanceFrom, getGrid, getMovingType, getNextX, getNextY, getPosition, getWorldHeight, getWorldWidth, leap, setGrid, setIfEmptyXY, setMovingType, setX, setYMethods inherited from class microsim.space.SpacePosition
compareTo, equals, toString
-
Constructor Details
-
Turtle
public Turtle() -
Turtle
Create a turtle on the given grid at position (0,0).- Parameters:
grid- The grid upon the turtle moves.
-
Turtle
Create a turtle on the given grid at the given position.- Parameters:
x- The initial x coordinate of the turtle.y- The initial y coordinate of the turtle.grid- The grid upon the turtle moves.
-
-
Method Details
-
getHeading
public int getHeading()Description copied from class:AbstractTurtleReturns the current heading of the turtle.- Specified by:
getHeadingin classAbstractTurtle- Returns:
- The current heading.
-
setHeading
public void setHeading(int heading) Set the current heading.- Specified by:
setHeadingin classAbstractTurtle- Parameters:
heading- The new heading.
-
setRandomHeading
public void setRandomHeading()Randomize the turtle's heading. It gets a random number from 0 to 359 degrees.- Specified by:
setRandomHeadingin classAbstractTurtle
-
setCardinalHeading
Set the turtle's heading using a constant for cardinal points.- Specified by:
setCardinalHeadingin classAbstractTurtle- Parameters:
directionType- One of DIR_NORTH, DIR_NORTH_EAST, ... constant.
-
turnCardinalLeft
public void turnCardinalLeft(int steps) - Specified by:
turnCardinalLeftin classAbstractTurtle
-
turnCardinalRight
public void turnCardinalRight(int steps) - Specified by:
turnCardinalRightin classAbstractTurtle
-
turnRight
public void turnRight(int degrees) Change the current heading rotating it to the right.- Specified by:
turnRightin classAbstractTurtle- Parameters:
degrees- The number of degrees to rotate the heading.
-
turnLeft
public void turnLeft(int degrees) Change the current heading rotating it to the left.- Specified by:
turnLeftin classAbstractTurtle- Parameters:
degrees- The number of degrees to rotate the heading.
-
forward
public void forward(int steps) Make some steps forward, according the current heading.- Specified by:
forwardin classAbstractTurtle- Parameters:
steps- The number of steps the turtle has to make.
-
leap
public boolean leap(int steps) Make some steps forward, but moves only if the target position is empty.- Specified by:
leapin classAbstractTurtle- Parameters:
steps- The number of steps the turtle has to make.- Returns:
- True only if the turtle has moved.
-
setXY
public void setXY(int x, int y) Set a new position.- Overrides:
setXYin classAbstractTurtle- Parameters:
x- The newxcoordinate.y- The newycoordinate.
-
getNextX
public int getNextX(int steps) Return the newxcoordinate walking the current heading direction for given steps. It uses the grid bound checking methods according to the turtle's current moving type.- Specified by:
getNextXin classAbstractTurtle- Parameters:
steps- The number of steps to move forward.- Returns:
- The candidate
xcoordinate.
-
getNextY
public int getNextY(int steps) Return the newycoordinate walking the current heading direction for given steps. It uses the grid bound checking methods according to the turtle's current moving type.- Specified by:
getNextYin classAbstractTurtle- Parameters:
steps- The number of steps to move forward.- Returns:
- The candidate
ycoordinate.
-