changes in Methods for trading with bank.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package ch.zhaw.catan;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@@ -54,8 +55,12 @@ public class Player {
|
||||
* @param resource to add
|
||||
* @param numberToAdd how much to add
|
||||
*/
|
||||
public void addResource(Config.Resource resource, int numberToAdd) {
|
||||
resources.put(resource, resources.get(resource) + numberToAdd);
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,12 +69,18 @@ public class Player {
|
||||
* @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) {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -79,13 +90,13 @@ public class Player {
|
||||
* in possesion by the player.
|
||||
* @return true if road can be created false if road can't be created
|
||||
*/
|
||||
public boolean buildRoad() {
|
||||
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) {
|
||||
resources.put(resource,resources.get(resource)-1);
|
||||
substractResource(resource, 1, bank);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -96,13 +107,13 @@ public class Player {
|
||||
* in possession by the player.
|
||||
* @return true if road can be created false if road can't be created.
|
||||
*/
|
||||
public boolean buildSettlement() {
|
||||
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) {
|
||||
resources.put(resource,resources.get(resource)-1);
|
||||
substractResource(resource, 1, bank);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -113,13 +124,13 @@ public class Player {
|
||||
* 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() {
|
||||
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){
|
||||
resources.put(resource,resources.get(resource)-1);
|
||||
substractResource(resource, 1, bank);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user