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 {
33  Player(String name, boolean champion, Board board, int row, int column) {
34  this.playerId = board.generatePlayerId();
35  this.name = name;
36  this.board = board;
37  this.score = 0;
38  this.x = column;
39  this.y = row;
40  this.champion = champion;
41  this.dirCounter= new int[DirRange.End]; // yes we spoil some memory. Java is worst.
42  this.path = new ArrayList<Integer[]>();
43  int[] m = {
44  Position.toID(row, column), row, column, Const.noSupply
45  };
46  board.updateMove(m, playerId);
47  }
48 
56  Player(String name, boolean champion, Board board, int tileId) {
57  this.playerId = board.generatePlayerId();
58  this.name = name;
59  this.board = board;
60  this.score = 0;
61  this.x = Position.toCol(tileId);
62  this.y = Position.toRow(tileId);
63  this.champion = champion;
64  this.dirCounter= new int[DirRange.End]; // yes we spoil some memory. Java is worst.
65  this.path = new ArrayList<Integer[]>();
66  int[] m = {
67  tileId, Position.toRow(tileId), Position.toCol(tileId), Const.noSupply
68  };
69  board.updateMove(m, playerId);
70  }
93  int[] move(int id) {
94  // Initialize return array with the current data
95  int[] ret = new int[Const.moveItems];
96  ret[0] = id;
97  ret[1] = Position.toRow(id);
98  ret[2] = Position.toCol(id);
99  ret[3] = Const.noSupply;
100  int supplyFlag =0, moveFlag =0;
101 
102  int diceDirection = board.dice(); // throw the dice
103  if (board.isWalkable(id, diceDirection)) { // The result is walkable
104  moveFlag =1; // mark the successful move
105  // Get next tile
106  Position next = new Position(Position.toRow(id), Position.toCol(id), diceDirection);
107  ret[0] = next.getId(); // Update player's and return data
108  ret[1] = y = next.getRow();
109  ret[2] = x = next.getCol();
110  // In case of a champion player, try also to pick a supply
111  if (champion && (ret[3] = board.tryPickSupply(next.getId())) != Const.noSupply) {
112  supplyFlag =1; // mark the successful supply pickup
113  ++score; // keep score
114  }
115  ++dirCounter[diceDirection]; // update direction counters
116  board.updateMove(ret, playerId);
117  }
118  // update path
119  Integer[] p = {
120  ret[0], diceDirection, moveFlag, supplyFlag,
121  dirCounter[Direction.UP], dirCounter[Direction.RIGHT], dirCounter[Direction.DOWN], dirCounter[Direction.LEFT],
123  };
124  path.add(p);
125  return ret;
126  }
127 
131  void statistics() {
132  if (!path.isEmpty()) {
133  Integer[] last = path.get(path.size()-1);
134  String who = String.format("%12s", name);
135  System.out.print(who + ": score[" + score + "]" + ", dice =" + last[1] + ", tileId =" + last[0] + " (" + Position.toRow(last[0]) + ", " + Position.toCol(last[0]) + ")");
136  if (last[2] == 0)
137  System.out.println(" *Can not move.");
138  else if (last[3] != 0)
139  System.out.println(" *Found a supply.");
140  else
141  System.out.println("");
142  }
143  }
144 
153  void final_statistics () { }
154 
156  int playerTileId() { return Position.toID(y, x); }
158  int playerRow() { return y; }
160  int playerCol() { return x; }
170  int getPlayerId () { return playerId; }
171  String getName() { return name; }
172  Board getBoard () { return board; }
173  int getScore () { return score; }
174  int getX() { return x; }
175  int getY() { return y; }
176  boolean getChampion(){ return champion; }
177  int[] getDirCounter(){ return dirCounter; }
178  ArrayList<Integer[]> getPath() {
179  return path;
180  }
181 
182 
183  void setPlayerId(int id) { playerId = id; }
184  void setName(String name) { this.name = name; }
185  void setBoard (Board board){ this.board = board; }
186  void setScore(int score) { this.score = score; }
187  void setX(int x) {
188  assert (x >= 0 && x< Session.boardSize) : "X(column) coordinate must be in the range [0, Session.boardSize)";
189  this.x = x;
190  }
191  void setY(int y) {
192  assert (y >= 0 && y< Session.boardSize) : "Y(row) coordinate must be in the range [0, Session.boardSize)";
193  this.y = y;
194  }
195  void setChampion (boolean champion) {
196  this.champion = champion;
197  }
198  public void setDirCounter(int[] dirCounter) {
199  this.dirCounter = dirCounter;
200  }
201 
202  public void setPath(ArrayList<Integer[]> path) {
203  this.path = path;
204  }
209  protected int playerId;
210  protected String name;
211  protected Board board;
212  protected int score;
213  protected int x;
214  protected int y;
215  protected boolean champion;
216  protected int dirCounter[];
217  protected ArrayList<Integer[]> path;
235 }
static final int RIGHT
East direction.
Definition: Common.java:68
void setBoard(Board board)
Definition: Player.java:185
This class is the representation of the games&#39;s board.
Definition: Board.java:26
Class to hold constant values for entire application.
Definition: Common.java:21
ArrayList< Integer[]> getPath()
Definition: Player.java:178
int [] getDirCounter()
Definition: Player.java:177
void setX(int x)
Definition: Player.java:187
void setChampion(boolean champion)
Definition: Player.java:195
void setName(String name)
Definition: Player.java:184
int playerCol()
Utility to access player&#39;s column position (column coordinate)
Definition: Player.java:160
void setDirCounter(int[] dirCounter)
Definition: Player.java:198
Helper C++-like enumerator class to hold direction.
Definition: Common.java:66
boolean champion
Champion indicate a player who plays against the Minotaur.
Definition: Player.java:215
static final int End
Iterator style end of range direction (one place after the last)
Definition: Common.java:58
int playerTileId()
Utility to access player&#39;s tileID.
Definition: Player.java:156
int x
The column coordinate of the player on the board.
Definition: Player.java:213
This class represents the game&#39;s player.
Definition: Player.java:21
Board board
Reference to the session&#39;s boards.
Definition: Player.java:211
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:33
void setPath(ArrayList< Integer[]> path)
Definition: Player.java:202
int [] move(int id)
Player&#39;s move.
Definition: Player.java:93
void statistics()
Prints round information for the player.
Definition: Player.java:131
void setPlayerId(int id)
Definition: Player.java:183
void final_statistics()
Prints final statistics for the player.
Definition: Player.java:153
Helper C++ like enumerator class for direction ranged loops.
Definition: Common.java:56
void setScore(int score)
Definition: Player.java:186
int getId()
Read access to id coordinate.
Definition: Common.java:143
boolean getChampion()
Definition: Player.java:176
static final int moveItems
The number of items return by move()
Definition: Common.java:27
int getRow()
Read access to virtual row coordinate.
Definition: Common.java:141
int y
The row coordinate of the player on the board.
Definition: Player.java:214
int getCol()
Read access to virtual column coordinate.
Definition: Common.java:142
static int toRow(int id)
Takes Id coordinate and return the corresponding row coordinate.
Definition: Common.java:163
int score
The current score of the player.
Definition: Player.java:212
static int toCol(int id)
Takes Id coordinate and return the corresponding column coordinate.
Definition: Common.java:171
An Application wide board position implementation holding just the id coordinate. ...
Definition: Common.java:102
int generatePlayerId()
Utility function to create player IDs.
Definition: Board.java:243
int dice()
A plain fair dice functionality provided by the board.
Definition: Board.java:214
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
static int boardSize
Default board&#39;s size (if no one set it via command line)
Definition: Common.java:37
boolean isWalkable(int tileId, int direction)
Predicate to check if a direction is Walkable.
Definition: Board.java:159
static final int LEFT
West direction.
Definition: Common.java:70
void setY(int y)
Definition: Player.java:191
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
void updateMove(int[] m, int playerId)
Utility to update the moves of each player.
Definition: Board.java:258
String name
The name of the player.
Definition: Player.java:210
static final int DOWN
South direction.
Definition: Common.java:69
int playerRow()
Utility to access player&#39;s row position (row coordinate)
Definition: Player.java:158
ArrayList< Integer[]> path
our history.
Definition: Player.java:217
int tryPickSupply(int tileId)
Try to pick supply from a tile.
Definition: Board.java:201
int playerId
The unique identifier of the player.
Definition: Player.java:209
Player(String name, boolean champion, Board board, int tileId)
Create a new player and put him at the row-column coordinates.
Definition: Player.java:56