fixed minor refactoring issues and removed import of ch.zhaw.catan in SiedlerBoard and used as package.

This commit is contained in:
Leonardo Brandenberger 2021-12-09 18:18:10 +01:00
parent 187302afd5
commit afd874bafa
8 changed files with 40 additions and 46 deletions

View File

@ -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;

View File

@ -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) {

View File

@ -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.

View File

@ -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);

View File

@ -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:

View File

@ -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<Land, Settlement, Road, String> {
public class SiedlerBoard extends HexBoard<Config.Land, Settlement, Road, String> {
/**
* HashMap to save all Fields which are set yet.
@ -26,7 +25,7 @@ public class SiedlerBoard extends HexBoard<Land, Settlement, Road, String> {
* //TODO Enhance JavaDoc
* Value: Field Object
*/
HashMap<Point, Field> fields = new HashMap<>();
private final HashMap<Point, Field> fields = new HashMap<>();
Config.Faction longestRoadFaction = null;
int longestRoadLenth = 0;
@ -34,9 +33,9 @@ public class SiedlerBoard extends HexBoard<Land, Settlement, Road, String> {
* Method to create the predefined game field from Config.
*/
public void createFixGameField() {
Map<Point, Land> resourcePlacement = Config.getStandardLandPlacement();
Map<Point, Config.Land> resourcePlacement = Config.getStandardLandPlacement();
Map<Point, Integer> dicePlacement = Config.getStandardDiceNumberPlacement();
for (Map.Entry<Point, Land> resourceField : resourcePlacement.entrySet()) {
for (Map.Entry<Point, Config.Land> 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<Land, Settlement, Road, String> {
}
/**
* 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<Land> getLandsForCorner(Point corner) {
public List<Config.Land> 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<Land, Settlement, Road, String> {
}
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<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 Config.Faction getLongestRoadFaction(List<Config.Faction> factionList) {
List<Settlement> corners = getCorners();

View File

@ -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.
*
* <p>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<Faction> getPlayerFactions() {
List<Faction> factions = new ArrayList<>();
public List<Config.Faction> getPlayerFactions() {
List<Config.Faction> 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<Resource, Integer> getCurrentPlayerResource() {
public HashMap<Config.Resource, Integer> getCurrentPlayerResource() {
return allPlayers.get(activePlayer).getResources();
}
@ -202,7 +199,7 @@ public class SiedlerGame {
* {@link Config#MAX_CARDS_IN_HAND_NO_DROP} resource cards.
* <p>
* If a player does not get resource cards, the list for this players'
* {@link Faction} is <b>an empty list (not null)</b>!.
* {@link Config.Faction} is <b>an empty list (not null)</b>!.
*
* <p>
* 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<Faction, List<Resource>> throwDice(int diceThrow) {
public Map<Config.Faction, List<Config.Resource>> throwDice(int diceThrow) {
if (diceThrow == 7) {
for (Player player : allPlayers) {
handleDiceThrow7(player);
}
} else {
Map<Faction, List<Resource>> returnMap = new HashMap<>();
Map<Config.Faction, List<Config.Resource>> returnMap = new HashMap<>();
List<Point> diceValueFields = board.getFieldsForDiceValue(diceThrow);
for (Player player : allPlayers) {
returnMap.put(player.getFaction(), new ArrayList<>());
for (Point field : diceValueFields) {
List<Resource> resources = board.getResourcesForFaction(field, player.getFaction());
List<Config.Resource> 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<Config.Resource> resourceArrayList = new ArrayList<>();
HashMap<Resource, Integer> resources = player.getResources();
HashMap<Config.Resource, Integer> 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<Settlement> 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();
}

View File

@ -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;