Minor Grammar and Cameltow fixes

This commit is contained in:
Leonardo Brandenberger 2021-12-09 17:19:40 +01:00
parent f3b9cd2771
commit ffc97a39ef
11 changed files with 254 additions and 250 deletions

View File

@ -6,22 +6,21 @@ import java.util.HashMap;
public class Bank {
private final HashMap<Config.Resource, Integer> resources = new HashMap<>();
public Bank(){
public Bank() {
resources.putAll(Config.INITIAL_RESOURCE_CARDS_BANK);
}
public void storeResourceToBank(Config.Resource resource, int numberOfResources) {
resources.put(resource,resources.get(resource)+numberOfResources);
resources.put(resource, resources.get(resource) + numberOfResources);
}
public boolean getResourceFromBank(Config.Resource resource,int numberOfResources) {
if(resources.get(resource) >= numberOfResources) {
public boolean getResourceFromBank(Config.Resource resource, int numberOfResources) {
if (resources.get(resource) >= numberOfResources) {
Integer newResourceNumber = resources.get(resource) - numberOfResources;
resources.put(resource, newResourceNumber);
return true;
}
else {
} else {
return false;
}
}

View File

@ -5,7 +5,7 @@ import java.awt.Point;
public class City extends Settlement {
public City(Config.Faction faction, Point position) {
super(faction,position);
super(faction, position);
}
public String toString() {

View File

@ -2,17 +2,16 @@ 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");
NEXTPLAYER("next player"), BUILDSETTLEMENT("build settlement"), BUILDCITY("build city"),
BUILDROAD("build road"), TRADEWITHBANK("trade with bank"), QUIT("quit");
private final String commandWord;
Command(String commandWord) {
this.commandWord = commandWord;
}
public String toString() {
return commandWord;
}

View File

@ -7,12 +7,12 @@ public class Field {
private Config.Land land;
private final Label label;
public Field(Config.Land land, Label label){
public Field(Config.Land land, Label label) {
this.land = land;
this.label = label;
}
public Config.Resource getResource(){
public Config.Resource getResource() {
return land.getResource();
}

View File

@ -2,7 +2,8 @@ package ch.zhaw.catan;
import java.util.HashMap;
import java.util.List;
//TODO Java Doc bearbeiten
//TODO Java Doc
/**
* New Class PLayer
* This class is here in order to maintain the resources of the players, the amount of structures that they can build,
@ -20,14 +21,14 @@ public class Player {
* @param faction this is the faction of the player.
*/
public Player(Config.Faction faction) {
//Datafields
//Data fields
this.faction = faction;
//Available Structures initialize
//Initialize available structures
structureToUse = new HashMap<>();
structureToUse.put(Config.Structure.ROAD, Config.Structure.ROAD.getStockPerPlayer());
structureToUse.put(Config.Structure.SETTLEMENT, Config.Structure.SETTLEMENT.getStockPerPlayer());
structureToUse.put(Config.Structure.CITY, Config.Structure.CITY.getStockPerPlayer());
//Resources initialize
//Initialize resources
resources = new HashMap<>();
for (Config.Resource resource : Config.Resource.values()) {
resources.put(resource, 0);
@ -73,18 +74,18 @@ public class Player {
}
/**
* This method substracts a specific resource from resourcess but check first if player has enough resources.
* This method subtracts a specific resource from the player's resources. but check first if player has the specific resources.
*
* @param resource to substract
* @param numberToSubstract how much to substract
* @return true if resource has been substracted false if player has not enough resources
* @param resource to subtract
* @param numberToSubtract how much to subtract
* @return true if resource has been subtracted false if player has not enough resources
*/
public boolean substractResource(Config.Resource resource, int numberToSubstract) {
int inPossesion = resources.get(resource);
if (inPossesion - numberToSubstract < 0) {
public boolean subtractResource(Config.Resource resource, int numberToSubtract) {
int inPossession = resources.get(resource);
if (inPossession - numberToSubtract < 0) {
return false;
}
resources.put(resource, inPossesion - numberToSubstract);
resources.put(resource, inPossession - numberToSubtract);
return true;
}
@ -93,6 +94,7 @@ public class Player {
* and resources to build one more. 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.
*
* @return true if the structure can be created false if the structure can't be created.
*/
public boolean build(Config.Structure structure) {

View File

@ -1,13 +1,14 @@
package ch.zhaw.catan;
import java.awt.Point;
/// TODO: 09/12/2021 Java Doc
import java.awt.*;
/// TODO: 09/12/2021 Java Doc
public class Road extends Structure {
private final Point start,end;
private final Point start, end;
public Road(Config.Faction faction,Point start,Point end) {
public Road(Config.Faction faction, Point start, Point end) {
super(faction);
this.start = start;
this.end = end;

View File

@ -1,12 +1,13 @@
package ch.zhaw.catan;
import java.awt.Point;
import java.awt.*;
//TODO Java Doc
public class Settlement extends Structure {
private Point position;
public Settlement(Config.Faction faction,Point position) {
public Settlement(Config.Faction faction, Point position) {
super(faction);
this.position = position;
}

View File

@ -9,6 +9,7 @@ import java.util.Random;
public class Siedler {
/**
* The main Method of the game. It creates a Parser Object and calls the Methods foundingPhase (to create a new Game) and gamePhase
*
* @param args There are no arguments needed.
*/
public static void main(String[] args) {
@ -19,24 +20,25 @@ public class Siedler {
/**
* The Method for the usual game Process. It contains the Main loop to get new commands from the players and calls the associated methods.
*
* @param parser The Parser Object which will interact with the player.
* @param game The Game object which is already created and has the founding phase completed.
* @param game The Game object which is already created and has the founding phase completed.
*/
private static void gamePhase(Parser parser, SiedlerGame game) {
boolean running = true;
boolean diceThrown = false;
while (running){
while (running) {
Config.Faction currentPlayerFaction = game.getCurrentPlayerFaction();
parser.displayGameboard(game.getBoard().getTextView()); //todo jedesmal ausgeben? oder nur wenn neuer Spieler oder separater Befehl?
parser.playerTurn(currentPlayerFaction);
if(!diceThrown) {
if (!diceThrown) {
throwDice(parser, game);
diceThrown = true;
}
parser.displayPlayerInfo(game.getCurrentPlayerResource(), game.getCurrentPlayerWinPoints());
switch (parser.getAction()) {
case NEXTPLAYER:
if(caseNextPlayer(parser, game)){
if (caseNextPlayer(parser, game)) {
running = false;
} else {
diceThrown = false;
@ -66,13 +68,14 @@ public class Siedler {
/**
* The Method which is called if the player chooses the command "next Player".
*
* @param parser The parser Object which will interact with the player.
* @param game The game Object which will be used to execute the Method.
* @param game The game Object which will be used to execute the Method.
* @return true: if there is a winner and game is finished. false: if there is no winner (yet).
*/
private static boolean caseNextPlayer(Parser parser, SiedlerGame game){
private static boolean caseNextPlayer(Parser parser, SiedlerGame game) {
Config.Faction winner = game.getWinner();
if(winner == null) {
if (winner == null) {
game.switchToNextPlayer();
return false;
} else {
@ -83,42 +86,45 @@ public class Siedler {
/**
* The Method which is called if the player chooses the command "Trade with bank".
*
* @param parser The parser Object which will interact with the player.
* @param game The game Object which will be used to execute the Method.
* @param game The game Object which will be used to execute the Method.
*/
private static void caseTradeWithBank(Parser parser, SiedlerGame game) {
Config.Resource offer = parser.trade(true);
Config.Resource want = parser.trade(false);
if(!game.tradeWithBankFourToOne(offer, want)){
if (!game.tradeWithBankFourToOne(offer, want)) {
parser.errorMessage();
}
}
/**
* The Method which is called if the player chooses a command to build any structure.
* @param parser The parser Object which will interact with the player.
* @param game The game Object which will be used to execute the Method.
*
* @param parser The parser Object which will interact with the player.
* @param game The game Object which will be used to execute the Method.
* @param structure The kind of Structure the player selected to build.
*/
private static void caseBuildStructure(Parser parser, SiedlerGame game, Config.Structure structure) {
parser.giveCoordinatesForStructures(structure);
boolean successfully = false;
if(structure == Config.Structure.SETTLEMENT){
if (structure == Config.Structure.SETTLEMENT) {
successfully = game.buildSettlement(parser.getPoint());
} else if(structure == Config.Structure.CITY) {
} else if (structure == Config.Structure.CITY) {
successfully = game.buildCity(parser.getPoint());
} else if(structure == Config.Structure.ROAD){
} else if (structure == Config.Structure.ROAD) {
successfully = game.buildRoad(parser.getPoint(), parser.getPoint());
}
if(!successfully){
if (!successfully) {
parser.errorMessage();
}
}
/**
* The method which calculates a random dice number and calls the method throwDice in the game Object.
*
* @param parser The parser Object which will interact with the player.
* @param game The game Object which will be used to execute the Method.
* @param game The game Object which will be used to execute the Method.
*/
private static void throwDice(Parser parser, SiedlerGame game) {
Random random = new Random();
@ -130,6 +136,7 @@ public class Siedler {
/**
* The Method which executes the whole founding phase. It Creates a new SiedlerGame Object and prompts the players to build their first structures.
*
* @param parser The parser Object which will interact with the player.
* @return The new SiedlerGame Object which is created.
*/
@ -138,15 +145,15 @@ public class Siedler {
SiedlerGame game = new SiedlerGame(gameInfo.get("NumberOfWinPoints"), gameInfo.get("NumberOfPlayers"));
//each Player builds their first Settlement and first Road in reverse order than in the rest of the game
for(int player = 1; player <= gameInfo.get("NumberOfPlayers"); player++){
for (int player = 1; player <= gameInfo.get("NumberOfPlayers"); player++) {
buildStructuresInFoundingPhase(parser, game, false);
if(player < gameInfo.get("NumberOfPlayers")){
if (player < gameInfo.get("NumberOfPlayers")) {
game.switchToPreviousPlayer();
}
}
//each Player builds their second Settlement and second Road and gets a Payout for these Structures in usual order.
for(int player = 1; player <= gameInfo.get("NumberOfPlayers"); player++){
for (int player = 1; player <= gameInfo.get("NumberOfPlayers"); player++) {
buildStructuresInFoundingPhase(parser, game, true);
game.switchToNextPlayer();
}
@ -155,37 +162,36 @@ public class Siedler {
/**
* The Method is called by foundingPhase. It prompts the Player to build a Settlement and a Road.
*
* @param parser The parser Object which will interact with the player.
* @param game The game Object which will be used to execute the Method.
* @param game The game Object which will be used to execute the Method.
* @param payout true (for second Settlement in founding Phase): the Player gets a payout for the settlement. false (for first Settlement in founding Phase): The Player gets no payout.
*/
private static void buildStructuresInFoundingPhase(Parser parser, SiedlerGame game, Boolean payout){
private static void buildStructuresInFoundingPhase(Parser parser, SiedlerGame game, Boolean payout) {
parser.displayGameboard(game.getBoard().getTextView());
parser.playerTurn(game.getCurrentPlayerFaction());
//build Settlement
parser.giveCoordinatesForStructures(Config.Structure.SETTLEMENT);
boolean successful = false;
do{
if(game.placeInitialSettlement(parser.getPoint(), payout)) {
do {
if (game.placeInitialSettlement(parser.getPoint(), payout)) {
successful = true;
}
else{
} else {
parser.errorMessage();
}
} while(!successful);
} while (!successful);
//build Road
parser.displayGameboard(game.getBoard().getTextView());
parser.giveCoordinatesForStructures(Config.Structure.ROAD);
successful = false;
do{
if(game.placeInitialRoad(parser.getPoint(), parser.getPoint())) {
do {
if (game.placeInitialRoad(parser.getPoint(), parser.getPoint())) {
successful = true;
}
else{
} else {
parser.errorMessage();
}
} while(!successful);
} while (!successful);
}
}

View File

@ -5,8 +5,15 @@ import ch.zhaw.hexboard.HexBoard;
import ch.zhaw.hexboard.Label;
import java.awt.Point;
import java.util.*;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Iterator;
import java.util.Collections;
//TODO Enhance JavaDoc
/**
* Subclass of HexBoard
* Saves the fields which are set and handles Methods with specific Dice Numbers.
@ -22,21 +29,20 @@ public class SiedlerBoard extends HexBoard<Land, Settlement, Road, String> {
HashMap<Point, Field> fields = new HashMap<>();
/**
* Method to create the predefined gamefield from Config.
* Method to create the predefined game field from Config.
*/
public void createFixGamefield(){
Map<Point,Land> resourcePlacement = Config.getStandardLandPlacement();
public void createFixGameField() {
Map<Point, Land> resourcePlacement = Config.getStandardLandPlacement();
Map<Point, Integer> dicePlacement = Config.getStandardDiceNumberPlacement();
for (Map.Entry<Point,Land> resourceField : resourcePlacement.entrySet()) {
addField(resourceField.getKey(),resourceField.getValue());
if(dicePlacement.get(resourceField.getKey()) != null){
for (Map.Entry<Point, Land> resourceField : resourcePlacement.entrySet()) {
addField(resourceField.getKey(), resourceField.getValue());
if (dicePlacement.get(resourceField.getKey()) != null) {
String numberAsString = dicePlacement.get(resourceField.getKey()).toString();
char[] numbersInChar = numberAsString.toCharArray();
if (numberAsString.length() < 2) {
fields.put(resourceField.getKey(), new Field(resourceField.getValue(), new Label('0', numbersInChar[0])));
}
else {
fields.put(resourceField.getKey(), new Field(resourceField.getValue(), new Label(numbersInChar[0],numbersInChar[1])));
} else {
fields.put(resourceField.getKey(), new Field(resourceField.getValue(), new Label(numbersInChar[0], numbersInChar[1])));
}
}
}
@ -44,6 +50,7 @@ public class SiedlerBoard extends HexBoard<Land, Settlement, Road, String> {
/**
* Method to get the DiceNumber of a specific field.
*
* @param field Point with coordinates of the specific field
* @return the DiceNumber of the field.
*/
@ -53,11 +60,12 @@ public class SiedlerBoard extends HexBoard<Land, Settlement, Road, String> {
}
/**
* Method to create a SiedlerBoardTextView Object set the LowerFieldLabels with theirs Dicenumber.
* Method to create a SiedlerBoardTextView Object set the LowerFieldLabels with theirs dice number.
* It is used to print the actual board in TextIO.
*
* @return String of actual board.
*/
public String getTextView () {
public String getTextView() {
SiedlerBoardTextView view = new SiedlerBoardTextView(this);
for (Map.Entry<Point, Field> field : fields.entrySet()) {
view.setLowerFieldLabel(field.getKey(), field.getValue().getLabel());
@ -73,9 +81,9 @@ public class SiedlerBoard extends HexBoard<Land, Settlement, Road, String> {
*/
public List<Point> getFieldsForDiceValue(int dice) {
ArrayList<Point> fields = new ArrayList<>();
for(Point field : this.fields.keySet()){
if(getDiceNumber(field) == dice){
fields.add(field);
for (Point field : this.fields.keySet()) {
if (getDiceNumber(field) == dice) {
fields.add(field);
}
}
return fields;
@ -84,12 +92,12 @@ public class SiedlerBoard extends HexBoard<Land, Settlement, Road, String> {
/**
* Method to get the Resources which are paid to a specific faction for a specific field.
*
* @param point The Point with the Coordinates of the field, which should pay resources.
* @param point The Point with the Coordinates of the field, which should pay resources.
* @param faction The faction, which should get paid.
* @return a ArrayList with all resources, which will be paid to the specified faction.
*/
public ArrayList<Config.Resource> getResourcesForFaction(Point point, Config.Faction faction){
List <Settlement> possibleSettlementField = super.getCornersOfField(point);
public ArrayList<Config.Resource> getResourcesForFaction(Point point, Config.Faction faction) {
List<Settlement> possibleSettlementField = super.getCornersOfField(point);
ArrayList<Config.Resource> resourcesToPlayer = new ArrayList<>();
for (Settlement settlement : possibleSettlementField) {
if (settlement.getFaction() == faction) {
@ -110,67 +118,67 @@ public class SiedlerBoard extends HexBoard<Land, Settlement, Road, String> {
*/
public List<Land> getLandsForCorner(Point corner) {
Point above = new Point(corner.x, corner.y + 2);
Point below = new Point(corner.x, corner.y -2);
Point below = new Point(corner.x, corner.y - 2);
Land[] lands = new Land[3];
if (hasField(above)) {
lands[0] = getField(above);
lands[1] = getField(new Point(corner.x + 1, corner.y - 1));
lands[2] = getField(new Point(corner.x - 1, corner.y - 1));
}
else if (hasField(below)) {
} else if (hasField(below)) {
lands[0] = getField(below);
lands[1] = getField(new Point(corner.x + 1, corner.y + 1));
lands[2] = getField(new Point(corner.x - 1, corner.y + 1));
}
else {
} else {
return Collections.emptyList();
}
return List.of(lands);
}
//TODO Java Doc no return
/**
* This method checks for the player with the longest road according to the catan game rules.
* @return a HashMap<Faction,Integer> where faction is the player with the longest road longer according to catan game rules
* 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 void getLongestRoadFaction(HashMap<Config.Faction,Integer> currentLongestRoad,List<Config.Faction> factionList) {
public void getLongestRoadFaction(HashMap<Config.Faction, Integer> currentLongestRoad, List<Config.Faction> factionList) {
List<Settlement> corners = getCorners();
HashMap<Config.Faction,Integer> players = new HashMap<>();
HashMap<Config.Faction, Integer> players = new HashMap<>();
for(Config.Faction faction : factionList) {
for (Config.Faction faction : factionList) {
int count = 0;
players.put(faction,count);
for(Settlement settlement : corners){
if(settlement.getFaction() == faction){
players.put(faction, count);
for (Settlement settlement : corners) {
if (settlement.getFaction() == faction) {
HashSet<Road> roads = new HashSet<>();
roads = countRoad(faction,settlement.getPosition(),roads,true);
roads = countRoad(faction, settlement.getPosition(), roads, true);
count = roads.size();
int currentCount = players.get(faction);
if(count > currentCount) {
players.put(faction,count);
if (count > currentCount) {
players.put(faction, count);
}
}
}
}
if(currentLongestRoad.size() == 0) {
if (currentLongestRoad.size() == 0) {
Config.Faction currentFaction = null;
int currentRoad = 4;
for(Config.Faction factionA : players.keySet()) {
if(players.get(factionA)>currentRoad) {
for (Config.Faction factionA : players.keySet()) {
if (players.get(factionA) > currentRoad) {
currentFaction = factionA;
currentRoad = players.get(factionA);
}
}
if(currentFaction != null){
currentLongestRoad.put(currentFaction,currentRoad);
if (currentFaction != null) {
currentLongestRoad.put(currentFaction, currentRoad);
}
}else{
for(Config.Faction faction : players.keySet()) {
for(Config.Faction faction1 : currentLongestRoad.keySet()) {
if(players.get(faction) >= 5 && players.get(faction) > currentLongestRoad.get(faction1)) {
} else {
for (Config.Faction faction : players.keySet()) {
for (Config.Faction faction1 : currentLongestRoad.keySet()) {
if (players.get(faction) >= 5 && players.get(faction) > currentLongestRoad.get(faction1)) {
currentLongestRoad.remove(faction1);
currentLongestRoad.put(faction,players.get(faction));
currentLongestRoad.put(faction, players.get(faction));
}
}
}
@ -180,111 +188,101 @@ public class SiedlerBoard extends HexBoard<Land, Settlement, Road, String> {
/**
* 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.
*
* @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 player's 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 being added to roads.
* @return HashSet with all roads from a specific player which are string together.
*/
private HashSet<Road> countRoad(Config.Faction faction,Point position,HashSet<Road> roads,boolean add) {
List<Road> roadslist = getAdjacentEdges(position);
if(getCorner(position) != null && getCorner(position).getFaction() != faction) {
private HashSet<Road> countRoad(Config.Faction faction, Point position, HashSet<Road> roads, boolean add) {
List<Road> roadsList = getAdjacentEdges(position);
if (getCorner(position) != null && getCorner(position).getFaction() != faction) {
return roads;
}
Iterator<Road> it2 = roads.iterator();
while(it2.hasNext()) {
Road roadsroad = it2.next();
Iterator<Road> it3 = roadslist.iterator();
while (it3.hasNext()){
Road roadslistRoad = it3.next();
if(roadslistRoad == roadsroad || roadslistRoad.getFaction() != faction) {
for (Road roadsRoad : roads) {
Iterator<Road> it3 = roadsList.iterator();
while (it3.hasNext()) {
Road roadsListRoad = 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) {
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);
if(add) {
for (Road road : listOne) {
listTwo.add(road);
}
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) {
listTwo.addAll(listOne);
roads = listTwo;
}else {
} else {
HashSet<Road> tallest;
if(listOne.size()>= listTwo.size()) {
if (listOne.size() >= listTwo.size()) {
tallest = listOne;
}else{
} else {
tallest = listTwo;
}
for (Road road : tallest) {
roads.add(road);
}
roads.addAll(tallest);
}
}
else if(roadslist.size() == 3) {
} 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);
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<Road> tallest;
HashSet<Road> secondtallest;
HashSet<Road> secondTallest;
if(listOne.size()>=listTwo.size()) {
if (listOne.size() >= listTwo.size()) {
tallest = listOne;
secondtallest = listTwo;
}else {
secondTallest = listTwo;
} else {
tallest = listTwo;
secondtallest = listOne;
}if(listThree.size() >= secondtallest.size()) {
secondtallest = listThree;
secondTallest = listOne;
}
for(Road road : secondtallest) {
tallest.add(road);
if (listThree.size() >= secondTallest.size()) {
secondTallest = listThree;
}
tallest.addAll(secondTallest);
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
* This method is being 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.
* @return return the opposite point of the current point.
*/
private Point getNextPoint(Road road,Point position) {
private Point getNextPoint(Road road, Point position) {
Point start = road.getStart();
Point end = road.getEnd();
if(position.equals(start)) {
if (position.equals(start)) {
position = end;
}else {
} else {
position = start;
}
return position;

View File

@ -9,8 +9,6 @@ import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Iterator;
import java.util.HashSet;
/**
@ -29,7 +27,7 @@ public class SiedlerGame {
private final int winPointsForWin;
private final Bank bank;
private int activePlayer;
private HashMap<Config.Faction,Integer> longestRoadFaction;
private final HashMap<Config.Faction, Integer> longestRoadFaction;
/**
* Constructs a SiedlerGame game state object.
@ -40,12 +38,12 @@ public class SiedlerGame {
* three 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) {
if (winPoints < 3 || numberOfPlayers < Config.MIN_NUMBER_OF_PLAYERS || numberOfPlayers > 4) {
throw new IllegalArgumentException();
}
bank = new Bank();
board = new SiedlerBoard();
board.createFixGamefield();
board.createFixGameField();
allPlayers = new ArrayList<>();
createPlayer(numberOfPlayers);
activePlayer = 0;
@ -63,10 +61,9 @@ public class SiedlerGame {
* Switches to the next player in the defined sequence of players.
*/
public void switchToNextPlayer() {
if (activePlayer < allPlayers.size() -1){
if (activePlayer < allPlayers.size() - 1) {
activePlayer++;
}
else if (activePlayer == allPlayers.size() -1){
} else if (activePlayer == allPlayers.size() - 1) {
activePlayer = 0;
}
}
@ -75,24 +72,25 @@ public class SiedlerGame {
* Switches to the previous player in the defined sequence of players.
*/
public void switchToPreviousPlayer() {
if (activePlayer > 0){
if (activePlayer > 0) {
activePlayer--;
}
else if (activePlayer == 0){
activePlayer = allPlayers.size()-1;
} else if (activePlayer == 0) {
activePlayer = allPlayers.size() - 1;
}
}
//TODO JavaDoc
private boolean addResourcesToPlayer(Player player, Resource resource, int numberToAdd){
if(bank.getResourceFromBank(resource, numberToAdd)){
private boolean addResourcesToPlayer(Player player, Resource resource, int numberToAdd) {
if (bank.getResourceFromBank(resource, numberToAdd)) {
player.addResource(resource, numberToAdd);
return true;
}
return false;
}
//TODO JavaDoc
private boolean subtractResourceFromPlayer(Player player, Resource resource, int numberToSubtract){
if(player.substractResource(resource, numberToSubtract)){
private boolean subtractResourceFromPlayer(Player player, Resource resource, int numberToSubtract) {
if (player.subtractResource(resource, numberToSubtract)) {
bank.storeResourceToBank(resource, numberToSubtract);
return true;
}
@ -114,14 +112,12 @@ public class SiedlerGame {
*/
public List<Faction> getPlayerFactions() {
List<Faction> factions = new ArrayList<>();
for (Player player: allPlayers ) {
for (Player player : allPlayers) {
factions.add(player.getFaction());
}
return factions;
}
/**
* Returns the game board.
*
@ -168,11 +164,11 @@ public class SiedlerGame {
* @return true, if the placement was successful
*/
public boolean placeInitialSettlement(Point position, boolean payout) {
if(!validPositionForSettlement(position)){
if (!validPositionForSettlement(position)) {
return false;
}
board.setCorner(position, new Settlement(allPlayers.get(activePlayer).getFaction(),position));
if(payout) {
board.setCorner(position, new Settlement(allPlayers.get(activePlayer).getFaction(), position));
if (payout) {
List<Config.Land> lands = board.getLandsForCorner(position);
for (Config.Land land : lands) {
if (land.getResource() != null) {
@ -192,10 +188,10 @@ public class SiedlerGame {
* @return true, if the placement was successful
*/
public boolean placeInitialRoad(Point roadStart, Point roadEnd) {
if (!validPositionForRoad(roadStart, roadEnd)){
if (!validPositionForRoad(roadStart, roadEnd)) {
return false;
}
board.setEdge(roadStart, roadEnd, new Road(allPlayers.get(activePlayer).getFaction(),roadStart,roadEnd));
board.setEdge(roadStart, roadEnd, new Road(allPlayers.get(activePlayer).getFaction(), roadStart, roadEnd));
return true;
}
@ -222,17 +218,17 @@ public class SiedlerGame {
*/
public Map<Faction, List<Resource>> throwDice(int diceThrow) {
if (diceThrow == 7) {
for(Player player : allPlayers) {
for (Player player : allPlayers) {
handleDiceThrow7(player);
}
} else {
Map<Faction,List<Resource>> returnMap= new HashMap<>();
Map<Faction, List<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());
for (Config.Resource resource : resources){
List<Resource> resources = board.getResourcesForFaction(field, player.getFaction());
for (Config.Resource resource : resources) {
returnMap.get(player.getFaction()).add(resource);
addResourcesToPlayer(player, resource, 1);
}
@ -242,26 +238,26 @@ public class SiedlerGame {
}
return null;
}
//TODO JavaDoc
public void handleDiceThrow7(Player player) {
ArrayList<Config.Resource> resourceArrayList = new ArrayList<>();
HashMap<Resource, Integer> resources = player.getResources();
for(Config.Resource resource : resources.keySet()){
for(int i = 0; i < resources.get(resource); i++) {
for (Config.Resource resource : resources.keySet()) {
for (int i = 0; i < resources.get(resource); i++) {
resourceArrayList.add(resource);
}
}
if(resourceArrayList.size() > Config.MAX_CARDS_IN_HAND_NO_DROP){
int resourcesToRemove =resourceArrayList.size() - (resourceArrayList.size() / 2);
if (resourceArrayList.size() > Config.MAX_CARDS_IN_HAND_NO_DROP) {
int resourcesToRemove = resourceArrayList.size() - (resourceArrayList.size() / 2);
Random random = new Random();
for(int i = 0; i < resourcesToRemove; i++){
for (int i = 0; i < resourcesToRemove; i++) {
subtractResourceFromPlayer(player, resourceArrayList.remove(random.nextInt(resourceArrayList.size())), 1);
}
}
}
/**
* Builds a settlement at the specified position on the board.
*
@ -277,11 +273,11 @@ public class SiedlerGame {
*/
public boolean buildSettlement(Point position) {
//1. Check if position is corner && is empty && neighbour Corners are empty
if(!validPositionForSettlement(position)) {
if (!validPositionForSettlement(position)) {
return false;
}
//2. Check if neighbourEdge are Roads belong to active Player
if(!checkAdjacentEdgesList(position)) {
if (!checkAdjacentEdgesList(position)) {
return false;
}
//3. Can Player build Settlement
@ -295,7 +291,7 @@ public class SiedlerGame {
}
//4. Insert Settlement to map
board.setCorner(position, new Settlement(allPlayers.get(activePlayer).getFaction(),position));
board.setCorner(position, new Settlement(allPlayers.get(activePlayer).getFaction(), position));
return true;
}
@ -314,16 +310,16 @@ public class SiedlerGame {
*/
public boolean buildCity(Point position) {
//1. Check if Corner.
if (!board.hasCorner(position)){
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()){
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)){
if (!allPlayers.get(activePlayer).build(Config.Structure.CITY)) {
return false;
}
@ -333,7 +329,7 @@ public class SiedlerGame {
}
//4.Insert City into the map.
board.setCorner(position,new City(allPlayers.get(activePlayer).getFaction(),position));
board.setCorner(position, new City(allPlayers.get(activePlayer).getFaction(), position));
return true;
}
@ -354,7 +350,7 @@ public class SiedlerGame {
*/
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)){
if (!validPositionForRoad(roadStart, roadEnd)) {
return false;
}
//2. Can Player build road
@ -368,18 +364,19 @@ public class SiedlerGame {
}
//3. Insert Road to map
board.setEdge(roadStart, roadEnd, new Road(allPlayers.get(activePlayer).getFaction(),roadStart,roadEnd));
board.setEdge(roadStart, roadEnd, new Road(allPlayers.get(activePlayer).getFaction(), roadStart, roadEnd));
return true;
}
/**
* This Method is used to check if the chosen position for a road is valid or not.
*
* @param roadStart the coordinates where the road begins.
* @param roadEnd the coordinates where the road ends.
* @param roadEnd the coordinates where the road ends.
* @return true if road position is valid otherwise false
*/
private boolean validPositionForRoad(Point roadStart, Point roadEnd){
private boolean validPositionForRoad(Point roadStart, Point roadEnd) {
//1. Check if it is an edge
if (!board.hasEdge(roadStart, roadEnd)) {
return false;
@ -390,7 +387,7 @@ public class SiedlerGame {
}
//3. Check if neighbouring edges are roads
boolean hasNeighbourRoad = (checkAdjacentEdgesList(roadStart) || checkAdjacentEdgesList(roadEnd));
if(hasNeighbourRoad) {
if (hasNeighbourRoad) {
return true;
}
//4.Check if roadStart or roadEnd is Settlement of current player
@ -400,20 +397,21 @@ public class SiedlerGame {
/**
* Can be used for both initial Settlement and normal Phase.
*
* @param position the position on the board to check for valid settlement position
* @return true if valid position for settlement
*/
private boolean validPositionForSettlement(Point position){
private boolean validPositionForSettlement(Point position) {
//1. Check if corner
if (!board.hasCorner(position)) {
return false;
}
//2. Check if water
if(checkIfWater(position)) {
if (checkIfWater(position)) {
return false;
}
//3. Check if corner is empty
if(board.getCorner(position) != null) {
if (board.getCorner(position) != null) {
return false;
}
//3. Check if neighbouring corners are empty
@ -422,8 +420,8 @@ public class SiedlerGame {
private boolean checkIfWater(Point point) {
List<Config.Land> fields = board.getFields(point);
for(Config.Land land : fields) {
if(!land.equals(Config.Land.WATER)) {
for (Config.Land land : fields) {
if (!land.equals(Config.Land.WATER)) {
return false;
}
}
@ -432,6 +430,7 @@ public class SiedlerGame {
/**
* This method checks if there are Roads build by active Player on adjacent edges
*
* @param point point to check on
* @return true if there is a road build next to the point.
*/
@ -447,6 +446,7 @@ public class SiedlerGame {
/**
* Checks if Adjacent Corners are empty
*
* @param point Corner to check
* @return true if all Neighbour Corners are emtpy
*/
@ -468,7 +468,7 @@ public class SiedlerGame {
*/
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)){
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);
return true;
}
@ -480,34 +480,35 @@ public class SiedlerGame {
*
* @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 Faction getWinner() {
if (getCurrentPlayerWinPoints() >= winPointsForWin) {
return getCurrentPlayerFaction();
}
//Todo Java Doc
public int getCurrentPlayerWinPoints(){
int winPoints = 0;
List<Settlement> settlements = board.getCorners();
for(Structure structure : settlements) {
return null;
}
int newWinPoints = 0;
if(structure instanceof City){
newWinPoints = 2;
} else if(structure instanceof Settlement) {
newWinPoints = 1;
}
if(structure.getFaction() == getCurrentPlayerFaction()){
winPoints += newWinPoints;
}
//Todo Java Doc
public int getCurrentPlayerWinPoints() {
int winPoints = 0;
List<Settlement> settlements = board.getCorners();
for (Structure structure : settlements) {
int newWinPoints = 0;
if (structure instanceof City) {
newWinPoints = 2;
} else if (structure instanceof Settlement) {
newWinPoints = 1;
}
board.getLongestRoadFaction(longestRoadFaction,getPlayerFactions());
if(longestRoadFaction.get(getCurrentPlayerFaction()) != null){
winPoints += 2;
if (structure.getFaction() == getCurrentPlayerFaction()) {
winPoints += newWinPoints;
}
return winPoints;
}
board.getLongestRoadFaction(longestRoadFaction, getPlayerFactions());
if (longestRoadFaction.get(getCurrentPlayerFaction()) != null) {
winPoints += 2;
}
return winPoints;
}
/**
* Places the thief on the specified field and steals a random resource card (if

View File

@ -4,9 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import ch.zhaw.catan.games.ThreePlayerStandard;
import org.beryx.textio.TextIO;
import org.beryx.textio.TextIoFactory;
import org.beryx.textio.TextTerminal;
import org.junit.jupiter.api.*;
import java.awt.*;
@ -45,7 +42,7 @@ public class SiedlerGameTest {
List<Config.Faction> factionList = Arrays.asList(Config.Faction.values());
SiedlerBoard board = new SiedlerBoard();
board.createFixGamefield();
board.createFixGameField();
board.setEdge(new Point(6, 6), new Point(5, 7), new Road(Config.Faction.BLUE,new Point(6, 6),new Point(5, 7)));
board.setEdge(new Point(4, 6), new Point(5, 7), new Road(Config.Faction.BLUE,new Point(4, 6),new Point(5, 7)));
board.setEdge(new Point(4, 6), new Point(4, 4), new Road(Config.Faction.BLUE,new Point(4, 6),new Point(4, 4)));