package ch.zhaw.catan; import ch.zhaw.catan.Config.Faction; import ch.zhaw.catan.Config.Resource; import java.awt.*; import java.util.*; import java.util.List; /** * This class performs all actions related to modifying the game state. *

* TODO: (your documentation) * * @author TODO */ public class SiedlerGame { static final int FOUR_TO_ONE_TRADE_OFFER = 4; static final int FOUR_TO_ONE_TRADE_WANT = 1; private SiedlerBoard board; private ArrayList allPlayers; private int winPointsForWin; private Bank bank; private int activePlayer; /** * Constructs a SiedlerGame game state object. * * @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 */ public SiedlerGame(int winPoints, int numberOfPlayers) { if(winPoints < 3 || numberOfPlayers < 2 || numberOfPlayers > 4) { throw new IllegalArgumentException(); } bank = new Bank(); board = new SiedlerBoard(); board.createFixGamefield(); allPlayers = new ArrayList<>(); createPlayer(numberOfPlayers); activePlayer = 0; this.winPointsForWin = winPoints; } private void createPlayer(int numberOfPlayers) { for (int i = 0; i < numberOfPlayers; i++) { allPlayers.add(new Player(Config.Faction.values()[i])); } } /** * Switches to the next player in the defined sequence of players. */ public void switchToNextPlayer() { if (activePlayer < allPlayers.size() -1){ activePlayer++; } else if (activePlayer == allPlayers.size() -1){ activePlayer = 0; } } /** * Switches to the previous player in the defined sequence of players. */ public void switchToPreviousPlayer() { if (activePlayer > 0){ activePlayer--; } else if (activePlayer == 0){ activePlayer = allPlayers.size()-1; } } private boolean addResourcesToPlayer(Player player, Resource resource, int numberToAdd){ if(bank.getResourceFromBank(resource, numberToAdd)){ player.addResource(resource, numberToAdd); return true; } return false; } private boolean substractResourceFromPlayer(Player player, Resource resource, int numberToSubstract){ if(player.substractResource(resource, numberToSubstract)){ bank.storeResourceToBank(resource, numberToSubstract); return true; } return false; } /** * Returns the {@link Faction}s of the active players. * *

The order of the player's factions in the list must * correspond to the oder in which they play. * Hence, the player that sets the first settlement must be * at position 0 in the list etc. * * Important note: The list must contain the * factions of active players only.

* * @return the list with player's factions */ public List getPlayerFactions() { List factions = new ArrayList<>(); for (Player player: allPlayers ) { factions.add(player.getFaction()); } return factions; } /** * Returns the game board. * * @return the game board */ public SiedlerBoard getBoard() { return board; } /** * Returns the {@link Faction} of the current player. * * @return the faction of the current player */ public Faction getCurrentPlayerFaction() { return allPlayers.get(activePlayer).getFaction(); } /** * Returns how many resource cards of the specified type * the current player owns. * * @param resource the resource type * @return the number of resource cards of this type */ public int getCurrentPlayerResourceStock(Resource resource) { return allPlayers.get(activePlayer).getSpecificResource(resource); } public HashMap getCurrentPlayerResource() { return allPlayers.get(activePlayer).getResources(); } /** * Places a settlement in the founder's phase (phase II) of the game. * *

The placement does not cost any resource cards. If payout is * set to true, for each adjacent resource-producing field, a resource card of the * type of the resource produced by the field is taken from the bank (if available) and added to * the players' stock of resource cards.

* * @param position the position of the settlement * @param payout if true, the player gets one resource card per adjacent resource-producing field * @return true, if the placement was successful */ public boolean placeInitialSettlement(Point position, boolean payout) { if(!validPositionForSettlement(position)){ return false; } board.setCorner(position, new Settlement(allPlayers.get(activePlayer).getFaction(),position)); if(payout) { List lands = board.getLandsForCorner(position); for (Config.Land land : lands) { if (land.getResource() != null) { addResourcesToPlayer(allPlayers.get(activePlayer), land.getResource(), 1); } } } return true; } /** * Places a road in the founder's phase (phase II) of the game. * The placement does not cost any resource cards. * * @param roadStart position of the start of the road * @param roadEnd position of the end of the road * @return true, if the placement was successful */ public boolean placeInitialRoad(Point roadStart, Point roadEnd) { if (!validPositionForRoad(roadStart, roadEnd)){ return false; } board.setEdge(roadStart, roadEnd, new Road(allPlayers.get(activePlayer).getFaction(),roadStart,roadEnd)); return true; } /** * This method takes care of actions depending on the dice throw result. *

* A key action is the payout of the resource cards to the players * according to the payout rules of the game. This includes the * "negative payout" in case a 7 is thrown and a player has more than * {@link Config#MAX_CARDS_IN_HAND_NO_DROP} resource cards. *

* If a player does not get resource cards, the list for this players' * {@link Faction} is an empty list (not null)!. * *

* The payout rules of the game take into account factors such as, the number * of resource cards currently available in the bank, settlement types * (settlement or city), and the number of players that should get resource * cards of a certain type (relevant if there are not enough left in the bank). *

* * @param dicethrow the resource cards that have been distributed to the players * @return the resource cards added to the stock of the different players */ public Map> throwDice(int dicethrow) { if (dicethrow == 7) { for(Player player : allPlayers) { handleDiceThrow7(player); } } else { Map> returnMap= new HashMap<>(); List diceValueFields = board.getFieldsForDiceValue(dicethrow); for (Player player : allPlayers) { returnMap.put(player.getFaction(), new ArrayList()); for (Point field : diceValueFields) { List resources = board.getResourcesforFaction(field,player.getFaction()); for (Config.Resource resource : resources){ returnMap.get(player.getFaction()).add(resource); addResourcesToPlayer(player, resource, 1); } } } return returnMap; } return null; } public void handleDiceThrow7(Player player) { ArrayList resourceArrayList = new ArrayList<>(); HashMap resources = player.getResources(); for(Config.Resource resource : resources.keySet()){ for(int i = 0; i < resources.get(resource); i++) { resourceArrayList.add(resource); } } if(resourceArrayList.size() > 7){ int resourcesToRemove =resourceArrayList.size() - (resourceArrayList.size() / 2); Random random = new Random(); for(int i = 0; i < resourcesToRemove; i++){ substractResourceFromPlayer(player, resourceArrayList.remove(random.nextInt(resourceArrayList.size())), 1); } } } /** * Builds a settlement at the specified position on the board. * *

The settlement can be built if: *

    *
  • the player possesses the required resource cards
  • *
  • a settlement to place on the board
  • *
  • the specified position meets the build rules for settlements
  • *
* * @param position the position of the settlement * @return true, if the placement was successful */ public boolean buildSettlement(Point position) { //1. Check if position is corner && is empty && neighbour Corners are empty if(!validPositionForSettlement(position)) { return false; } //2. Check if neighbourEdge are Roads belong to active Player if(!checkAdjacentEdgesList(position)) { return false; } //3. Can Player build Settlement if (!allPlayers.get(activePlayer).build(Config.Structure.SETTLEMENT)) { return false; } List costs = Config.Structure.SETTLEMENT.getCosts(); for (Config.Resource resource : costs) { substractResourceFromPlayer(allPlayers.get(activePlayer), resource, 1); } //4. Insert Settlement to map board.setCorner(position, new Settlement(allPlayers.get(activePlayer).getFaction(),position)); return true; } /** * Builds a city at the specified position on the board. * *

The city can be built if: *

    *
  • the player possesses the required resource cards
  • *
  • a city to place on the board
  • *
  • the specified position meets the build rules for cities
  • *
* * @param position the position of the city * @return true, if the placement was successful */ public boolean buildCity(Point position) { //1. Check if Corner. if (!board.hasCorner(position)){ return false; } //2. Check if Settlement has already been built Settlement atCurrentPosition = board.getCorner(position); if (atCurrentPosition == null || atCurrentPosition instanceof City || atCurrentPosition.getFaction() != allPlayers.get(activePlayer).getFaction()){ return false; } //3. Can player build a City. if(!allPlayers.get(activePlayer).build(Config.Structure.CITY)){ return false; } List costs = Config.Structure.CITY.getCosts(); for (Config.Resource resource : costs) { substractResourceFromPlayer(allPlayers.get(activePlayer), resource, 1); } //4.Insert City into the map. board.setCorner(position,new City(allPlayers.get(activePlayer).getFaction(),position)); return true; } /** * Builds a road at the specified position on the board. * *

The road can be built if: *

    *
  • the player possesses the required resource cards
  • *
  • a road to place on the board
  • *
  • the specified position meets the build rules for roads
  • *
* * @param roadStart the position of the start of the road * @param roadEnd the position of the end of the road * @return true, if the placement was successful */ public boolean buildRoad(Point roadStart, Point roadEnd) { //1. Check if is edge && is empty && if neighbour Edge or Corners belong to Settlement of active Player if (!validPositionForRoad(roadStart,roadEnd)){ return false; } //2. Can Player build road if (!allPlayers.get(activePlayer).build(Config.Structure.ROAD)) { return false; } List costs = Config.Structure.ROAD.getCosts(); for (Config.Resource resource : costs) { substractResourceFromPlayer(allPlayers.get(activePlayer), resource, 1); } //3. Insert Road to map board.setEdge(roadStart, roadEnd, new Road(allPlayers.get(activePlayer).getFaction(),roadStart,roadEnd)); return true; } /** * Can be used for both initial Settlement and normal Phase. * @param roadStart * @param roadEnd * @return */ private boolean validPositionForRoad(Point roadStart, Point roadEnd){ //1. Check if Edge if (!board.hasEdge(roadStart, roadEnd)) { return false; } //2. Check if Edge is empty if (board.getEdge(roadStart, roadEnd) != null) { return false; } //3. Check if NeighbourEdge are Roads boolean hasNeighbourRoad = (checkAdjacentEdgesList(roadStart) || checkAdjacentEdgesList(roadEnd)); if(hasNeighbourRoad) { return true; } //4.Check if roadStart or roadEnd is Settlement of current player if((board.getCorner(roadStart)!=null && board.getCorner(roadStart).getFaction() == allPlayers.get(activePlayer).getFaction()) ||(board.getCorner(roadEnd)!=null && board.getCorner(roadEnd).getFaction() == allPlayers.get(activePlayer).getFaction())) { return true; } return false; } /** * Can be used for both initial Settlement and normal Phase. * @param position * @return */ private boolean validPositionForSettlement(Point position){ //1. Check if Corner if (!board.hasCorner(position)) { return false; } //2. Check if water if(checkIfWater(position)) { return false; } //3. Check if Corner is empty if(board.getCorner(position) != null) { return false; } //3. Check if neighbourCorners are empty if(!checkAdjacentCornerList(position)) { return false; } return true; } private boolean checkIfWater(Point point) { List fields = board.getFields(point); for(Config.Land land : fields) { if(!land.equals(Config.Land.WATER)) { return false; } } return true; } /** * This method checks if there are Roads build by active Player on adjacent edges * @param point * @return */ private boolean checkAdjacentEdgesList(Point point) { List results = board.getAdjacentEdges(point); for(int i = 0; i < results.size(); i++) { if(results.get(i).getFaction() == allPlayers.get(activePlayer).getFaction()) { return true; } } return false; } /** * Checks if Adjacent Corners are empty * @param point Corner to check * @return true if all Neighbour Corners are emtpy */ private boolean checkAdjacentCornerList(Point point) { List results = board.getNeighboursOfCorner(point); if(results.size() > 0) { return false; } return true; } /** * Trades in {@link #FOUR_TO_ONE_TRADE_OFFER} resource cards of the * offered type for {@link #FOUR_TO_ONE_TRADE_WANT} resource cards of the wanted type. *

* The trade only works when bank and player possess the resource cards * for the trade before the trade is executed. * * @param offer offered type * @param want wanted type * @return true, if the trade was successful */ public boolean tradeWithBankFourToOne(Resource offer, Resource want) { Player player = allPlayers.get(activePlayer); if(player.getSpecificResource(offer) >= FOUR_TO_ONE_TRADE_OFFER && addResourcesToPlayer(player, want, FOUR_TO_ONE_TRADE_WANT)){ substractResourceFromPlayer(player, offer, FOUR_TO_ONE_TRADE_OFFER); return true; } return false; } /** * Returns the winner of the game, if any. * * @return the winner of the game or null, if there is no winner (yet) */ public Faction getWinner() { if(getCurrentPlayerWinpoints() >= winPointsForWin){ return getCurrentPlayerFaction(); } return null; } public int getCurrentPlayerWinpoints(){ int winPoints = 0; List settlements = board.getCorners(); for(Structure structure : settlements) { int newWinPoints = 0; if(structure instanceof City){ newWinPoints = 2; } else if(structure instanceof Settlement) { newWinPoints = 1; } if(structure.getFaction() == getCurrentPlayerFaction()){ winPoints ++; } } if(getLongestRoadFaction() == getCurrentPlayerFaction()){ winPoints += 2; } return winPoints; } /** * 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() { 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); for(Settlement settlement : corners){ HashSet roads = new HashSet<>(); roads = countRoad(faction,settlement.getPosition(),roads,true); count = roads.size(); int currentCount = players.get(faction); if(count > currentCount) { players.put(faction,count); } } } for(Config.Faction faction : players.keySet()) { if(players.get(faction) >= 5 && players.get(faction)>highest) { highest = players.get(faction); longestRoad = faction; } } return longestRoad; } /** * This method is recursive and adds all roads which belongs to a specific players and stringing together to a HashSet. * The length of the HashSet represents the length of the longest Road the player has. * @param faction the faction of the player to check on * @param position there has to be a starting point to start counting. In this case it's a corner where a settlement belonging to the players faction is build on. * @param roads is the hashset with all roads belong to the player which are stringing together * @param add if true branches needs to be count together. (for example if it is the starting point(first time of counting)) otherwise the longest branch is beeing added to roads. * @return HashSet with all roads from a specific player which are string together. */ private HashSet countRoad(Config.Faction faction,Point position,HashSet roads,boolean add) { List roadslist = board.getAdjacentEdges(position); if(board.getCorner(position) != null || board.getCorner(position).getFaction() != faction) { return roads; } Iterator it2 = roads.iterator(); while(it2.hasNext()) { Road roadsroad = (Road) it2.next(); Iterator it3 = roadslist.iterator(); while (it3.hasNext()){ Road roadslistRoad = (Road) it3.next(); if(roadslistRoad == roadsroad || roadslistRoad.getFaction() != faction) { it3.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); if(add) { for (Road road : listOne) { listTwo.add(road); } roads = listTwo; }else { HashSet tallest; if(listOne.size()>= listTwo.size()) { tallest = listOne; }else{ tallest = listTwo; } for (Road road : tallest) { roads.add(road); } } } 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); HashSet tallest; HashSet secondtallest; if(listOne.size()>=listTwo.size()) { tallest = listOne; secondtallest = listTwo; }else { tallest = listTwo; secondtallest = listOne; }if(listThree.size() >= secondtallest.size()) { secondtallest = listThree; } for(Road road : secondtallest) { tallest.add(road); } roads = tallest; } return roads; } /** * This method is beeing used to evaluate the next starting position to get the adjacent Roads from it. * @param road the next road to check on * @param position the current starting point * @return return the oposite point of the current point. */ private Point getNextPoint(Road road,Point position) { Point start = road.getStart(); Point end = road.getEnd(); if(position.equals(start)) { position = end; }else { position = start; } return position; } /** * Places the thief on the specified field and steals a random resource card (if * the player has such cards) from a random player with a settlement at that * field (if there is a settlement) and adds it to the resource cards of the * current player. * * @param field the field on which to place the thief * @return false, if the specified field is not a field or the thief cannot be * placed there (e.g., on water) */ public boolean placeThiefAndStealCard(Point field) { //TODO: Implement (or longest road functionality) return false; } }