From afd874bafaf4be92446977002ade0ee16072f3b2 Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Thu, 9 Dec 2021 18:18:10 +0100 Subject: [PATCH] fixed minor refactoring issues and removed import of ch.zhaw.catan in SiedlerBoard and used as package. --- src/ch/zhaw/catan/Command.java | 4 +-- src/ch/zhaw/catan/Field.java | 2 +- src/ch/zhaw/catan/Parser.java | 4 +-- src/ch/zhaw/catan/Settlement.java | 2 +- src/ch/zhaw/catan/Siedler.java | 12 ++++----- src/ch/zhaw/catan/SiedlerBoard.java | 21 +++++++--------- src/ch/zhaw/catan/SiedlerGame.java | 39 +++++++++++++---------------- src/ch/zhaw/catan/Structure.java | 2 +- 8 files changed, 40 insertions(+), 46 deletions(-) diff --git a/src/ch/zhaw/catan/Command.java b/src/ch/zhaw/catan/Command.java index c84f078..4fa2e77 100644 --- a/src/ch/zhaw/catan/Command.java +++ b/src/ch/zhaw/catan/Command.java @@ -2,8 +2,8 @@ package ch.zhaw.catan; //TODO:JavaDoc public enum Command { - NEXTPLAYER("next player"), BUILDSETTLEMENT("build settlement"), BUILDCITY("build city"), - BUILDROAD("build road"), TRADEWITHBANK("trade with bank"), QUIT("quit"); + NEXT_PLAYER("next player"), BUILD_SETTLEMENT("build settlement"), BUILD_CITY("build city"), + BUILD_ROAD("build road"), TRADE_WITH_BANK("trade with bank"), QUIT("quit"); private final String commandWord; diff --git a/src/ch/zhaw/catan/Field.java b/src/ch/zhaw/catan/Field.java index 973de5d..9d43114 100644 --- a/src/ch/zhaw/catan/Field.java +++ b/src/ch/zhaw/catan/Field.java @@ -4,7 +4,7 @@ import ch.zhaw.hexboard.Label; public class Field { - private Config.Land land; + private final Config.Land land; private final Label label; public Field(Config.Land land, Label label) { diff --git a/src/ch/zhaw/catan/Parser.java b/src/ch/zhaw/catan/Parser.java index be0d42c..a43352a 100644 --- a/src/ch/zhaw/catan/Parser.java +++ b/src/ch/zhaw/catan/Parser.java @@ -13,8 +13,8 @@ import java.util.HashMap; * @author Leonardo Brandenberger */ public class Parser { - TextIO textIO; - TextTerminal textTerminal; + private final TextIO textIO; + private final TextTerminal textTerminal; /** * Constructs a parser Object, initializes the textIO components TextTerminal and textIO. diff --git a/src/ch/zhaw/catan/Settlement.java b/src/ch/zhaw/catan/Settlement.java index 5ca8495..250daa5 100644 --- a/src/ch/zhaw/catan/Settlement.java +++ b/src/ch/zhaw/catan/Settlement.java @@ -5,7 +5,7 @@ import java.awt.*; //TODO Java Doc public class Settlement extends Structure { - private Point position; + private final Point position; public Settlement(Config.Faction faction, Point position) { super(faction); diff --git a/src/ch/zhaw/catan/Siedler.java b/src/ch/zhaw/catan/Siedler.java index 1fd0adb..7211246 100644 --- a/src/ch/zhaw/catan/Siedler.java +++ b/src/ch/zhaw/catan/Siedler.java @@ -29,7 +29,7 @@ public class Siedler { boolean diceThrown = false; while (running) { Config.Faction currentPlayerFaction = game.getCurrentPlayerFaction(); - parser.displayGameboard(game.getBoard().getTextView()); //todo jedesmal ausgeben? oder nur wenn neuer Spieler oder separater Befehl? + parser.displayGameboard(game.getBoard().getTextView()); //TODO Every turn or separate command? parser.playerTurn(currentPlayerFaction); if (!diceThrown) { throwDice(parser, game); @@ -37,23 +37,23 @@ public class Siedler { } parser.displayPlayerInfo(game.getCurrentPlayerResource(), game.getCurrentPlayerWinPoints()); switch (parser.getAction()) { - case NEXTPLAYER: + case NEXT_PLAYER: if (caseNextPlayer(parser, game)) { running = false; } else { diceThrown = false; } break; - case BUILDSETTLEMENT: + case BUILD_SETTLEMENT: caseBuildStructure(parser, game, Config.Structure.SETTLEMENT); break; - case BUILDCITY: + case BUILD_CITY: caseBuildStructure(parser, game, Config.Structure.CITY); break; - case BUILDROAD: + case BUILD_ROAD: caseBuildStructure(parser, game, Config.Structure.ROAD); break; - case TRADEWITHBANK: + case TRADE_WITH_BANK: caseTradeWithBank(parser, game); break; case QUIT: diff --git a/src/ch/zhaw/catan/SiedlerBoard.java b/src/ch/zhaw/catan/SiedlerBoard.java index 2939544..e4590cc 100644 --- a/src/ch/zhaw/catan/SiedlerBoard.java +++ b/src/ch/zhaw/catan/SiedlerBoard.java @@ -1,6 +1,5 @@ package ch.zhaw.catan; -import ch.zhaw.catan.Config.Land; import ch.zhaw.hexboard.HexBoard; import ch.zhaw.hexboard.Label; @@ -18,7 +17,7 @@ import java.util.Collections; * Subclass of HexBoard * Saves the fields which are set and handles Methods with specific Dice Numbers. */ -public class SiedlerBoard extends HexBoard { +public class SiedlerBoard extends HexBoard { /** * HashMap to save all Fields which are set yet. @@ -26,7 +25,7 @@ public class SiedlerBoard extends HexBoard { * //TODO Enhance JavaDoc * Value: Field Object */ - HashMap fields = new HashMap<>(); + private final HashMap fields = new HashMap<>(); Config.Faction longestRoadFaction = null; int longestRoadLenth = 0; @@ -34,9 +33,9 @@ public class SiedlerBoard extends HexBoard { * Method to create the predefined game field from Config. */ public void createFixGameField() { - Map resourcePlacement = Config.getStandardLandPlacement(); + Map resourcePlacement = Config.getStandardLandPlacement(); Map dicePlacement = Config.getStandardDiceNumberPlacement(); - for (Map.Entry resourceField : resourcePlacement.entrySet()) { + for (Map.Entry resourceField : resourcePlacement.entrySet()) { addField(resourceField.getKey(), resourceField.getValue()); if (dicePlacement.get(resourceField.getKey()) != null) { String numberAsString = dicePlacement.get(resourceField.getKey()).toString(); @@ -113,15 +112,15 @@ public class SiedlerBoard extends HexBoard { } /** - * Returns the {@link Land}s adjacent to the specified corner. + * Returns the {@link Config.Land}s adjacent to the specified corner. * * @param corner the corner - * @return the list with the adjacent {@link Land}s + * @return the list with the adjacent {@link Config.Land}s */ - public List getLandsForCorner(Point corner) { + public List getLandsForCorner(Point corner) { Point above = new Point(corner.x, corner.y + 2); Point below = new Point(corner.x, corner.y - 2); - Land[] lands = new Land[3]; + Config.Land[] lands = new Config.Land[3]; if (hasField(above)) { lands[0] = getField(above); lands[1] = getField(new Point(corner.x + 1, corner.y - 1)); @@ -135,13 +134,11 @@ public class SiedlerBoard extends HexBoard { } return List.of(lands); } -//TODO Java Doc no return +//TODO Java Doc more details /** * This method checks for the player with the longest road according to the siedler game rules. * - * @return a HashMap 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 Config.Faction getLongestRoadFaction(List factionList) { List corners = getCorners(); diff --git a/src/ch/zhaw/catan/SiedlerGame.java b/src/ch/zhaw/catan/SiedlerGame.java index 5100391..300c483 100644 --- a/src/ch/zhaw/catan/SiedlerGame.java +++ b/src/ch/zhaw/catan/SiedlerGame.java @@ -1,8 +1,5 @@ package ch.zhaw.catan; -import ch.zhaw.catan.Config.Faction; -import ch.zhaw.catan.Config.Resource; - import java.awt.Point; import java.util.Map; import java.util.HashMap; @@ -78,7 +75,7 @@ public class SiedlerGame { } //TODO JavaDoc - private boolean addResourcesToPlayer(Player player, Resource resource, int numberToAdd) { + private boolean addResourcesToPlayer(Player player, Config.Resource resource, int numberToAdd) { if (bank.getResourceFromBank(resource, numberToAdd)) { player.addResource(resource, numberToAdd); return true; @@ -87,7 +84,7 @@ public class SiedlerGame { } //TODO JavaDoc - private boolean subtractResourceFromPlayer(Player player, Resource resource, int numberToSubtract) { + private boolean subtractResourceFromPlayer(Player player, Config.Resource resource, int numberToSubtract) { if (player.subtractResource(resource, numberToSubtract)) { bank.storeResourceToBank(resource, numberToSubtract); return true; @@ -96,7 +93,7 @@ public class SiedlerGame { } /** - * Returns the {@link Faction}s of the active players. + * Returns the {@link Config.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. @@ -108,8 +105,8 @@ public class SiedlerGame { * * @return the list with player's factions */ - public List getPlayerFactions() { - List factions = new ArrayList<>(); + public List getPlayerFactions() { + List factions = new ArrayList<>(); for (Player player : allPlayers) { factions.add(player.getFaction()); } @@ -126,11 +123,11 @@ public class SiedlerGame { } /** - * Returns the {@link Faction} of the current player. + * Returns the {@link Config.Faction} of the current player. * * @return the faction of the current player */ - public Faction getCurrentPlayerFaction() { + public Config.Faction getCurrentPlayerFaction() { return allPlayers.get(activePlayer).getFaction(); } @@ -141,11 +138,11 @@ public class SiedlerGame { * @param resource the resource type * @return the number of resource cards of this type */ - public int getCurrentPlayerResourceStock(Resource resource) { + public int getCurrentPlayerResourceStock(Config.Resource resource) { return allPlayers.get(activePlayer).getSpecificResource(resource); } - public HashMap getCurrentPlayerResource() { + public HashMap getCurrentPlayerResource() { return allPlayers.get(activePlayer).getResources(); } @@ -202,7 +199,7 @@ public class SiedlerGame { * {@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)!. + * {@link Config.Faction} is an empty list (not null)!. * *

* The payout rules of the game take into account factors such as, the number @@ -214,18 +211,18 @@ public class SiedlerGame { * @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) { + public Map> throwDice(int diceThrow) { if (diceThrow == 7) { for (Player player : allPlayers) { handleDiceThrow7(player); } } else { - Map> returnMap = new HashMap<>(); + 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()); + List resources = board.getResourcesForFaction(field, player.getFaction()); for (Config.Resource resource : resources) { returnMap.get(player.getFaction()).add(resource); addResourcesToPlayer(player, resource, 1); @@ -240,7 +237,7 @@ public class SiedlerGame { //TODO JavaDoc public void handleDiceThrow7(Player player) { ArrayList resourceArrayList = new ArrayList<>(); - HashMap resources = player.getResources(); + HashMap resources = player.getResources(); for (Config.Resource resource : resources.keySet()) { for (int i = 0; i < resources.get(resource); i++) { resourceArrayList.add(resource); @@ -379,7 +376,7 @@ public class SiedlerGame { if (!board.hasEdge(roadStart, roadEnd)) { return false; } - //2. Check if edge is empty + //2. Check if edge is empty //TODO Check if always inverted is allowed if (board.getEdge(roadStart, roadEnd) != null) { return false; } @@ -446,7 +443,7 @@ public class SiedlerGame { * Checks if Adjacent Corners are empty * * @param point Corner to check - * @return true if all Neighbour Corners are emtpy + * @return true if all Neighbour Corners are empty */ private boolean checkAdjacentCornerList(Point point) { List results = board.getNeighboursOfCorner(point); @@ -464,7 +461,7 @@ public class SiedlerGame { * @param want wanted type * @return true, if the trade was successful */ - public boolean tradeWithBankFourToOne(Resource offer, Resource want) { + public boolean tradeWithBankFourToOne(Config.Resource offer, Config.Resource want) { Player player = allPlayers.get(activePlayer); if (player.getSpecificResource(offer) >= FOUR_TO_ONE_TRADE_OFFER && addResourcesToPlayer(player, want, FOUR_TO_ONE_TRADE_WANT)) { subtractResourceFromPlayer(player, offer, FOUR_TO_ONE_TRADE_OFFER); @@ -478,7 +475,7 @@ public class SiedlerGame { * * @return the winner of the game or null, if there is no winner (yet) */ - public Faction getWinner() { + public Config.Faction getWinner() { if (getCurrentPlayerWinPoints() >= winPointsForWin) { return getCurrentPlayerFaction(); } diff --git a/src/ch/zhaw/catan/Structure.java b/src/ch/zhaw/catan/Structure.java index 54e589f..1218ebb 100644 --- a/src/ch/zhaw/catan/Structure.java +++ b/src/ch/zhaw/catan/Structure.java @@ -1,7 +1,7 @@ package ch.zhaw.catan; public abstract class Structure { - private Config.Faction faction; + private final Config.Faction faction; public Structure(Config.Faction faction) { this.faction = faction;