jas.space
Class AbstractTurtle

java.lang.Object
  extended by jas.space.AbstractTurtle
All Implemented Interfaces:
IColored, IGridPosition
Direct Known Subclasses:
DigitalTurtle, Turtle

public abstract class AbstractTurtle
extends java.lang.Object
implements IGridPosition, IColored

An agent able to move itself upon an object grid. It has got some specific instruction for movement. Each turtle has an 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.

Title: JAS

Description: Java Agent-based Simulation library

Copyright (C) 2002 Michele Sonnessa

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

Author:
Michele Sonnessa


Field Summary
static java.awt.Color DEFALUT_COLOR
          The turtle's default color.
static int DIR_EAST
           
static int DIR_NORTH
           
static int DIR_NORTH_EAST
           
static int DIR_NORTH_WEST
           
static int DIR_SOUTH
           
static int DIR_SOUTH_EAST
           
static int DIR_SOUTH_WEST
           
static int DIR_WEST
           
static int MOVE_BOUND
          Turtle moves on a bounded grid.
static int MOVE_REFLECT
          Turtle moves on a walled grid.
static int MOVE_TORUS
          Turtle moves on a toroidal grid.
 
Constructor Summary
AbstractTurtle(int id, IObjGrid grid)
          Create a turtle with a given identifier on the given grid at position (0,0).
AbstractTurtle(int id, IObjGrid grid, int x, int y)
          Create a turtle with a given identifier on the given grid at the given position.
AbstractTurtle(int id, IObjGrid grid, int x, int y, java.awt.Color color)
          Create a turtle with a given identifier on the given grid at the given position.
 
Method Summary
 void forward()
          Make a step forward, according the current heading.
abstract  void forward(int steps)
          Make some steps forward, according the current heading.
 java.awt.Color getColor()
          Return the current turtle's color.
 double getDistanceFrom(int xCor, int yCor)
          Compute the cartesian distance from its position to the given position.
abstract  int getHeading()
          Return the current heading of the turtle.
 int getID()
          Return the identifier of the turtle.
 int getMovingType()
          Return the behaviour of the turtle when it goes out of bounds of the grid.
 int getNextX()
          Return the new x coordinate walking the current heading direction for 1 step.
abstract  int getNextX(int steps)
          Return the new x coordinate walking the current heading direction for given steps.
 int getNextY()
          Return the new y coordinate walking the current heading direction for 1 step.
abstract  int getNextY(int steps)
          Return the new y coordinate walking the current heading direction for given steps.
 int getWorldHeight()
          Return the y size of the grid.
 int getWorldWidth()
          Return the x size of the grid.
 int getX()
          Return the current x position.
 int getY()
          Return the current y position.
 boolean leap()
          Make a steps forward, but moves only if the target position is empty.
abstract  boolean leap(int steps)
          Make some steps forward, but moves only if the target position is empty.
abstract  void setCardinalHeading(int directionType)
          Set the turtle's heading using a constant for cardinal points.
 void setColor(java.awt.Color color)
          Change turtle's color.
abstract  void setHeading(int heading)
          Set the current heading.
 boolean setIfEmptyXY(int x, int y)
          Set a new position, only if new position is empty.
 void setMovingType(int movingType)
          Set the behaviour of the turtle when it goes out of bounds of the grid.
abstract  void setRandomHeading()
          Randomize the turtle's heading.
 void setX(int x)
          Set a new x position.
 void setXY(int x, int y)
          Set a new position.
 void setY(int y)
          Set a new y position.
abstract  void turnCardinalLeft(int steps)
           
abstract  void turnCardinalRight(int steps)
           
abstract  void turnLeft(int degrees)
          Change the current heading rotating it to the left.
abstract  void turnRight(int degrees)
          Change the current heading rotating it to the right.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFALUT_COLOR

public static final java.awt.Color DEFALUT_COLOR
The turtle's default color.


DIR_EAST

public static final int DIR_EAST
See Also:
Constant Field Values

DIR_NORTH

public static final int DIR_NORTH
See Also:
Constant Field Values

DIR_NORTH_EAST

public static final int DIR_NORTH_EAST
See Also:
Constant Field Values

DIR_NORTH_WEST

public static final int DIR_NORTH_WEST
See Also:
Constant Field Values

DIR_SOUTH

public static final int DIR_SOUTH
See Also:
Constant Field Values

DIR_SOUTH_EAST

public static final int DIR_SOUTH_EAST
See Also:
Constant Field Values

DIR_SOUTH_WEST

public static final int DIR_SOUTH_WEST
See Also:
Constant Field Values

DIR_WEST

public static final int DIR_WEST
See Also:
Constant Field Values

MOVE_BOUND

public static final int MOVE_BOUND
Turtle moves on a bounded grid.

See Also:
Constant Field Values

MOVE_REFLECT

public static final int MOVE_REFLECT
Turtle moves on a walled grid.

See Also:
Constant Field Values

MOVE_TORUS

public static final int MOVE_TORUS
Turtle moves on a toroidal grid.

See Also:
Constant Field Values
Constructor Detail

AbstractTurtle

public AbstractTurtle(int id,
                      IObjGrid grid)
Create a turtle with a given identifier on the given grid at position (0,0).

Parameters:
id - The identifier for turtle.
grid - The grid upon the turtle moves.

AbstractTurtle

public AbstractTurtle(int id,
                      IObjGrid grid,
                      int x,
                      int y)
Create a turtle with a given identifier on the given grid at the given position.

Parameters:
id - The identifier for turtle.
x - The initial x coordinate of the turtle.
y - The initial y coordinate of the turtle.
grid - The grid upon the turtle moves.

AbstractTurtle

public AbstractTurtle(int id,
                      IObjGrid grid,
                      int x,
                      int y,
                      java.awt.Color color)
Create a turtle with a given identifier on the given grid at the given position. Set its color to the given color.

Parameters:
id - The identifier for turtle.
x - The initial x coordinate of the turtle.
y - The initial y coordinate of the turtle.
color - The initial color of the turtle.
grid - The grid upon the turtle moves.
Method Detail

forward

public void forward()
Make a step forward, according the current heading.


forward

public abstract void forward(int steps)
Make some steps forward, according the current heading.

Parameters:
steps - The number of steps the turtle has to make.

getColor

public java.awt.Color getColor()
Return the current turtle's color.

Specified by:
getColor in interface IColored
Returns:
Current color.

getDistanceFrom

public double getDistanceFrom(int xCor,
                              int yCor)
Compute the cartesian distance from its position to the given position. WARNING The (xCor, yCor) must be within the grid. The bounds are not checked.

Parameters:
xCor - The target x coordinate.
yCor - The target y coordinate.
Returns:
The cartesian distance between points.

getHeading

public abstract int getHeading()
Return the current heading of the turtle.

Returns:
The current heading.

getID

public int getID()
Return the identifier of the turtle.

Returns:
The id of the turtle.

getMovingType

public int getMovingType()
Return the behaviour of the turtle when it goes out of bounds of the grid.

Returns:
The moving type identifier.

getNextX

public int getNextX()
Return the new x 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.

getNextX

public abstract int getNextX(int steps)
Return the new x 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.

getNextY

public int getNextY()
Return the new y 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.

getNextY

public abstract int getNextY(int steps)
Return the new y 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.

getWorldHeight

public int getWorldHeight()
Return the y size of the grid.

Returns:
The height of the grid.

getWorldWidth

public int getWorldWidth()
Return the x size of the grid.

Returns:
The width of the grid.

getX

public int getX()
Return the current x position.

Specified by:
getX in interface IGridPosition
Returns:
The x coordinate.

getY

public int getY()
Return the current y position.

Specified by:
getY in interface IGridPosition
Returns:
The y coordinate.

leap

public boolean leap()
Make a steps 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)
Make 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.

setCardinalHeading

public abstract void setCardinalHeading(int directionType)
Set the turtle's heading using a constant for cardinal points.

Parameters:
directionType - One of DIR_NORTH, DIR_NORTH_EAST, ... constant.

setColor

public void setColor(java.awt.Color color)
Change turtle's color.

Parameters:
color - The new color.

setHeading

public abstract void setHeading(int heading)
Set the current heading.

Parameters:
heading - The new heading.

setIfEmptyXY

public boolean setIfEmptyXY(int x,
                            int y)
Set a new position, only if new position is empty.

Parameters:
x - The new x coordinate.
y - The new y coordinate.
Returns:
True only if the turtle has moved.

setMovingType

public void setMovingType(int movingType)
Set the behaviour of the turtle when it goes out of bounds of the grid.

Parameters:
movingType - A moving type identifier.

setRandomHeading

public abstract void setRandomHeading()
Randomize the turtle's heading. It gets a random number from 0 to 359 degrees. The random generator is synchronized with JAS randomizer.


setX

public void setX(int x)
Set a new x position.

Specified by:
setX in interface IGridPosition
Parameters:
x - The new x coordinate.

setXY

public void setXY(int x,
                  int y)
Set a new position.

Parameters:
x - The new x coordinate.
y - The new y coordinate.

setY

public void setY(int y)
Set a new y position.

Specified by:
setY in interface IGridPosition
Parameters:
y - The new y coordinate.

turnCardinalLeft

public abstract void turnCardinalLeft(int steps)

turnCardinalRight

public abstract void turnCardinalRight(int steps)

turnLeft

public abstract void turnLeft(int degrees)
Change the current heading rotating it to the left.

Parameters:
degrees - The number of degrees to rotate the heading.

turnRight

public abstract void turnRight(int degrees)
Change the current heading rotating it to the right.

Parameters:
degrees - The number of degrees to rotate the heading.