Labyrinth
A labyrinth game assignment
Tile.java
Go to the documentation of this file.
1 
10 package host.labyrinth;
11 
19 class Tile {
20 
34  Tile(int row, int col, boolean up, boolean down, boolean left, boolean right) {
35  // Boundary checks
36  assert (row >= 0 && row< Session.boardSize) : "Row coordinate must be in the range [0, Session.boardSize)";
37  assert (col >= 0 && col< Session.boardSize) : "Column coordinate must be in the range [0, Session.boardSize)";
38 
39  // Initialization
40  this.tileId = Position.toID(row, col);
41  this.x =col;
42  this.y =row;
43  this.up = up;
44  this.down = down;
45  this.left = left;
46  this.right = right;
47  }
48 
58  Tile(int id, boolean up, boolean down, boolean left, boolean right) {
59  // Boundary check
60  assert (id >= 0 && id <= Position.toID(Session.boardSize-1, Session.boardSize-1))
61  : "TileId must be in the range of [0, Session.boardSize^2)";
62  // Initialization
63  this.tileId = id;
64  this.x =Position.toCol(id);
65  this.y =Position.toRow(id);
66  this.up = up;
67  this.down = down;
68  this.left = left;
69  this.right = right;
70  }
71 
75  Tile (Tile t) {
76  this.tileId = t.tileId;
77  this.x = t.x;
78  this.y = t.y;
79  this.up = t.up;
80  this.down = t.down;
81  this.left = t.left;
82  this.right = t.right;
83  // We achieve deep copy as the members are all primitives.
84  }
94  Position position () { return new Position (tileId); }
95 
104  Position position (int row, int col) {
105  // Boundary checks
106  assert (row >= 0 && row< Session.boardSize) : "Row coordinate must be in the range [0, Session.boardSize)";
107  assert (col >= 0 && col< Session.boardSize) : "Column coordinate must be in the range [0, Session.boardSize)";
108 
109  Position p = new Position (row, col);
110  this.x = p.getCol(); // =col;
111  this.y = p.getRow(); // =row;
112  this.tileId = p.getId();
113  return p;
114  }
115 
124  // Boundary check
125  assert (tileId >= 0 && tileId <= Position.toID(Session.boardSize-1, Session.boardSize-1))
126  : "TileId must be in the range of [0, Session.boardSize^2)";
127 
128  Position p = new Position (tileId);
129  this.x = p.getCol();
130  this.y = p.getRow();
131  this.tileId = p.getId(); // =tileId;
132  return p;
133  }
134 
139  void setWall (int direction) {
140  switch (direction) {
141  case Direction.UP: this.up = true; break;
142  case Direction.DOWN: this.down = true; break;
143  case Direction.LEFT: this.left = true; break;
144  case Direction.RIGHT:this.right = true; break;
145  }
146  }
147 
152  void clearWall (int direction) {
153  switch (direction) {
154  case Direction.UP: this.up = false; break;
155  case Direction.DOWN: this.down = false; break;
156  case Direction.LEFT: this.left = false; break;
157  case Direction.RIGHT:this.right = false; break;
158  }
159  }
160 
166  boolean hasWall (int direction) {
167  switch (direction) {
168  case Direction.UP: return up;
169  case Direction.RIGHT: return right;
170  case Direction.DOWN: return down;
171  case Direction.LEFT: return left;
172  }
173  return false;
174  }
175 
180  int hasWalls () {
181  return ((up?1:0) + (down?1:0) + (left?1:0) + (right?1:0));
182  }
183 
189  int hasSupply (Supply[] supplies) {
190  for (Supply s: supplies)
191  if (s.position().getId() == position().getId())
192  return s.getSupplyId();
193  return Const.noSupply;
194  }
195 
201  void pickSupply (Supply[] supplies, int supplyId) {
202  for (Supply s: supplies)
203  if (s.getSupplyId() == supplyId)
204  s.removeSupply();
205  }
215  int getTileId () { return tileId; }
216  int getX () { return x; }
217  int getY () { return y; }
218  boolean getUp () { return up; }
219  boolean getDown () { return down; }
220  boolean getLeft () { return left; }
221  boolean getRight () { return right; }
222 
223  void setTileId(int tileId) {
224  assert (tileId >= 0 && tileId <= Position.toID(Session.boardSize-1, Session.boardSize-1))
225  : "TileId must be in the range of [0, Session.boardSize^2)";
226  this.tileId = tileId;
227  this.x = Position.toCol(tileId);
228  this.y = Position.toRow(tileId);
229  }
230  void setX(int x) {
231  assert (x >= 0 && x< Session.boardSize) : "X(column) coordinate must be in the range [0, Session.boardSize)";
232  this.x = x;
233  this.tileId = Position.toID(this.x, this.y);
234  }
235  void setY(int y) {
236  assert (y >= 0 && y< Session.boardSize) : "Y(row) coordinate must be in the range [0, Session.boardSize)";
237  this.y = y;
238  this.tileId = Position.toID(this.x, this.y);
239  }
240  void setUp(boolean up) { this.up = up; }
241  void setDown(boolean down) { this.down = down; }
242  void setRight(boolean right) { this.right = right; }
243  void setLeft(boolean left) { this.left = left; }
248  private int tileId;
252  private int x;
253  private int y;
254  private boolean up;
255  private boolean down;
256  private boolean left;
257  private boolean right;
266 }
int tileId
The unique identifier of the tile.
Definition: Tile.java:248
This class is the representation of the supplies in the game.
Definition: Supply.java:20
static final int RIGHT
East direction.
Definition: Common.java:39
int hasWalls()
Checks if the tile has walls and return the number of them.
Definition: Tile.java:180
void clearWall(int direction)
Clears the tile&#39;s wall in the requested direction.
Definition: Tile.java:152
Position position()
Definition: Tile.java:94
Class to hold constant values for entire application.
Definition: Common.java:17
void pickSupply(Supply[] supplies, int supplyId)
Utility to find a supply in the supplies array and removes it.
Definition: Tile.java:201
Tile(int id, boolean up, boolean down, boolean left, boolean right)
The main constructor of the Tile constructed from tileId.
Definition: Tile.java:58
void setUp(boolean up)
Definition: Tile.java:240
Helper C++-like enumerator class to hold direction.
Definition: Common.java:37
boolean hasWall(int direction)
Checks if the tile has wall in the requested direction.
Definition: Tile.java:166
void setLeft(boolean left)
Definition: Tile.java:243
boolean down
Indicator of a wall in the south side of the tile.
Definition: Tile.java:255
int x
The x coordinate(column) of the tile as if the board lies in the 1st quadrant.
Definition: Tile.java:252
This class is the representation of the board&#39;s tile.
Definition: Tile.java:19
int getId()
Read access to id coordinate.
Definition: Common.java:119
Position position(int row, int col)
Set the position of the tile from a (row, column) pair.
Definition: Tile.java:104
boolean getLeft()
Definition: Tile.java:220
int getRow()
Read access to virtual row coordinate.
Definition: Common.java:117
int getCol()
Read access to virtual column coordinate.
Definition: Common.java:118
static int toRow(int id)
Takes Id coordinate and return the corresponding row coordinate.
Definition: Common.java:139
void setX(int x)
Definition: Tile.java:230
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:34
Tile(Tile t)
A deep copy constructor.
Definition: Tile.java:75
int y
The y coordinate(row) of the tile as if the board lies in the 1st quadrant.
Definition: Tile.java:253
static int toCol(int id)
Takes Id coordinate and return the corresponding column coordinate.
Definition: Common.java:147
An Application wide board position implementation holding just the id coordinate. ...
Definition: Common.java:78
Position position(int tileId)
Set the position of the tile from a tileId.
Definition: Tile.java:123
int hasSupply(Supply[] supplies)
Utility to check if the tile has a supply.
Definition: Tile.java:189
void setWall(int direction)
Sets the tile&#39;s wall in the requested direction.
Definition: Tile.java:139
static final int noSupply
Number to indicate the absent of supply.
Definition: Common.java:19
static int toID(int row, int col)
Takes row and column coordinates and return the calculated Id coordinate.
Definition: Common.java:130
void setTileId(int tileId)
Definition: Tile.java:223
void setY(int y)
Definition: Tile.java:235
boolean right
Indicator of a wall in the right side of the tile.
Definition: Tile.java:257
Application wide object to hold settings like values for the session.
Definition: Common.java:26
static int boardSize
Default board&#39;s size (if no one set it via command line)
Definition: Common.java:27
static final int LEFT
West direction.
Definition: Common.java:41
boolean getDown()
Definition: Tile.java:219
static final int UP
North direction.
Definition: Common.java:38
void setRight(boolean right)
Definition: Tile.java:242
static final int DOWN
South direction.
Definition: Common.java:40
boolean up
Indicator of a wall in the north side of the tile.
Definition: Tile.java:254
boolean getUp()
Definition: Tile.java:218
boolean getRight()
Definition: Tile.java:221
void setDown(boolean down)
Definition: Tile.java:241
boolean left
Indicator of a wall in the left side of the tile.
Definition: Tile.java:256