* 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;
- SiedlerBoard board;
+ private SiedlerBoard board;
+ private ArrayList 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.
- *
- * Important note: The list must contain the
- * factions of active players only. 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.
- * 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).
- * The settlement can be built if:
- * The city can be built if:
- * The road can be built if:
- * 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.
+ *
+ * Important note: The list must contain the
+ * factions of active players only. 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.
+ * 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 an empty list (not null)!.
+ *
+ *
+ * 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).
+ * The settlement can be built if:
+ * The city can be built if:
+ * The road can be built if:
+ *
+ * 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) {
+ 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() {
+ if(getCurrentPlayerWinpoints() >= winPointsForWin){
+ return getCurrentPlayerFaction();
+ }
+ return null;
+ }
+
+ public int getCurrentPlayerWinpoints(){
+ int winPoints = 0;
+ List
- *
- *
- * @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.
- *
- *
- *
- *
- * @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.
- *
- *
- *
- *
- * @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) {
- // TODO: Implement
- // 0.Is Edge 1. Check if Edge is empty 2. Check if One neighbors Corner is own settlement 3. Set road
- boolean validTask = true;
- while (validTask) {
- validTask = board.hasEdge(roadStart,roadEnd);
- validTask = board.getEdge(roadStart,roadEnd) == null;
+ /**
+ * 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.winPointsForWin = winPoints;
}
- return false;
- }
+ private void createPlayer(int numberOfPlayers) {
+ for (int i = 0; i < numberOfPlayers; i++) {
+ allPlayers.add(new Player(Config.Faction.values()[i]));
+ }
+ }
-
- /**
- * 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 false;
- }
+ /**
+ * Switches to the next player in the defined sequence of players.
+ */
+ public void switchToNextPlayer() {
+ if (activePlayer < allPlayers.size() -1){
+ activePlayer++;
+ }
+ else if (activePlayer == allPlayers.size() -1){
+ activePlayer = 0;
+ }
+ }
- /**
- * 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;
- }
+ /**
+ * Switches to the previous player in the defined sequence of players.
+ */
+ public void switchToPreviousPlayer() {
+ if (activePlayer > 0){
+ activePlayer--;
+ }
+ else if (activePlayer == 0){
+ activePlayer = allPlayers.size()-1;
+ }
+ }
+
+ /**
+ * Returns the {@link Faction}s of the active players.
+ *
+ *
+ *
+ *
+ * @param position the position of the settlement
+ * @return true, if the placement was successful
+ */
+ public boolean buildSettlement(Point position) {
+ //1. Check if position is corner && is empty && neighbour Corners are empty
+ if(!validPositionForSettlement(position)) {
+ return false;
+ }
+ //2. Check if neighbourEdge are Roads belong to active Player
+ if(!checkAdjacentEdgesList(position)) {
+ return false;
+ }
+ //3. Can Player build Settlement
+ if (!allPlayers.get(activePlayer).buildSettlement()) {
+ return false;
+ }
+ //4. Insert Settlement to map
+ board.setCorner(position, new Settlement(allPlayers.get(activePlayer).getFaction()));
+ //5. Give Resources to bank
+ bank.storeResourceToBank(Config.Structure.SETTLEMENT.getCosts());
+ return true;
+ }
+
+ /**
+ * Builds a city at the specified position on the board.
+ *
+ *
+ *
+ *
+ * @param position the position of the city
+ * @return true, if the placement was successful
+ */
+ public boolean buildCity(Point position) {
+ // TODO: OPTIONAL task - Implement
+ //1. Check if Corner.
+ if (!board.hasCorner(position)){
+ return false;
+ }
+ //2. Check if Settlement has already been built
+ Settlement atCurrentPosition = board.getCorner(position);
+ if (atCurrentPosition == null){
+ return false;
+ }
+ //3. Can player build a City.
+ if(!allPlayers.get(activePlayer).buildCity()){
+ return false;
+ }
+ //4.Insert City into the map.
+ board.setCorner(position,new City(allPlayers.get(activePlayer).getFaction()));
+ //5. Give resources to the bank.
+ bank.storeResourceToBank(Config.Structure.CITY.getCosts());
+ return false;
+ }
+
+ /**
+ * Builds a road at the specified position on the board.
+ *
+ *
+ *
+ *
+ * @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 is edge && is empty && if neighbour Edge or Corners belong to Settlement of active Player
+ if (!validPositionForRoad(roadStart,roadEnd)){
+ return false;
+ }
+ //2. Can Player build road
+ if (!allPlayers.get(activePlayer).buildRoad()) {
+ // TODO: Error message
+ return false;
+ }
+ //3. Insert Road to map
+ board.setEdge(roadStart, roadEnd, new Road(allPlayers.get(activePlayer).getFaction()));
+ //4. Give Resource to bank
+ bank.storeResourceToBank(Config.Structure.ROAD.getCosts());
+ return true;
+ }
+
+
+ /**
+ * Can be used for both initial Settlement and normal Phase.
+ * @param roadStart
+ * @param roadEnd
+ * @return
+ */
+ private boolean validPositionForRoad(Point roadStart, Point roadEnd){
+ //1. Check if Edge
+ if (!board.hasEdge(roadStart, roadEnd)) {
+ return false;
+ }
+ //2. Check if Edge is empty
+ if (board.getEdge(roadStart, roadEnd) != null) {
+ return false;
+ }
+ //3. Check if NeighbourEdge are Roads
+ boolean hasNeighbourRoad = (checkAdjacentEdgesList(roadStart) || checkAdjacentEdgesList(roadEnd));
+ //4.Check if roadStart or roadEnd is Settlement of current player or if 3. is valid
+ if(board.getCorner(roadStart).getFaction() == allPlayers.get(activePlayer).getFaction()
+ || board.getCorner(roadEnd).getFaction() == allPlayers.get(activePlayer).getFaction()
+ || hasNeighbourRoad){
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Can be used for both initial Settlement and normal Phase.
+ * @param position
+ * @return
+ */
+ private boolean validPositionForSettlement(Point position){
+ //1. Check if Corner
+ if (!board.hasCorner(position)) {
+ return false;
+ }
+ //2. Check if Corner is empty
+ if(board.getCorner(position) != null) {
+ return false;
+ }
+ //3. Check if neighbourCorners are empty
+ if(!checkAdjacentCornerList(position)) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * This method checks if there are Roads build by active Player on adjacent edges
+ * @param point
+ * @return
+ */
+ private boolean checkAdjacentEdgesList(Point point) {
+ List