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 numOfPlayers = 2;
23  static final int maxTileWalls = 2;
24  static final int noSupply =-1;
25  static final int noOpponent =-1;
26  static final int noTileId =-1;
27  static final int EOR =-1;
28  static final int viewDistance =3;
29  static final int noView = viewDistance+1;
30 
33  static final double opponentFactor = 1.0;
34  static final double supplyFactor = 0.65;
35  static final double preMoveFactor = 0.65;
36  static final double postMoveFactor = 0.35;
37  static final int minimaxTreeDepth = 4;
39 }
43 class Session {
44  static int boardSize = 15;
45  static int supplySize = 4;
46  static int maxRounds = 100;
47  static boolean loopGuard = false;
48  static boolean interactive = false;
49 }
50 
63 class DirRange {
64  static final int Begin =1;
65  static final int End =8;
66  static final int Step =2;
67  static final int numOfDirections =4;
68 }
69 
73 class Direction {
74  static final int UP =1;
75  static final int RIGHT =3;
76  static final int DOWN =5;
77  static final int LEFT =7;
78  static final int NONE =8;
85  static int opposite (int direction) {
86  return (direction != NONE) ? (direction+4)%DirRange.End : NONE;
87  }
88 
89  static int get (int fromId, int toId) {
90  if (Position.toID(Position.toRow(fromId), Position.toCol(fromId)-1) == toId)
91  return Direction.LEFT;
92  else if (Position.toID(Position.toRow(fromId), Position.toCol(fromId)+1) == toId)
93  return Direction.RIGHT;
94  else if (Position.toID(Position.toRow(fromId)+1, Position.toCol(fromId) ) == toId)
95  return Direction.UP;
96  else if (Position.toID(Position.toRow(fromId)-1, Position.toCol(fromId) ) == toId)
97  return Direction.DOWN;
98  else
99  return DirRange.End;
100  }
101 }
102 
112 class Position {
113 
119  Position(int row, int col) {
120  this.id = toID(row, col);
121  }
122 
127  Position(int tileId) {
128  this.id = tileId;
129  }
130 
140  Position(int row, int col, int direction) {
141  switch (direction) {
142  case Direction.UP: this.id = toID(row+1, col); break;
143  case Direction.DOWN: this.id = toID(row-1, col); break;
144  case Direction.LEFT: this.id = toID(row, col-1); break;
145  case Direction.RIGHT:this.id = toID(row, col+1); break;
146  case Direction.NONE: this.id = toID(row, col); break;
147  }
148  }
149 
152  int getRow() { return toRow(id); }
153  int getCol() { return toCol(id); }
154  int getId() { return id; }
165  static int toID(int row, int col) {
166  return row * Session.boardSize + col;
167  }
168 
174  static int toRow(int id){
175  return id / Session.boardSize;
176  }
182  static int toCol(int id) {
183  return id % Session.boardSize;
184  }
189  private int id;
191 }
192 
196 class Range {
202  Range (int begin, int end) {
203  numbers = new ArrayList<Integer>();
204  init (begin, end, 1);
205  }
212  Range(int begin, int end, int step) {
213  numbers = new ArrayList<Integer>();
214  init (begin, end, step);
215  }
216 
220  private void init (int begin, int end, int step) {
221  numbers.clear();
222  for (int i=begin ; i<end ; i+=step)
223  numbers.add(i);
224  }
229  int get () {
230  if (!numbers.isEmpty())
231  return numbers.remove(0);
232  return Const.EOR;
233  }
234 
238  int size () {
239  return numbers.size();
240  }
241 
244  protected ArrayList<Integer> numbers;
246 }
247 
251 class ShuffledRange extends Range {
257  ShuffledRange(int begin, int end) {
258  super(begin, end); // Delegate
259  Collections.shuffle(numbers);
260  }
269  ShuffledRange(int begin, int end, int step) {
270  super(begin, end, step); // Delegate
271  Collections.shuffle(numbers);
272  }
273 }
274 
306 class Edge {
313  Edge(int tileId, int direction) {
314  int N = Session.boardSize +1;
315  switch (direction) {
316  case Direction.UP:
317  v1= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId);
318  v2= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId) + 1;
319  break;
320  case Direction.DOWN:
321  v1= (Position.toRow(tileId))*N + Position.toCol(tileId);
322  v2= (Position.toRow(tileId))*N + Position.toCol(tileId) + 1;
323  break;
324  case Direction.LEFT:
325  v1= (Position.toRow(tileId))*N + Position.toCol(tileId);
326  v2= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId);
327  break;
328  case Direction.RIGHT:
329  v1= (Position.toRow(tileId))*N + Position.toCol(tileId) + 1;
330  v2= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId) +1;
331  break;
332  }
333  }
335  Edge(Edge e) {
336  v1 = e.getV1();
337  v2 = e.getV2();
338  }
340  int getV1() { return v1; }
342  int getV2() { return v2; }
343 
344  private int v1;
345  private int v2;
346 }
347 
369 class Graph {
374  Graph (int v) {
375  V = v;
376  E = new ArrayList<Graph>();
377  }
382  Graph (Edge e) {
383  V = e.getV1();
384  E = new ArrayList<Graph>();
385  E.add(new Graph(e.getV2()));
386  }
387 
389  int getV() { return V; }
391  ArrayList<Graph> getE() { return E; }
392 
401  boolean attach (Edge e) {
402  return tryAttach(e, 0) > 0;
403  }
404 
410  int count (int v) {
411  return tryCount (v, 0);
412  }
413 
425  private int tryAttach (Edge e, int count) {
426  for (Graph n: E)
427  count = n.tryAttach (e, count);
428  if (V == e.getV1()) {
429  E.add(new Graph(e.getV2()));
430  ++count;
431  }
432  if (V == e.getV2()) {
433  E.add(new Graph(e.getV1()));
434  ++count;
435  }
436  return count;
437  }
438 
447  private int tryCount (int v, int count) {
448  for (Graph n: E)
449  count = n.tryCount (v, count);
450  if (V == v)
451  return ++count;
452  return count;
453  }
454 
455  private int V;
456  private ArrayList<Graph> E;
457 }
static final int RIGHT
East direction.
Definition: Common.java:75
Class to hold constant values for entire application.
Definition: Common.java:21
static final double preMoveFactor
pre move distances factor
Definition: Common.java:35
int getV1()
Access of the first node of the edge.
Definition: Common.java:340
Helper C++-like enumerator class to hold direction.
Definition: Common.java:73
Position(int tileId)
Basic constructor from Id.
Definition: Common.java:127
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:189
Graph(int v)
Constructs a node of the graph using the value of a vertex(node).
Definition: Common.java:374
static final int minimaxTreeDepth
The maximum depth of the minimax tree.
Definition: Common.java:37
ArrayList< Graph > E
A list of all the child nodes.
Definition: Common.java:456
void init(int begin, int end, int step)
Common utility to create the range for all constructors.
Definition: Common.java:220
int getV2()
Access of the second node of the edge.
Definition: Common.java:342
Range(int begin, int end, int step)
Create the range [begin, end) using step as interval between items.
Definition: Common.java:212
int v2
Second vertex of the edge.
Definition: Common.java:345
ShuffledRange(int begin, int end)
Create a shuffled version of range [begin, end)
Definition: Common.java:257
static final int numOfPlayers
Definition: Common.java:22
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:313
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:447
static final double postMoveFactor
post move distances factor
Definition: Common.java:36
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:425
static final double opponentFactor
Parameters to control move evaluation.
Definition: Common.java:33
static final double supplyFactor
supply distance factor
Definition: Common.java:34
Provides a graph functionality for the room preventing algorithm.
Definition: Common.java:369
Helper C++ like enumerator class for direction ranged loops.
Definition: Common.java:63
int getId()
Read access to id coordinate.
Definition: Common.java:154
static final int noView
Definition: Common.java:29
Position(int row, int col)
Basic constructor from row-column coordinates.
Definition: Common.java:119
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:455
int getRow()
Read access to virtual row coordinate.
Definition: Common.java:152
int getCol()
Read access to virtual column coordinate.
Definition: Common.java:153
ArrayList< Graph > getE()
Access to the links of the current vertex.
Definition: Common.java:391
static int toRow(int id)
Takes Id coordinate and return the corresponding row coordinate.
Definition: Common.java:174
Edge(Edge e)
A deep copy contructor.
Definition: Common.java:335
static final int EOR
Number to indicate the End Of Range.
Definition: Common.java:27
Graph(Edge e)
Constructor that transform an edge into graph.
Definition: Common.java:382
static int toCol(int id)
Takes Id coordinate and return the corresponding column coordinate.
Definition: Common.java:182
Position(int row, int col, int direction)
Constructor from row-column coordinates and a direction.
Definition: Common.java:140
An Application wide board position implementation holding just the id coordinate. ...
Definition: Common.java:112
static final int maxTileWalls
Number of maximum walls for each tile on the board.
Definition: Common.java:23
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:401
int v1
First vertex of the edge.
Definition: Common.java:344
int getV()
Access to the current vertex.
Definition: Common.java:389
static final int NONE
No direction.
Definition: Common.java:78
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
Class to create ranges of numbers.
Definition: Common.java:196
ArrayList< Integer > numbers
handle to range
Definition: Common.java:244
static int boardSize
Default board&#39;s size (if no one set it via command line)
Definition: Common.java:44
static final int LEFT
West direction.
Definition: Common.java:77
static final int noTileId
Number to indicate wrong tileId.
Definition: Common.java:26
A utility class used for room prevent algorithm.
Definition: Common.java:306
int count(int v)
Counts the number of vertices on the graph with the value of v
Definition: Common.java:410
static final int UP
North direction.
Definition: Common.java:74
static final int noOpponent
Number to indicate the absent of supply.
Definition: Common.java:25
static int opposite(int direction)
Utility to get the opposite direction.
Definition: Common.java:85
static final int DOWN
South direction.
Definition: Common.java:76
Range(int begin, int end)
Create the range [begin, end)
Definition: Common.java:202
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:269
Class to create shuffled ranges of numbers.
Definition: Common.java:251