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