JTP
Class Level

java.lang.Object
  extended by JTP.Level
All Implemented Interfaces:
Serializable

public class Level
extends Object
implements Serializable

Deals with handling and storage of individual levels.

Author:
V.Termanis
See Also:
Serialized Form

Constructor Summary
Level(int width, int height, int maxTileHeight)
          Constructs a new single-player Level instance with the given attributes and no time limit.
Level(int width, int height, int maxTileHeight, boolean twoPlayer)
          Constructs a new Level instance with the given attributes and no time limit.
Level(int width, int height, int maxTileHeight, int timeLimit)
          Constructs a new single-player Level instance with the given attributes.
Level(int width, int height, int maxTileHeight, int timeLimit, boolean twoPlayer)
          Constructs a new Level instance with the given attributes.
 
Method Summary
 void clear()
          Clears a level.
 String getInText()
          Get the level start message
 String getOutText()
          Get the level end message
 Point getStart()
          Returns 1st player start location
 Point getStart2()
          Returns 2nd player start location
protected  Tile[][] getTiles()
          Direct access to tile array.
 int getTimeLimit()
          Get the time limit of this level
 boolean hasEqualStarts()
          Checks whether both players have equal starts
 boolean isEmpty()
          Determines whether the level is empty / blank.
 boolean isValid()
          Validates a level.
 void manipulateMultiple(Point corner1, Point corner2, Tile attributeTile)
          Push or pop multiple tiles.
 void popTile(Point location)
          Pops a tile from the grid.
protected  boolean pushTile(Point location, boolean isStart)
          Only adds normal tiles and starts.
 void pushTile(Point location, Tile attributeTile, boolean p2Start)
          Pushes a tile onto the grid (works a bit like a stack - hence the name).
 void setInText(String s)
          Sets level start message to specified string
 void setOutText(String s)
          Sets level end message to specified string
 void setTimeLimit(int seconds)
          Sets time limit for this level
 int size()
          Counts number of tiles (not piles) in this level
protected  Tile tile(int x, int y)
          Provides direct access to a tile at the given location
protected  Tile tile(Point location)
          Provides direct access to a tile at the given location
 int tileType(Point location)
          Returns type of tile at given location
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Level

public Level(int width,
             int height,
             int maxTileHeight,
             int timeLimit,
             boolean twoPlayer)
Constructs a new Level instance with the given attributes.

Parameters:
width - in tiles of the level
height - in tiles of the level
maxTileHeight - maximum height allowed for any one tile pile
timeLimit - in which player has to complete this level. Set to 0 for no time limit
twoPlayer - specifies whether this a single or two-player level
Throws:
IllegalArgumentException - if any of the following are satisfied:
  • width < 2
  • height < 2
  • maxTileHeight < 1
  • timeLimit < 0

Level

public Level(int width,
             int height,
             int maxTileHeight,
             boolean twoPlayer)
Constructs a new Level instance with the given attributes and no time limit.

Parameters:
width - in tiles of the level
height - in tiles of the level
maxTileHeight - maximum height allowed for any one tile pile
twoPlayer - specifies whether this a single or two-player level
Throws:
IllegalArgumentException - if any of the following are satisfied:
  • width < 2
  • height < 2
  • maxTileHeight < 1

Level

public Level(int width,
             int height,
             int maxTileHeight,
             int timeLimit)
Constructs a new single-player Level instance with the given attributes.

Parameters:
width - in tiles of the level
height - in tiles of the level
maxTileHeight - maximum height allowed for any one tile pile
timeLimit - in which player has to complete this level. Set to 0 for no time limit
Throws:
IllegalArgumentException - if any of the following are satisfied:
  • width < 2
  • height < 2
  • maxTileHeight < 1
  • timeLimit < 0

Level

public Level(int width,
             int height,
             int maxTileHeight)
Constructs a new single-player Level instance with the given attributes and no time limit.

Parameters:
width - in tiles of the level
height - in tiles of the level
maxTileHeight - maximum height allowed for any one tile pile
Throws:
IllegalArgumentException - if any of the following are satisfied:
  • width < 2
  • height < 2
  • maxTileHeight < 1
Method Detail

pushTile

public void pushTile(Point location,
                     Tile attributeTile,
                     boolean p2Start)
              throws NullPointerException,
                     IllegalArgumentException
Pushes a tile onto the grid (works a bit like a stack - hence the name). If the given tile has the same parameters (apart from height), the tile at location gets a height increase. Otherwise the tile's type and attributes are changed but height stays the same (unless changing from zero height start tile). If attributes == null also just increase height (or create normal tile)

Parameters:
location - on grid where to push the given tile on
p2Start - if attributeTile is a start tile then this indicates whether it is the start of the 1st or 2nd player
Throws:
NullPointerException - if location is null
IllegalArgumentException - if location is invalid

popTile

public void popTile(Point location)
             throws NullPointerException,
                    IllegalArgumentException
Pops a tile from the grid.

Parameters:
location - on grid where to pop tile from
Throws:
NullPointerException - if location is null
IllegalArgumentException - if location is invalid

manipulateMultiple

public void manipulateMultiple(Point corner1,
                               Point corner2,
                               Tile attributeTile)
Push or pop multiple tiles. Notes:

Parameters:
corner1 - 1st corner for specifying push/pop area
corner2 - 2nd corner for specifying push/pop area
attributeTile - tile to push, or set to null to pop

tileType

public int tileType(Point location)
             throws NullPointerException,
                    IllegalArgumentException
Returns type of tile at given location

Parameters:
location - of tile
Returns:
the type of the tile as specified in the Tile class
Throws:
NullPointerException - if location is null
IllegalArgumentException - if location is invalid

getStart

public Point getStart()
Returns 1st player start location

Returns:
start location of 1st player.

getStart2

public Point getStart2()
Returns 2nd player start location

Returns:
start location of 2nd player.

clear

public void clear()
Clears a level. This returns it to the state of a newly create level.


isValid

public boolean isValid()
Validates a level. This method does not determine whether a level is soluble. Instead it uses the following rules:
A level needs a start position and has to contain at least one tile adjacent to the start position (or both in case of two player mode)

Returns:
true if the level meets the above criteria

isEmpty

public boolean isEmpty()
Determines whether the level is empty / blank.

Returns:
true if no tiles have been placed

pushTile

protected boolean pushTile(Point location,
                           boolean isStart)
Only adds normal tiles and starts. This method is used in the Challenge class.


hasEqualStarts

public boolean hasEqualStarts()
Checks whether both players have equal starts

Returns:
true if both players start at the same location

tile

protected Tile tile(Point location)
Provides direct access to a tile at the given location


tile

protected Tile tile(int x,
                    int y)
Provides direct access to a tile at the given location


getTiles

protected Tile[][] getTiles()
Direct access to tile array. Used by TilePainter class for quick access during rendering


size

public int size()
Counts number of tiles (not piles) in this level

Returns:
the total number of tiles in this level

setInText

public void setInText(String s)
Sets level start message to specified string

Parameters:
s - message to use for level start

setOutText

public void setOutText(String s)
Sets level end message to specified string

Parameters:
s - message to use for level end

getInText

public String getInText()
Get the level start message

Returns:
the level start message

getOutText

public String getOutText()
Get the level end message

Returns:
the level end message

getTimeLimit

public int getTimeLimit()
Get the time limit of this level

Returns:
time limit in seconds

setTimeLimit

public void setTimeLimit(int seconds)
                  throws IllegalArgumentException
Sets time limit for this level

Parameters:
seconds - time limit
Throws:
IllegalArgumentException - if either
  • seconds < 0 or
  • seconds > 3599 (59m, 59s)