From 4c56518ba0f5a2bf1974d48eaa47b013aabf5c0e Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 7 Dec 2021 11:44:51 +0100 Subject: [PATCH] fixing Method checkResourceToBuild --- src/ch/zhaw/catan/Player.java | 16 +++++++++++----- src/ch/zhaw/catan/SiedlerGame.java | 10 +++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/ch/zhaw/catan/Player.java b/src/ch/zhaw/catan/Player.java index ed2ac1f..c535b68 100644 --- a/src/ch/zhaw/catan/Player.java +++ b/src/ch/zhaw/catan/Player.java @@ -95,10 +95,17 @@ public class Player { //returns true if player has enough resources else false - private boolean checkRessourceToBuild(List liste) { - for (Config.Resource resource : liste) { - int possesion = resources.get(resource); - if (possesion == 0) { + private boolean checkResourceToBuild(List list) { + HashMap costs = new HashMap<>(); + for(Config.Resource resource : list){ + if(costs.containsKey(resource)){ + costs.put(resource, costs.get(resource) + 1); + } else { + costs.put(resource, 1); + } + } + for (Config.Resource resource : costs.keySet()) { + if (resources.get(resource) < costs.get(resource)) { return false; } } @@ -106,5 +113,4 @@ public class Player { } - } diff --git a/src/ch/zhaw/catan/SiedlerGame.java b/src/ch/zhaw/catan/SiedlerGame.java index 0241ed0..ec52e4b 100644 --- a/src/ch/zhaw/catan/SiedlerGame.java +++ b/src/ch/zhaw/catan/SiedlerGame.java @@ -479,11 +479,11 @@ public class SiedlerGame { */ public boolean tradeWithBankFourToOne(Resource offer, Resource want) { Player player = allPlayers.get(activePlayer); - 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; + 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; } /**