created method in SiedlerGame:

-checkAdjacentCornerList
-checkAdjacentEdgesList
-validPositionForSettlement
-validPositionForRoad

updated method in SiedlerGame:
-buildRoad
-buildSettlement
This commit is contained in:
Andrin Fassbind 2021-12-03 09:42:29 +01:00
parent 060310aebc
commit a5f856313d
4 changed files with 83 additions and 27 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <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" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

View File

@ -8,7 +8,7 @@ import java.awt.*;
import java.util.List; import java.util.List;
import java.util.*; import java.util.*;
public class SiedlerBoard extends HexBoard<Land, Structure, Road, String> { public class SiedlerBoard extends HexBoard<Land, Settlement, Road, String> {
Map<Point, ch.zhaw.hexboard.Label> lowerFieldLabel = new HashMap<>(); Map<Point, ch.zhaw.hexboard.Label> lowerFieldLabel = new HashMap<>();

View File

@ -5,8 +5,9 @@ import ch.zhaw.hexboard.HexBoardTextView;
import ch.zhaw.hexboard.Label; import ch.zhaw.hexboard.Label;
import java.awt.*; import java.awt.*;
import java.util.Set;
public class SiedlerBoardTextView extends HexBoardTextView<Land, Structure, Road, String> { public class SiedlerBoardTextView extends HexBoardTextView<Land, Settlement, Road, String> {
public SiedlerBoardTextView(SiedlerBoard board) { public SiedlerBoardTextView(SiedlerBoard board) {
super(board); super(board);

View File

@ -204,20 +204,19 @@ public class SiedlerGame {
* @return true, if the placement was successful * @return true, if the placement was successful
*/ */
public boolean buildSettlement(Point position) { public boolean buildSettlement(Point position) {
//TODO Errors should be caught after this method is executed and returns false //1. Check if position is corner && is empty && neighbour Corners are empty
//1. Check if Corner if(!validPositionForSettlement(position)) {
if (!board.hasCorner(position)) {
return false; return false;
} }
//2. Check if Corner is empty //2. Check if neighbourEdge are Roads belong to active Player
if (board.getCorner(position) != null) { if(!checkAdjacentEdgesList(position)) {
return false; return false;
} }
//3. Can Player build Settlement //3. Can Player build Settlement
if (!allPlayers.get(activePlayer).buildSettlement()) { if (!allPlayers.get(activePlayer).buildSettlement()) {
return false; return false;
} }
//4. Insert Road to map //4. Insert Settlement to map
board.setCorner(position, new Settlement(allPlayers.get(activePlayer).getFaction())); board.setCorner(position, new Settlement(allPlayers.get(activePlayer).getFaction()));
//5. Give Resoure to bank //5. Give Resoure to bank
bank.storeResourceToBank(Config.Structure.SETTLEMENT.getCosts()); bank.storeResourceToBank(Config.Structure.SETTLEMENT.getCosts());
@ -257,39 +256,95 @@ public class SiedlerGame {
* @return true, if the placement was successful * @return true, if the placement was successful
*/ */
public boolean buildRoad(Point roadStart, Point roadEnd) { public boolean buildRoad(Point roadStart, Point roadEnd) {
//1. Check if Edge //1. Check if is edge && is empty && if neighbour Edge or Corners belong to Settlement of active Player
if (!board.hasEdge(roadStart, roadEnd)) { if (!validPositionForRoad(roadStart,roadEnd)){
// TODO: Error message
return false; return false;
} }
//2. Check if Edge is empty //2. Can Player build road
if (board.getEdge(roadStart, roadEnd) != null) {
// TODO: Error message
return false;
}
//3. Can Player build road
if (!allPlayers.get(activePlayer).buildRoad()) { if (!allPlayers.get(activePlayer).buildRoad()) {
// TODO: Error message // TODO: Error message
return false; return false;
} }
//4. Insert Road to map //3. Insert Road to map
board.setEdge(roadStart, roadEnd, new Road(allPlayers.get(activePlayer).getFaction())); 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()); bank.storeResourceToBank(Config.Structure.ROAD.getCosts());
return true; 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;
}
/**
* Do not use for initial Settlement
private boolean validPositionForRoad(Point position){ * @param position
//todo implement * @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; 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<Road> 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<Settlement> results = board.getNeighboursOfCorner(point);
if(results.size() > 0) {
return false;
}
return true; return true;
} }
@ -325,7 +380,7 @@ public class SiedlerGame {
private HashMap<Faction, Integer> getWinPoints(){ private HashMap<Faction, Integer> getWinPoints(){
HashMap<Faction, Integer> winPoints = new HashMap<>(); HashMap<Faction, Integer> winPoints = new HashMap<>();
List<Structure> structures = board.getCorners(); List<Settlement> structures = board.getCorners();
for(Structure structure : structures) { for(Structure structure : structures) {
int newWinPoints = 0; int newWinPoints = 0;