diff --git a/.idea/misc.xml b/.idea/misc.xml index c3dfb30..b573818 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/ch/zhaw/catan/SiedlerBoard.java b/src/ch/zhaw/catan/SiedlerBoard.java index e27f855..5a36dc7 100644 --- a/src/ch/zhaw/catan/SiedlerBoard.java +++ b/src/ch/zhaw/catan/SiedlerBoard.java @@ -8,7 +8,7 @@ import java.awt.*; import java.util.List; import java.util.*; -public class SiedlerBoard extends HexBoard { +public class SiedlerBoard extends HexBoard { Map lowerFieldLabel = new HashMap<>(); diff --git a/src/ch/zhaw/catan/SiedlerBoardTextView.java b/src/ch/zhaw/catan/SiedlerBoardTextView.java index 97f6401..ce2ca66 100644 --- a/src/ch/zhaw/catan/SiedlerBoardTextView.java +++ b/src/ch/zhaw/catan/SiedlerBoardTextView.java @@ -5,8 +5,9 @@ import ch.zhaw.hexboard.HexBoardTextView; import ch.zhaw.hexboard.Label; import java.awt.*; +import java.util.Set; -public class SiedlerBoardTextView extends HexBoardTextView { +public class SiedlerBoardTextView extends HexBoardTextView { public SiedlerBoardTextView(SiedlerBoard board) { super(board); diff --git a/src/ch/zhaw/catan/SiedlerGame.java b/src/ch/zhaw/catan/SiedlerGame.java index 9559984..df260e2 100644 --- a/src/ch/zhaw/catan/SiedlerGame.java +++ b/src/ch/zhaw/catan/SiedlerGame.java @@ -204,20 +204,19 @@ public class SiedlerGame { * @return true, if the placement was successful */ public boolean buildSettlement(Point position) { - //TODO Errors should be caught after this method is executed and returns false - //1. Check if Corner - if (!board.hasCorner(position)) { + //1. Check if position is corner && is empty && neighbour Corners are empty + if(!validPositionForSettlement(position)) { return false; } - //2. Check if Corner is empty - if (board.getCorner(position) != null) { + //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 Road to map + //4. Insert Settlement to map board.setCorner(position, new Settlement(allPlayers.get(activePlayer).getFaction())); //5. Give Resoure to bank bank.storeResourceToBank(Config.Structure.SETTLEMENT.getCosts()); @@ -257,39 +256,95 @@ public class SiedlerGame { * @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 + //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. Check if Edge is empty - if (board.getEdge(roadStart, roadEnd) != null) { - // TODO: Error message - return false; - } - //3. Can Player build road + //2. Can Player build road if (!allPlayers.get(activePlayer).buildRoad()) { // TODO: Error message return false; } - //4. Insert Road to map + //3. Insert Road to map board.setEdge(roadStart, roadEnd, new Road(allPlayers.get(activePlayer).getFaction())); - //5. Give Resource to bank + //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; + } - - - private boolean validPositionForRoad(Point position){ - //todo implement + /** + * Do not use for initial Settlement + * @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; } - private boolean validPositionForSettlement(Point position){ - //todo implement + /** + * This method checks if there are Roads build by active Player on adjacent edges + * @param point + * @return + */ + private boolean checkAdjacentEdgesList(Point point) { + List results = board.getAdjacentEdges(point); + for(int i = 0; i < results.size(); i++) { + if(results.get(i).getFaction() == allPlayers.get(activePlayer).getFaction()) { + return true; + } + } + return false; + } + + /** + * Checks if Adjacent Corners are empty + * @param point Corner to check + * @return true if all Neighbour Corners are emtpy + */ + private boolean checkAdjacentCornerList(Point point) { + List results = board.getNeighboursOfCorner(point); + if(results.size() > 0) { + return false; + } return true; } @@ -325,7 +380,7 @@ public class SiedlerGame { private HashMap getWinPoints(){ HashMap winPoints = new HashMap<>(); - List structures = board.getCorners(); + List structures = board.getCorners(); for(Structure structure : structures) { int newWinPoints = 0;