Refactoring getLongestRoadFaction

This commit is contained in:
schrom01 2021-12-09 17:42:43 +01:00
parent ffc97a39ef
commit 187302afd5
3 changed files with 14 additions and 15 deletions

View File

@ -27,6 +27,8 @@ public class SiedlerBoard extends HexBoard<Land, Settlement, Road, String> {
* Value: Field Object
*/
HashMap<Point, Field> fields = new HashMap<>();
Config.Faction longestRoadFaction = null;
int longestRoadLenth = 0;
/**
* Method to create the predefined game field from Config.
@ -141,7 +143,7 @@ public class SiedlerBoard extends HexBoard<Land, Settlement, Road, String> {
* @return a HashMap<Faction,Integer> where faction is the player with the longest road longer according to siedler game rules
* and the Integer representing the length of the road
*/
public void getLongestRoadFaction(HashMap<Config.Faction, Integer> currentLongestRoad, List<Config.Faction> factionList) {
public Config.Faction getLongestRoadFaction(List<Config.Faction> factionList) {
List<Settlement> corners = getCorners();
HashMap<Config.Faction, Integer> players = new HashMap<>();
@ -161,7 +163,7 @@ public class SiedlerBoard extends HexBoard<Land, Settlement, Road, String> {
}
}
if (currentLongestRoad.size() == 0) {
if (longestRoadFaction == null) {
Config.Faction currentFaction = null;
int currentRoad = 4;
for (Config.Faction factionA : players.keySet()) {
@ -171,18 +173,18 @@ public class SiedlerBoard extends HexBoard<Land, Settlement, Road, String> {
}
}
if (currentFaction != null) {
currentLongestRoad.put(currentFaction, currentRoad);
longestRoadFaction = currentFaction;
longestRoadLenth = currentRoad;
}
} else {
for (Config.Faction faction : players.keySet()) {
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));
}
if (players.get(faction) >= 5 && players.get(faction) > longestRoadLenth) {
longestRoadFaction = faction;
longestRoadLenth = players.get(faction);
}
}
}
return longestRoadFaction;
}
/**

View File

@ -27,7 +27,6 @@ public class SiedlerGame {
private final int winPointsForWin;
private final Bank bank;
private int activePlayer;
private final HashMap<Config.Faction, Integer> longestRoadFaction;
/**
* Constructs a SiedlerGame game state object.
@ -47,7 +46,6 @@ public class SiedlerGame {
allPlayers = new ArrayList<>();
createPlayer(numberOfPlayers);
activePlayer = 0;
longestRoadFaction = new HashMap<>();
this.winPointsForWin = winPoints;
}
@ -503,8 +501,7 @@ public class SiedlerGame {
winPoints += newWinPoints;
}
}
board.getLongestRoadFaction(longestRoadFaction, getPlayerFactions());
if (longestRoadFaction.get(getCurrentPlayerFaction()) != null) {
if (getCurrentPlayerFaction() == board.getLongestRoadFaction(getPlayerFactions())) {
winPoints += 2;
}
return winPoints;

View File

@ -51,9 +51,9 @@ public class SiedlerGameTest {
board.setEdge(new Point(3, 9), new Point(4, 10), new Road(Config.Faction.BLUE,new Point(3, 9),new Point(4, 10)));
board.setEdge(new Point(4, 10), new Point(5, 9), new Road(Config.Faction.BLUE,new Point(4, 10),new Point(5, 9)));
board.setCorner(new Point(3,7),new Settlement(Config.Faction.BLUE,new Point(3,7)));
board.getLongestRoadFaction(currentLongestRoad,factionList);
assertEquals(6,currentLongestRoad.get(Config.Faction.BLUE));
assertEquals(Config.Faction.BLUE, board.getLongestRoadFaction(factionList));
//assertEquals(6,currentLongestRoad.get(Config.Faction.BLUE));
//todo prüfen ob länge Stimmt.
}
/**