Monopoly.java
package edu.chl.hajo.monopoly.core;
import edu.chl.hajo.monopoly.core.space.Space;
import edu.chl.hajo.monopoly.core.visitor.BuyVisitor;
import edu.chl.hajo.monopoly.core.visitor.IVisitor;
import edu.chl.hajo.monopoly.core.visitor.SellVisitor;
import edu.chl.hajo.monopoly.core.visitor.TaxVisitor;
import java.util.Collection;
import java.util.List;
import java.util.Random;
/**
* Class representing the overall game
*
* @author hajo
*
*/
public class Monopoly implements IMonopoly {
private final List<Player> players;
private final DicePair dicePair;
private final Board board;
private final Deck deck;
private Player actual;
public Monopoly(List<Player> players, Board board, DicePair dicePair, Deck deck) {
this.dicePair = dicePair;
this.board = board;
this.players = players;
this.deck = deck;
// Always create a new game when starting
actual = players.get(new Random().nextInt(players.size()));
}
@Override
public void next() {
if (!actual.hasDebt()) {
int i = players.indexOf(actual);
actual = players.get((i + 1) % players.size());
dicePair.setEnabled(true);
}
}
@Override
public void move() {
if (dicePair.isEnabled() && !actual.hasDebt()) {
Space oldPos = actual.getPosition();
dicePair.roll();
Space newPos = board.getSpace(oldPos, dicePair.getTotal());
actual.setPosition(newPos);
dicePair.setEnabled(dicePair.bothAre(6));
if (board.hasPassedGo(oldPos, newPos)) {
//System.out.println("passed go");
actual.income(MonopolyOptions.getBonus());
}
checkForRent(newPos);
checkForCard(newPos);
checkForTax(newPos);
}
}
private void checkForTax(Space position) {
IVisitor v = new TaxVisitor();
position.accept(v, actual);
}
private void checkForRent(Space position) {
IVisitor v = new TaxVisitor();
position.accept(v, actual);
}
private void checkForCard(Space space) {
// CardVisitor
}
@Override
public void sell(Space s) {
IVisitor v = new SellVisitor();
Space position = actual.getPosition();
position.accept(v, actual);
}
@Override
public void buy() {
if (!actual.hasDebt()) {
IVisitor v = new BuyVisitor();
Space position = actual.getPosition();
position.accept(v, actual);
}
}
@Override
public void payDept() {
actual.payDebt();
}
// ------- Accessors -------------
@Override
public Player getActual() {
return actual;
}
@Override
public Collection<Player> getPlayers() {
return players;
}
@Override
public DicePair getDicePair() {
return dicePair;
}
@Override
public Board getBoard() {
return board;
}
}