From bfb1433fd244d5b1a817e95775a3e4392253fa2c Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 26 Nov 2021 12:15:18 +0100 Subject: [PATCH 1/3] Player added methods and javadoc --- src/ch/zhaw/catan/Player.java | 73 +++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/src/ch/zhaw/catan/Player.java b/src/ch/zhaw/catan/Player.java index 965f7fd..9dc99f1 100644 --- a/src/ch/zhaw/catan/Player.java +++ b/src/ch/zhaw/catan/Player.java @@ -1,6 +1,7 @@ package ch.zhaw.catan; import java.util.HashMap; +import java.util.List; /** * New Class PLayer @@ -31,31 +32,79 @@ public class Player { /** * This method returns all the resources the player has at the moment - * @return HashMap + * @return HashMap with the count of every resource */ public HashMap getResources() { return resources; } + /** + * This method adds a specific resource to resourcess + * @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); + } + /** + * 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) { + int inPossesion = resources.get(resource); + if(inPossesion - numberToTake < 0) { + return false; + } + resources.put(resource,inPossesion - numberToTake); + 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() { - if (roadsToUse > 0) { - roadsToUse--; - return true; + List costs = Config.Structure.ROAD.getCosts(); + if ( roadsToUse == 0 || !checkRessourceToBuild(costs)) { + return false; } - return false; - + for (Config.Resource resource : costs) { + resources.put(resource,resources.get(resource)-1); + } + 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 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 buildSettlement() { - if (settlementsToUse > 0) { - settlementsToUse--; - return true; + List costs = Config.Structure.SETTLEMENT.getCosts(); + if ( settlementsToUse == 0 || !checkRessourceToBuild(costs)) { + return false; } - return false; + for (Config.Resource resource : costs) { + resources.put(resource,resources.get(resource)-1); + } + return true; } - + //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) { + return false; + } + } + return true; + } } From 5e0ebedfeabd15768f17e734dfdb7aa05d568d2e Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 26 Nov 2021 12:21:57 +0100 Subject: [PATCH 2/3] Player added methods and javadoc --- .idea/misc.xml | 2 +- src/ch/zhaw/catan/Player.java | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index c3dfb30..b573818 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/ch/zhaw/catan/Player.java b/src/ch/zhaw/catan/Player.java index 9dc99f1..7fafac1 100644 --- a/src/ch/zhaw/catan/Player.java +++ b/src/ch/zhaw/catan/Player.java @@ -6,7 +6,6 @@ import java.util.List; /** * New Class PLayer * This class is here to add players to the game. - * @author: Stefan Amador */ public class Player { From 9a79bf0cf4c59df4b7a228fcaa9334bb3ea65720 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 26 Nov 2021 12:32:26 +0100 Subject: [PATCH 3/3] Player added methods and javadoc --- src/ch/zhaw/catan/Player.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/ch/zhaw/catan/Player.java b/src/ch/zhaw/catan/Player.java index 7fafac1..4eb49b8 100644 --- a/src/ch/zhaw/catan/Player.java +++ b/src/ch/zhaw/catan/Player.java @@ -37,6 +37,19 @@ public class Player { 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