gameLogic
Class GameState

java.lang.Object
  extended by gameLogic.GameState

public class GameState
extends java.lang.Object

The GameState is a representation of the game at a given moment in time. It contains references to the game board (containing a matrix of Squares, which in turn contain all game objects), the current Metadata (turns until growth/spawning of fruit, among other things), all snakes participating in this game session, and an ErrorState enum.

Author:
Sixten Hilborn, Arian Jafari

Method Summary
static Position calculateNextPosition(Direction direction, Position oldPosition)
          Gets the next position a snake would end up in if it continues in this direction.
static int distanceBetween(Position from, Position to)
          This method can be used to calculate the distance between two positions.
 Board getBoard()
          Returns a Board object, which constists of a 2D-array of Square objects.
 ErrorState getErrorState()
          Returns the ErrorState for the previous turn, for example telling a brain it took too long to decide last turn.
 java.util.ArrayList<Position> getFruits()
          Gets a list containing the positions of all the fruits currently on the board.
 Metadata getMetadata()
          Method for getting the current game metadata, containing (among other things) time until the next fruit spawns and time until snake growth.
static java.util.ArrayList<Direction> getRelativeDirections(Position from, Position to)
          Returns in which direction one has to move in order to reach one position from another one.
 java.util.Set<Snake> getSnakes()
          Returns a Set containing all snakes in the game, both dead ones and alive ones.
 java.util.ArrayList<Position> getWalls()
          Gets a list containing the positions of all the walls currently on the board.
 boolean willCollide(Snake snake, Direction dir)
          This method can be used to help calculate whether or not a given snake will collide next turn if it continues in a given direction.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getSnakes

public java.util.Set<Snake> getSnakes()
Returns a Set containing all snakes in the game, both dead ones and alive ones.

Returns:
A Set containing all snakes.
See Also:
Snake

getBoard

public Board getBoard()
Returns a Board object, which constists of a 2D-array of Square objects.

Returns:
A representation of the current game board.
See Also:
Board

getMetadata

public Metadata getMetadata()
Method for getting the current game metadata, containing (among other things) time until the next fruit spawns and time until snake growth.

Returns:
The current Metadata object.
See Also:
Metadata

getErrorState

public ErrorState getErrorState()
Returns the ErrorState for the previous turn, for example telling a brain it took too long to decide last turn.

Returns:
The ErrorState object for last turn.
See Also:
ErrorState

willCollide

public boolean willCollide(Snake snake,
                           Direction dir)
This method can be used to help calculate whether or not a given snake will collide next turn if it continues in a given direction. It looks at the square the snake will end up in, and then checks if that square contains a lethal object. Note that this method returning false does NOT guarantee that the snake will survive. For example, it is possible that a two snakes will move into an empty square during the same turn, causing the death of them both.

Parameters:
snake - The snake you wish you perform the check for.
dir - The hypothetical direction in which the snake moves.
Returns:
True if the next position contains a lethal object, false if not.

calculateNextPosition

public static Position calculateNextPosition(Direction direction,
                                             Position oldPosition)
Gets the next position a snake would end up in if it continues in this direction.

Parameters:
direction - The current direction of the snake.
oldPosition - The current position of the snake.
Returns:
The next position if movement continues in this direction.

getFruits

public java.util.ArrayList<Position> getFruits()
Gets a list containing the positions of all the fruits currently on the board. Note that the list will be empty if the number of fruits on the board is 0.

Returns:
The positions of the fruits currently on the board.

getWalls

public java.util.ArrayList<Position> getWalls()
Gets a list containing the positions of all the walls currently on the board. Note that the list will be empty if the number of walls on the board is 0.

Returns:
The positions of the walls currently on the board.

getRelativeDirections

public static java.util.ArrayList<Direction> getRelativeDirections(Position from,
                                                                   Position to)
Returns in which direction one has to move in order to reach one position from another one. Returns an ArrayList containing either one or two elements. For example, it might contain either only WEST if the destination postion is directly west of the starting position, or it may contain both WEST and NORTH if the destination is towards the northwest.

Parameters:
from - The starting position.
to - The destination position.
Returns:
containing either one or two Directions, pointing towards the destination.

distanceBetween

public static int distanceBetween(Position from,
                                  Position to)
This method can be used to calculate the distance between two positions.

Parameters:
from - The position from which you wish to calculate the distance.
to - The position to which you wish to calculate the distance.
Returns:
An integer representing the distance between two positions.