Skip to content

Commit

Permalink
Major code clean-up, some changes related to sonar, other to style.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsaglam committed Feb 4, 2024
1 parent c8c32d7 commit 6ccaece
Show file tree
Hide file tree
Showing 22 changed files with 72 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import carcassonne.model.tile.TileStack;
import carcassonne.settings.GameSettings;
import carcassonne.view.ViewFacade;
import carcassonne.view.main.MainView;

/**
* Is the abstract state of the state machine.
Expand Down Expand Up @@ -113,7 +114,7 @@ protected void startNewRound(int playerCount) {
updateStackSize();
if (settings.isGridSizeChanged()) {
settings.setGridSizeChanged(false);
views.onMainView(it -> it.rebuildGrid());
views.onMainView(MainView::rebuildGrid);
}
GridSpot spot = grid.getFoundation(); // starting spot.
views.onMainView(it -> it.setTile(spot.getTile(), spot.getX(), spot.getY()));
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/carcassonne/control/state/StateGameOver.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import carcassonne.model.grid.GridPattern;
import carcassonne.settings.GameSettings;
import carcassonne.view.ViewFacade;
import carcassonne.view.main.MainView;
import carcassonne.view.menubar.Scoreboard;
import carcassonne.view.util.GameMessage;

/**
Expand Down Expand Up @@ -65,7 +67,7 @@ public void placeTile(int x, int y) {
*/
@Override
public void skip() {
views.onScoreboard(it -> it.disable());
views.onScoreboard(Scoreboard::disable);
exit();
changeState(StateIdle.class);
}
Expand All @@ -82,7 +84,7 @@ protected void entry() {
}
updateScores();
updateStackSize();
views.onMainView(it -> it.resetMenuState());
views.onMainView(MainView::resetMenuState);
GameMessage.showMessage(GAME_OVER_MESSAGE + round.winningPlayers());
views.showGameStatistics(round);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/carcassonne/control/state/StateManning.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import carcassonne.model.tile.Tile;
import carcassonne.settings.GameSettings;
import carcassonne.view.ViewFacade;
import carcassonne.view.main.MainView;
import carcassonne.view.util.GameMessage;

/**
Expand Down Expand Up @@ -120,7 +121,7 @@ private void startNextTurn() {
changeState(StateGameOver.class);
} else {
if (!round.getActivePlayer().isComputerControlled()) {
views.onMainView(it -> it.resetPlacementHighlights());
views.onMainView(MainView::resetPlacementHighlights);
}
round.nextTurn();
views.onMainView(it -> it.setCurrentPlayer(round.getActivePlayer()));
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/carcassonne/control/state/StatePlacing.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import carcassonne.model.tile.Tile;
import carcassonne.settings.GameSettings;
import carcassonne.view.ViewFacade;
import carcassonne.view.main.MainView;
import carcassonne.view.util.GameMessage;

/**
Expand Down Expand Up @@ -86,7 +87,7 @@ private void skipPlacingTile() {
}
});
if (!round.getActivePlayer().isComputerControlled()) {
views.onMainView(it -> it.resetPlacementHighlights());
views.onMainView(MainView::resetPlacementHighlights);
}
round.nextTurn();
views.onMainView(it -> it.setCurrentPlayer(round.getActivePlayer()));
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/carcassonne/model/ai/RuleBasedComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public int compare(AbstractCarcassonneMove firstMove, AbstractCarcassonneMove se
if (firstMove.getGainedMeeples() != secondMove.getGainedMeeples()) {
// Rule 1: Prefer move with a maximal meeple gain
return firstMove.getGainedMeeples() - secondMove.getGainedMeeples();
} else if (firstMove.involvesMeeplePlacement() != secondMove.involvesMeeplePlacement()) {
}
if (firstMove.involvesMeeplePlacement() != secondMove.involvesMeeplePlacement()) {
// Rule 2: Prefer move without meeple placement
return preferFalse(firstMove.involvesMeeplePlacement(), secondMove.involvesMeeplePlacement());
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/carcassonne/model/grid/FieldsPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public FieldsPattern(GridSpot startingSpot, GridDirection startingDirection) {
startingSpot.setTag(startingDirection, this); // initial tag, is needed for adding meeples!
add(startingSpot); // initial tile
buildPattern(startingSpot, startingDirection);
adjacentCastles.forEach(it -> it.removeOwnTags()); // also remove the tile tags of the marked adjacentCastles
adjacentCastles.forEach(CastleAndRoadPattern::removeOwnTags); // also remove the tile tags of the marked adjacentCastles
}

@Override
Expand Down Expand Up @@ -128,12 +128,12 @@ private List<GridDirection> getFieldConnections(GridDirection position, Tile til
private GridDirection getFieldOpposite(GridDirection position, GridDirection neighborDirection) {
if (position.isSmallerOrEquals(WEST)) {
return position.opposite(); // top, right, bottom, left are simply inverted
} else if (position.isSmallerOrEquals(NORTH_WEST)) {
}
if (position.isSmallerOrEquals(NORTH_WEST)) {
if (neighborDirection.isLeftOf(position)) { // neighbor to the left of the corner
return position.opposite().nextDirectionTo(LEFT).nextDirectionTo(LEFT); // return opposite and two to the right
} else { // neighbor to the right of the corner
return position.opposite().nextDirectionTo(RIGHT).nextDirectionTo(RIGHT); // return opposite and two to the left
}
return position.opposite().nextDirectionTo(RIGHT).nextDirectionTo(RIGHT); // return opposite and two to the left
}
return position; // middle stays middle
}
Expand Down
35 changes: 17 additions & 18 deletions src/main/java/carcassonne/model/grid/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public List<GridPattern> getAllPatterns() {
}
}
}
patterns.forEach(it -> it.removeTileTags()); // IMPORTANT
patterns.forEach(GridPattern::removeTileTags); // IMPORTANT
return patterns;
}

Expand Down Expand Up @@ -93,7 +93,7 @@ public Collection<GridPattern> getLocalPatterns(GridSpot spot) {
for (GridSpot neighbor : getNeighbors(spot, false, GridDirection.directNeighbors())) {
gridPatterns.addAll(neighbor.createPatternList());
}
gridPatterns.forEach(it -> it.removeTileTags()); // VERY IMPORTANT!
gridPatterns.forEach(GridPattern::removeTileTags); // VERY IMPORTANT!
return gridPatterns; // get patterns.
}

Expand All @@ -108,7 +108,7 @@ public Collection<GridPattern> getModifiedPatterns(GridSpot spot) {
throw new IllegalArgumentException("Can't check for patterns on an free grid space");
}
Collection<GridPattern> modifiedPatterns = spot.createPatternList();
modifiedPatterns.forEach(it -> it.removeTileTags()); // VERY IMPORTANT!
modifiedPatterns.forEach(GridPattern::removeTileTags); // VERY IMPORTANT!
return modifiedPatterns; // get patterns.
}

Expand All @@ -122,9 +122,8 @@ public GridSpot getNeighbor(GridSpot spot, GridDirection direction) {
List<GridSpot> neighbors = getNeighbors(spot, false, direction);
if (neighbors.isEmpty()) {
return null; // return null if tile not placed or not on grid.
} else {
return neighbors.get(0);
}
return neighbors.get(0);
}

/**
Expand Down Expand Up @@ -241,7 +240,8 @@ public boolean place(int x, int y, Tile tile) {
private void checkParameters(GridSpot spot) {
if (spot == null) {
throw new IllegalArgumentException("Spot can't be null!");
} else if (!spots[spot.getX()][spot.getY()].equals(spot)) {
}
if (!spots[spot.getX()][spot.getY()].equals(spot)) {
throw new IllegalArgumentException("Spot is not on the grid!");
}
}
Expand All @@ -265,7 +265,8 @@ private void checkParameters(int x, int y) {
private void checkParameters(Tile tile) {
if (tile == null) {
throw new IllegalArgumentException("Tile can't be null.");
} else if (tile.getType() == TileType.Null) {
}
if (tile.getType() == TileType.Null) {
throw new IllegalArgumentException("Tile from type TileType.Null can't be placed.");
}
}
Expand All @@ -274,19 +275,17 @@ private void checkParameters(Tile tile) {
private boolean findBoundary(GridSpot spot, GridDirection direction, boolean[][] visitedPositions) {
int newX = direction.getX() + spot.getX(); // get coordinates
int newY = direction.getY() + spot.getY(); // of free space
if (isOnGrid(newX, newY)) { // if on grid
if (spots[newX][newY].isOccupied()) {
return false; // is a tile, can't go through tiles
} else if (!visitedPositions[newX][newY]) { // if not visited
visitedPositions[newX][newY] = true; // mark as visited
for (GridDirection newDirection : GridDirection.directNeighbors()) { // recursion
if (findBoundary(spots[newX][newY], newDirection, visitedPositions)) {
return true; // found boundary
}
if (!isOnGrid(newX, newY)) { // if not on grid
return true; // found boundary
}
if (spots[newX][newY].isOccupied()) {
} else if (!visitedPositions[newX][newY]) { // if not visited
visitedPositions[newX][newY] = true; // mark as visited
for (GridDirection newDirection : GridDirection.directNeighbors()) { // recursion
if (findBoundary(spots[newX][newY], newDirection, visitedPositions)) {
return true; // found boundary
}
}
} else { // if not on grid
return true; // found boundary
}
return false; // has not found boundary
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/carcassonne/model/grid/GridDirection.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public enum GridDirection {
public int getX() {
if (this == NORTH_EAST || this == EAST || this == SOUTH_EAST) {
return 1;
} else if (this == NORTH_WEST || this == WEST || this == SOUTH_WEST) {
}
if (this == NORTH_WEST || this == WEST || this == SOUTH_WEST) {
return -1;
}
return 0;
Expand All @@ -41,7 +42,8 @@ public int getX() {
public int getY() {
if (this == SOUTH_WEST || this == SOUTH || this == SOUTH_EAST) {
return 1;
} else if (this == NORTH_WEST || this == NORTH || this == NORTH_EAST) {
}
if (this == NORTH_WEST || this == NORTH || this == NORTH_EAST) {
return -1;
}
return 0;
Expand Down Expand Up @@ -100,7 +102,8 @@ public GridDirection nextDirectionTo(RotationDirection side) {
public GridDirection opposite() {
if (ordinal() <= 3) { // for NORTH, EAST, SOUTH and WEST:
return values()[smallOpposite(ordinal())];
} else if (ordinal() <= 7) { // for NORTH_EAST, SOUTH_EAST, SOUTH_WEST and NORTH_WEST:
}
if (ordinal() <= 7) { // for NORTH_EAST, SOUTH_EAST, SOUTH_WEST and NORTH_WEST:
return values()[bigOpposite(ordinal())];
}
return CENTER; // middle is the opposite of itself.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/carcassonne/model/grid/GridPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void removeOwnTags() {
* Removes all tags of all tiles of the pattern. Needs to be called after ALL patterns of a tile have been created.
*/
public void removeTileTags() {
containedSpots.forEach(it -> it.removeTags());
containedSpots.forEach(GridSpot::removeTags);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/carcassonne/model/grid/GridSpot.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public boolean isPlaceable(Tile tile, boolean allowEnclaves) {
* Removes all the tags from the tile.
*/
public void removeTags() {
tagMap.values().forEach(it -> it.clear());
tagMap.values().forEach(Set::clear);
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/carcassonne/model/terrain/TileTerrain.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ public Set<GridDirection> getMeepleSpots() {
* @return true if connected, false if not.
*/
public final boolean isConnected(GridDirection from, GridDirection towards) {
if (isDirectConnected(from, towards)) {
return true; // directly connected through the middle of the tile
} else if (from != CENTER && towards != CENTER && isIndirectConnected(from, towards)) {
if (isDirectConnected(from, towards) || (from != CENTER && towards != CENTER && isIndirectConnected(from, towards))) {
return true; // is not from or to middle but indirectly connected (counter)clockwise
} else if (terrain.get(from) == TerrainType.FIELDS && terrain.get(towards) == TerrainType.FIELDS) {
}
if (terrain.get(from) == TerrainType.FIELDS && terrain.get(towards) == TerrainType.FIELDS) {
return isImplicitlyConnected(from, towards); // is connected through implicit terrain information
}
return false;
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/carcassonne/model/tile/Tile.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,12 @@ public void placeMeeple(Player player, GridDirection position, GameSettings sett
}

public void placeMeeple(Player player, GridDirection position, Meeple meeple, GameSettings settings) {
if (this.meeple == null && allowsPlacingMeeple(position, player, settings)) {
this.meeple = meeple;
meeple.setLocation(gridSpot);
meeple.setPosition(position);
} else {
if ((this.meeple != null) || !allowsPlacingMeeple(position, player, settings)) {
throw new IllegalArgumentException("Tile can not have already a meeple placed on it: " + toString());
}
this.meeple = meeple;
meeple.setLocation(gridSpot);
meeple.setPosition(position);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/carcassonne/model/tile/TileStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public TileStack(TileDistribution distribution, int multiplier, Long sortingSeed
public Tile drawTile() {
if (tiles.isEmpty() && returnedTiles.isEmpty()) {
return null; // no tile to draw!
} else if (tiles.isEmpty()) {
}
if (tiles.isEmpty()) {
return returnedTiles.poll();
}
return tiles.pop();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/carcassonne/model/tile/TileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private TileUtil() {
* @return the number of possible rotations (between 1 and 3).
*/
public static int rotationLimitFor(TileType type) {
return rotations.computeIfAbsent(type, key -> determineRotationLimit(key));
return rotations.computeIfAbsent(type, TileUtil::determineRotationLimit);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/carcassonne/util/ConcurrentTileImageScaler.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ private static Image getOriginalImageUnsafe(Tile tile) {
String imagePath = GameSettings.TILE_FOLDER_PATH + tile.getType().name() + tile.getImageIndex() + GameSettings.TILE_FILE_TYPE;
if (TileImageScalingCache.containsScaledImage(tile, TILE_RESOLUTION, false)) {
return TileImageScalingCache.getScaledImage(tile, TILE_RESOLUTION);
} else if (tile.hasEmblem()) {
}
if (tile.hasEmblem()) {
return loadImageAndPaintEmblem(tile, imagePath);
} else {
Image image = ImageLoadingUtil.createBufferedImage(imagePath);
TileImageScalingCache.putScaledImage(image, tile, TILE_RESOLUTION, false);
return image;
}
Image image = ImageLoadingUtil.createBufferedImage(imagePath);
TileImageScalingCache.putScaledImage(image, tile, TILE_RESOLUTION, false);
return image;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/carcassonne/view/main/MeepleLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ public void placeMeeple(int x, int y, TerrainType terrain, GridDirection positio
* Refreshes all meeple labels in this layer. This updates the images to color changes.
*/
public void refreshLayer() {
meeplePanels.stream().forEach(it -> it.refreshAll());
meeplePanels.stream().forEach(MeepleDepictionPanel::refreshAll);
}

/**
* Resets all meeples in the layer.
*/
public void resetLayer() {
meeplePanels.stream().forEach(it -> it.resetAll());
meeplePanels.stream().forEach(MeepleDepictionPanel::resetAll);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/carcassonne/view/main/TileLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,18 @@ public void refreshHighlight(ImageIcon newHighlight) {
* @param newHighlight is the new image.
*/
public void refreshPlacementHighlights() {
placementHighlights.forEach(it -> it.refresh());
placementHighlights.forEach(TileDepiction::refresh);
}

public void resetPlacementHighlights() {
placementHighlights.forEach(it -> it.resetPlacementHighlight());
placementHighlights.forEach(TileDepiction::resetPlacementHighlight);
placementHighlights.clear();
}

/**
* Resets every tile label in this layer.
*/
public void resetLayer() {
tileLabels.stream().forEach(it -> it.reset());
tileLabels.stream().forEach(TileDepiction::reset);
}
}
2 changes: 1 addition & 1 deletion src/main/java/carcassonne/view/secondary/MeepleView.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void setTile(Tile tile, Player currentPlayer) {
}
this.tile = tile;
setCurrentPlayer(currentPlayer);
ThreadingUtil.runAndCallback(() -> updatePlacementButtons(), () -> showUI());
ThreadingUtil.runAndCallback(this::updatePlacementButtons, this::showUI);
}

// build button grid
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/carcassonne/view/secondary/PlacementButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ public boolean isHackyEnabled() {
String osName = System.getProperty(OS_NAME);
if (osName.startsWith(MAC) || osName.equals(WINDOWS_10)) {
return isEnabled(); // normal function on mac os x
} else {
// own implementation to fix the functionality which is destroyed by the hack. If the
// original isEnabled method is overwritten, it breaks some functionality (e.g.updating
// the background):
return enabled;
}
// own implementation to fix the functionality which is destroyed by the hack. If the
// original isEnabled method is overwritten, it breaks some functionality (e.g.updating
// the background):
return enabled;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/carcassonne/view/secondary/TileView.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void setTiles(Player currentPlayer) {
if (!currentPlayer.getHandOfTiles().isEmpty()) {
tiles.addAll(currentPlayer.getHandOfTiles());
setCurrentPlayer(currentPlayer);
ThreadingUtil.runAndCallback(() -> updatePreviewLabels(), () -> showUI());
ThreadingUtil.runAndCallback(this::updatePreviewLabels, this::showUI);
}
}

Expand Down
Loading

0 comments on commit 6ccaece

Please sign in to comment.