Labyrinth
A labyrinth game assignment
Supply.java
Go to the documentation of this file.
1 
10 package host.labyrinth;
11 
20 class Supply {
21 
32  Supply(int id, int row, int col) {
33  // Boundary checks
34  assert (row >= 0 && row< Session.boardSize) : "Row coordinate must be in the range [0, Session.boardSize)";
35  assert (col >= 0 && col< Session.boardSize) : "Column coordinate must be in the range [0, Session.boardSize)";
36 
37  // Initialization
38  this.supplyId =id;
39  this.x =col;
40  this.y =row;
41  this.supplyTileId = Position.toID(row, col);
42  }
43 
50  Supply(int id, int tileId) {
51  // Boundary check
52  assert (id >= 0 && id <= Position.toID(Session.boardSize-1, Session.boardSize-1))
53  : "TileId must be in the range of [0, Session.boardSize^2)";
54 
55  // Initialization
56  this.supplyId = id;
57  this.supplyTileId = tileId;
58  this.x = Position.toCol(tileId);
59  this.y = Position.toRow(tileId);
60  }
61 
65  Supply (Supply s) {
66  this.supplyId = s.supplyId;
67  this.x = s.x;
68  this.y = s.y;
69  this.supplyTileId = s.supplyTileId;
70  // We achieve deep copy as the members are all primitives.
71  }
78  int supplyId () { return supplyId; }
85  int supplyId (int sID) { return supplyId = sID; }
86 
91  Position position () { return new Position (supplyTileId); }
92 
101  Position position (int row, int col) {
102  // Boundary checks
103  assert (row >= 0 && row< Session.boardSize) : "Row coordinate must be in the range [0, Session.boardSize)";
104  assert (col >= 0 && col< Session.boardSize) : "Column coordinate must be in the range [0, Session.boardSize)";
105 
106  Position p = new Position (row, col);
107  this.x = p.getCol(); // =col;
108  this.y = p.getRow(); // =row;
109  this.supplyTileId = p.getId();
110  return p;
111  }
112 
120  Position position (int tileId) {
121  // Boundary check
122  assert (tileId >= 0 && tileId <= Position.toID(Session.boardSize-1, Session.boardSize-1))
123  : "TileId must be in the range of [0, Session.boardSize^2)";
124 
125  Position p = new Position (tileId);
126  this.x = p.getCol();
127  this.y = p.getRow();
128  this.supplyTileId = p.getId(); // =tileId;
129  return p;
130  }
131 
135  void removeSupply () { this.supplyId = Const.noSupply; }
145  int getSupplyId () { return supplyId; }
146  int getX() { return x; }
147  int getY() { return y; }
148  int getSupplyTileId(){ return supplyTileId; }
149 
150  void setSupplyId(int Id) { supplyId = Id; }
151  void setX(int x) {
152  assert (x >= 0 && x< Session.boardSize) : "X(column) coordinate must be in the range [0, Session.boardSize)";
153  this.x = x;
154  this.supplyTileId = Position.toID(this.x, this.y);
155  }
156  void setY(int y) {
157  assert (y >= 0 && y< Session.boardSize) : "Y(row) coordinate must be in the range [0, Session.boardSize)";
158  this.y = y;
159  this.supplyTileId = Position.toID(this.x, this.y);
160  }
161  void setSupplyTileId(int tileId) {
162  assert (tileId >= 0 && tileId <= Position.toID(Session.boardSize-1, Session.boardSize-1))
163  : "TileId must be in the range of [0, Session.boardSize^2)";
164  this.supplyTileId = tileId;
165  this.x = Position.toCol(tileId);
166  this.y = Position.toRow(tileId);
167 
168  }
173  private int supplyId;
174  private int x;
175  private int y;
176  private int supplyTileId;
185 }
This class is the representation of the supplies in the game.
Definition: Supply.java:20
void setX(int x)
Definition: Supply.java:151
Class to hold constant values for entire application.
Definition: Common.java:17
int supplyId
The unique identifier of the tile.
Definition: Supply.java:173
Supply(int id, int row, int col)
The main constructor of the Supply constructed from (row,column)
Definition: Supply.java:32
Position position(int row, int col)
Set the position of the supply from a (row, column) pair.
Definition: Supply.java:101
void removeSupply()
Marks the supply removed.
Definition: Supply.java:135
void setY(int y)
Definition: Supply.java:156
int supplyTileId
The Id of the tile on the board, in witch the supply is located.
Definition: Supply.java:176
int x
The x coordinate of the tile as if the board lies in the 1st quadrant.
Definition: Supply.java:174
int getId()
Read access to id coordinate.
Definition: Common.java:119
int getRow()
Read access to virtual row coordinate.
Definition: Common.java:117
Position position(int tileId)
Set the position of the supply from a tileId.
Definition: Supply.java:120
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 setSupplyId(int Id)
Definition: Supply.java:150
Supply(Supply s)
A deep copy constructor.
Definition: Supply.java:65
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
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
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
Position position()
Definition: Supply.java:91
int supplyId(int sID)
Set the supplyId.
Definition: Supply.java:85
void setSupplyTileId(int tileId)
Definition: Supply.java:161
int y
The y coordinate of the tile as if the board lies in the 1st quadrant.
Definition: Supply.java:175
Supply(int id, int tileId)
A second constructor of the Supply constructed from supplyTileId.
Definition: Supply.java:50