From 46484df77cd532cd4c133cd8821aa7c8fc71a68b Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Tue, 7 Dec 2021 15:33:39 +0100 Subject: [PATCH] adjust countRoad in SiedlerGame --- .idea/misc.xml | 2 +- src/ch/zhaw/catan/SiedlerGame.java | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 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/SiedlerGame.java b/src/ch/zhaw/catan/SiedlerGame.java index fa64686..c87ce28 100644 --- a/src/ch/zhaw/catan/SiedlerGame.java +++ b/src/ch/zhaw/catan/SiedlerGame.java @@ -24,6 +24,7 @@ public class SiedlerGame { private int winPointsForWin; private Bank bank; private int activePlayer; + private HashMap longestRoadFaction; /** * Constructs a SiedlerGame game state object. @@ -43,6 +44,7 @@ public class SiedlerGame { allPlayers = new ArrayList<>(); createPlayer(numberOfPlayers); activePlayer = 0; + longestRoadFaction = new HashMap<>(); this.winPointsForWin = winPoints; } @@ -504,7 +506,8 @@ public class SiedlerGame { winPoints ++; } } - if(getLongestRoadFaction() == getCurrentPlayerFaction()){ + longestRoadFaction = getLongestRoadFaction(longestRoadFaction); + if(longestRoadFaction.get(getCurrentPlayerFaction()) != null){ winPoints += 2; } return winPoints; @@ -515,12 +518,12 @@ public class SiedlerGame { * This method returns the faction of the player with the longest road longer than 5. * @return null if there is no player with a road longer than 5 otherwise it returns the faction of the specific player */ - private Faction getLongestRoadFaction() { + private HashMap getLongestRoadFaction(HashMap currentLongestRoad) { List corners = board.getCorners(); List factionList = getPlayerFactions(); HashMap players = new HashMap<>(); int highest = 0; - Config.Faction longestRoad = null; + for(Config.Faction faction : factionList) { int count = 0; players.put(faction,count); @@ -535,12 +538,14 @@ public class SiedlerGame { } } for(Config.Faction faction : players.keySet()) { - if(players.get(faction) >= 5 && players.get(faction)>highest) { - highest = players.get(faction); - longestRoad = faction; + for(Config.Faction faction1 : currentLongestRoad.keySet()) { + if(players.get(faction) >= 5 && players.get(faction) > currentLongestRoad.get(faction1)) { + currentLongestRoad.remove(faction1); + currentLongestRoad.put(faction,players.get(faction)); + } } } - return longestRoad; + return currentLongestRoad; } /**