Package microsim.space.turtle
Class DigitalTurtle
java.lang.Object
microsim.space.SpacePosition
microsim.space.turtle.AbstractTurtle
microsim.space.turtle.DigitalTurtle
- All Implemented Interfaces:
Serializable
,Comparable<SpacePosition>
- 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, moving
Fields inherited from class microsim.space.SpacePosition
x, y
-
Constructor Summary
ConstructorDescriptionCreate a turtle at position (0,0).DigitalTurtle
(@NonNull ObjectSpace grid) Create a turtle on the given grid at position (0,0).DigitalTurtle
(@NonNull ObjectSpace grid, int x, int y) Create a turtle on the given grid at the given position. -
Method Summary
Modifier and TypeMethodDescriptionvoid
forward
(int steps) Makes some steps forward, according the current heading.int
Returns the current heading of the turtle.int
getNextX
(int steps) Returns the newx
coordinate walking the current heading direction for given steps.int
getNextY
(int steps) Return the newy
coordinate walking the current heading direction for given steps.boolean
leap
(int steps) Makes some steps forward, but moves only if the target position is empty.void
setCardinalHeading
(@NonNull AbstractTurtle.Direction directionType) Sets the turtle's heading using a constant for cardinal points.void
setHeading
(int heading) Sets the current heading.void
Randomizes the turtle's heading.void
turnCardinalLeft
(int steps) void
turnCardinalRight
(int steps) void
turnLeft
(int degrees) Changes the current heading rotating it to the left.void
turnRight
(int degrees) Changes 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, setXY, setY
Methods inherited from class microsim.space.SpacePosition
compareTo, equals, toString
-
Field Details
-
heading
-
-
Constructor Details
-
DigitalTurtle
public DigitalTurtle()Create a turtle at position (0,0). -
DigitalTurtle
Create a turtle on the given grid at position (0,0).- Parameters:
grid
- The grid upon the turtle moves.
-
DigitalTurtle
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
-
turnLeft
public void turnLeft(int degrees) Description copied from class:AbstractTurtle
Changes the current heading rotating it to the left.- Specified by:
turnLeft
in classAbstractTurtle
- Parameters:
degrees
- The number of degrees to rotate the heading.
-
turnCardinalLeft
public void turnCardinalLeft(int steps) - Specified by:
turnCardinalLeft
in classAbstractTurtle
-
setRandomHeading
public void setRandomHeading()Description copied from class:AbstractTurtle
Randomizes the turtle's heading. It gets a random number from 0 to 359 degrees. The random generator is synchronized with JAS randomizer.- Specified by:
setRandomHeading
in classAbstractTurtle
-
getHeading
public int getHeading()Description copied from class:AbstractTurtle
Returns the current heading of the turtle.- Specified by:
getHeading
in classAbstractTurtle
- Returns:
- The current heading.
-
setHeading
public void setHeading(int heading) Description copied from class:AbstractTurtle
Sets the current heading.- Specified by:
setHeading
in classAbstractTurtle
- Parameters:
heading
- The new heading.
-
forward
public void forward(int steps) Description copied from class:AbstractTurtle
Makes some steps forward, according the current heading.- Specified by:
forward
in classAbstractTurtle
- Parameters:
steps
- The number of steps the turtle has to make.
-
leap
public boolean leap(int steps) Description copied from class:AbstractTurtle
Makes some steps forward, but moves only if the target position is empty.- Specified by:
leap
in classAbstractTurtle
- Parameters:
steps
- The number of steps the turtle has to make.- Returns:
- True only if the turtle has moved.
-
turnRight
public void turnRight(int degrees) Description copied from class:AbstractTurtle
Changes the current heading rotating it to the right.- Specified by:
turnRight
in classAbstractTurtle
- Parameters:
degrees
- The number of degrees to rotate the heading.
-
turnCardinalRight
public void turnCardinalRight(int steps) - Specified by:
turnCardinalRight
in classAbstractTurtle
-
getNextX
public int getNextX(int steps) Description copied from class:AbstractTurtle
Returns the newx
coordinate 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:
getNextX
in classAbstractTurtle
- Parameters:
steps
- The number of steps to move forward.- Returns:
- The candidate
x
coordinate.
-
setCardinalHeading
Description copied from class:AbstractTurtle
Sets the turtle's heading using a constant for cardinal points.- Specified by:
setCardinalHeading
in classAbstractTurtle
- Parameters:
directionType
- One of DIR_NORTH, DIR_NORTH_EAST, ... constant.
-
getNextY
public int getNextY(int steps) Description copied from class:AbstractTurtle
Return the newy
coordinate 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:
getNextY
in classAbstractTurtle
- Parameters:
steps
- The number of steps to move forward.- Returns:
- The candidate
y
coordinate.
-