SiedlerGame.java added few methodes

This commit is contained in:
Andrin Fassbind 2021-11-29 16:47:02 +01:00
parent 3ddd86e4e1
commit 971cfe8868
4 changed files with 290 additions and 257 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="openjdk-17" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -9,15 +9,13 @@ import java.util.List;
*/
public class Player {
private String name;
private Config.Faction faction;
private HashMap<Config.Resource,Integer> resources;
private int roadsToUse;
private int settlementsToUse;
public Player (String name, Config.Faction faction){
public Player (Config.Faction faction){
//Datenfelder
this.name = name;
this.faction = faction;
roadsToUse = Config.Structure.ROAD.getStockPerPlayer();
settlementsToUse = Config.Structure.SETTLEMENT.getStockPerPlayer();

View File

@ -7,6 +7,7 @@ import org.beryx.textio.TextIoFactory;
import org.beryx.textio.TextTerminal;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -14,39 +15,48 @@ import java.util.Map;
/**
* This class performs all actions related to modifying the game state.
*
* <p>
* 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 Player[] allPlayers;
private ArrayList<Player> allPlayers;
private int winPoints;
private Bank bank;
private Player activePlayer;
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 Player[numberOfPlayers];
allPlayers = new ArrayList<>();
createPlayer(numberOfPlayers);
activePlayer = 0;
this.winPoints = 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.
*/
@ -76,10 +86,12 @@ public class SiedlerGame {
*/
public List<Faction> getPlayerFactions() {
// TODO: Implement
Faction[] factions = new Faction[allPlayers.length];
List<Faction> factions = new ArrayList<>();
for (Player player: allPlayers ) {
factions.add(player.getFaction());
}
return Collections.emptyList();
return factions;
}
@ -89,7 +101,6 @@ public class SiedlerGame {
* @return the game board
*/
public SiedlerBoard getBoard() {
// TODO: Implement
return board;
}
@ -99,8 +110,7 @@ public class SiedlerGame {
* @return the faction of the current player
*/
public Faction getCurrentPlayerFaction() {
// TODO: Implement
return null;
return allPlayers.get(activePlayer).getFaction();
}
/**
@ -111,8 +121,8 @@ public class SiedlerGame {
* @return the number of resource cards of this type
*/
public int getCurrentPlayerResourceStock(Resource resource) {
// TODO: Implement
return 0;
int ressourceInStock = allPlayers.get(activePlayer).getSpecificResource(resource);
return ressourceInStock;
}
/**
@ -147,12 +157,12 @@ public class SiedlerGame {
/**
* This method takes care of actions depending on the dice throw result.
*
* <p>
* 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.
*
* <p>
* If a player does not get resource cards, the list for this players'
* {@link Faction} is <b>an empty list (not null)</b>!.
*
@ -185,9 +195,25 @@ public class SiedlerGame {
* @return true, if the placement was successful
*/
public boolean buildSettlement(Point position) {
// TODO: Implement
//1. Check if Edge
if (!board.hasCorner(position)) {
// TODO: Error message
return false;
}
//2. Check if Edge is empty
if (board.getCorner(position) != null) {
// TODO: Error message
return false;
}
//3. Can Player build road
if (!allPlayers.get(activePlayer).buildSettlement()) {
// TODO: Error message
return false;
}
//4. Insert Road to map
board.setCorner(position, new Settlement(allPlayers.get(activePlayer).getFaction()));
return true;
}
/**
* Builds a city at the specified position on the board.
@ -225,23 +251,28 @@ public class SiedlerGame {
//1. Check if Edge
if (!board.hasEdge(roadStart, roadEnd)) {
// TODO: Error message
return false;
}
//2. Check if Edge is empty
if (board.getEdge(roadStart, roadEnd) != null) {
// TODO: Error message
return false;
}
//3. Can Player build road
// TODO
if (!allPlayers.get(activePlayer).buildRoad()) {
// TODO: Error message
return false;
}
//4. Insert Road to map
board.setEdge(roadStart, roadEnd, new Road(allPlayers.get(activePlayer).getFaction()));
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.
*
* <p>
* The trade only works when bank and player possess the resource cards
* for the trade before the trade is executed.
*

View File

@ -6,4 +6,8 @@ public abstract class Structure {
public Structure(Config.Faction faction) {
this.faction = faction;
}
public Config.Faction getFaction() {
return faction;
}
}