Labyrinth
A labyrinth game assignment
Player.java
Go to the documentation of this file.
1 
13 package host.labyrinth;
14 
15 import java.util.ArrayList;
16 
21 class Player {
23  static final int MOVE_DATA_SIZE = 4;
24  static final int MOVE_TILE_ID = 0;
25  static final int MOVE_ROW = 1;
26  static final int MOVE_COLUMN = 2;
27  static final int MOVE_DICE = 3;
40  Player(String name, boolean champion, Board board, int row, int column) throws Exception {
42  this.name = name;
43  this.board = board;
44  this.score = 0;
45  this.x = column;
46  this.y = row;
47  this.champion = champion;
48  this.dirCounter= new int[DirRange.End]; // yes we spoil some memory. Java is the worst.
49  this.path = new ArrayList<Integer[]>();
50  int[] m = {
51  Position.toID(row, column), row, column, Const.noSupply
52  };
54  }
55 
63  Player(String name, boolean champion, Board board, int tileId) throws Exception {
65  this.name = name;
66  this.board = board;
67  this.score = 0;
68  this.x = Position.toCol(tileId);
69  this.y = Position.toRow(tileId);
70  this.champion = champion;
71  this.dirCounter= new int[DirRange.End]; // yes we spoil some memory. Java is the worst.
72  this.path = new ArrayList<Integer[]>();
73  int[] m = {
74  tileId, Position.toRow(tileId), Position.toCol(tileId), Const.noSupply
75  };
77  }
100  int[] move(int id) {
101  // Initialize return array with the current data
102  int[] ret = new int[MOVE_DATA_SIZE];
103  ret[MOVE_TILE_ID] = id;
104  ret[MOVE_ROW] = Position.toRow(id);
105  ret[MOVE_COLUMN] = Position.toCol(id);
106  ret[MOVE_DICE] = Direction.NONE;
107  int supplyFlag =0, moveFlag =0;
108 
109  int diceDirection;
110  do
111  diceDirection = board.dice(); // throw the dice
112  while (!board.isWalkable(id, diceDirection));
113  moveFlag =1; // mark the successful move
114  // Get next tile
115  Position next = new Position(Position.toRow(id), Position.toCol(id), diceDirection);
116  ret[MOVE_TILE_ID] = next.getId(); // Update move's return data
117  ret[MOVE_ROW] = y = next.getRow();
118  ret[MOVE_COLUMN] = x = next.getCol();
119  ret[MOVE_DICE] = diceDirection;
120  // In case of a champion player, try also to pick a supply
121  if (champion && (board.tryPickSupply(next.getId()) != Const.noSupply)) {
122  supplyFlag =1; // mark the successful supply pickup
123  ++score; // keep score
124  }
125  ++dirCounter[diceDirection]; // update direction counters
126  board.updateMove(ret, playerId);
127  // update path
128  Integer[] p = {
129  ret[MOVE_TILE_ID], diceDirection, moveFlag, supplyFlag,
130  dirCounter[Direction.UP], dirCounter[Direction.RIGHT], dirCounter[Direction.DOWN], dirCounter[Direction.LEFT],
132  };
133  path.add(p);
134  return ret;
135  }
136 
140  void statistics() {
141  if (!path.isEmpty()) {
142  Integer[] last = path.get(path.size()-1);
143  String who = String.format("%12s", name);
144  System.out.print(who + ": score[" + score + "]" + ", dice =" + last[1] + ", tileId =" + last[0] + " (" + Position.toRow(last[0]) + ", " + Position.toCol(last[0]) + ")");
145  if (last[2] == 0)
146  System.out.println(" *Can not move.");
147  else if (last[3] != 0)
148  System.out.println(" *Found a supply.");
149  else
150  System.out.println("");
151  }
152  }
153 
162  void final_statistics () { }
163 
165  int playerTileId() { return Position.toID(y, x); }
167  int playerRow() { return y; }
169  int playerCol() { return x; }
179  int getPlayerId () { return playerId; }
180  String getName() { return name; }
181  Board getBoard () { return board; }
182  int getScore () { return score; }
183  int getX() { return x; }
184  int getY() { return y; }
185  boolean getChampion(){ return champion; }
186  int[] getDirCounter(){ return dirCounter; }
187  ArrayList<Integer[]> getPath() {
188  return path;
189  }
190 
191 
192  void setPlayerId(int id) { playerId = id; }
193  void setName(String name) { this.name = name; }
194  void setBoard (Board board){ this.board = board; }
195  void setScore(int score) { this.score = score; }
196  void setX(int x) {
197  assert (x >= 0 && x< Session.boardSize) : "X(column) coordinate must be in the range [0, Session.boardSize)";
198  this.x = x;
199  }
200  void setY(int y) {
201  assert (y >= 0 && y< Session.boardSize) : "Y(row) coordinate must be in the range [0, Session.boardSize)";
202  this.y = y;
203  }
204  void setChampion (boolean champion) {
205  this.champion = champion;
206  }
207  void setDirCounter(int[] dirCounter) {
208  this.dirCounter = dirCounter;
209  }
210 
211  void setPath(ArrayList<Integer[]> path) {
212  this.path = path;
213  }
218  protected int playerId;
219  protected String name;
220  protected Board board;
221  protected int score;
222  protected int x;
223  protected int y;
224  protected boolean champion;
225  protected int dirCounter[];
226  protected ArrayList<Integer[]> path;
244 }
static final int RIGHT
East direction.
Definition: Common.java:75
static final int MOVE_DICE
The index of dice information.
Definition: Player.java:27
void setBoard(Board board)
Definition: Player.java:194
This class is the representation of the games&#39;s board.
Definition: Board.java:25
Class to hold constant values for entire application.
Definition: Common.java:21
ArrayList< Integer[]> getPath()
Definition: Player.java:187
int [] getDirCounter()
Definition: Player.java:186
void setX(int x)
Definition: Player.java:196
void setChampion(boolean champion)
Definition: Player.java:204
void setName(String name)
Definition: Player.java:193
int playerCol()
Utility to access player&#39;s column position (column coordinate)
Definition: Player.java:169
void setDirCounter(int[] dirCounter)
Definition: Player.java:207
Helper C++-like enumerator class to hold direction.
Definition: Common.java:73
boolean champion
Champion indicate a player who plays against the Minotaur.
Definition: Player.java:224
static final int End
Iterator style end of range direction (one place after the last)
Definition: Common.java:65
int playerTileId()
Utility to access player&#39;s tileID.
Definition: Player.java:165
int x
The column coordinate of the player on the board.
Definition: Player.java:222
static final int MOVE_TILE_ID
Index of the tileId information of the move.
Definition: Player.java:24
This class represents the game&#39;s player.
Definition: Player.java:21
Board board
Reference to the session&#39;s boards.
Definition: Player.java:220
Player(String name, boolean champion, Board board, int row, int column)
Create a new player and put him at the row-column coordinates.
Definition: Player.java:40
void setPath(ArrayList< Integer[]> path)
Definition: Player.java:211
int [] move(int id)
Player&#39;s move.
Definition: Player.java:100
void statistics()
Prints round information for the player.
Definition: Player.java:140
static final int MOVE_DATA_SIZE
Helper variables to keep track of the move() return values.
Definition: Player.java:23
void setPlayerId(int id)
Definition: Player.java:192
void final_statistics()
Prints final statistics for the player.
Definition: Player.java:162
Helper C++ like enumerator class for direction ranged loops.
Definition: Common.java:63
void setScore(int score)
Definition: Player.java:195
int getId()
Read access to id coordinate.
Definition: Common.java:154
boolean getChampion()
Definition: Player.java:185
static final int MOVE_ROW
The index of row information.
Definition: Player.java:25
int getRow()
Read access to virtual row coordinate.
Definition: Common.java:152
int y
The row coordinate of the player on the board.
Definition: Player.java:223
int getCol()
Read access to virtual column coordinate.
Definition: Common.java:153
static int toRow(int id)
Takes Id coordinate and return the corresponding row coordinate.
Definition: Common.java:174
int score
The current score of the player.
Definition: Player.java:221
static int toCol(int id)
Takes Id coordinate and return the corresponding column coordinate.
Definition: Common.java:182
An Application wide board position implementation holding just the id coordinate. ...
Definition: Common.java:112
int generatePlayerId()
Utility function to create player IDs.
Definition: Board.java:238
static final int NONE
No direction.
Definition: Common.java:78
int dice()
A plain fair dice functionality provided by the board.
Definition: Board.java:226
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
static int boardSize
Default board&#39;s size (if no one set it via command line)
Definition: Common.java:44
boolean isWalkable(int tileId, int direction)
Predicate to check if a direction is Walkable.
Definition: Board.java:171
static final int LEFT
West direction.
Definition: Common.java:77
void setY(int y)
Definition: Player.java:200
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
void updateMove(int[] m, int playerId)
Utility to update the moves of each player.
Definition: Board.java:275
String name
The name of the player.
Definition: Player.java:219
static final int DOWN
South direction.
Definition: Common.java:76
static final int MOVE_COLUMN
The index of column information.
Definition: Player.java:26
int playerRow()
Utility to access player&#39;s row position (row coordinate)
Definition: Player.java:167
ArrayList< Integer[]> path
our history.
Definition: Player.java:226
int tryPickSupply(int tileId)
Try to pick supply from a tile.
Definition: Board.java:213
int playerId
The unique identifier of the player.
Definition: Player.java:218
Player(String name, boolean champion, Board board, int tileId)
Create a new player and put him at the row-column coordinates.
Definition: Player.java:63