Merge remote-tracking branch 'origin/main'
# Conflicts: # .idea/misc.xml # src/ch/zhaw/catan/Road.java # src/ch/zhaw/catan/SiedlerBoard.java
This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<Config.Land, Settlement, Road, String
|
||||
/**
|
||||
* HashMap to save all Fields which are set yet.
|
||||
* Key: Point with coordinates of the field
|
||||
* //TODO Enhance JavaDoc
|
||||
* Value: Field Object
|
||||
*/
|
||||
private final HashMap<Point, Field> 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<Config.Land, Settlement, Road, String
|
||||
|
||||
/**
|
||||
* This method checks for the player with the longest road according to the siedler game rules.
|
||||
*
|
||||
* @param factionList a List with all factions which can place structures on the board
|
||||
* @return the faction who owns the longest road with minimum length of 5, null there is no road longer then 4
|
||||
*/
|
||||
@@ -161,33 +160,18 @@ public class SiedlerBoard extends HexBoard<Config.Land, Settlement, Road, String
|
||||
}
|
||||
}
|
||||
|
||||
if (longestRoadFaction == null) {
|
||||
Config.Faction currentFaction = null;
|
||||
int currentRoad = 4;
|
||||
for (Config.Faction factionA : players.keySet()) {
|
||||
if (players.get(factionA) > 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<Config.Land, Settlement, Road, String
|
||||
*/
|
||||
private HashSet<Road> countRoad(Config.Faction faction, Point position, HashSet<Road> roads, boolean add) {
|
||||
List<Road> 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<Road> it3 = roadsList.iterator();
|
||||
while (it3.hasNext()) {
|
||||
Road roadsListRoad = it3.next();
|
||||
Iterator<Road> 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<Road> listOne = (HashSet<Road>) roads.clone();
|
||||
HashSet<Road> listTwo = (HashSet<Road>) 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<HashSet<Road>> 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<Road> 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<Road> listOne = (HashSet<Road>) roads.clone();
|
||||
HashSet<Road> listTwo = (HashSet<Road>) roads.clone();
|
||||
HashSet<Road> listThree = (HashSet<Road>) 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<HashSet<Road>> possibleRoads = checkRoadsSwitch(faction,3,roadsList,position);
|
||||
HashSet<Road> tallest;
|
||||
HashSet<Road> 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<Config.Land, Settlement, Road, String
|
||||
}
|
||||
return position;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method checks at a roads switch, which road starting at the switch will be the longest possibility.
|
||||
* @param faction the faction of the player to check on
|
||||
* @param numberOfSwitches how many adjacent roads there are at the corner
|
||||
* @param roadsList the list of adjacent roads
|
||||
* @param position the current corner from where to count further.
|
||||
* @return ArrayList of HashSet with all possible forks.
|
||||
*/
|
||||
private ArrayList<HashSet<Road>> checkRoadsSwitch(Config.Faction faction,int numberOfSwitches,List<Road> roadsList,Point position) {
|
||||
ArrayList<HashSet<Road>> possibleRoads = new ArrayList<>();
|
||||
for (int i = 0;i<numberOfSwitches;i++) {
|
||||
HashSet<Road> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user