changes in Methods for trading with bank.

This commit is contained in:
schrom01 2021-12-04 16:33:55 +01:00
parent e23f8bcdde
commit 4b0bedcabf
2 changed files with 31 additions and 21 deletions

View File

@ -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;
}

View File

@ -156,7 +156,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);
allPlayers.get(activePlayer).addResource(land.getResource(), 1, bank);
}
}
}
@ -214,7 +214,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);
player.addResource(resource,1, bank);
}
}
}
@ -263,13 +263,11 @@ public class SiedlerGame {
return false;
}
//3. Can Player build Settlement
if (!allPlayers.get(activePlayer).buildSettlement()) {
if (!allPlayers.get(activePlayer).buildSettlement(bank)) {
return false;
}
//4. Insert Settlement to map
board.setCorner(position, new Settlement(allPlayers.get(activePlayer).getFaction()));
//5. Give Resources to bank
bank.storeResourceToBank(Config.Structure.SETTLEMENT.getCosts());
return true;
}
@ -298,13 +296,11 @@ public class SiedlerGame {
return false;
}
//3. Can player build a City.
if(!allPlayers.get(activePlayer).buildCity()){
if(!allPlayers.get(activePlayer).buildCity(bank)){
return false;
}
//4.Insert City into the map.
board.setCorner(position,new City(allPlayers.get(activePlayer).getFaction()));
//5. Give resources to the bank.
bank.storeResourceToBank(Config.Structure.CITY.getCosts());
return false;
}
@ -328,14 +324,12 @@ public class SiedlerGame {
return false;
}
//2. Can Player build road
if (!allPlayers.get(activePlayer).buildRoad()) {
if (!allPlayers.get(activePlayer).buildRoad(bank)) {
// TODO: Error message
return false;
}
//3. Insert Road to map
board.setEdge(roadStart, roadEnd, new Road(allPlayers.get(activePlayer).getFaction()));
//4. Give Resource to bank
bank.storeResourceToBank(Config.Structure.ROAD.getCosts());
return true;
}
@ -443,7 +437,12 @@ public class SiedlerGame {
* @return true, if the trade was successful
*/
public boolean tradeWithBankFourToOne(Resource offer, Resource want) {
return bank.tradeWithBank(want, offer, FOUR_TO_ONE_TRADE_WANT, FOUR_TO_ONE_TRADE_OFFER);
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);
return true;
}
return false;
}
/**