gruppe06-hufflepuff-projekt.../src/ch/zhaw/catan/Player.java

165 lines
5.8 KiB
Java

package ch.zhaw.catan;
import java.util.*;
/**
* New Class PLayer
* This class is here to add players to the game.
*/
public class Player {
private Config.Faction faction;
private HashMap<Config.Resource,Integer> resources;
private int roadsToUse;
private int settlementsToUse;
private int citiesToUse;
public Player (Config.Faction faction){
//Datenfelder
this.faction = faction;
roadsToUse = Config.Structure.ROAD.getStockPerPlayer();
settlementsToUse = Config.Structure.SETTLEMENT.getStockPerPlayer();
citiesToUse = Config.Structure.CITY.getStockPerPlayer();
//Ressourcen initialisiern
resources = new HashMap<>();
for(Config.Resource resource : Config.Resource.values()) {
resources.put(resource,0);
}
}
/**
* This method returns all the resources the player has at the moment
* @return HashMap with the count of every resource
*/
public HashMap<Config.Resource,Integer> getResources() {
return resources;
}
/**
* This method returns player faction
* @return
*/
public Config.Faction getFaction() { return faction; }
/**
* This method returns for specific resource how much player possesess.
* @param resource
* @return
*/
public int getSpecificResource(Config.Resource resource) { return resources.get(resource); }
/**
* This method adds a specific resource to resourcess
* @param resource to add
* @param numberToAdd how much to add
*/
public boolean addResource(Config.Resource resource, int numberToAdd, Bank bank) {
if(bank.getResourceFromBank(resource, numberToAdd)){
resources.put(resource, resources.get(resource) + numberToAdd);
return true;
}
return false;
}
/**
* This method substracts a specific resource from resourcess but check first if player has enough resources.
* @param resource to substract
* @param numberToTake how much to substract
* @return true if resource has been substracted false if player has not enough resources
*/
public boolean substractResource(Config.Resource resource, int numberToTake, Bank bank) {
int inPossesion = resources.get(resource);
if(inPossesion - numberToTake < 0) {
return false;
}
resources.put(resource,inPossesion - numberToTake);
ArrayList<Config.Resource> resourcesForBank = new ArrayList<>();
for(int i = 0; i < numberToTake; i++){
resourcesForBank.add(resource);
}
bank.storeResourceToBank(resourcesForBank);
return true;
}
/**
* This method has to be used when a player wants to build a road. It checks if a player has enough roads
* and resources to build one more. If player is able to build, the method substract the buildcost from the resources
* in possesion by the player.
* @return true if road can be created false if road can't be created
*/
public boolean buildRoad(Bank bank) {
List<Config.Resource> costs = Config.Structure.ROAD.getCosts();
if ( roadsToUse == 0 || !checkRessourceToBuild(costs)) {
return false;
}
for (Config.Resource resource : costs) {
substractResource(resource, 1, bank);
}
return true;
}
/**
* This method has to be used when a player wants to build a settlement. It checks if a player has enough roads
* and resources to build one more. If the player is able to build, this method subtracts the buildcost from the resources
* in possession by the player.
* @return true if road can be created false if road can't be created.
*/
public boolean buildSettlement(Bank bank) {
List<Config.Resource> costs = Config.Structure.SETTLEMENT.getCosts();
if ( settlementsToUse == 0 || !checkRessourceToBuild(costs)) {
return false;
}
for (Config.Resource resource : costs) {
substractResource(resource, 1, bank);
}
return true;
}
/**
* This method has to be used when a player wants to build a city. It checks if a player already has a settlement
* on that position and if he has enough resource to build one. If the player is able to build, this method subtracts
* the buildcost from the resources in possession by the player.
* @return true if road can be created false if road can't be created.
*/
public boolean buildCity(Bank bank) {
List<Config.Resource> costs = Config.Structure.CITY.getCosts();
if ( citiesToUse == 0 || !checkRessourceToBuild(costs)){
return false;
}
for (Config.Resource resource : costs){
substractResource(resource, 1, bank);
}
return true;
}
//returns true if player has enough resources else false
private boolean checkRessourceToBuild(List<Config.Resource> liste) {
for (Config.Resource resource : liste) {
int possesion = resources.get(resource);
if (possesion == 0) {
return false;
}
}
return true;
}
public void handleDiceThrow7(Bank bank) {
ArrayList<Config.Resource> resourceArrayList = new ArrayList<>();
for(Config.Resource resource : resources.keySet()){
for(int i = 0; i < resources.get(resource); i++) {
resourceArrayList.add(resource);
}
}
if(resourceArrayList.size() > 7){
int resourcesToRemove =resourceArrayList.size() - (resourceArrayList.size() / 2);
Random random = new Random();
for(int i = 0; i < resourcesToRemove; i++){
substractResource(resourceArrayList.remove(random.nextInt(resourceArrayList.size())), 1, bank);
}
}
}
}