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 }
int tileId
The unique identifier of the tile.
Definition: Tile.java:251
This class is the representation of the supplies in the game.
Definition: Supply.java:23
static final int RIGHT
East direction.
Definition: Common.java:75
int hasWalls()
Checks if the tile has walls and return the number of them.
Definition: Tile.java:183
void clearWall(int direction)
Clears the tile&#39;s wall in the requested direction.
Definition: Tile.java:155
Position position()
Definition: Tile.java:97
Class to hold constant values for entire application.
Definition: Common.java:21
void pickSupply(Supply[] supplies, int supplyId)
Utility to find a supply in the supplies array and removes it.
Definition: Tile.java:204
Tile(int id, boolean up, boolean down, boolean left, boolean right)
The main constructor of the Tile constructed from tileId.
Definition: Tile.java:61
void setUp(boolean up)
Definition: Tile.java:243
Helper C++-like enumerator class to hold direction.
Definition: Common.java:73
boolean hasWall(int direction)
Checks if the tile has wall in the requested direction.
Definition: Tile.java:169
void setLeft(boolean left)
Definition: Tile.java:246
boolean down
Indicator of a wall in the south side of the tile.
Definition: Tile.java:258
int x
The x coordinate(column) of the tile as if the board lies in the 1st quadrant.
Definition: Tile.java:255
This class is the representation of the board&#39;s tile.
Definition: Tile.java:22
int getId()
Read access to id coordinate.
Definition: Common.java:154
Position position(int row, int col)
Set the position of the tile from a (row, column) pair.
Definition: Tile.java:107
boolean getLeft()
Definition: Tile.java:223
int getRow()
Read access to virtual row coordinate.
Definition: Common.java:152
int getCol()
Read access to virtual column coordinate.
Definition: Common.java:153
static int toRow(int id)
Takes Id coordinate and return the corresponding row coordinate.
Definition: Common.java:174
void setX(int x)
Definition: Tile.java:233
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
Tile(Tile t)
A deep copy constructor.
Definition: Tile.java:78
int y
The y coordinate(row) of the tile as if the board lies in the 1st quadrant.
Definition: Tile.java:256
static int toCol(int id)
Takes Id coordinate and return the corresponding column coordinate.
Definition: Common.java:182
An Application wide board position implementation holding just the id coordinate. ...
Definition: Common.java:112
Position position(int tileId)
Set the position of the tile from a tileId.
Definition: Tile.java:126
int hasSupply(Supply[] supplies)
Utility to check if the tile has a supply.
Definition: Tile.java:192
void setWall(int direction)
Sets the tile&#39;s wall in the requested direction.
Definition: Tile.java:142
static final int noSupply
Number to indicate the absent of supply.
Definition: Common.java:24
static int toID(int row, int col)
Takes row and column coordinates and return the calculated Id coordinate.
Definition: Common.java:165
void setTileId(int tileId)
Definition: Tile.java:226
void setY(int y)
Definition: Tile.java:238
boolean right
Indicator of a wall in the right side of the tile.
Definition: Tile.java:260
Application wide object to hold settings like values for the session.
Definition: Common.java:43
static int boardSize
Default board&#39;s size (if no one set it via command line)
Definition: Common.java:44
static final int LEFT
West direction.
Definition: Common.java:77
boolean getDown()
Definition: Tile.java:222
static final int UP
North direction.
Definition: Common.java:74
void setRight(boolean right)
Definition: Tile.java:245
static final int DOWN
South direction.
Definition: Common.java:76
boolean up
Indicator of a wall in the north side of the tile.
Definition: Tile.java:257
boolean getUp()
Definition: Tile.java:221
boolean getRight()
Definition: Tile.java:224
void setDown(boolean down)
Definition: Tile.java:244
boolean left
Indicator of a wall in the left side of the tile.
Definition: Tile.java:259