Labyrinth
A labyrinth game assignment
Tile.java
Go to the documentation of this file.
1 
13 package host.labyrinth;
14 
22 class Tile {
23 
37  Tile(int row, int col, boolean up, boolean down, boolean left, boolean right) {
38  // Boundary checks
39  assert (row >= 0 && row< Session.boardSize) : "Row coordinate must be in the range [0, Session.boardSize)";
40  assert (col >= 0 && col< Session.boardSize) : "Column coordinate must be in the range [0, Session.boardSize)";
41 
42  // Initialization
43  this.tileId = Position.toID(row, col);
44  this.x =col;
45  this.y =row;
46  this.up = up;
47  this.down = down;
48  this.left = left;
49  this.right = right;
50  }
51 
61  Tile(int id, boolean up, boolean down, boolean left, boolean right) {
62  // Boundary check
63  assert (id >= 0 && id <= Position.toID(Session.boardSize-1, Session.boardSize-1))
64  : "TileId must be in the range of [0, Session.boardSize^2)";
65  // Initialization
66  this.tileId = id;
67  this.x =Position.toCol(id);
68  this.y =Position.toRow(id);
69  this.up = up;
70  this.down = down;
71  this.left = left;
72  this.right = right;
73  }
74 
78  Tile (Tile t) {
79  this.tileId = t.tileId;
80  this.x = t.x;
81  this.y = t.y;
82  this.up = t.up;
83  this.down = t.down;
84  this.left = t.left;
85  this.right = t.right;
86  // We achieve deep copy as the members are all primitives.
87  }
97  Position position () { return new Position (tileId); }
98 
107  Position position (int row, int col) {
108  // Boundary checks
109  assert (row >= 0 && row< Session.boardSize) : "Row coordinate must be in the range [0, Session.boardSize)";
110  assert (col >= 0 && col< Session.boardSize) : "Column coordinate must be in the range [0, Session.boardSize)";
111 
112  Position p = new Position (row, col);
113  this.x = p.getCol(); // =col;
114  this.y = p.getRow(); // =row;
115  this.tileId = p.getId();
116  return p;
117  }
118 
127  // Boundary check
128  assert (tileId >= 0 && tileId <= Position.toID(Session.boardSize-1, Session.boardSize-1))
129  : "TileId must be in the range of [0, Session.boardSize^2)";
130 
131  Position p = new Position (tileId);
132  this.x = p.getCol();
133  this.y = p.getRow();
134  this.tileId = p.getId(); // =tileId;
135  return p;
136  }
137 
142  void setWall (int direction) {
143  switch (direction) {
144  case Direction.UP: this.up = true; break;
145  case Direction.DOWN: this.down = true; break;
146  case Direction.LEFT: this.left = true; break;
147  case Direction.RIGHT:this.right = true; break;
148  }
149  }
150 
155  void clearWall (int direction) {
156  switch (direction) {
157  case Direction.UP: this.up = false; break;
158  case Direction.DOWN: this.down = false; break;
159  case Direction.LEFT: this.left = false; break;
160  case Direction.RIGHT:this.right = false; break;
161  }
162  }
163 
169  boolean hasWall (int direction) {
170  switch (direction) {
171  case Direction.UP: return up;
172  case Direction.RIGHT: return right;
173  case Direction.DOWN: return down;
174  case Direction.LEFT: return left;
175  }
176  return false;
177  }
178 
183  int hasWalls () {
184  return ((up?1:0) + (down?1:0) + (left?1:0) + (right?1:0));
185  }
186 
192  int hasSupply (Supply[] supplies) {
193  for (Supply s: supplies)
194  if (s.position().getId() == position().getId())
195  return s.getSupplyId();
196  return Const.noSupply;
197  }
198 
204  void pickSupply (Supply[] supplies, int supplyId) {
205  for (Supply s: supplies)
206  if (s.getSupplyId() == supplyId)
207  s.removeSupply();
208  }
218  int getTileId () { return tileId; }
219  int getX () { return x; }
220  int getY () { return y; }
221  boolean getUp () { return up; }
222  boolean getDown () { return down; }
223  boolean getLeft () { return left; }
224  boolean getRight () { return right; }
225 
226  void setTileId(int tileId) {
227  assert (tileId >= 0 && tileId <= Position.toID(Session.boardSize-1, Session.boardSize-1))
228  : "TileId must be in the range of [0, Session.boardSize^2)";
229  this.tileId = tileId;
230  this.x = Position.toCol(tileId);
231  this.y = Position.toRow(tileId);
232  }
233  void setX(int x) {
234  assert (x >= 0 && x< Session.boardSize) : "X(column) coordinate must be in the range [0, Session.boardSize)";
235  this.x = x;
236  this.tileId = Position.toID(this.x, this.y);
237  }
238  void setY(int y) {
239  assert (y >= 0 && y< Session.boardSize) : "Y(row) coordinate must be in the range [0, Session.boardSize)";
240  this.y = y;
241  this.tileId = Position.toID(this.x, this.y);
242  }
243  void setUp(boolean up) { this.up = up; }
244  void setDown(boolean down) { this.down = down; }
245  void setRight(boolean right) { this.right = right; }
246  void setLeft(boolean left) { this.left = left; }
251  private int tileId;
255  private int x;
256  private int y;
257  private boolean up;
258  private boolean down;
259  private boolean left;
260  private boolean right;
269 }
host.labyrinth.Tile.setRight
void setRight(boolean right)
Definition: Tile.java:245
host.labyrinth.Position.toCol
static int toCol(int id)
Takes Id coordinate and return the corresponding column coordinate.
Definition: Common.java:150
host.labyrinth.Direction.LEFT
static final int LEFT
West direction.
Definition: Common.java:44
host.labyrinth.Const
Class to hold constant values for entire application.
Definition: Common.java:20
host.labyrinth.Tile.pickSupply
void pickSupply(Supply[] supplies, int supplyId)
Utility to find a supply in the supplies array and removes it.
Definition: Tile.java:204
host.labyrinth.Tile.y
int y
The y coordinate(row) of the tile as if the board lies in the 1st quadrant.
Definition: Tile.java:256
host.labyrinth.Tile.getRight
boolean getRight()
Definition: Tile.java:224
host.labyrinth.Tile.position
Position position(int row, int col)
Set the position of the tile from a (row, column) pair.
Definition: Tile.java:107
host.labyrinth.Tile.hasWall
boolean hasWall(int direction)
Checks if the tile has wall in the requested direction.
Definition: Tile.java:169
host.labyrinth.Tile.setX
void setX(int x)
Definition: Tile.java:233
host.labyrinth.Position.getRow
int getRow()
Read access to virtual row coordinate.
Definition: Common.java:120
host.labyrinth.Tile.getUp
boolean getUp()
Definition: Tile.java:221
host.labyrinth.Supply
This class is the representation of the supplies in the game.
Definition: Supply.java:23
host.labyrinth.Tile.setUp
void setUp(boolean up)
Definition: Tile.java:243
host.labyrinth.Tile.getX
int getX()
Definition: Tile.java:219
host.labyrinth.Tile.getLeft
boolean getLeft()
Definition: Tile.java:223
host.labyrinth.Session
Application wide object to hold settings like values for the session.
Definition: Common.java:29
host.labyrinth.Tile.position
Position position(int tileId)
Set the position of the tile from a tileId.
Definition: Tile.java:126
host.labyrinth.Direction.DOWN
static final int DOWN
South direction.
Definition: Common.java:43
host.labyrinth.Tile.hasSupply
int hasSupply(Supply[] supplies)
Utility to check if the tile has a supply.
Definition: Tile.java:192
host.labyrinth.Tile.setY
void setY(int y)
Definition: Tile.java:238
host.labyrinth.Direction.RIGHT
static final int RIGHT
East direction.
Definition: Common.java:42
host.labyrinth.Tile.clearWall
void clearWall(int direction)
Clears the tile's wall in the requested direction.
Definition: Tile.java:155
host.labyrinth.Tile.position
Position position()
Definition: Tile.java:97
host.labyrinth.Tile.tileId
int tileId
The unique identifier of the tile.
Definition: Tile.java:251
host.labyrinth.Tile.hasWalls
int hasWalls()
Checks if the tile has walls and return the number of them.
Definition: Tile.java:183
host.labyrinth.Direction.UP
static final int UP
North direction.
Definition: Common.java:41
host.labyrinth.Tile.Tile
Tile(int id, boolean up, boolean down, boolean left, boolean right)
The main constructor of the Tile constructed from tileId.
Definition: Tile.java:61
host.labyrinth.Tile
This class is the representation of the board's tile.
Definition: Tile.java:22
host.labyrinth.Session.boardSize
static int boardSize
Default board's size (if no one set it via command line)
Definition: Common.java:30
host.labyrinth.Position.toRow
static int toRow(int id)
Takes Id coordinate and return the corresponding row coordinate.
Definition: Common.java:142
host.labyrinth.Tile.setWall
void setWall(int direction)
Sets the tile's wall in the requested direction.
Definition: Tile.java:142
host.labyrinth.Tile.setLeft
void setLeft(boolean left)
Definition: Tile.java:246
host.labyrinth.Tile.Tile
Tile(int row, int col, boolean up, boolean down, boolean left, boolean right)
The main constructor of the Tile constructed from (row,column)
Definition: Tile.java:37
host.labyrinth.Position
An Application wide board position implementation holding just the id coordinate.
Definition: Common.java:81
host.labyrinth.Tile.down
boolean down
Indicator of a wall in the south side of the tile.
Definition: Tile.java:258
host.labyrinth.Tile.left
boolean left
Indicator of a wall in the left side of the tile.
Definition: Tile.java:259
host.labyrinth.Tile.getY
int getY()
Definition: Tile.java:220
host.labyrinth.Tile.setDown
void setDown(boolean down)
Definition: Tile.java:244
host.labyrinth.Tile.getDown
boolean getDown()
Definition: Tile.java:222
host.labyrinth.Tile.right
boolean right
Indicator of a wall in the right side of the tile.
Definition: Tile.java:260
host.labyrinth.Tile.getTileId
int getTileId()
Definition: Tile.java:218
host.labyrinth.Tile.Tile
Tile(Tile t)
A deep copy constructor.
Definition: Tile.java:78
host.labyrinth.Direction
Helper C++-like enumerator class to hold direction.
Definition: Common.java:40
host.labyrinth.Position.getCol
int getCol()
Read access to virtual column coordinate.
Definition: Common.java:121
host.labyrinth.Const.noSupply
static final int noSupply
Number to indicate the absent of supply.
Definition: Common.java:22
host.labyrinth.Position.getId
int getId()
Read access to id coordinate.
Definition: Common.java:122
host.labyrinth.Tile.up
boolean up
Indicator of a wall in the north side of the tile.
Definition: Tile.java:257
host.labyrinth.Tile.x
int x
The x coordinate(column) of the tile as if the board lies in the 1st quadrant.
Definition: Tile.java:255
host.labyrinth.Tile.setTileId
void setTileId(int tileId)
Definition: Tile.java:226
host.labyrinth.Position.toID
static int toID(int row, int col)
Takes row and column coordinates and return the calculated Id coordinate.
Definition: Common.java:133