SiedlerGame.java added few methodes
This commit is contained in:
parent
3ddd86e4e1
commit
971cfe8868
|
@ -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>
|
|
@ -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();
|
||||
|
|
|
@ -7,277 +7,308 @@ 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;
|
||||
|
||||
|
||||
/**
|
||||
* This class performs all actions related to modifying the game state.
|
||||
*
|
||||
* 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;
|
||||
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 int winPoints;
|
||||
private Bank bank;
|
||||
private Player 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) {
|
||||
bank = new Bank();
|
||||
board = new SiedlerBoard();
|
||||
board.createFixGamefield();
|
||||
allPlayers = new Player[numberOfPlayers];
|
||||
this.winPoints = winPoints;
|
||||
}
|
||||
private SiedlerBoard board;
|
||||
private ArrayList<Player> allPlayers;
|
||||
private int winPoints;
|
||||
private Bank bank;
|
||||
private int activePlayer;
|
||||
|
||||
/**
|
||||
* Switches to the next player in the defined sequence of players.
|
||||
*/
|
||||
public void switchToNextPlayer() {
|
||||
// TODO: Implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Switches to the previous player in the defined sequence of players.
|
||||
*/
|
||||
public void switchToPreviousPlayer() {
|
||||
// TODO: Implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link 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.
|
||||
* Hence, the player that sets the first settlement must be
|
||||
* at position 0 in the list etc.
|
||||
*
|
||||
* <strong>Important note:</strong> The list must contain the
|
||||
* factions of active players only.</p>
|
||||
*
|
||||
* @return the list with player's factions
|
||||
*/
|
||||
public List<Faction> getPlayerFactions() {
|
||||
// TODO: Implement
|
||||
Faction[] factions = new Faction[allPlayers.length];
|
||||
|
||||
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the game board.
|
||||
*
|
||||
* @return the game board
|
||||
*/
|
||||
public SiedlerBoard getBoard() {
|
||||
// TODO: Implement
|
||||
return board;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link Faction} of the current player.
|
||||
*
|
||||
* @return the faction of the current player
|
||||
*/
|
||||
public Faction getCurrentPlayerFaction() {
|
||||
// TODO: Implement
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns how many resource cards of the specified type
|
||||
* the current player owns.
|
||||
*
|
||||
* @param resource the resource type
|
||||
* @return the number of resource cards of this type
|
||||
*/
|
||||
public int getCurrentPlayerResourceStock(Resource resource) {
|
||||
// TODO: Implement
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Places a settlement in the founder's phase (phase II) of the game.
|
||||
*
|
||||
* <p>The placement does not cost any resource cards. If payout is
|
||||
* set to true, for each adjacent resource-producing field, a resource card of the
|
||||
* type of the resource produced by the field is taken from the bank (if available) and added to
|
||||
* the players' stock of resource cards.</p>
|
||||
*
|
||||
* @param position the position of the settlement
|
||||
* @param payout if true, the player gets one resource card per adjacent resource-producing field
|
||||
* @return true, if the placement was successful
|
||||
*/
|
||||
public boolean placeInitialSettlement(Point position, boolean payout) {
|
||||
// TODO: Implement
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Places a road in the founder's phase (phase II) of the game.
|
||||
* The placement does not cost any resource cards.
|
||||
*
|
||||
* @param roadStart position of the start of the road
|
||||
* @param roadEnd position of the end of the road
|
||||
* @return true, if the placement was successful
|
||||
*/
|
||||
public boolean placeInitialRoad(Point roadStart, Point roadEnd) {
|
||||
// TODO: Implement
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method takes care of actions depending on the dice throw result.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* If a player does not get resource cards, the list for this players'
|
||||
* {@link Faction} is <b>an empty list (not null)</b>!.
|
||||
*
|
||||
* <p>
|
||||
* The payout rules of the game take into account factors such as, the number
|
||||
* of resource cards currently available in the bank, settlement types
|
||||
* (settlement or city), and the number of players that should get resource
|
||||
* cards of a certain type (relevant if there are not enough left in the bank).
|
||||
* </p>
|
||||
*
|
||||
* @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) {
|
||||
// TODO: Implement
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a settlement at the specified position on the board.
|
||||
*
|
||||
* <p>The settlement can be built if:
|
||||
* <ul>
|
||||
* <li> the player possesses the required resource cards</li>
|
||||
* <li> a settlement to place on the board</li>
|
||||
* <li> the specified position meets the build rules for settlements</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param position the position of the settlement
|
||||
* @return true, if the placement was successful
|
||||
*/
|
||||
public boolean buildSettlement(Point position) {
|
||||
// TODO: Implement
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a city at the specified position on the board.
|
||||
*
|
||||
* <p>The city can be built if:
|
||||
* <ul>
|
||||
* <li> the player possesses the required resource cards</li>
|
||||
* <li> a city to place on the board</li>
|
||||
* <li> the specified position meets the build rules for cities</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param position the position of the city
|
||||
* @return true, if the placement was successful
|
||||
*/
|
||||
public boolean buildCity(Point position) {
|
||||
// TODO: OPTIONAL task - Implement
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a road at the specified position on the board.
|
||||
*
|
||||
* <p>The road can be built if:
|
||||
* <ul>
|
||||
* <li> the player possesses the required resource cards</li>
|
||||
* <li> a road to place on the board</li>
|
||||
* <li> the specified position meets the build rules for roads</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param roadStart the position of the start of the road
|
||||
* @param roadEnd the position of the end of the road
|
||||
* @return true, if the placement was successful
|
||||
*/
|
||||
public boolean buildRoad(Point roadStart, Point roadEnd) {
|
||||
//1. Check if Edge
|
||||
if(!board.hasEdge(roadStart,roadEnd)){
|
||||
// TODO: Error message
|
||||
/**
|
||||
* 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 ArrayList<>();
|
||||
createPlayer(numberOfPlayers);
|
||||
activePlayer = 0;
|
||||
this.winPoints = winPoints;
|
||||
}
|
||||
//2. Check if Edge is empty
|
||||
if(board.getEdge(roadStart,roadEnd) != null) {
|
||||
// TODO: Error message
|
||||
|
||||
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.
|
||||
*/
|
||||
public void switchToNextPlayer() {
|
||||
// TODO: Implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Switches to the previous player in the defined sequence of players.
|
||||
*/
|
||||
public void switchToPreviousPlayer() {
|
||||
// TODO: Implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link 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.
|
||||
* Hence, the player that sets the first settlement must be
|
||||
* at position 0 in the list etc.
|
||||
*
|
||||
* <strong>Important note:</strong> The list must contain the
|
||||
* factions of active players only.</p>
|
||||
*
|
||||
* @return the list with player's factions
|
||||
*/
|
||||
public List<Faction> getPlayerFactions() {
|
||||
// TODO: Implement
|
||||
List<Faction> factions = new ArrayList<>();
|
||||
for (Player player: allPlayers ) {
|
||||
factions.add(player.getFaction());
|
||||
}
|
||||
|
||||
return factions;
|
||||
}
|
||||
//3. Can Player build road
|
||||
// TODO
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Returns the game board.
|
||||
*
|
||||
* @return the game board
|
||||
*/
|
||||
public SiedlerBoard getBoard() {
|
||||
return board;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* The trade only works when bank and player possess the resource cards
|
||||
* for the trade before the trade is executed.
|
||||
*
|
||||
* @param offer offered type
|
||||
* @param want wanted type
|
||||
* @return true, if the trade was successful
|
||||
*/
|
||||
public boolean tradeWithBankFourToOne(Resource offer, Resource want) {
|
||||
// TODO: Implement
|
||||
return bank.tradeWithBank(want,offer, FOUR_TO_ONE_TRADE_WANT, FOUR_TO_ONE_TRADE_OFFER);
|
||||
}
|
||||
/**
|
||||
* Returns the {@link Faction} of the current player.
|
||||
*
|
||||
* @return the faction of the current player
|
||||
*/
|
||||
public Faction getCurrentPlayerFaction() {
|
||||
return allPlayers.get(activePlayer).getFaction();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the winner of the game, if any.
|
||||
*
|
||||
* @return the winner of the game or null, if there is no winner (yet)
|
||||
*/
|
||||
public Faction getWinner() {
|
||||
// TODO: Implement
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Places the thief on the specified field and steals a random resource card (if
|
||||
* the player has such cards) from a random player with a settlement at that
|
||||
* field (if there is a settlement) and adds it to the resource cards of the
|
||||
* current player.
|
||||
*
|
||||
* @param field the field on which to place the thief
|
||||
* @return false, if the specified field is not a field or the thief cannot be
|
||||
* placed there (e.g., on water)
|
||||
*/
|
||||
public boolean placeThiefAndStealCard(Point field) {
|
||||
//TODO: Implement (or longest road functionality)
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Returns how many resource cards of the specified type
|
||||
* the current player owns.
|
||||
*
|
||||
* @param resource the resource type
|
||||
* @return the number of resource cards of this type
|
||||
*/
|
||||
public int getCurrentPlayerResourceStock(Resource resource) {
|
||||
int ressourceInStock = allPlayers.get(activePlayer).getSpecificResource(resource);
|
||||
return ressourceInStock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Places a settlement in the founder's phase (phase II) of the game.
|
||||
*
|
||||
* <p>The placement does not cost any resource cards. If payout is
|
||||
* set to true, for each adjacent resource-producing field, a resource card of the
|
||||
* type of the resource produced by the field is taken from the bank (if available) and added to
|
||||
* the players' stock of resource cards.</p>
|
||||
*
|
||||
* @param position the position of the settlement
|
||||
* @param payout if true, the player gets one resource card per adjacent resource-producing field
|
||||
* @return true, if the placement was successful
|
||||
*/
|
||||
public boolean placeInitialSettlement(Point position, boolean payout) {
|
||||
// TODO: Implement
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Places a road in the founder's phase (phase II) of the game.
|
||||
* The placement does not cost any resource cards.
|
||||
*
|
||||
* @param roadStart position of the start of the road
|
||||
* @param roadEnd position of the end of the road
|
||||
* @return true, if the placement was successful
|
||||
*/
|
||||
public boolean placeInitialRoad(Point roadStart, Point roadEnd) {
|
||||
// TODO: Implement
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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>!.
|
||||
*
|
||||
* <p>
|
||||
* The payout rules of the game take into account factors such as, the number
|
||||
* of resource cards currently available in the bank, settlement types
|
||||
* (settlement or city), and the number of players that should get resource
|
||||
* cards of a certain type (relevant if there are not enough left in the bank).
|
||||
* </p>
|
||||
*
|
||||
* @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) {
|
||||
// TODO: Implement
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a settlement at the specified position on the board.
|
||||
*
|
||||
* <p>The settlement can be built if:
|
||||
* <ul>
|
||||
* <li> the player possesses the required resource cards</li>
|
||||
* <li> a settlement to place on the board</li>
|
||||
* <li> the specified position meets the build rules for settlements</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param position the position of the settlement
|
||||
* @return true, if the placement was successful
|
||||
*/
|
||||
public boolean buildSettlement(Point position) {
|
||||
//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.
|
||||
*
|
||||
* <p>The city can be built if:
|
||||
* <ul>
|
||||
* <li> the player possesses the required resource cards</li>
|
||||
* <li> a city to place on the board</li>
|
||||
* <li> the specified position meets the build rules for cities</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param position the position of the city
|
||||
* @return true, if the placement was successful
|
||||
*/
|
||||
public boolean buildCity(Point position) {
|
||||
// TODO: OPTIONAL task - Implement
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a road at the specified position on the board.
|
||||
*
|
||||
* <p>The road can be built if:
|
||||
* <ul>
|
||||
* <li> the player possesses the required resource cards</li>
|
||||
* <li> a road to place on the board</li>
|
||||
* <li> the specified position meets the build rules for roads</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param roadStart the position of the start of the road
|
||||
* @param roadEnd the position of the end of the road
|
||||
* @return true, if the placement was successful
|
||||
*/
|
||||
public boolean buildRoad(Point roadStart, Point roadEnd) {
|
||||
//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
|
||||
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.
|
||||
*
|
||||
* @param offer offered type
|
||||
* @param want wanted type
|
||||
* @return true, if the trade was successful
|
||||
*/
|
||||
public boolean tradeWithBankFourToOne(Resource offer, Resource want) {
|
||||
// TODO: Implement
|
||||
return bank.tradeWithBank(want, offer, FOUR_TO_ONE_TRADE_WANT, FOUR_TO_ONE_TRADE_OFFER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the winner of the game, if any.
|
||||
*
|
||||
* @return the winner of the game or null, if there is no winner (yet)
|
||||
*/
|
||||
public Faction getWinner() {
|
||||
// TODO: Implement
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Places the thief on the specified field and steals a random resource card (if
|
||||
* the player has such cards) from a random player with a settlement at that
|
||||
* field (if there is a settlement) and adds it to the resource cards of the
|
||||
* current player.
|
||||
*
|
||||
* @param field the field on which to place the thief
|
||||
* @return false, if the specified field is not a field or the thief cannot be
|
||||
* placed there (e.g., on water)
|
||||
*/
|
||||
public boolean placeThiefAndStealCard(Point field) {
|
||||
//TODO: Implement (or longest road functionality)
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,4 +6,8 @@ public abstract class Structure {
|
|||
public Structure(Config.Faction faction) {
|
||||
this.faction = faction;
|
||||
}
|
||||
|
||||
public Config.Faction getFaction() {
|
||||
return faction;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue