Skip to content

Commit

Permalink
FIX: Fixed the off-by-one error when sharing gird patterns with odd s…
Browse files Browse the repository at this point in the history
…core values.
  • Loading branch information
tsaglam committed Oct 21, 2018
1 parent f5ee1d8 commit e3c5053
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 40 deletions.
32 changes: 3 additions & 29 deletions src/main/java/carcassonne/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
public class Player {
private static final int MAX_MEEPLES = 7;
private int freeMeeples;
private Map<TerrainType, Integer> multiplierMap;
private final int number;
private int overallScore;
private Map<TerrainType, Integer> scoreMap;
Expand All @@ -24,7 +23,6 @@ public class Player {
public Player(int number) {
this.number = number;
freeMeeples = MAX_MEEPLES;
initializeMultiplierMap();
initializeScores();
}

Expand All @@ -34,10 +32,9 @@ public Player(int number) {
* @param scoreType determines the score multiplier.
* @param gameOver determines if the game is running or not. Changes score multipliers.
*/
public void addScore(int amount, TerrainType scoreType, boolean gameOver) {
int scoreToAdd = calculateScore(amount, scoreType, gameOver);
scoreMap.put(scoreType, scoreMap.get(scoreType) + scoreToAdd);
overallScore += scoreToAdd;
public void addScore(int amount, TerrainType scoreType) {
scoreMap.put(scoreType, scoreMap.get(scoreType) + amount);
overallScore += amount;
}

/**
Expand Down Expand Up @@ -104,34 +101,11 @@ public String toString() {
return "Player[number: " + number + ", score: " + overallScore + ", free meeples: " + freeMeeples + "]";
}

/**
* Multiplies the amount of score by the multiplier of the type of the score.
* @param amount sets the amount of score.
* @param scoreType is the type of score, which influences the multiplier.
* @param gameOver determines if the game is running or not. Changes score multipliers.
* @return the multiplied score.
*/
private int calculateScore(int amount, TerrainType scoreType, boolean gameOver) {
if (scoreType == TerrainType.CASTLE && gameOver) {
return amount;
}
return amount * multiplierMap.get(scoreType); // TODO (HIGH) divide after multiplier
}

private void initializeMultiplierMap() {
multiplierMap = new HashMap<>();
multiplierMap.put(TerrainType.CASTLE, 2);
multiplierMap.put(TerrainType.ROAD, 1);
multiplierMap.put(TerrainType.MONASTERY, 1);
multiplierMap.put(TerrainType.FIELDS, 3);
}

private void initializeScores() {
overallScore = 0;
scoreMap = new HashMap<>();
for (int i = 0; i < TerrainType.values().length - 1; i++) {
scoreMap.put(TerrainType.values()[i], 0); // initial scores are zero
}
}

}
12 changes: 10 additions & 2 deletions src/main/java/carcassonne/model/grid/CastleAndRoadPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ public class CastleAndRoadPattern extends GridPattern { // TODO (HIGH) use subcl
* @param grid is the grid the pattern is created on.
*/
public CastleAndRoadPattern(GridSpot startingSpot, GridDirection startingDirection, TerrainType patternType, Grid grid) {
super(patternType);
super(patternType, (patternType == TerrainType.CASTLE) ? 2 : 1);
checkArgs(startingSpot, startingDirection, patternType, grid);
startingSpot.setTag(startingDirection, this); // initial tag
add(startingSpot); // initial tile
complete = buildPattern(startingSpot, startingDirection, grid); // recursive algorithm.
}

@Override
public void forceDisburse() {
if (!complete) { // if castle is not complete
scoreMultiplier = 1; // reduce score multiplier
}
super.forceDisburse();
}

private boolean buildPattern(GridSpot spot, GridDirection startingPoint, Grid grid) {
boolean isClosed = true;
for (GridDirection direction : GridDirection.directNeighbors()) { // for every side
Expand All @@ -40,7 +48,7 @@ private boolean buildPattern(GridSpot spot, GridDirection startingPoint, Grid gr
private void checkArgs(GridSpot spot, GridDirection direction, TerrainType terrain, Grid grid) {
if (terrain != TerrainType.CASTLE && terrain != TerrainType.ROAD) {
throw new IllegalArgumentException("Can only create CastleAndRoadPatterns from type castle or road");
}
}
checkArgs(spot, direction, grid);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/carcassonne/model/grid/FieldsPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class FieldsPattern extends GridPattern {
private final Grid grid;

public FieldsPattern(GridSpot startingSpot, GridDirection startingDirection, Grid grid) {
super(FIELDS);
super(FIELDS, 3);
this.grid = grid;
adjacentCastles = new LinkedList<>();
checkArgs(startingSpot, startingDirection, grid);
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/carcassonne/model/grid/GridPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,18 @@ public abstract class GridPattern {
protected List<Meeple> meepleList;
protected boolean complete;
private boolean disbursed;
private boolean reducedPoints;
protected int scoreMultiplier;

/**
* Basic constructor taking only a tile type.
* @param patternType is the type of the pattern.
*/
protected GridPattern(TerrainType patternType) {
protected GridPattern(TerrainType patternType, int scoreMultiplier) {
this.patternType = patternType;
this.scoreMultiplier = scoreMultiplier;
spotList = new LinkedList<>();
meepleList = new LinkedList<>();
involvedPlayers = new HashMap<>();
complete = false;
disbursed = false;
reducedPoints = false;
}

/**
Expand Down Expand Up @@ -67,7 +65,7 @@ public void disburse() {
involvedPlayers.remove(player); // remove players who don't get points
}
for (Player player : involvedPlayers.keySet()) { // other players split the pot
player.addScore((int) Math.ceil(getSize() / involvedPlayers.size()), patternType, reducedPoints);
player.addScore((int) Math.ceil(getSize() * scoreMultiplier / involvedPlayers.size()), patternType);
}
for (Meeple meeple : meepleList) {
meeple.removePlacement(); // remove meeples from tiles.
Expand All @@ -83,7 +81,6 @@ public void disburse() {
public void forceDisburse() {
if (!complete) {
complete = true;
reducedPoints = true;
disburse();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MonasteryGridPattern extends GridPattern {
* @param grid is the grid the pattern is created from.
*/
public MonasteryGridPattern(GridSpot spot, Grid grid) {
super(MONASTERY);
super(MONASTERY, 1);
if (spot.getTile().getTerrain(MIDDLE) != MONASTERY) {
throw new IllegalArgumentException("Can't create monastery pattern from non monastery tile");
}
Expand Down

0 comments on commit e3c5053

Please sign in to comment.