Player.java
package edu.chl.hajo.monopoly.core;
import edu.chl.hajo.monopoly.core.card.Card;
import edu.chl.hajo.monopoly.core.space.Space;
import java.util.ArrayList;
import java.util.List;
/**
* A single player of the game
*
* Handles everything that has to do with a single player!
*
*
* @author hajo
*
*/
public class Player {
public static final Player NONE = new Player("none", null, 0);
private final Piece piece;
private final String name; // Uniqur name for player
private boolean isBroke;
private final List<Space> properties = new ArrayList<>();
private Debt debt;
private int balance;
private Space position; // The actual position
private final List<Card> cards = new ArrayList<>();
public Player(String name, Piece piece, int balance) {
this.name = name;
this.piece = piece;
this.balance = balance;
}
public boolean withDraw(int amount) {
if (balance >= amount) {
balance -= amount;
return true;
} else {
return false;
}
}
public void income(int amount) {
this.balance += amount;
}
public void addCard(Card card) {
cards.add(card);
}
public Card getLastCard() {
return cards.get(cards.size() - 1);
}
public void addDebt(Debt debt) {
this.debt = debt;
}
public boolean hasDebt() {
return debt != null;
}
public void payDebt() {
if (balance >= debt.getAmount()) {
debt.getReciecver().income(debt.getAmount());
balance -= debt.getAmount();
debt = null;
}
}
public boolean isBroke() {
return isBroke;
}
public void setBroke(boolean b) {
isBroke = b;
}
public Piece getPiece() {
return piece;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Player other = (Player) obj;
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
return true;
}
@Override
public String toString() {
StringBuilder ps = new StringBuilder();
for( Space a : properties){
ps.append(a.getName()).append(",");
}
StringBuilder cs = new StringBuilder();
for( Card c : cards){
cs.append(c.getName()).append(",");
}
return "(" + name + " b:" + balance + " d:" + debt + "p:" + ps + "c:" + cs + ")";
}
public int getAllResources() {
// Sum up everything (to chech if broke)
return balance; // TODO for now
}
public String getName() {
return name;
}
public Space getPosition() {
return position;
}
public void setPosition(Space position) {
this.position = position;
}
public int getBalance() {
return balance;
}
}