refactoring in SiedlerGame.java and Player.java

This commit is contained in:
schrom01 2021-12-07 10:59:02 +01:00
parent 8052ac97ff
commit da6913d997
2 changed files with 82 additions and 81 deletions

View File

@ -10,6 +10,7 @@ public class Player {
private Config.Faction faction;
private HashMap<Config.Resource,Integer> resources;
private HashMap<Config.Structure,Integer> structureToUse;
private int roadsToUse;
private int settlementsToUse;
private int citiesToUse;
@ -17,9 +18,11 @@ public class Player {
public Player (Config.Faction faction){
//Datenfelder
this.faction = faction;
roadsToUse = Config.Structure.ROAD.getStockPerPlayer();
settlementsToUse = Config.Structure.SETTLEMENT.getStockPerPlayer();
citiesToUse = Config.Structure.CITY.getStockPerPlayer();
//Verfügbare Structures initialisieren
structureToUse = new HashMap<>();
structureToUse.put(Config.Structure.ROAD, Config.Structure.ROAD.getStockPerPlayer());
structureToUse.put(Config.Structure.SETTLEMENT, Config.Structure.SETTLEMENT.getStockPerPlayer());
structureToUse.put(Config.Structure.CITY, Config.Structure.CITY.getStockPerPlayer());
//Ressourcen initialisiern
resources = new HashMap<>();
for(Config.Resource resource : Config.Resource.values()) {
@ -54,85 +57,42 @@ public class Player {
* @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;
public void addResource(Config.Resource resource, int numberToAdd) {
resources.put(resource, resources.get(resource) + numberToAdd);
}
/**
* 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
* @param numberToSubstract 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) {
public boolean substractResource(Config.Resource resource, int numberToSubstract) {
int inPossesion = resources.get(resource);
if(inPossesion - numberToTake < 0) {
if(inPossesion - numberToSubstract < 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);
}
resources.put(resource,inPossesion - numberToSubstract);
return true;
}
//todo java doc aktualisieren
/**
* 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)) {
public boolean build(Config.Structure structure){
List<Config.Resource> costs = structure.getCosts();
if(structureToUse.get(structure) < 1 || !checkRessourceToBuild(costs)){
return false;
}
for (Config.Resource resource : costs) {
substractResource(resource, 1, bank);
}
structureToUse.put(structure, structureToUse.get(structure) - 1);
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) {
@ -145,20 +105,6 @@ public class Player {
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);
}
}
}
}

View File

@ -76,6 +76,26 @@ public class SiedlerGame {
}
}
private boolean addResourcesToPlayer(Player player, Resource resource, int numberToAdd){
if(bank.getResourceFromBank(resource, numberToAdd)){
player.addResource(resource, numberToAdd);
return true;
}
return false;
}
private boolean substractResourceFromPlayer(Player player, Resource resource, int numberToSubstract){
if(player.substractResource(resource, numberToSubstract)){
ArrayList<Config.Resource> resourcesForBank = new ArrayList<>();
for(int i = 0; i < numberToSubstract; i++){
resourcesForBank.add(resource);
}
bank.storeResourceToBank(resourcesForBank);
return true;
}
return false;
}
/**
* Returns the {@link Faction}s of the active players.
*
@ -154,7 +174,7 @@ public class SiedlerGame {
List<Config.Land> lands = board.getLandsForCorner(position);
for (Config.Land land : lands) {
if (land.getResource() != null) {
allPlayers.get(activePlayer).addResource(land.getResource(), 1, bank);
addResourcesToPlayer(allPlayers.get(activePlayer), land.getResource(), 1);
}
}
}
@ -202,7 +222,7 @@ public class SiedlerGame {
public Map<Faction, List<Resource>> throwDice(int dicethrow) {
if (dicethrow == 7) {
for(Player player : allPlayers) {
player.handleDiceThrow7(bank);
handleDiceThrow7(player);
}
} else {
Map<Faction,List<Resource>> returnMap= new HashMap<>();
@ -214,7 +234,7 @@ public class SiedlerGame {
for (Config.Resource resource : resources){
//TODO: Check if Resource Null notwendig?
returnMap.get(player.getFaction()).add(resource);
player.addResource(resource,1, bank);
addResourcesToPlayer(allPlayers.get(activePlayer), resource, 1);
}
}
}
@ -223,6 +243,23 @@ public class SiedlerGame {
return null;
}
public void handleDiceThrow7(Player player) {
ArrayList<Config.Resource> resourceArrayList = new ArrayList<>();
HashMap<Resource, Integer> resources = player.getResources();
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++){
substractResourceFromPlayer(player, resourceArrayList.remove(random.nextInt(resourceArrayList.size())), 1);
}
}
}
/**
@ -248,9 +285,15 @@ public class SiedlerGame {
return false;
}
//3. Can Player build Settlement
if (!allPlayers.get(activePlayer).buildSettlement(bank)) {
if (!allPlayers.get(activePlayer).build(Config.Structure.SETTLEMENT)) {
return false;
}
List<Config.Resource> costs = Config.Structure.SETTLEMENT.getCosts();
for (Config.Resource resource : costs) {
substractResourceFromPlayer(allPlayers.get(activePlayer), resource, 1);
}
//4. Insert Settlement to map
board.setCorner(position, new Settlement(allPlayers.get(activePlayer).getFaction(),position));
return true;
@ -281,9 +324,15 @@ public class SiedlerGame {
return false;
}
//3. Can player build a City.
if(!allPlayers.get(activePlayer).buildCity(bank)){
if(!allPlayers.get(activePlayer).build(Config.Structure.CITY)){
return false;
}
List<Config.Resource> costs = Config.Structure.CITY.getCosts();
for (Config.Resource resource : costs) {
substractResourceFromPlayer(allPlayers.get(activePlayer), resource, 1);
}
//4.Insert City into the map.
board.setCorner(position,new City(allPlayers.get(activePlayer).getFaction(),position));
return true;
@ -310,10 +359,16 @@ public class SiedlerGame {
return false;
}
//2. Can Player build road
if (!allPlayers.get(activePlayer).buildRoad(bank)) {
if (!allPlayers.get(activePlayer).build(Config.Structure.ROAD)) {
// TODO: Error message
return false;
}
List<Config.Resource> costs = Config.Structure.ROAD.getCosts();
for (Config.Resource resource : costs) {
substractResourceFromPlayer(allPlayers.get(activePlayer), resource, 1);
}
//3. Insert Road to map
board.setEdge(roadStart, roadEnd, new Road(allPlayers.get(activePlayer).getFaction(),roadStart,roadEnd));
return true;
@ -424,8 +479,8 @@ public class SiedlerGame {
*/
public boolean tradeWithBankFourToOne(Resource offer, Resource want) {
Player player = allPlayers.get(activePlayer);
if(player.getSpecificResource(offer) >= FOUR_TO_ONE_TRADE_OFFER && player.addResource(want, FOUR_TO_ONE_TRADE_WANT, bank)){
player.substractResource(offer, FOUR_TO_ONE_TRADE_OFFER, bank);
if(player.getSpecificResource(offer) >= FOUR_TO_ONE_TRADE_OFFER && addResourcesToPlayer(player, want, FOUR_TO_ONE_TRADE_WANT)){
substractResourceFromPlayer(player, offer, FOUR_TO_ONE_TRADE_OFFER);
return true;
}
return false;