Class TronState

java.lang.Object
  extended by TronState
All Implemented Interfaces:
game.GameState, TronStateInterface

public class TronState
extends java.lang.Object
implements game.GameState, TronStateInterface

This represents the state of the Tron board. April 2010

Author:
Ben Bederson

Constructor Summary
TronState(int dim)
          Constructor that initializes the board of size (dim x dim) to being unplayed (i.e., each cell should be empty.)
TronState(TronState otherState)
          Copy Constructor - does deep copy.
 
Method Summary
 java.lang.Object clone()
          Returns a duplicate of this state
 java.lang.String dump()
          Generates and returns a multi-line string representing a human-readable pretty version of the board.
 boolean equals(TronStateInterface otherState)
          Determines if the specified instance TronState has the same contents as this instance.
 int[][] getBoard()
          Returns a copy of the array representing the board of the game.
 Cell getHead(int playerNum)
          Returns the head for the specified player
 java.util.ArrayList<TronObstacle> getObstacles()
          Returns the obstacles in this board.
 boolean rectContainsPoint(int x, int y, int width, int height, Cell pt)
           
 void setCell(int row, int col, int value)
          Sets the specified cell of the board to the specified value.
 void setHead(int playerNum, Cell head)
          Sets the head for the specified player
 java.lang.String toString()
          Generates and returns a single line string that must contain exactly (dim*dim) string representations of integers separated by commas (with no spaces), where dim is the dimension of the board.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TronState

public TronState(int dim)
Constructor that initializes the board of size (dim x dim) to being unplayed (i.e., each cell should be empty.)

Parameters:
dim - the dimensions of each side of the square board

TronState

public TronState(TronState otherState)
Copy Constructor - does deep copy.

Parameters:
otherState - the state to copy
Method Detail

clone

public java.lang.Object clone()
Returns a duplicate of this state

Specified by:
clone in interface game.GameState
Overrides:
clone in class java.lang.Object
Returns:
the new state object

equals

public boolean equals(TronStateInterface otherState)
Determines if the specified instance TronState has the same contents as this instance.

Parameters:
otherState - The other instance to check equality with
Returns:
true if the specified parameter is equal to this, or false otherwise

getBoard

public int[][] getBoard()
Returns a copy of the array representing the board of the game. Cells are TronConstants.CELL_EMPTY if unplayed, or player number if played.

Note that the board contains only an indication of where players have been. It does not contain the head position of the players, nor does it contain the obstacles. Your program will have to use all three elements of the state to create a correct solution.

Specified by:
getBoard in interface TronStateInterface
Returns:
the board
See Also:
getHead, getObstacles

setCell

public void setCell(int row,
                    int col,
                    int value)
Sets the specified cell of the board to the specified value. The cell is specified by the row and col parameters

Specified by:
setCell in interface TronStateInterface
Parameters:
row - is row of the cell to set
col - is column of the cell to set
value - is new value of the cell

getHead

public Cell getHead(int playerNum)
Returns the head for the specified player

Specified by:
getHead in interface TronStateInterface
Parameters:
playerNum - the player number to get the head row of (always 0 or 1)
Returns:
the head row

setHead

public void setHead(int playerNum,
                    Cell head)
Sets the head for the specified player

Specified by:
setHead in interface TronStateInterface
Parameters:
playerNum - the player number to get the head column of (always 0 or 1)
head - the position of the head

getObstacles

public java.util.ArrayList<TronObstacle> getObstacles()
Returns the obstacles in this board.

Specified by:
getObstacles in interface TronStateInterface
Returns:
the list of obstacles
See Also:
TronObstacle

rectContainsPoint

public boolean rectContainsPoint(int x,
                                 int y,
                                 int width,
                                 int height,
                                 Cell pt)

dump

public java.lang.String dump()
Generates and returns a multi-line string representing a human-readable pretty version of the board. This is like toString() in that it should return a String that represents the board. But while toString() generates a single line in a format more easily understandable by computers, this string contains multiple lines designed to be readable by humans. In particular, the textual driver program uses this method to display the game state after each player's move.

The string must have multiple lines separated by '\n'. There must be one line per row of the board, and each line must have exactly the same number characters as the width of the board. In other words, the string should be display as a grid of characters, one per cell in the board. The characters should be:

Unlike in the previous project, you do not have any flexibility in what this method generates - it must create a string exactly as described above.

Specified by:
dump in interface game.GameState
Returns:
the string that represents the board

toString

public java.lang.String toString()
Generates and returns a single line string that must contain exactly (dim*dim) string representations of integers separated by commas (with no spaces), where dim is the dimension of the board. The integers must represent the cells of the board in natural grid order, one row at a time starting at the top, with each row going from left to right. The values you should use in this string are as follow. Empty cells must converted into TronConstants.STATE_EMPTY, head cells must be converted into TronConstants.STATE_HEAD, obstacles must be converted into TronConstants.STATE_OBSTACLE, and other spaces must be converted into the player number in that cell.

A simple example would be as follows for a 4x4 board with one obstacle in the lower-left, and two short traces ending in heads in the upper-left and upper-right corners, respectively: "-2,-1,-1,-2,0,-1,-1,1,0,-1,-1,1,-3,-1,-1,-1".

Note that toString() is used by the tournament engine, so it won't have any impact anywhere except there (and in the release tests.)

Overrides:
toString in class java.lang.Object


Web Accessibility