Merge remote-tracking branch 'origin/main'

This commit is contained in:
Andrin Fassbind 2021-12-07 15:33:48 +01:00
commit 99a8feca03
2 changed files with 93 additions and 27 deletions

View File

@ -1,4 +1,5 @@
package ch.zhaw.catan; package ch.zhaw.catan;
import org.beryx.textio.TextIO; import org.beryx.textio.TextIO;
import org.beryx.textio.TextIoFactory; import org.beryx.textio.TextIoFactory;
import org.beryx.textio.TextTerminal; import org.beryx.textio.TextTerminal;
@ -8,81 +9,146 @@ import java.util.HashMap;
import static ch.zhaw.catan.Command.*; import static ch.zhaw.catan.Command.*;
/**
* This class performs all communication with the player, includes asking for input and showing the map.
*
* @author Leonardo Brandenberger
*/
public class Parser { public class Parser {
TextIO textIO = TextIoFactory.getTextIO(); TextIO textIO;
TextTerminal<?> textTerminal; TextTerminal<?> textTerminal;
/**
* Constructs a parser Object, initializes the textIO components TextTerminal and textIO.
*/
public Parser() { public Parser() {
textIO = TextIoFactory.getTextIO();
textTerminal = textIO.getTextTerminal(); textTerminal = textIO.getTextTerminal();
} }
/**
* Ask user to input two coordinates with have to be higher than 0 and returns them as a Point.
*
* @return Point that the user specified with an input
*/
public Point getPoint() { public Point getPoint() {
return new Point(textIO.newIntInputReader().withMinVal(0).read("x coordinate:"), return new Point(textIO.newIntInputReader().withMinVal(0).read("x coordinate:"),
textIO.newIntInputReader().withMinVal(0).read("y coordinate:")); textIO.newIntInputReader().withMinVal(0).read("y coordinate:"));
} }
/**
* Takes care of displaying the gameboard that it receives as parameter.
*
* @param gameboard that has to be displayed
*/
public void displayGameboard(String gameboard) { public void displayGameboard(String gameboard) {
textTerminal.println(gameboard); textTerminal.println(gameboard);
} }
public void displayPlayerInfo(HashMap<Config.Resource, Integer> currentPlayerResource, int winpoints){ /**
* Displays Player info that includes his current resources and his winpoint he currently owns
*
* @param currentPlayerResource Hashmap of all Resources with corresponding number that a player currently owns.
* @param winpoints that the player is currently holding
*/
public void displayPlayerInfo(HashMap<Config.Resource, Integer> currentPlayerResource, int winpoints) {
textTerminal.println("You are currently holding " + winpoints + " winpoints."); textTerminal.println("You are currently holding " + winpoints + " winpoints.");
textTerminal.println("You own the follwing resources:"); textTerminal.println("You own the follwing resources:");
for(Config.Resource resource : currentPlayerResource.keySet()){ for (Config.Resource resource : currentPlayerResource.keySet()) {
textTerminal.println(resource.name() + ":" + currentPlayerResource.get(resource)); textTerminal.println(resource.name() + ":" + currentPlayerResource.get(resource));
} }
} }
public void displayWinnertext(Config.Faction winner){ /**
* Displays the winner of the game in the Console.
*
* @param winner the faction that has won
*/
public void displayWinnertext(Config.Faction winner) {
textTerminal.println(winner.name() + " won the game!"); textTerminal.println(winner.name() + " won the game!");
} }
public HashMap<String, Integer> gameStart(){ /**
* Method that is used at the start of the game to get how many players will be playing and how many winpoints
* should be set.
*
* @return HashMap that includes the number of players and how many winpoints the game should have.
*/
public HashMap<String, Integer> gameStart() {
HashMap<String, Integer> gameStartValues = new HashMap<>(); HashMap<String, Integer> gameStartValues = new HashMap<>();
gameStartValues.put("NumberOfPlayers", textIO.newIntInputReader().withMinVal(2).withMaxVal(4).read("Number of players:")); gameStartValues.put("NumberOfPlayers", textIO.newIntInputReader().withMinVal(2).withMaxVal(4).read("Number of players:"));
gameStartValues.put("NumberOfWinPoints", textIO.newIntInputReader().withMinVal(5).withMaxVal(15).read("Winpoints needed for Victory:")); gameStartValues.put("NumberOfWinPoints", textIO.newIntInputReader().withMinVal(5).withMaxVal(15).read("Winpoints needed for Victory:"));
return gameStartValues; return gameStartValues;
} }
/**
* Asks the player for coordinates to place a structure if the structure is a road the player will be notified
* that he will need to input two coordinates a start and a finish one.
*
* @param structure the structure that should be asked for coordinates
*/
public void giveCoordinatesForStructures(Config.Structure structure) { public void giveCoordinatesForStructures(Config.Structure structure) {
textTerminal.println("Please insert coordinates for " + structure); textTerminal.println("Please insert coordinates for " + structure);
if(structure == Config.Structure.ROAD) { if (structure == Config.Structure.ROAD) {
textTerminal.println("Please first insert the start coordinate and when prompted again the coordinate of the end of the road."); textTerminal.println("Please first insert the start coordinate and when prompted again the coordinate of the end of the road.");
} }
} }
public void thrownDices(int number){ /**
textTerminal.println ("Dices have been thrown, the combined value is: " + number); * Displays the dice number that has been thrown.
*
* @param number the thrown dice number
*/
public void thrownDices(int number) {
textTerminal.println("Dices have been thrown, the combined value is: " + number);
} }
/**
* Outputs which player currently is at turn
*
* @param faction the faction which turn it is
*/
public void playerTurn(Config.Faction faction) { public void playerTurn(Config.Faction faction) {
textTerminal.println("It is " + faction.name() + "'s turn."); textTerminal.println("It is " + faction.name() + "'s turn.");
} }
public void errorMessage(){ /**
textTerminal.println("The command was not excecuted successfully!"); * Outputs error message.
*/
public void errorMessage() {
textTerminal.println("The command was not excecuted successfully!");
} }
/**
* Ask the player what action he would like to perform.
* As possible possibilities the Enum Command is used.
*
* @return Command which action has been choosen
*/
public Command getAction() { public Command getAction() {
return textIO.newEnumInputReader(Command.class).read("What would you like to do?"); return textIO.newEnumInputReader(Command.class).read("What would you like to do?");
} }
/** /**
* Asks the player which resources he would like to trade.
* As possibilites the Enum in Config.Resource.class is used.
* *
* @param give if true ask for resource to give if false for resource to receive * @param give if true ask for resource to give if false for resource to receive
* @return * @return Config.Resource the resource the player would like to give or receive
*/ */
public Config.Resource trade(boolean give) { public Config.Resource trade(boolean give) {
String output = "give"; String output = "give";
if (!give){ if (!give) {
output = "receive"; output = "receive";
} }
return textIO.newEnumInputReader(Config.Resource.class).read("Which Resource would you like to " + output ); return textIO.newEnumInputReader(Config.Resource.class).read("Which Resource would you like to " + output);
} }
public void quit(){
/**
* Clears the instances of textIO and textTerminal.
*/
public void quit() {
textTerminal.dispose(); textTerminal.dispose();
textIO.dispose(); textIO.dispose();
} }
} }

View File

@ -19,10 +19,10 @@ public class SiedlerGame {
static final int FOUR_TO_ONE_TRADE_OFFER = 4; static final int FOUR_TO_ONE_TRADE_OFFER = 4;
static final int FOUR_TO_ONE_TRADE_WANT = 1; static final int FOUR_TO_ONE_TRADE_WANT = 1;
private SiedlerBoard board; private final SiedlerBoard board;
private ArrayList<Player> allPlayers; private final ArrayList<Player> allPlayers;
private int winPointsForWin; private final int winPointsForWin;
private Bank bank; private final Bank bank;
private int activePlayer; private int activePlayer;
private HashMap<Config.Faction,Integer> longestRoadFaction; private HashMap<Config.Faction,Integer> longestRoadFaction;
@ -224,7 +224,7 @@ public class SiedlerGame {
Map<Faction,List<Resource>> returnMap= new HashMap<>(); Map<Faction,List<Resource>> returnMap= new HashMap<>();
List<Point> diceValueFields = board.getFieldsForDiceValue(dicethrow); List<Point> diceValueFields = board.getFieldsForDiceValue(dicethrow);
for (Player player : allPlayers) { for (Player player : allPlayers) {
returnMap.put(player.getFaction(), new ArrayList()); returnMap.put(player.getFaction(), new ArrayList<>());
for (Point field : diceValueFields) { for (Point field : diceValueFields) {
List<Resource> resources = board.getResourcesforFaction(field,player.getFaction()); List<Resource> resources = board.getResourcesforFaction(field,player.getFaction());
for (Config.Resource resource : resources){ for (Config.Resource resource : resources){
@ -369,9 +369,9 @@ public class SiedlerGame {
/** /**
* Can be used for both initial Settlement and normal Phase. * This Method is used to check if the chosen position for a road is valid or not.
* @param roadStart * @param roadStart the coordinates where the road begins.
* @param roadEnd * @param roadEnd the coordinates where the roads ends.
* @return * @return
*/ */
private boolean validPositionForRoad(Point roadStart, Point roadEnd){ private boolean validPositionForRoad(Point roadStart, Point roadEnd){