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 //import java.util.function.IntFunction;
17 
21 class Const {
22  static final int maxTileWalls = 2;
23  static final int noSupply =-1;
24  static final int noOpponent =-1;
25  static final int noTileId =-1;
26  static final int EOR =-1;
27  static final int moveItems =4;
28  static final int viewDistance =3;
30  static final double opponentFactor =1.0;
31  static final double supplyFactor =0.65;
32 }
36 class Session {
37  static int boardSize = 15;
38  static int supplySize = 4;
39  static int maxRounds = 100;
40  static boolean loopGuard = false;
41  static boolean interactive = false;
42 }
43 
56 class DirRange {
57  static final int Begin =1;
58  static final int End =8;
59  static final int Step =2;
60  static final int numOfDirections =4;
61 }
62 
66 class Direction {
67  static final int UP =1;
68  static final int RIGHT =3;
69  static final int DOWN =5;
70  static final int LEFT =7;
77  static int opposite (int direction) { return (direction+4)%DirRange.End; }
78 
79  static int get (int fromId, int toId) {
80  if (Position.toID(Position.toRow(fromId), Position.toCol(fromId)-1) == toId)
81  return Direction.LEFT;
82  else if (Position.toID(Position.toRow(fromId), Position.toCol(fromId)+1) == toId)
83  return Direction.RIGHT;
84  else if (Position.toID(Position.toRow(fromId)+1, Position.toCol(fromId) ) == toId)
85  return Direction.UP;
86  else if (Position.toID(Position.toRow(fromId)-1, Position.toCol(fromId) ) == toId)
87  return Direction.DOWN;
88  else
89  return DirRange.End;
90  }
91 }
92 
102 class Position {
103 
109  Position(int row, int col) {
110  this.id = toID(row, col);
111  }
112 
117  Position(int tileId) {
118  this.id = tileId;
119  }
120 
130  Position(int row, int col, int direction) {
131  switch (direction) {
132  case Direction.UP: this.id = toID(row+1, col); break;
133  case Direction.DOWN: this.id = toID(row-1, col); break;
134  case Direction.LEFT: this.id = toID(row, col-1); break;
135  case Direction.RIGHT:this.id = toID(row, col+1); break;
136  }
137  }
138 
141  int getRow() { return toRow(id); }
142  int getCol() { return toCol(id); }
143  int getId() { return id; }
154  static int toID(int row, int col) {
155  return row * Session.boardSize + col;
156  }
157 
163  static int toRow(int id){
164  return id / Session.boardSize;
165  }
171  static int toCol(int id) {
172  return id % Session.boardSize;
173  }
178  private int id;
180 }
181 
185 class Range {
191  Range (int begin, int end) {
192  numbers = new ArrayList<Integer>();
193  init (begin, end, 1);
194  }
201  Range(int begin, int end, int step) {
202  numbers = new ArrayList<Integer>();
203  init (begin, end, step);
204  }
205 
209  private void init (int begin, int end, int step) {
210  numbers.clear();
211  for (int i=begin ; i<end ; i+=step)
212  numbers.add(i);
213  }
218  int get () {
219  if (!numbers.isEmpty())
220  return numbers.remove(0);
221  return Const.EOR;
222  }
223 
227  int size () {
228  return numbers.size();
229  }
230 
233  protected ArrayList<Integer> numbers;
235 }
236 
240 class ShuffledRange extends Range {
246  ShuffledRange(int begin, int end) {
247  super(begin, end); // Delegate
248  Collections.shuffle(numbers);
249  }
258  ShuffledRange(int begin, int end, int step) {
259  super(begin, end, step); // Delegate
260  Collections.shuffle(numbers);
261  }
262 }
263 
295 class Edge {
302  Edge(int tileId, int direction) {
303  int N = Session.boardSize +1;
304  switch (direction) {
305  case Direction.UP:
306  v1= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId);
307  v2= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId) + 1;
308  break;
309  case Direction.DOWN:
310  v1= (Position.toRow(tileId))*N + Position.toCol(tileId);
311  v2= (Position.toRow(tileId))*N + Position.toCol(tileId) + 1;
312  break;
313  case Direction.LEFT:
314  v1= (Position.toRow(tileId))*N + Position.toCol(tileId);
315  v2= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId);
316  break;
317  case Direction.RIGHT:
318  v1= (Position.toRow(tileId))*N + Position.toCol(tileId) + 1;
319  v2= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId) +1;
320  break;
321  }
322  }
324  Edge(Edge e) {
325  v1 = e.getV1();
326  v2 = e.getV2();
327  }
329  int getV1() { return v1; }
331  int getV2() { return v2; }
332 
333  private int v1;
334  private int v2;
335 }
336 
358 class Graph {
363  Graph (int v) {
364  V = v;
365  E = new ArrayList<Graph>();
366  }
371  Graph (Edge e) {
372  V = e.getV1();
373  E = new ArrayList<Graph>();
374  E.add(new Graph(e.getV2()));
375  }
376 
378  int getV() { return V; }
380  ArrayList<Graph> getE() { return E; }
381 
390  boolean attach (Edge e) {
391  return tryAttach(e, 0) > 0;
392  }
393 
399  int count (int v) {
400  return tryCount (v, 0);
401  }
402 
414  private int tryAttach (Edge e, int count) {
415  for (Graph n: E)
416  count = n.tryAttach (e, count);
417  if (V == e.getV1()) {
418  E.add(new Graph(e.getV2()));
419  ++count;
420  }
421  if (V == e.getV2()) {
422  E.add(new Graph(e.getV1()));
423  ++count;
424  }
425  return count;
426  }
427 
436  private int tryCount (int v, int count) {
437  for (Graph n: E)
438  count = n.tryCount (v, count);
439  if (V == v)
440  return ++count;
441  return count;
442  }
443 
444  private int V;
445  private ArrayList<Graph> E;
446 }
static final int RIGHT
East direction.
Definition: Common.java:68
Class to hold constant values for entire application.
Definition: Common.java:21
int getV1()
Access of the first node of the edge.
Definition: Common.java:329
Helper C++-like enumerator class to hold direction.
Definition: Common.java:66
Position(int tileId)
Basic constructor from Id.
Definition: Common.java:117
static final int End
Iterator style end of range direction (one place after the last)
Definition: Common.java:58
int id
The id coordinate of the constructed Position object.
Definition: Common.java:178
Graph(int v)
Constructs a node of the graph using the value of a vertex(node).
Definition: Common.java:363
ArrayList< Graph > E
A list of all the child nodes.
Definition: Common.java:445
void init(int begin, int end, int step)
Common utility to create the range for all constructors.
Definition: Common.java:209
int getV2()
Access of the second node of the edge.
Definition: Common.java:331
Range(int begin, int end, int step)
Create the range [begin, end) using step as interval between items.
Definition: Common.java:201
int v2
Second vertex of the edge.
Definition: Common.java:334
ShuffledRange(int begin, int end)
Create a shuffled version of range [begin, end)
Definition: Common.java:246
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:302
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:436
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:414
static final double opponentFactor
Definition: Common.java:30
static final double supplyFactor
Definition: Common.java:31
Provides a graph functionality for the room preventing algorithm.
Definition: Common.java:358
Helper C++ like enumerator class for direction ranged loops.
Definition: Common.java:56
int getId()
Read access to id coordinate.
Definition: Common.java:143
static final int moveItems
The number of items return by move()
Definition: Common.java:27
Position(int row, int col)
Basic constructor from row-column coordinates.
Definition: Common.java:109
static final int viewDistance
The max distance of the Heuristic player&#39;s ability to see.
Definition: Common.java:28
int V
The value of the current vertex/node.
Definition: Common.java:444
int getRow()
Read access to virtual row coordinate.
Definition: Common.java:141
int getCol()
Read access to virtual column coordinate.
Definition: Common.java:142
ArrayList< Graph > getE()
Access to the links of the current vertex.
Definition: Common.java:380
static int toRow(int id)
Takes Id coordinate and return the corresponding row coordinate.
Definition: Common.java:163
Edge(Edge e)
A deep copy contructor.
Definition: Common.java:324
static final int EOR
Number to indicate the End Of Range.
Definition: Common.java:26
Graph(Edge e)
Constructor that transform an edge into graph.
Definition: Common.java:371
static int toCol(int id)
Takes Id coordinate and return the corresponding column coordinate.
Definition: Common.java:171
Position(int row, int col, int direction)
Constructor from row-column coordinates and a direction.
Definition: Common.java:130
An Application wide board position implementation holding just the id coordinate. ...
Definition: Common.java:102
static final int maxTileWalls
Number of maximum walls for each tile on the board.
Definition: Common.java:22
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:390
int v1
First vertex of the edge.
Definition: Common.java:333
int getV()
Access to the current vertex.
Definition: Common.java:378
static final int noSupply
Number to indicate the absent of supply.
Definition: Common.java:23
static int toID(int row, int col)
Takes row and column coordinates and return the calculated Id coordinate.
Definition: Common.java:154
Application wide object to hold settings like values for the session.
Definition: Common.java:36
Class to create ranges of numbers.
Definition: Common.java:185
ArrayList< Integer > numbers
handle to range
Definition: Common.java:233
static int boardSize
Default board&#39;s size (if no one set it via command line)
Definition: Common.java:37
static final int LEFT
West direction.
Definition: Common.java:70
static final int noTileId
Number to indicate wrong tileId.
Definition: Common.java:25
A utility class used for room prevent algorithm.
Definition: Common.java:295
int count(int v)
Counts the number of vertices on the graph with the value of v
Definition: Common.java:399
static final int UP
North direction.
Definition: Common.java:67
static final int noOpponent
Number to indicate the absent of supply.
Definition: Common.java:24
static int opposite(int direction)
Utility to get the opposite direction.
Definition: Common.java:77
static final int DOWN
South direction.
Definition: Common.java:69
Range(int begin, int end)
Create the range [begin, end)
Definition: Common.java:191
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:258
Class to create shuffled ranges of numbers.
Definition: Common.java:240