Package microsim.space.turtle
Class AbstractTurtle
java.lang.Object
microsim.space.SpacePosition
microsim.space.turtle.AbstractTurtle
- All Implemented Interfaces:
Serializable
,Comparable<SpacePosition>
- Direct Known Subclasses:
DigitalTurtle
,Turtle
An agent 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
Modifier and TypeClassDescriptionstatic enum
static enum
-
Field Summary
Fields inherited from class microsim.space.SpacePosition
x, y
-
Constructor Summary
ConstructorDescriptionCreates a turtle with a given identifier on the given grid at position (0,0).AbstractTurtle
(@NonNull ObjectSpace grid) Creates a turtle on the given grid at position(0,0).AbstractTurtle
(@Nullable ObjectSpace grid, int x, int y) Creates a turtle with a given identifier on the given grid at the given position. -
Method Summary
Modifier and TypeMethodDescriptionvoid
forward()
Makes a step forward, according the current heading.abstract void
forward
(int steps) Makes some steps forward, according the current heading.double
getDistanceFrom
(int xCor, int yCor) Computes the cartesian distance from its position to the given position.@Nullable ObjectSpace
getGrid()
abstract int
Returns the current heading of the turtle.@NonNull AbstractTurtle.MoveMode
Returns the behaviour of the turtle when it goes out of bounds of the grid.int
getNextX()
Returns the newx
coordinate walking the current heading direction for 1 step.abstract int
getNextX
(int steps) Returns the newx
coordinate walking the current heading direction for given steps.int
getNextY()
Return the newy
coordinate walking the current heading direction for 1 step.abstract int
getNextY
(int steps) Return the newy
coordinate walking the current heading direction for given steps.@NonNull SpacePosition
int
Returns they
size of the grid.int
Returns thex
size of the grid.boolean
leap()
Makes a step forward, but moves only if the target position is empty.abstract boolean
leap
(int steps) Makes some steps forward, but moves only if the target position is empty.abstract void
setCardinalHeading
(@NonNull AbstractTurtle.Direction directionType) Sets the turtle's heading using a constant for cardinal points.void
setGrid
(@Nullable ObjectSpace grid) abstract void
setHeading
(int heading) Sets the current heading.boolean
setIfEmptyXY
(int x, int y) Sets a new position, only if new position is empty.void
setMovingType
(@NonNull AbstractTurtle.MoveMode movingType) Sets the behaviour of the turtle when it goes out of bounds of the grid.abstract void
Randomizes the turtle's heading.void
setX
(int x) Sets a newx
position.void
setXY
(int x, int y) Sets a new position.void
setY
(int y) Sets a newy
position.abstract void
turnCardinalLeft
(int steps) abstract void
turnCardinalRight
(int steps) abstract void
turnLeft
(int degrees) Changes the current heading rotating it to the left.abstract void
turnRight
(int degrees) Changes the current heading rotating it to the right.Methods inherited from class microsim.space.SpacePosition
compareTo, equals, toString
-
Field Details
-
grid
-
moving
-
-
Constructor Details
-
AbstractTurtle
public AbstractTurtle()Creates a turtle with a given identifier on the given grid at position (0,0). -
AbstractTurtle
Creates a turtle on the given grid at position(0,0).- Parameters:
grid
- The grid upon the turtle moves.
-
AbstractTurtle
Creates a turtle with a given identifier on the given grid at the given position. Set its color to the given color.- Parameters:
x
- The initialx
coordinate of the turtle.y
- The initialy
coordinate of the turtle.grid
- The grid upon the turtle moves.
-
-
Method Details
-
getPosition
-
getMovingType
Returns the behaviour of the turtle when it goes out of bounds of the grid.- Returns:
- The moving type identifier.
-
setMovingType
Sets the behaviour of the turtle when it goes out of bounds of the grid.- Parameters:
movingType
- A moving type identifier.
-
getWorldHeight
public int getWorldHeight()Returns they
size of the grid.- Returns:
- The height of the grid.
-
getWorldWidth
public int getWorldWidth()Returns thex
size of the grid.- Returns:
- The width of the grid.
-
getHeading
public abstract int getHeading()Returns the current heading of the turtle.- Returns:
- The current heading.
-
setHeading
public abstract void setHeading(int heading) Sets the current heading.- Parameters:
heading
- The new heading.
-
setRandomHeading
public abstract void setRandomHeading()Randomizes the turtle's heading. It gets a random number from 0 to 359 degrees. The random generator is synchronized with JAS randomizer. -
setCardinalHeading
Sets the turtle's heading using a constant for cardinal points.- Parameters:
directionType
- One of DIR_NORTH, DIR_NORTH_EAST, ... constant.
-
turnRight
public abstract void turnRight(int degrees) Changes the current heading rotating it to the right.- Parameters:
degrees
- The number of degrees to rotate the heading.
-
turnCardinalRight
public abstract void turnCardinalRight(int steps) -
turnLeft
public abstract void turnLeft(int degrees) Changes the current heading rotating it to the left.- Parameters:
degrees
- The number of degrees to rotate the heading.
-
turnCardinalLeft
public abstract void turnCardinalLeft(int steps) -
forward
public void forward()Makes a step forward, according the current heading. -
forward
public abstract void forward(int steps) Makes some steps forward, according the current heading.- Parameters:
steps
- The number of steps the turtle has to make.
-
leap
public boolean leap()Makes a step forward, but moves only if the target position is empty.- Returns:
- True only if the turtle has moved.
-
leap
public abstract boolean leap(int steps) Makes some steps forward, but moves only if the target position is empty.- Parameters:
steps
- The number of steps the turtle has to make.- Returns:
- True only if the turtle has moved.
-
getDistanceFrom
public double getDistanceFrom(int xCor, int yCor) Computes the cartesian distance from its position to the given position.- Parameters:
xCor
- Thex
coordinate.yCor
- They
coordinate.- Returns:
- The cartesian distance between points.
-
setX
public void setX(int x) Sets a newx
position.- Parameters:
x
- The newx
coordinate.
-
setY
public void setY(int y) Sets a newy
position.- Parameters:
y
- The newy
coordinate.
-
setXY
public void setXY(int x, int y) Sets a new position.- Parameters:
x
- The newx
coordinate.y
- The newy
coordinate.
-
setIfEmptyXY
public boolean setIfEmptyXY(int x, int y) Sets a new position, only if new position is empty.- Parameters:
x
- The newx
coordinate.y
- The newy
coordinate.- Returns:
- True only if the turtle has moved.
-
getNextX
public abstract int getNextX(int steps) 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.- Parameters:
steps
- The number of steps to move forward.- Returns:
- The candidate
x
coordinate.
-
getNextX
public int getNextX()Returns the newx
coordinate walking the current heading direction for 1 step. It uses the grid bound checking methods according to the turtle's current moving type.- Returns:
- The candidate
x
coordinate.
-
getNextY
public abstract int getNextY(int steps) 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.- Parameters:
steps
- The number of steps to move forward.- Returns:
- The candidate
y
coordinate.
-
getNextY
public int getNextY()Return the newy
coordinate walking the current heading direction for 1 step. It uses the grid bound checking methods according to the turtle's current moving type.- Returns:
- The candidate
y
coordinate.
-
getGrid
-
setGrid
-