Labyrinth
A labyrinth game assignment
Supply.java
Go to the documentation of this file.
1 
13 package host.labyrinth;
14 
23 class Supply {
24 
35  Supply(int id, int row, int col) {
36  // Boundary checks
37  assert (row >= 0 && row< Session.boardSize) : "Row coordinate must be in the range [0, Session.boardSize)";
38  assert (col >= 0 && col< Session.boardSize) : "Column coordinate must be in the range [0, Session.boardSize)";
39 
40  // Initialization
41  this.supplyId =id;
42  this.x =col;
43  this.y =row;
44  this.supplyTileId = Position.toID(row, col);
45  }
46 
53  Supply(int id, int tileId) {
54  // Boundary check
55  assert (id >= 0 && id <= Position.toID(Session.boardSize-1, Session.boardSize-1))
56  : "TileId must be in the range of [0, Session.boardSize^2)";
57 
58  // Initialization
59  this.supplyId = id;
60  this.supplyTileId = tileId;
61  this.x = Position.toCol(tileId);
62  this.y = Position.toRow(tileId);
63  }
64 
68  Supply (Supply s) {
69  this.supplyId = s.supplyId;
70  this.x = s.x;
71  this.y = s.y;
72  this.supplyTileId = s.supplyTileId;
73  // We achieve deep copy as the members are all primitives.
74  }
81  int supplyId () { return supplyId; }
88  int supplyId (int sID) { return supplyId = sID; }
89 
94  Position position () { return new Position (supplyTileId); }
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.supplyTileId = p.getId();
113  return p;
114  }
115 
123  Position position (int tileId) {
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.supplyTileId = p.getId(); // =tileId;
132  return p;
133  }
134 
138  void removeSupply () { this.supplyId = Const.noSupply; }
148  int getSupplyId () { return supplyId; }
149  int getX() { return x; }
150  int getY() { return y; }
151  int getSupplyTileId(){ return supplyTileId; }
152 
153  void setSupplyId(int Id) { supplyId = Id; }
154  void setX(int x) {
155  assert (x >= 0 && x< Session.boardSize) : "X(column) coordinate must be in the range [0, Session.boardSize)";
156  this.x = x;
157  this.supplyTileId = Position.toID(this.x, this.y);
158  }
159  void setY(int y) {
160  assert (y >= 0 && y< Session.boardSize) : "Y(row) coordinate must be in the range [0, Session.boardSize)";
161  this.y = y;
162  this.supplyTileId = Position.toID(this.x, this.y);
163  }
164  void setSupplyTileId(int tileId) {
165  assert (tileId >= 0 && tileId <= Position.toID(Session.boardSize-1, Session.boardSize-1))
166  : "TileId must be in the range of [0, Session.boardSize^2)";
167  this.supplyTileId = tileId;
168  this.x = Position.toCol(tileId);
169  this.y = Position.toRow(tileId);
170 
171  }
176  private int supplyId;
177  private int x;
178  private int y;
179  private int supplyTileId;
188 }
This class is the representation of the supplies in the game.
Definition: Supply.java:23
void setX(int x)
Definition: Supply.java:154
Class to hold constant values for entire application.
Definition: Common.java:21
int supplyId
The unique identifier of the tile.
Definition: Supply.java:176
Supply(int id, int row, int col)
The main constructor of the Supply constructed from (row,column)
Definition: Supply.java:35
Position position(int row, int col)
Set the position of the supply from a (row, column) pair.
Definition: Supply.java:104
void removeSupply()
Marks the supply removed.
Definition: Supply.java:138
void setY(int y)
Definition: Supply.java:159
int supplyTileId
The Id of the tile on the board, in witch the supply is located.
Definition: Supply.java:179
int x
The x coordinate of the tile as if the board lies in the 1st quadrant.
Definition: Supply.java:177
int getId()
Read access to id coordinate.
Definition: Common.java:154
int getRow()
Read access to virtual row coordinate.
Definition: Common.java:152
Position position(int tileId)
Set the position of the supply from a tileId.
Definition: Supply.java:123
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 setSupplyId(int Id)
Definition: Supply.java:153
Supply(Supply s)
A deep copy constructor.
Definition: Supply.java:68
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
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
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
Position position()
Definition: Supply.java:94
int supplyId(int sID)
Set the supplyId.
Definition: Supply.java:88
void setSupplyTileId(int tileId)
Definition: Supply.java:164
int y
The y coordinate of the tile as if the board lies in the 1st quadrant.
Definition: Supply.java:178
Supply(int id, int tileId)
A second constructor of the Supply constructed from supplyTileId.
Definition: Supply.java:53