Labyrinth
A labyrinth game assignment
Player.java
Go to the documentation of this file.
1 
10 package host.labyrinth;
11 
16 class Player {
29  Player(int id, String name, boolean champion, Board board, int row, int column) {
30  this.playerId = id;
31  this.name = name;
32  this.board = board;
33  this.score = 0;
34  this.x = column;
35  this.y = row;
36  this.champion = champion;
37  }
38 
47  Player(int id, String name, boolean champion, Board board, int tileId) {
48  this.playerId = id;
49  this.name = name;
50  this.board = board;
51  this.score = 0;
52  this.x = Position.toCol(tileId);
53  this.y = Position.toRow(tileId);
54  this.champion = champion;
55  }
78  int[] move(int id) {
79  // Initialize return array with the current data
80  int[] ret = {id, Position.toRow(id), Position.toCol(id), Const.noSupply};
81 
82  int diceDirection = board.dice(); // throw the dice
83  if (board.isWalkable(id, diceDirection)) { // The result is walkable
84  // Get next tile
85  Position next = new Position(Position.toRow(id), Position.toCol(id), diceDirection);
86  ret[0] = next.getId(); // Update player's and return data
87  ret[1] = y = next.getRow();
88  ret[2] = x = next.getCol();
89  // In case of a champion player, try also to pick a supply
90  if (champion && (ret[3] = board.tryPickSupply(next.getId())) != Const.noSupply) {
91  ++score; // keep score
92  System.out.println(name + ":\t*Found a supply. [score: " + score + "]");
93  }
94  }
95  else
96  System.out.println(name + ":\t*Can not move.");
97  return ret;
98  }
99 
101  int playerTileId() { return Position.toID(y, x); }
103  int playerRow() { return y; }
105  int playerCol() { return x; }
115  int getPlayerId () { return playerId; }
116  String getName() { return name; }
117  Board getBoard () { return board; }
118  int getScore () { return score; }
119  int getX() { return x; }
120  int getY() { return y; }
121  boolean getChampion(){ return champion; }
122 
123  void setPlayerId(int id) { playerId = id; }
124  void setName(String name) { this.name = name; }
125  void setBoard (Board board){ this.board = board; }
126  void setScore(int score) { this.score = score; }
127  void setX(int x) {
128  assert (x >= 0 && x< Session.boardSize) : "X(column) coordinate must be in the range [0, Session.boardSize)";
129  this.x = x;
130  }
131  void setY(int y) {
132  assert (y >= 0 && y< Session.boardSize) : "Y(row) coordinate must be in the range [0, Session.boardSize)";
133  this.y = y;
134  }
135  void setChampion (boolean champion) {
136  this.champion = champion;
137  }
138 
143  private int playerId;
144  private String name;
145  private Board board;
146  private int score;
147  private int x;
148  private int y;
149  private boolean champion;
151 }
void setBoard(Board board)
Definition: Player.java:125
This class is the representation of the games&#39;s board.
Definition: Board.java:22
Class to hold constant values for entire application.
Definition: Common.java:17
void setX(int x)
Definition: Player.java:127
void setChampion(boolean champion)
Definition: Player.java:135
void setName(String name)
Definition: Player.java:124
int playerCol()
Utility to access player&#39;s column position (column coordinate)
Definition: Player.java:105
boolean champion
Champion indicate a player who plays against the Minotaur.
Definition: Player.java:149
int playerTileId()
Utility to access player&#39;s tileID.
Definition: Player.java:101
int x
The column coordinate of the player on the board.
Definition: Player.java:147
This class represents the game&#39;s player.
Definition: Player.java:16
Board board
Reference to the session&#39;s boards.
Definition: Player.java:145
Player(int id, String name, boolean champion, Board board, int tileId)
Create a new player and put him at the row-column coordinates.
Definition: Player.java:47
int [] move(int id)
Player&#39;s move.
Definition: Player.java:78
Player(int id, 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:29
void setPlayerId(int id)
Definition: Player.java:123
void setScore(int score)
Definition: Player.java:126
int getId()
Read access to id coordinate.
Definition: Common.java:119
boolean getChampion()
Definition: Player.java:121
int getRow()
Read access to virtual row coordinate.
Definition: Common.java:117
int y
The row coordinate of the player on the board.
Definition: Player.java:148
int getCol()
Read access to virtual column coordinate.
Definition: Common.java:118
static int toRow(int id)
Takes Id coordinate and return the corresponding row coordinate.
Definition: Common.java:139
int score
The current score of the player.
Definition: Player.java:146
static int toCol(int id)
Takes Id coordinate and return the corresponding column coordinate.
Definition: Common.java:147
An Application wide board position implementation holding just the id coordinate. ...
Definition: Common.java:78
int dice()
A plain fair dice functionality provided by the board.
Definition: Board.java:194
static final int noSupply
Number to indicate the absent of supply.
Definition: Common.java:19
static int toID(int row, int col)
Takes row and column coordinates and return the calculated Id coordinate.
Definition: Common.java:130
Application wide object to hold settings like values for the session.
Definition: Common.java:26
static int boardSize
Default board&#39;s size (if no one set it via command line)
Definition: Common.java:27
boolean isWalkable(int tileId, int direction)
Predicate to check if a direction is Walkable.
Definition: Board.java:149
void setY(int y)
Definition: Player.java:131
String name
The name of the player.
Definition: Player.java:144
int playerRow()
Utility to access player&#39;s row position (row coordinate)
Definition: Player.java:103
int tryPickSupply(int tileId)
Try to pick supply from a tile.
Definition: Board.java:182
int playerId
The unique identifier of the player.
Definition: Player.java:143