Labyrinth
A labyrinth game assignment
Common.java
Go to the documentation of this file.
1 
9 package host.labyrinth;
10 
11 import java.util.ArrayList;
12 import java.util.Collections;
13 
17 class Const {
18  static final int maxTileWalls = 2;
19  static final int noSupply =-1;
20  static final int noTileId =-1;
21  static final int EOR =-1;
22 }
26 class Session {
27  static int boardSize = 15;
28  static int supplySize = 4;
29  static int maxRounds = 100;
30  static boolean loopGuard = false;
31  static boolean interactive = false;
32 }
33 
37 class Direction {
38  static final int UP =1;
39  static final int RIGHT =3;
40  static final int DOWN =5;
41  static final int LEFT =7;
48  static int opposite (int direction) { return (direction+4)%DirRange.End; }
49 }
50 
63 class DirRange {
64  static final int Begin =1;
65  static final int End =8;
66  static final int Step =2;
67 }
68 
78 class Position {
79 
85  Position(int row, int col) {
86  this.id = toID(row, col);
87  }
88 
93  Position(int tileId) {
94  this.id = tileId;
95  }
96 
106  Position(int row, int col, int direction) {
107  switch (direction) {
108  case Direction.UP: this.id = toID(row+1, col); break;
109  case Direction.DOWN: this.id = toID(row-1, col); break;
110  case Direction.LEFT: this.id = toID(row, col-1); break;
111  case Direction.RIGHT:this.id = toID(row, col+1); break;
112  }
113  }
114 
117  int getRow() { return toRow(id); }
118  int getCol() { return toCol(id); }
119  int getId() { return id; }
130  static int toID(int row, int col) {
131  return row * Session.boardSize + col;
132  }
133 
139  static int toRow(int id){
140  return id / Session.boardSize;
141  }
147  static int toCol(int id) {
148  return id % Session.boardSize;
149  }
154  private int id;
156 }
157 
161 class Range {
167  Range (int begin, int end) {
168  numbers = new ArrayList<Integer>();
169  init (begin, end, 1);
170  }
177  Range(int begin, int end, int step) {
178  numbers = new ArrayList<Integer>();
179  init (begin, end, step);
180  }
181 
185  private void init (int begin, int end, int step) {
186  numbers.clear();
187  for (int i=begin ; i<end ; i+=step)
188  numbers.add(i);
189  }
194  int get () {
195  if (!numbers.isEmpty())
196  return numbers.remove(0);
197  return Const.EOR;
198  }
199 
202  protected ArrayList<Integer> numbers;
204 }
205 
209 class ShuffledRange extends Range {
215  ShuffledRange(int begin, int end) {
216  super(begin, end); // Delegate
217  Collections.shuffle(numbers);
218  }
227  ShuffledRange(int begin, int end, int step) {
228  super(begin, end, step); // Delegate
229  Collections.shuffle(numbers);
230  }
231 }
232 
264 class Edge {
271  Edge(int tileId, int direction) {
272  int N = Session.boardSize +1;
273  switch (direction) {
274  case Direction.UP:
275  v1= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId);
276  v2= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId) + 1;
277  break;
278  case Direction.DOWN:
279  v1= (Position.toRow(tileId))*N + Position.toCol(tileId);
280  v2= (Position.toRow(tileId))*N + Position.toCol(tileId) + 1;
281  break;
282  case Direction.LEFT:
283  v1= (Position.toRow(tileId))*N + Position.toCol(tileId);
284  v2= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId);
285  break;
286  case Direction.RIGHT:
287  v1= (Position.toRow(tileId))*N + Position.toCol(tileId) + 1;
288  v2= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId) +1;
289  break;
290  }
291  }
293  Edge(Edge e) {
294  v1 = e.getV1();
295  v2 = e.getV2();
296  }
298  int getV1() { return v1; }
300  int getV2() { return v2; }
301 
302  private int v1;
303  private int v2;
304 }
305 
327 class Graph {
332  Graph (int v) {
333  V = v;
334  E = new ArrayList<Graph>();
335  }
340  Graph (Edge e) {
341  V = e.getV1();
342  E = new ArrayList<Graph>();
343  E.add(new Graph(e.getV2()));
344  }
345 
347  int getV() { return V; }
349  ArrayList<Graph> getE() { return E; }
350 
359  boolean attach (Edge e) {
360  return tryAttach(e, 0) > 0;
361  }
362 
368  int count (int v) {
369  return tryCount (v, 0);
370  }
371 
383  private int tryAttach (Edge e, int count) {
384  for (Graph n: E)
385  count = n.tryAttach (e, count);
386  if (V == e.getV1()) {
387  E.add(new Graph(e.getV2()));
388  ++count;
389  }
390  if (V == e.getV2()) {
391  E.add(new Graph(e.getV1()));
392  ++count;
393  }
394  return count;
395  }
396 
405  private int tryCount (int v, int count) {
406  for (Graph n: E)
407  count = n.tryCount (v, count);
408  if (V == v)
409  return ++count;
410  return count;
411  }
412 
413  private int V;
414  private ArrayList<Graph> E;
415 }
static final int RIGHT
East direction.
Definition: Common.java:39
Class to hold constant values for entire application.
Definition: Common.java:17
int getV1()
Access of the first node of the edge.
Definition: Common.java:298
Helper C++-like enumerator class to hold direction.
Definition: Common.java:37
Position(int tileId)
Basic constructor from Id.
Definition: Common.java:93
static final int End
Iterator style end of range direction (one place after the last)
Definition: Common.java:65
int id
The id coordinate of the constructed Position object.
Definition: Common.java:154
Graph(int v)
Constructs a node of the graph using the value of a vertex(node).
Definition: Common.java:332
ArrayList< Graph > E
A list of all the child nodes.
Definition: Common.java:414
void init(int begin, int end, int step)
Common utility to create the range for all constructors.
Definition: Common.java:185
int getV2()
Access of the second node of the edge.
Definition: Common.java:300
Range(int begin, int end, int step)
Create the range [begin, end) using step as interval between items.
Definition: Common.java:177
int v2
Second vertex of the edge.
Definition: Common.java:303
ShuffledRange(int begin, int end)
Create a shuffled version of range [begin, end)
Definition: Common.java:215
Edge(int tileId, int direction)
This constructor acts as the interface between the application&#39;s wall representation and the one base...
Definition: Common.java:271
int tryCount(int v, int count)
Recursive algorithm that tries to count the number of vertices on the graph with the same value as v...
Definition: Common.java:405
int tryAttach(Edge e, int count)
Recursive algorithm that tries to attach an edge into a graph IFF the graph already has a vertex with...
Definition: Common.java:383
Provides a graph functionality for the room preventing algorithm.
Definition: Common.java:327
Helper C++ like enumerator class for direction ranged loops.
Definition: Common.java:63
int getId()
Read access to id coordinate.
Definition: Common.java:119
Position(int row, int col)
Basic constructor from row-column coordinates.
Definition: Common.java:85
int V
The value of the current vertex/node.
Definition: Common.java:413
int getRow()
Read access to virtual row coordinate.
Definition: Common.java:117
int getCol()
Read access to virtual column coordinate.
Definition: Common.java:118
ArrayList< Graph > getE()
Access to the links of the current vertex.
Definition: Common.java:349
static int toRow(int id)
Takes Id coordinate and return the corresponding row coordinate.
Definition: Common.java:139
Edge(Edge e)
A deep copy contructor.
Definition: Common.java:293
static final int EOR
Number to indicate the End Of Range.
Definition: Common.java:21
Graph(Edge e)
Constructor that transform an edge into graph.
Definition: Common.java:340
static int toCol(int id)
Takes Id coordinate and return the corresponding column coordinate.
Definition: Common.java:147
Position(int row, int col, int direction)
Constructor from row-column coordinates and a direction.
Definition: Common.java:106
An Application wide board position implementation holding just the id coordinate. ...
Definition: Common.java:78
static final int maxTileWalls
Number of maximum walls for each tile on the board.
Definition: Common.java:18
boolean attach(Edge e)
Attach an edge into a graph IFF the graph already has a vertex with the same value as one of the vert...
Definition: Common.java:359
int v1
First vertex of the edge.
Definition: Common.java:302
int getV()
Access to the current vertex.
Definition: Common.java:347
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
Class to create ranges of numbers.
Definition: Common.java:161
ArrayList< Integer > numbers
handle to range
Definition: Common.java:202
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
static final int noTileId
Number to indicate wrong tileId.
Definition: Common.java:20
A utility class used for room prevent algorithm.
Definition: Common.java:264
int count(int v)
Counts the number of vertices on the graph with the value of v
Definition: Common.java:368
static final int UP
North direction.
Definition: Common.java:38
static int opposite(int direction)
Utility to get the opposite direction.
Definition: Common.java:48
static final int DOWN
South direction.
Definition: Common.java:40
Range(int begin, int end)
Create the range [begin, end)
Definition: Common.java:167
ShuffledRange(int begin, int end, int step)
Create a shuffled version of the range [begin, end) using step as interval between items...
Definition: Common.java:227
Class to create shuffled ranges of numbers.
Definition: Common.java:209