diff --git a/.idea/misc.xml b/.idea/misc.xml index c3dfb30..6bc01a8 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/README.md b/README.md index 7ca0144..d703580 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,8 @@ to be able to dominate your friends. #Rules 1. The min. Player amount is 2 and the max. Player amount is 4. -2. Each Player can build a road and a settlement free of cost at the beginning of -the game. +2. Each Player can build two roads and two settlements free of cost at the beginning of +the game. After the placement, each player gets resources from the field around the second settlement. 3. Every Player is being sorted into a faction. There cannot be two players in the same faction. 4. A player cannot build two settlements or cities right next one another. @@ -25,7 +25,7 @@ For a more detailed version of the rules please look here: https://www.catan.de/ #Usermanual First the program will ask how many players will be playing the game. The minimum amount of players is 2 and the maximum amount of players is 4. Each player will -be appointed to a faction. Now every player can build a settlement and a road +be appointed to a faction. In the next step, the program ask for the number of win points. The first player who reaches this number of win points, will win the game. Now every player can build a settlement and a road after every player has built a settlement and road, they can build a second settlement and road in reversed order. When the players build their second settlement they then receive the resources surrounding that specific settlement. diff --git a/src/ch/zhaw/catan/Player.java b/src/ch/zhaw/catan/Player.java index dc1f8a8..366270f 100644 --- a/src/ch/zhaw/catan/Player.java +++ b/src/ch/zhaw/catan/Player.java @@ -4,10 +4,10 @@ import java.util.HashMap; import java.util.List; /** - * This class is here in order to maintain the resources of the players, the amount of structures that they can build, - * control to see if a player has enough to build a new structure and to add them into a faction. + * This class is here in order to maintain the resources of the players, the amount of structures that they can build. + * And check if a player holds enough resources to build. * - * @author Leonardo Brandenberger, Roman Schrom, Andrin Fassbind, Stefan Amador + * @author Leonardo Brandenberger, Roman Schenk, Andrin Fassbind, Stefan Amador */ public class Player { @@ -18,7 +18,7 @@ public class Player { /** * The constructor initializes the faction and the Hashmaps resources and structureToUse. * - * @param faction this is the faction of the player. + * @param faction faction of the player. */ public Player(Config.Faction faction) { //Data fields @@ -36,7 +36,7 @@ public class Player { } /** - * This method returns all the resources the player has at the moment + * This method returns all the resources the player has at the moment. * * @return HashMap with the count of every resource */ @@ -45,7 +45,7 @@ public class Player { } /** - * This method returns player faction + * This method returns the player's faction. * * @return the faction of the player. */ @@ -56,15 +56,15 @@ public class Player { /** * This method returns the amount of a specific resource that a player owns. * - * @param resource the resource that is needed. - * @return the amount of the specific resource. + * @param resource the resource that is needed + * @return the amount of the specific resource */ public int getSpecificResource(Config.Resource resource) { return resources.get(resource); } /** - * This method adds a specific resource to resources + * This method adds a specific resource to resources of the player. * * @param resource to add * @param numberToAdd how much to add @@ -91,7 +91,7 @@ public class Player { /** * This method has to be used when a player wants to build a structure. It checks if a player has enough of the specific structure - * and resources to build one more. If the player is able to build, this method subtracts the building cost from the resources + * and resources to build . If the player is able to build, this method subtracts the building cost from the resources * in possession by the player. * It reduces the amount of the specific structure a player can build by 1. * diff --git a/src/ch/zhaw/catan/Road.java b/src/ch/zhaw/catan/Road.java index 6542231..a3e2ad9 100644 --- a/src/ch/zhaw/catan/Road.java +++ b/src/ch/zhaw/catan/Road.java @@ -1,12 +1,13 @@ package ch.zhaw.catan; -import java.awt.*; +import java.awt.Point; -/// TODO: 09/12/2021 Java Doc /** - * sub Class of Structure + * Sub Class of Structure * Can be saved in Siedler Board on Edges. + * + * @author Andrin Fassbind, Michael Ziegler */ public class Road extends Structure { @@ -14,10 +15,11 @@ public class Road extends Structure { private final Point start, end; /** - * Constructor of Road - * @param faction The faction of the owner - * @param start the coordinates of the start Point - * @param end the coordinates of the End Point + * Constructs a road with a start and endpoint. + * + * @param faction The faction of the road owner + * @param start the coordinates of the start as a Point + * @param end the coordinates of the end as a Point */ public Road(Config.Faction faction, Point start, Point end) { super(faction); @@ -26,16 +28,18 @@ public class Road extends Structure { } /** + * Returns the Coordinate as a Point of the start of the road. * - * @return the coordinates of the start Point + * @return Point of the start of the road */ public Point getStart() { return start; } /** + * Returns the Coordinate as a Point of the end of the road. * - * @return the coordinates of the end Point + * @return Point of the end of the road */ public Point getEnd() { return end; diff --git a/src/ch/zhaw/catan/Settlement.java b/src/ch/zhaw/catan/Settlement.java index 41f8e29..6897cb6 100644 --- a/src/ch/zhaw/catan/Settlement.java +++ b/src/ch/zhaw/catan/Settlement.java @@ -5,6 +5,8 @@ import java.awt.*; /** * Sub Class of Structure and Super Class of City * Can be saved in Siedler Board on a corner + * + * @author Andrin Fassbind, Roman Schenk */ public class Settlement extends Structure { @@ -17,6 +19,8 @@ public class Settlement extends Structure { } /** + * This Methode Returns the Position of the Settlement + * * @return the datafield position */ public Point getPosition() { diff --git a/src/ch/zhaw/catan/Siedler.java b/src/ch/zhaw/catan/Siedler.java index 7211246..5df282c 100644 --- a/src/ch/zhaw/catan/Siedler.java +++ b/src/ch/zhaw/catan/Siedler.java @@ -5,6 +5,8 @@ import java.util.Random; /** * This Class manages the game process and contains the Main Method which creates and starts a new Parser and a new Game. + * + * @author Leonardo Brandeberger, Roman Schenk */ public class Siedler { /** @@ -29,7 +31,7 @@ public class Siedler { boolean diceThrown = false; while (running) { Config.Faction currentPlayerFaction = game.getCurrentPlayerFaction(); - parser.displayGameboard(game.getBoard().getTextView()); //TODO Every turn or separate command? + parser.displayGameboard(game.getBoard().getTextView()); parser.playerTurn(currentPlayerFaction); if (!diceThrown) { throwDice(parser, game); diff --git a/src/ch/zhaw/catan/SiedlerBoard.java b/src/ch/zhaw/catan/SiedlerBoard.java index bcfab3b..ce3ea15 100644 --- a/src/ch/zhaw/catan/SiedlerBoard.java +++ b/src/ch/zhaw/catan/SiedlerBoard.java @@ -4,14 +4,13 @@ import ch.zhaw.hexboard.HexBoard; import ch.zhaw.hexboard.Label; import java.awt.Point; -import java.util.List; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.Map; import java.util.Iterator; -import java.util.Collections; -//TODO Enhance JavaDoc +import java.util.List; +import java.util.Map; /** * Subclass of HexBoard @@ -22,12 +21,11 @@ public class SiedlerBoard extends HexBoard fields = new HashMap<>(); - Config.Faction longestRoadFaction = null; - int longestRoadLength = 0; + private Config.Faction longestRoadFaction = null; + private int longestRoadLenth = 0; /** * Method to create the predefined game field from Config. @@ -138,6 +136,7 @@ public class SiedlerBoard extends HexBoard currentRoad) { - currentFaction = factionA; - currentRoad = players.get(factionA); - } - } - if (currentFaction != null) { - longestRoadFaction = currentFaction; - longestRoadLength = currentRoad; - } - } else { - for (Config.Faction faction : players.keySet()) { - if (players.get(faction) >= 5 && players.get(faction) > longestRoadLength) { - longestRoadFaction = faction; - longestRoadLength = players.get(faction); - } + for (Config.Faction factionA : players.keySet()) { + if (players.get(factionA) > longestRoadLenth && players.get(factionA) > 4) { + longestRoadFaction = factionA; + longestRoadLenth = players.get(factionA); } } return longestRoadFaction; } //todo javadoc - public int getLongestRoadLength() { - return longestRoadLength; + public int getLongestRoadLenth() { + return longestRoadLenth; } /** @@ -202,75 +186,55 @@ public class SiedlerBoard extends HexBoard countRoad(Config.Faction faction, Point position, HashSet roads, boolean add) { List roadsList = getAdjacentEdges(position); + //Checks if roads is interrupted by other players settlement if (getCorner(position) != null && getCorner(position).getFaction() != faction) { return roads; } - + //removes roads from roadsList which doesn't belong to the same player or which are allready added to roads. for (Road roadsRoad : roads) { - Iterator it3 = roadsList.iterator(); - while (it3.hasNext()) { - Road roadsListRoad = it3.next(); + Iterator it = roadsList.iterator(); + while (it.hasNext()) { + Road roadsListRoad = it.next(); if (roadsListRoad == roadsRoad || roadsListRoad.getFaction() != faction) { - it3.remove(); + it.remove(); } } } - if (roadsList.size() == 1) { roads.add(roadsList.get(0)); position = getNextPoint(roadsList.get(0), position); roads = countRoad(faction, position, roads, false); } else if (roadsList.size() == 2) { - HashSet listOne = (HashSet) roads.clone(); - HashSet listTwo = (HashSet) roads.clone(); - listOne.add(roadsList.get(0)); - Point positionOne = getNextPoint(roadsList.get(0), position); - listTwo.add(roadsList.get(1)); - Point positionTwo = getNextPoint(roadsList.get(1), position); - listOne = countRoad(faction, positionOne, listOne, false); - listTwo = countRoad(faction, positionTwo, listTwo, false); + ArrayList> possibleRoads = checkRoadsSwitch(faction,2,roadsList,position); if (add) { - listTwo.addAll(listOne); - roads = listTwo; + possibleRoads.get(1).addAll(possibleRoads.get(0)); + roads.addAll(possibleRoads.get(1)); } else { HashSet tallest; - if (listOne.size() >= listTwo.size()) { - tallest = listOne; + if (possibleRoads.get(0).size() >= possibleRoads.get(1).size()) { + tallest = possibleRoads.get(0); } else { - tallest = listTwo; + tallest = possibleRoads.get(1); } roads.addAll(tallest); } } else if (roadsList.size() == 3) { - HashSet listOne = (HashSet) roads.clone(); - HashSet listTwo = (HashSet) roads.clone(); - HashSet listThree = (HashSet) roads.clone(); - listOne.add(roadsList.get(0)); - Point positionOne = getNextPoint(roadsList.get(0), position); - listTwo.add(roadsList.get(1)); - Point positionTwo = getNextPoint(roadsList.get(1), position); - listThree.add(roadsList.get(2)); - Point positionThree = getNextPoint(roadsList.get(2), position); - listOne = countRoad(faction, positionOne, listOne, false); - listTwo = countRoad(faction, positionTwo, listTwo, false); - listThree = countRoad(faction, positionThree, listThree, false); - + ArrayList> possibleRoads = checkRoadsSwitch(faction,3,roadsList,position); HashSet tallest; HashSet secondTallest; - - if (listOne.size() >= listTwo.size()) { - tallest = listOne; - secondTallest = listTwo; + if (possibleRoads.get(0).size() >= possibleRoads.get(1).size()) { + tallest = possibleRoads.get(0); + secondTallest = possibleRoads.get(1); } else { - tallest = listTwo; - secondTallest = listOne; + tallest = possibleRoads.get(1); + secondTallest = possibleRoads.get(0); } - if (listThree.size() >= secondTallest.size()) { - secondTallest = listThree; + if (possibleRoads.get(2).size() >= secondTallest.size()) { + secondTallest = possibleRoads.get(2); } tallest.addAll(secondTallest); - roads = tallest; + roads.addAll(tallest); } return roads; } @@ -292,4 +256,24 @@ public class SiedlerBoard extends HexBoard> checkRoadsSwitch(Config.Faction faction,int numberOfSwitches,List roadsList,Point position) { + ArrayList> possibleRoads = new ArrayList<>(); + for (int i = 0;i list = new HashSet<>(); + list.add(roadsList.get(i)); + Point nextPosition = getNextPoint(roadsList.get(i),position); + list = countRoad(faction, nextPosition, list, false); + possibleRoads.add(list); + } + return possibleRoads; + } } diff --git a/src/ch/zhaw/catan/SiedlerGame.java b/src/ch/zhaw/catan/SiedlerGame.java index 300c483..8e0feff 100644 --- a/src/ch/zhaw/catan/SiedlerGame.java +++ b/src/ch/zhaw/catan/SiedlerGame.java @@ -30,8 +30,8 @@ public class SiedlerGame { * * @param winPoints the number of points required to win the game * @param numberOfPlayers the number of players - * @throws IllegalArgumentException if winPoints is lower than - * three or players is not between two and four + * @throws IllegalArgumentException if winPoints is lower than 3 + * or players is not between two and four */ public SiedlerGame(int winPoints, int numberOfPlayers) { if (winPoints < 3 || numberOfPlayers < Config.MIN_NUMBER_OF_PLAYERS || numberOfPlayers > 4) {