Labyrinth
A labyrinth game assignment
Common.java
Go to the documentation of this file.
1 
12 package host.labyrinth;
13 
14 import java.util.ArrayList;
15 import java.util.Collections;
16 
20 class Const {
21  static final int maxTileWalls = 2;
22  static final int noSupply =-1;
23  static final int noTileId =-1;
24  static final int EOR =-1;
25 }
29 class Session {
30  static int boardSize = 15;
31  static int supplySize = 4;
32  static int maxRounds = 100;
33  static boolean loopGuard = false;
34  static boolean interactive = false;
35 }
36 
40 class Direction {
41  static final int UP =1;
42  static final int RIGHT =3;
43  static final int DOWN =5;
44  static final int LEFT =7;
51  static int opposite (int direction) { return (direction+4)%DirRange.End; }
52 }
53 
66 class DirRange {
67  static final int Begin =1;
68  static final int End =8;
69  static final int Step =2;
70 }
71 
81 class Position {
82 
88  Position(int row, int col) {
89  this.id = toID(row, col);
90  }
91 
96  Position(int tileId) {
97  this.id = tileId;
98  }
99 
109  Position(int row, int col, int direction) {
110  switch (direction) {
111  case Direction.UP: this.id = toID(row+1, col); break;
112  case Direction.DOWN: this.id = toID(row-1, col); break;
113  case Direction.LEFT: this.id = toID(row, col-1); break;
114  case Direction.RIGHT:this.id = toID(row, col+1); break;
115  }
116  }
117 
120  int getRow() { return toRow(id); }
121  int getCol() { return toCol(id); }
122  int getId() { return id; }
133  static int toID(int row, int col) {
134  return row * Session.boardSize + col;
135  }
136 
142  static int toRow(int id){
143  return id / Session.boardSize;
144  }
150  static int toCol(int id) {
151  return id % Session.boardSize;
152  }
157  private int id;
159 }
160 
164 class Range {
170  Range (int begin, int end) {
171  numbers = new ArrayList<Integer>();
172  init (begin, end, 1);
173  }
180  Range(int begin, int end, int step) {
181  numbers = new ArrayList<Integer>();
182  init (begin, end, step);
183  }
184 
188  private void init (int begin, int end, int step) {
189  numbers.clear();
190  for (int i=begin ; i<end ; i+=step)
191  numbers.add(i);
192  }
197  int get () {
198  if (!numbers.isEmpty())
199  return numbers.remove(0);
200  return Const.EOR;
201  }
202 
205  protected ArrayList<Integer> numbers;
207 }
208 
212 class ShuffledRange extends Range {
218  ShuffledRange(int begin, int end) {
219  super(begin, end); // Delegate
220  Collections.shuffle(numbers);
221  }
230  ShuffledRange(int begin, int end, int step) {
231  super(begin, end, step); // Delegate
232  Collections.shuffle(numbers);
233  }
234 }
235 
267 class Edge {
274  Edge(int tileId, int direction) {
275  int N = Session.boardSize +1;
276  switch (direction) {
277  case Direction.UP:
278  v1= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId);
279  v2= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId) + 1;
280  break;
281  case Direction.DOWN:
282  v1= (Position.toRow(tileId))*N + Position.toCol(tileId);
283  v2= (Position.toRow(tileId))*N + Position.toCol(tileId) + 1;
284  break;
285  case Direction.LEFT:
286  v1= (Position.toRow(tileId))*N + Position.toCol(tileId);
287  v2= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId);
288  break;
289  case Direction.RIGHT:
290  v1= (Position.toRow(tileId))*N + Position.toCol(tileId) + 1;
291  v2= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId) +1;
292  break;
293  }
294  }
296  Edge(Edge e) {
297  v1 = e.getV1();
298  v2 = e.getV2();
299  }
301  int getV1() { return v1; }
303  int getV2() { return v2; }
304 
305  private int v1;
306  private int v2;
307 }
308 
330 class Graph {
335  Graph (int v) {
336  V = v;
337  E = new ArrayList<Graph>();
338  }
343  Graph (Edge e) {
344  V = e.getV1();
345  E = new ArrayList<Graph>();
346  E.add(new Graph(e.getV2()));
347  }
348 
350  int getV() { return V; }
352  ArrayList<Graph> getE() { return E; }
353 
362  boolean attach (Edge e) {
363  return tryAttach(e, 0) > 0;
364  }
365 
371  int count (int v) {
372  return tryCount (v, 0);
373  }
374 
386  private int tryAttach (Edge e, int count) {
387  for (Graph n: E)
388  count = n.tryAttach (e, count);
389  if (V == e.getV1()) {
390  E.add(new Graph(e.getV2()));
391  ++count;
392  }
393  if (V == e.getV2()) {
394  E.add(new Graph(e.getV1()));
395  ++count;
396  }
397  return count;
398  }
399 
408  private int tryCount (int v, int count) {
409  for (Graph n: E)
410  count = n.tryCount (v, count);
411  if (V == v)
412  return ++count;
413  return count;
414  }
415 
416  private int V;
417  private ArrayList<Graph> E;
418 }
host.labyrinth.DirRange.Step
static final int Step
Step for iterator style direction.
Definition: Common.java:69
host.labyrinth.Position.toCol
static int toCol(int id)
Takes Id coordinate and return the corresponding column coordinate.
Definition: Common.java:150
host.labyrinth.Direction.LEFT
static final int LEFT
West direction.
Definition: Common.java:44
host.labyrinth.Const
Class to hold constant values for entire application.
Definition: Common.java:20
host.labyrinth.Edge.Edge
Edge(int tileId, int direction)
This constructor acts as the interface between the application's wall representation and the one base...
Definition: Common.java:274
host.labyrinth.Session.maxRounds
static int maxRounds
Default number of rounds per game (if no one set it via command line)
Definition: Common.java:32
host.labyrinth.Position.id
int id
The id coordinate of the constructed Position object.
Definition: Common.java:157
host.labyrinth.ShuffledRange.ShuffledRange
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:230
host.labyrinth.Graph.tryAttach
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:386
host.labyrinth.Graph.V
int V
The value of the current vertex/node.
Definition: Common.java:416
host.labyrinth.Position.getRow
int getRow()
Read access to virtual row coordinate.
Definition: Common.java:120
host.labyrinth.Edge.getV2
int getV2()
Access of the second node of the edge.
Definition: Common.java:303
host.labyrinth.ShuffledRange
Class to create shuffled ranges of numbers.
Definition: Common.java:212
host.labyrinth.Graph.E
ArrayList< Graph > E
A list of all the child nodes.
Definition: Common.java:417
host.labyrinth.Edge.getV1
int getV1()
Access of the first node of the edge.
Definition: Common.java:301
host.labyrinth.Graph.tryCount
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:408
host.labyrinth.Session
Application wide object to hold settings like values for the session.
Definition: Common.java:29
host.labyrinth.Graph.attach
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:362
host.labyrinth.Direction.DOWN
static final int DOWN
South direction.
Definition: Common.java:43
host.labyrinth.Edge.Edge
Edge(Edge e)
A deep copy contructor.
Definition: Common.java:296
host.labyrinth.Direction.RIGHT
static final int RIGHT
East direction.
Definition: Common.java:42
host.labyrinth.Position.Position
Position(int row, int col, int direction)
Constructor from row-column coordinates and a direction.
Definition: Common.java:109
host.labyrinth.Edge.v1
int v1
First vertex of the edge.
Definition: Common.java:305
host.labyrinth.Graph.count
int count(int v)
Counts the number of vertices on the graph with the value of v
Definition: Common.java:371
host.labyrinth.Range.numbers
ArrayList< Integer > numbers
handle to range
Definition: Common.java:205
host.labyrinth.Position.Position
Position(int row, int col)
Basic constructor from row-column coordinates.
Definition: Common.java:88
host.labyrinth.Range.Range
Range(int begin, int end)
Create the range [begin, end)
Definition: Common.java:170
host.labyrinth.Direction.UP
static final int UP
North direction.
Definition: Common.java:41
host.labyrinth.Session.boardSize
static int boardSize
Default board's size (if no one set it via command line)
Definition: Common.java:30
host.labyrinth.Position.toRow
static int toRow(int id)
Takes Id coordinate and return the corresponding row coordinate.
Definition: Common.java:142
host.labyrinth.Position
An Application wide board position implementation holding just the id coordinate.
Definition: Common.java:81
host.labyrinth.DirRange
Helper C++ like enumerator class for direction ranged loops.
Definition: Common.java:66
host.labyrinth.Graph.getE
ArrayList< Graph > getE()
Access to the links of the current vertex.
Definition: Common.java:352
host.labyrinth.Graph.Graph
Graph(Edge e)
Constructor that transform an edge into graph.
Definition: Common.java:343
host.labyrinth.Range.init
void init(int begin, int end, int step)
Common utility to create the range for all constructors.
Definition: Common.java:188
host.labyrinth.Position.Position
Position(int tileId)
Basic constructor from Id.
Definition: Common.java:96
host.labyrinth.Edge
A utility class used for room prevent algorithm.
Definition: Common.java:267
host.labyrinth.DirRange.Begin
static final int Begin
Iterator style begin of range direction (starting north)
Definition: Common.java:67
host.labyrinth.DirRange.End
static final int End
Iterator style end of range direction (one place after the last)
Definition: Common.java:68
host.labyrinth.Edge.v2
int v2
Second vertex of the edge.
Definition: Common.java:306
host.labyrinth.Range.Range
Range(int begin, int end, int step)
Create the range [begin, end) using step as interval between items.
Definition: Common.java:180
host.labyrinth.Direction
Helper C++-like enumerator class to hold direction.
Definition: Common.java:40
host.labyrinth.Const.noTileId
static final int noTileId
Number to indicate wrong tileId.
Definition: Common.java:23
host.labyrinth.Position.getCol
int getCol()
Read access to virtual column coordinate.
Definition: Common.java:121
host.labyrinth.Range
Class to create ranges of numbers.
Definition: Common.java:164
host.labyrinth.Session.supplySize
static int supplySize
Default board's supply size (if no one set it via command line)
Definition: Common.java:31
host.labyrinth.Const.noSupply
static final int noSupply
Number to indicate the absent of supply.
Definition: Common.java:22
host.labyrinth.Direction.opposite
static int opposite(int direction)
Utility to get the opposite direction.
Definition: Common.java:51
host.labyrinth.Position.getId
int getId()
Read access to id coordinate.
Definition: Common.java:122
host.labyrinth.Const.EOR
static final int EOR
Number to indicate the End Of Range.
Definition: Common.java:24
host.labyrinth.Graph
Provides a graph functionality for the room preventing algorithm.
Definition: Common.java:330
host.labyrinth.Session.loopGuard
static boolean loopGuard
When true a wall creation guard is added to prevent closed rooms inside the board.
Definition: Common.java:33
host.labyrinth.Position.toID
static int toID(int row, int col)
Takes row and column coordinates and return the calculated Id coordinate.
Definition: Common.java:133
host.labyrinth.Const.maxTileWalls
static final int maxTileWalls
Number of maximum walls for each tile on the board.
Definition: Common.java:21
host.labyrinth.Graph.getV
int getV()
Access to the current vertex.
Definition: Common.java:350
host.labyrinth.ShuffledRange.ShuffledRange
ShuffledRange(int begin, int end)
Create a shuffled version of range [begin, end)
Definition: Common.java:218
host.labyrinth.Session.interactive
static boolean interactive
When true each round of the game requires user input.
Definition: Common.java:34
host.labyrinth.Graph.Graph
Graph(int v)
Constructs a node of the graph using the value of a vertex(node).
Definition: Common.java:335