Labyrinth
A labyrinth game assignment
|
Go to the documentation of this file.
13 package host.labyrinth;
15 import java.util.ArrayList;
16 import java.util.function.IntFunction;
38 walls =
new ArrayList<Edge>();
47 assert (
N%2 != 0) :
"Board's size has to be an odd number.";
48 assert (
S <= (
N*
N-2)) :
"At least 2 tiles has to be without supplies.";
54 walls =
new ArrayList<Edge>();
111 String[][] frame =
new String[2*
N+1][
N];
113 for (
int row=0 ; row<
N ; ++row) {
115 for (col =0 ; col<
N-1 ; ++col)
116 renderTile(frame, row, col, theseusTile, minotaurTile);
132 for (
int i=sBoard.length-1 ; i>=0 ; --i) {
133 for (String it : sBoard[i])
134 System.out.print(it);
135 System.out.println();
295 for (
int tileId, i=0 ; i<
supplies.length ; ++i) {
299 while (tileId == theseusTile || tileId == minotaurTile);
317 ArrayList<Edge> w =
new ArrayList<Edge>();
325 for (
int i =0,
S=w.size() ; i<
S ; ++i)
330 }
while (
size != w.size());
336 if (g.
count(it.getV1()) > 1)
return true;
337 if (g.
count(it.getV2()) > 1)
return true;
416 for (
int i =0 ; i<
tiles.length ; ++i) {
421 wallCount += ((up?1:0) + (down?1:0) + (left?1:0) + (right?1:0));
422 tiles[i] =
new Tile (i, up, down, left, right);
443 dir = randDirections.
get();
461 for (
int tileId, i =0,
walls =0, shuffleMark =0 ; true ; ) {
465 if (i == shuffleMark)
487 private String
getTileBody (
int row,
int col,
int theseusTile,
int minotaurTile) {
489 boolean T = (tileId == theseusTile) ?
true :
false;
490 boolean M = (tileId == minotaurTile) ?
true :
false;
493 if (T && !M)
return " T ";
494 else if (T && M)
return "T+M";
500 return String.format(
"s%02d",
S+1);
513 private void renderTile(String[][] frame,
int row,
int col,
int theseusTile,
int minotaurTile) {
514 IntFunction<Integer> toframe = (r)->{
return 2*r+1; };
519 +
getTileBody(row, col, theseusTile, minotaurTile);
533 private void renderSentinelTile(String[][] frame,
int row,
int col,
int theseusTile,
int minotaurTile ) {
534 IntFunction<Integer> toframe = (r)->{
return 2*r+1; };
static final int Step
Step for iterator style direction.
static int toCol(int id)
Takes Id coordinate and return the corresponding column coordinate.
static final int LEFT
West direction.
Class to hold constant values for entire application.
void pickSupply(Supply[] supplies, int supplyId)
Utility to find a supply in the supplies array and removes it.
void createInnerWall(int tileId)
Create randomly a wall in the wallable selected tile.
int createInnerWalls()
This utility creates the inner walls of the board.
IntFunction< Integer > leftTileId
boolean isUpSentinel(int tileId)
boolean hasWall(int direction)
Checks if the tile has wall in the requested direction.
String[][] getStringRepresentation(int theseusTile, int minotaurTile)
Returns a 2-D array with the string representation of the board.
void setTiles(Tile[] tiles)
Supply[] supplies
Array to hold all the supplies on the board.
int get()
Extract and return the first item from the range.
int tryPickSupply(int tileId)
Try to pick supply from a tile.
IntFunction< Integer > downTileId
int createBasicTileWalls()
This utility function create/allocate the tiles of the board and create the outer walls at the same t...
Class to create shuffled ranges of numbers.
This class is the representation of the supplies in the game.
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 ...
int S
The number of the supplies on the board.
ArrayList< Edge > walls
Array to hold all the walls using the edge representation required by the closed room preventing algo...
int W
The number of walls on the board.
Application wide object to hold settings like values for the session.
IntFunction< Integer > upTileId
boolean attach(Edge e)
Attach an edge into a graph IFF the graph already has a vertex with the same value as one of the vert...
static final int DOWN
South direction.
void createBoard(int theseusTile, int minotaurTile)
Creates the board with all the requested walls and supplies.
int hasSupply(Supply[] supplies)
Utility to check if the tile has a supply.
Board(int N, int S)
The main constructor for the application.
void createTiles()
This function creates randomly all the tiles of the board.
static final int RIGHT
East direction.
void setSupplies(Supply[] supplies)
int count(int v)
Counts the number of vertices on the graph with the value of v
int dice()
A plain fair dice functionality provided by the board.
boolean isLeftSentinel(int tileId)
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.
int hasWalls()
Checks if the tile has walls and return the number of them.
static final int UP
North direction.
void createSupplies(int theseusTile, int minotaurTile)
This function create randomly the board's supplies.
This class is the representation of the board's tile.
static int boardSize
Default board's size (if no one set it via command line)
static int toRow(int id)
Takes Id coordinate and return the corresponding row coordinate.
ArrayList< Edge > getWalls()
void setWall(int direction)
Sets the tile's wall in the requested direction.
boolean isWalkable(int tileId, int direction)
Predicate to check if a direction is Walkable.
An Application wide board position implementation holding just the id coordinate.
Helper C++ like enumerator class for direction ranged loops.
This class is the representation of the games's board.
boolean isWallable(int tileId)
Predicate to check if a tile is Wallable.
A utility class used for room prevent algorithm.
static final int Begin
Iterator style begin of range direction (starting north)
static final int End
Iterator style end of range direction (one place after the last)
boolean isWallableDir(int tileId, int direction)
Predicate to check if a tile direction is Wallable.
boolean isRightSentinel(int tileId)
int N
The size of each edge of the board.
Board(Board b)
Deep copy constructor.
Helper C++-like enumerator class to hold direction.
static final int noTileId
Number to indicate wrong tileId.
String getTileBody(int row, int col, int theseusTile, int minotaurTile)
Utility to get the body (center line) of the string representation of the tile.
void setWalls(ArrayList< Edge > walls)
Board()
The empty constructor for default initialization.
boolean isDownSentinel(int tileId)
Class to create ranges of numbers.
static final int noSupply
Number to indicate the absent of supply.
static int opposite(int direction)
Utility to get the opposite direction.
int getId()
Read access to id coordinate.
boolean isRoomCreator(int tileId, int direction)
Predicate to check if a wall creates a closed room.
static final int EOR
Number to indicate the End Of Range.
Provides a graph functionality for the room preventing algorithm.
static boolean loopGuard
When true a wall creation guard is added to prevent closed rooms inside the board.
void printBoard(String[][] sBoard)
Print board utility.
Tile[] tiles
Array to hold all the tiles for the board.
IntFunction< Integer > rightTileId
static int toID(int row, int col)
Takes row and column coordinates and return the calculated Id coordinate.
boolean isWalkable(int row, int col, int direction)
Predicate to check if a direction is Walkable.
static final int maxTileWalls
Number of maximum walls for each tile on the board.