gameLogic
Class Snake

java.lang.Object
  extended by gameLogic.GameObject
      extended by gameLogic.Snake
All Implemented Interfaces:
java.io.Serializable

public class Snake
extends GameObject
implements java.io.Serializable

The Snake class is a representation of each snake currently in the game, including its name, its current direction, its position on the game board, and its current statistics. It is a subclass of the GameObject class, in order for snakes to be able to be inserted into Square objects.

Author:
Sixten Hilborn, Arian Jafari
See Also:
Serialized Form

Constructor Summary
Snake(GameObjectType type, java.lang.String name, Brain brain, java.awt.Color color)
           
Snake(Snake other)
           
 
Method Summary
 java.awt.Color getColor()
           
 Direction getCurrentDirection()
          Gets the direction this snake moved in last turn.
 Direction getDirection(Position position)
          Get the direction of a specific segment in this snake.
 Position getHeadPosition()
          Gets the current position of this snake's head.
 int getLifespan()
          Gets the number of turns this snake has lived.
 java.lang.String getName()
           
 int getScore()
          Gets the number of fruits this snake has eaten.
 java.util.LinkedList<Position> getSegments()
          Get a list of all the squares this snake has occupied.
 Position getTailPosition()
          Gets the current position of the last segment of the snake's tail.
 boolean isDead()
          Returns whether or not this snake is dead.
 java.lang.String toString()
          Returns a string representation of this snake, consisting of a String of its name.
 
Methods inherited from class gameLogic.GameObject
getType
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Snake

public Snake(GameObjectType type,
             java.lang.String name,
             Brain brain,
             java.awt.Color color)

Snake

public Snake(Snake other)
Method Detail

getSegments

public java.util.LinkedList<Position> getSegments()
Get a list of all the squares this snake has occupied.

Returns:
A list of the positions of the occupied squares.

getDirection

public Direction getDirection(Position position)
Get the direction of a specific segment in this snake.

Parameters:
position - The position (in x-y coordinates) of the segment.
Returns:
The direction of that segment, or null if this snake has no segment on the specified position.

getHeadPosition

public Position getHeadPosition()
Gets the current position of this snake's head.

Returns:
The Position of the snake's head.
See Also:
Position

getTailPosition

public Position getTailPosition()
Gets the current position of the last segment of the snake's tail.

Returns:
The Position of the snake's last tail segment.

isDead

public boolean isDead()
Returns whether or not this snake is dead. Note that a dead snake won't be removed from the board; it will only be unable to move and unable to win the game.

Returns:
A boolean; true if the snake is dead, false if not.

getCurrentDirection

public Direction getCurrentDirection()
Gets the direction this snake moved in last turn. Note that it does not actually get the direction the snake will move in next turn.

Returns:
The Direction in which the snake moved last turn.

getScore

public int getScore()
Gets the number of fruits this snake has eaten.

Returns:
The number of fruits eaten by this snake.

getLifespan

public int getLifespan()
Gets the number of turns this snake has lived. If the snake is currently dead, this method returns the number of turns it was alive before its death.

Returns:
Age of the snake, in turns.

toString

public java.lang.String toString()
Returns a string representation of this snake, consisting of a String of its name.

Overrides:
toString in class java.lang.Object
Returns:
The snake's name as a string.

getColor

public java.awt.Color getColor()

getName

public java.lang.String getName()