Labyrinth
A labyrinth game assignment
host.labyrinth.Board Class Reference

This class is the representation of the games's board. More...

Package Functions

Constructors
 Board ()
 The empty constructor for default initialization. More...
 
 Board (int N, int S)
 The main constructor for the application. More...
 
 Board (Board b)
 Deep copy constructor. More...
 
Board's main application interface
void createBoard (int theseusTile, int minotaurTile)
 Creates the board with all the requested walls and supplies. More...
 
String [][] getStringRepresentation (int theseusTile, int minotaurTile)
 Returns a 2-D array with the string representation of the board. More...
 
void printBoard (String[][] sBoard)
 Print board utility. More...
 
boolean isWalkable (int tileId, int direction)
 Predicate to check if a direction is Walkable. More...
 
boolean isWalkable (int row, int col, int direction)
 Predicate to check if a direction is Walkable. More...
 
boolean hasSupply (int tileId)
 Utility function to check if there is a supply on the tile or not. More...
 
int tryPickSupply (int tileId)
 Try to pick supply from a tile. More...
 
int dice ()
 A plain fair dice functionality provided by the board. More...
 
int size ()
 
int [][] getOpponentMoves (int playerId)
 Boards utility to give access to other player moves. More...
 
int generatePlayerId ()
 Utility function to create player IDs. More...
 
void updateMove (int[] m, int playerId)
 Utility to update the moves of each player. More...
 
Accessor/Mutator interface
Note
Please consider not to use mutator interface. Its the abstraction killer :(
int getN ()
 
int getS ()
 
int getW ()
 
Tile [] getTiles ()
 
Supply [] getSupplies ()
 
ArrayList< EdgegetWalls ()
 
ArrayList< Integer[]> getMoves ()
 
void setN (int N)
 
void setS (int S)
 
void setW (int W)
 
void setTiles (Tile[] tiles)
 
void setSupplies (Supply[] supplies)
 
void setWalls (ArrayList< Edge > walls)
 
void setMoves (ArrayList< Integer[]> moves)
 

Private Member Functions

Sentinel predicates
boolean isLeftSentinel (int tileId)
 
boolean isRightSentinel (int tileId)
 
boolean isUpSentinel (int tileId)
 
boolean isDownSentinel (int tileId)
 
private functionality of the object
void createTiles ()
 This function creates randomly all the tiles of the board. More...
 
void createSupplies (int theseusTile, int minotaurTile)
 This function create randomly the board's supplies. More...
 
boolean isRoomCreator (int tileId, int direction)
 Predicate to check if a wall creates a closed room. More...
 
boolean isWallableDir (int tileId, int direction)
 Predicate to check if a tile direction is Wallable. More...
 
boolean isWallable (int tileId)
 Predicate to check if a tile is Wallable. More...
 
int createBasicTileWalls ()
 This utility function create/allocate the tiles of the board and create the outer walls at the same time. More...
 
void createInnerWall (int tileId)
 Create randomly a wall in the wallable selected tile. More...
 
int createInnerWalls ()
 This utility creates the inner walls of the board. More...
 
String getTileBody (int row, int col, int theseusTile, int minotaurTile)
 Utility to get the body (center line) of the string representation of the tile. More...
 
void renderTile (String[][] frame, int row, int col, int theseusTile, int minotaurTile)
 Utility to render the 3 strings of the tile in the representation frame. More...
 
void renderSentinelTile (String[][] frame, int row, int col, int theseusTile, int minotaurTile)
 Utility to render the 3 strings of the tile in the representation frame in the case the tile lies in the east wall. More...
 

Private Attributes

Neighbor access lambdas
IntFunction< Integer > leftTileId = (id) -> { return Position.toID(Position.toRow(id), Position.toCol(id)-1); }
 
IntFunction< Integer > rightTileId = (id) -> { return Position.toID(Position.toRow(id), Position.toCol(id)+1); }
 
IntFunction< Integer > upTileId = (id) -> { return Position.toID(Position.toRow(id)+1, Position.toCol(id) ); }
 
IntFunction< Integer > downTileId = (id) -> { return Position.toID(Position.toRow(id)-1, Position.toCol(id) ); }
 
Class data
int N
 The size of each edge of the board. More...
 
int S
 The number of the supplies on the board. More...
 
int W
 The number of walls on the board. More...
 
Tile [] tiles
 Array to hold all the tiles for the board. More...
 
Supply [] supplies
 Array to hold all the supplies on the board. More...
 
ArrayList< Edgewalls
 Array to hold all the walls using the edge representation required by the closed room preventing algorithm. More...
 
ArrayList< Integer[]> moves
 

Detailed Description

This class is the representation of the games's board.

The board is the square arrangement of the tiles. This class is also the owner of the tile and supply objects.

Definition at line 26 of file Board.java.

Constructor & Destructor Documentation

◆ Board() [1/3]

host.labyrinth.Board.Board ( )
package

The empty constructor for default initialization.

Definition at line 33 of file Board.java.

◆ Board() [2/3]

host.labyrinth.Board.Board ( int  N,
int  S 
)
package

The main constructor for the application.

Parameters
NThe size of each edge of the board
SThe number of supplies on the board

Definition at line 48 of file Board.java.

◆ Board() [3/3]

host.labyrinth.Board.Board ( Board  b)
package

Deep copy constructor.

Parameters
bThe board to copy
Note
The lack of value semantics in java is (in author's opinion) one of the greatest weakness of the language and one of the reasons why it will never be a language to care about. To quote Alexander Stepanof's words in "elements of programming" section 1.5: "Assignment is a procedure that takes two objects of the same type and makes the first object equal to the second without modifying the second". In this class we try to cope with this situation knowing that we can not do anything about assignment operator. We just add value semantics to the copy constructor and go on with our lifes...

Definition at line 73 of file Board.java.

Member Function Documentation

◆ createBasicTileWalls()

int host.labyrinth.Board.createBasicTileWalls ( )
private

This utility function create/allocate the tiles of the board and create the outer walls at the same time.

Returns
The number of walls created from the utility.

Definition at line 486 of file Board.java.

◆ createBoard()

void host.labyrinth.Board.createBoard ( int  theseusTile,
int  minotaurTile 
)
package

Creates the board with all the requested walls and supplies.

Parameters
theseusTile
minotaurTile

Definition at line 98 of file Board.java.

◆ createInnerWall()

void host.labyrinth.Board.createInnerWall ( int  tileId)
private

Create randomly a wall in the wallable selected tile.

Parameters
tileIdThe wallable tile to create the wall

Definition at line 510 of file Board.java.

◆ createInnerWalls()

int host.labyrinth.Board.createInnerWalls ( )
private

This utility creates the inner walls of the board.

Returns
The number of walls failed to create.

Definition at line 531 of file Board.java.

◆ createSupplies()

void host.labyrinth.Board.createSupplies ( int  theseusTile,
int  minotaurTile 
)
private

This function create randomly the board's supplies.

The supplies has to be in separate tiles and in tiles with no player

Parameters
theseusTileThe tile of the Theseus
minotaurTileThe tile of the Minotaur

Definition at line 365 of file Board.java.

◆ createTiles()

void host.labyrinth.Board.createTiles ( )
private

This function creates randomly all the tiles of the board.

Definition at line 350 of file Board.java.

◆ dice()

int host.labyrinth.Board.dice ( )
package

A plain fair dice functionality provided by the board.

Returns
A random direction;

Definition at line 214 of file Board.java.

◆ generatePlayerId()

int host.labyrinth.Board.generatePlayerId ( )
package

Utility function to create player IDs.

Returns
The generated player id.

Definition at line 243 of file Board.java.

◆ getMoves()

ArrayList<Integer[]> host.labyrinth.Board.getMoves ( )
package
Note
Use it with care. Any use of this function results to what Sean Parent calls "incidental data-structure". see also here
Returns
Reference to inner walls array.

Definition at line 298 of file Board.java.

◆ getN()

int host.labyrinth.Board.getN ( )
package

Definition at line 270 of file Board.java.

◆ getOpponentMoves()

int [][] host.labyrinth.Board.getOpponentMoves ( int  playerId)
package

Boards utility to give access to other player moves.

Parameters
playerIdThe id of player who asks
Returns
The moves data of all other players

Definition at line 228 of file Board.java.

◆ getS()

int host.labyrinth.Board.getS ( )
package

Definition at line 271 of file Board.java.

◆ getStringRepresentation()

String [][] host.labyrinth.Board.getStringRepresentation ( int  theseusTile,
int  minotaurTile 
)
package

Returns a 2-D array with the string representation of the board.

The rows of the array represent the Y-coordinate and the columns the X-coordinate. The only difference is that between each row there is an extra row with the possible walls. This way the number of rows of the returning array are 2N+1 and the number of columns N+1.
So each tile of the board is represented by 3 strings. One for the north wall, one for the body and one for the south wall.

Parameters
theseusTileThe current Theseus tile
minotaurTileThe current Minotaur tile
Returns
The string representation of the board

Definition at line 117 of file Board.java.

◆ getSupplies()

Supply [] host.labyrinth.Board.getSupplies ( )
package
Note
Use it with care. Any use of this function results to what Sean Parent calls "incidental data-structure". see also here
Returns
Reference to inner supplies array.

Definition at line 284 of file Board.java.

◆ getTileBody()

String host.labyrinth.Board.getTileBody ( int  row,
int  col,
int  theseusTile,
int  minotaurTile 
)
private

Utility to get the body (center line) of the string representation of the tile.

Parameters
rowWhat board's row to get.
colWhat board's column to get.
theseusTileThe current tile of the Theseus.
minotaurTileThe current tile of the Minotaur.
Returns
The body string

Definition at line 559 of file Board.java.

◆ getTiles()

Tile [] host.labyrinth.Board.getTiles ( )
package
Note
Use it with care. Any use of this function results to what Sean Parent calls "incidental data-structure". see also here
Returns
Reference to inner tiles array.

Definition at line 278 of file Board.java.

◆ getW()

int host.labyrinth.Board.getW ( )
package

Definition at line 272 of file Board.java.

◆ getWalls()

ArrayList<Edge> host.labyrinth.Board.getWalls ( )
package
Note
Use it with care. Any use of this function results to what Sean Parent calls "incidental data-structure". see also here
Returns
Reference to inner walls array.

Definition at line 291 of file Board.java.

◆ hasSupply()

boolean host.labyrinth.Board.hasSupply ( int  tileId)
package

Utility function to check if there is a supply on the tile or not.

Parameters
tileIdThe tile to check
Returns
Yes/no

Definition at line 188 of file Board.java.

◆ isDownSentinel()

boolean host.labyrinth.Board.isDownSentinel ( int  tileId)
private

Definition at line 339 of file Board.java.

◆ isLeftSentinel()

boolean host.labyrinth.Board.isLeftSentinel ( int  tileId)
private

Definition at line 336 of file Board.java.

◆ isRightSentinel()

boolean host.labyrinth.Board.isRightSentinel ( int  tileId)
private

Definition at line 337 of file Board.java.

◆ isRoomCreator()

boolean host.labyrinth.Board.isRoomCreator ( int  tileId,
int  direction 
)
private

Predicate to check if a wall creates a closed room.

This algorithm has a complexity of $ O(N^2logN) $ where N represents the total number of tiles. It should be used with care.

Parameters
tileIdThe tileId of the wall.
directionThe wall's relative direction.
Returns
True if the wall creates a closed room, false otherwise.

Definition at line 387 of file Board.java.

◆ isUpSentinel()

boolean host.labyrinth.Board.isUpSentinel ( int  tileId)
private

Definition at line 338 of file Board.java.

◆ isWalkable() [1/2]

boolean host.labyrinth.Board.isWalkable ( int  tileId,
int  direction 
)
package

Predicate to check if a direction is Walkable.

A walkable direction is a tile direction where:

  • The wall is not the DOWN wall from tile (0, 0).
  • There is not already a wall in the desired direction. (Implies no sentinel tile).
Parameters
tileIdThe starting tileId.
directionThe desired direction.
Returns
True if it is walkable.

Definition at line 159 of file Board.java.

◆ isWalkable() [2/2]

boolean host.labyrinth.Board.isWalkable ( int  row,
int  col,
int  direction 
)
package

Predicate to check if a direction is Walkable.

A walkable direction is a tile direction where:

  • The wall is not the DOWN wall from tile (0, 0).
  • There is not already a wall in the desired direction. (Implies no sentinel tile).
Parameters
rowRow position of the starting tile.
colColumn position of the starting tile.
directionThe desired direction.
Returns
True if it is walkable.

Definition at line 178 of file Board.java.

◆ isWallable()

boolean host.labyrinth.Board.isWallable ( int  tileId)
private

Predicate to check if a tile is Wallable.

A wallable tile is a tile where:

  • The tile has at most Const.maxTileWalls -1 walls.
  • There is at least one wallable direction on the tile.
Parameters
tileIdThe tile to check
Returns
True if the tile is wallable.

Definition at line 466 of file Board.java.

◆ isWallableDir()

boolean host.labyrinth.Board.isWallableDir ( int  tileId,
int  direction 
)
private

Predicate to check if a tile direction is Wallable.

A wallable direction is a tile direction where:

  • The wall is not the DOWN wall from tile (0, 0).
  • There is not already a wall in the desired direction. (Implies no sentinel tile).
  • The neighbor in this direction has at most Const.maxTileWalls -1 walls.
  • The wall does not create a closed room (Optional requirement).
Note
A wallable direction automatically implies that the direction in not an outer wall.
Parameters
tileIdThe tile to check.
directionThe direction to check
Returns
True if the direction is wallable.

Definition at line 432 of file Board.java.

◆ printBoard()

void host.labyrinth.Board.printBoard ( String  sBoard[][])
package

Print board utility.

Parameters
sBoardReference to string representation of the board to print.
Note
As the lower row addresses of the string representation of the board contain the south rows, in order to view the board correctly we have to print the rows in the opposite order.

Definition at line 138 of file Board.java.

◆ renderSentinelTile()

void host.labyrinth.Board.renderSentinelTile ( String  frame[][],
int  row,
int  col,
int  theseusTile,
int  minotaurTile 
)
private

Utility to render the 3 strings of the tile in the representation frame in the case the tile lies in the east wall.

We call these tiles sentinel tiles

Parameters
frameReference to the frame to print into.
rowThe board's row to print.
colThe board's column to print.
theseusTileThe current tile of the Theseus.
minotaurTileThe current tile of the Minotaur.

Definition at line 605 of file Board.java.

◆ renderTile()

void host.labyrinth.Board.renderTile ( String  frame[][],
int  row,
int  col,
int  theseusTile,
int  minotaurTile 
)
private

Utility to render the 3 strings of the tile in the representation frame.

Parameters
frameReference to the frame to print into.
rowThe board's row to print.
colThe board's column to print.
theseusTileThe current tile of the Theseus.
minotaurTileThe current tile of the Minotaur.

Definition at line 585 of file Board.java.

◆ setMoves()

void host.labyrinth.Board.setMoves ( ArrayList< Integer[]>  moves)
package
Parameters
movesReference to moves that we want to act as replacement for the inner moves vector.
Note
Use with care. Any call to this function will probably add memory for the garbage collector.

Definition at line 329 of file Board.java.

◆ setN()

void host.labyrinth.Board.setN ( int  N)
package

Definition at line 300 of file Board.java.

◆ setS()

void host.labyrinth.Board.setS ( int  S)
package

Definition at line 301 of file Board.java.

◆ setSupplies()

void host.labyrinth.Board.setSupplies ( Supply []  supplies)
package
Parameters
suppliesReference to supplies that we want to act as replacement for the inner supplies array.
Note
Use with care. Any call to this function will probably add memory for the garbage collector.

Definition at line 315 of file Board.java.

◆ setTiles()

void host.labyrinth.Board.setTiles ( Tile []  tiles)
package
Parameters
tilesReference to tiles that we want to act as replacement for the inner tiles array.
Note
Use with care. Any call to this function will probably add memory for the garbage collector.

Definition at line 309 of file Board.java.

◆ setW()

void host.labyrinth.Board.setW ( int  W)
package

Definition at line 302 of file Board.java.

◆ setWalls()

void host.labyrinth.Board.setWalls ( ArrayList< Edge walls)
package
Parameters
wallsReference to walls that we want to act as replacement for the inner walls vector.
Note
Use with care. Any call to this function will probably add memory for the garbage collector.

Definition at line 322 of file Board.java.

◆ size()

int host.labyrinth.Board.size ( )
package
Returns
the size of each site of the board.

Definition at line 220 of file Board.java.

◆ tryPickSupply()

int host.labyrinth.Board.tryPickSupply ( int  tileId)
package

Try to pick supply from a tile.

If succeed it also erases the supply from the board.

Parameters
tileIdThe tile to check
Returns
The id of supply.

Definition at line 201 of file Board.java.

◆ updateMove()

void host.labyrinth.Board.updateMove ( int []  m,
int  playerId 
)
package

Utility to update the moves of each player.

This function is used by the players to update their position on the board. After that a player can read other player positions using getOpponentMoves()

See also
getOpponentMoves()
Parameters
mReference to new move data
playerIdThe id of the player who update his/her data.

Definition at line 258 of file Board.java.

Member Data Documentation

◆ downTileId

IntFunction<Integer> host.labyrinth.Board.downTileId = (id) -> { return Position.toID(Position.toRow(id)-1, Position.toCol(id) ); }
private

Definition at line 622 of file Board.java.

◆ leftTileId

IntFunction<Integer> host.labyrinth.Board.leftTileId = (id) -> { return Position.toID(Position.toRow(id), Position.toCol(id)-1); }
private

Definition at line 619 of file Board.java.

◆ moves

ArrayList<Integer[]> host.labyrinth.Board.moves
private

Definition at line 636 of file Board.java.

◆ N

int host.labyrinth.Board.N
private

The size of each edge of the board.

Definition at line 627 of file Board.java.

◆ rightTileId

IntFunction<Integer> host.labyrinth.Board.rightTileId = (id) -> { return Position.toID(Position.toRow(id), Position.toCol(id)+1); }
private

Definition at line 620 of file Board.java.

◆ S

int host.labyrinth.Board.S
private

The number of the supplies on the board.

Definition at line 628 of file Board.java.

◆ supplies

Supply [] host.labyrinth.Board.supplies
private

Array to hold all the supplies on the board.

Definition at line 631 of file Board.java.

◆ tiles

Tile [] host.labyrinth.Board.tiles
private

Array to hold all the tiles for the board.

Definition at line 630 of file Board.java.

◆ upTileId

IntFunction<Integer> host.labyrinth.Board.upTileId = (id) -> { return Position.toID(Position.toRow(id)+1, Position.toCol(id) ); }
private

Definition at line 621 of file Board.java.

◆ W

int host.labyrinth.Board.W
private

The number of walls on the board.

Definition at line 629 of file Board.java.

◆ walls

ArrayList<Edge> host.labyrinth.Board.walls
private

Array to hold all the walls using the edge representation required by the closed room preventing algorithm.

Definition at line 632 of file Board.java.


The documentation for this class was generated from the following file: