diff --git a/build.gradle.kts b/build.gradle.kts index eb469f6..e735eaf 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -39,6 +39,8 @@ dependencies { // JGraphT (graph data structures and algorithms) val jGraphTVersion = "1.5.2" implementation("org.jgrapht:jgrapht-core:$jGraphTVersion") + val jacksonVersion = "2.16.0" + implementation("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion") // JavaFX: comment out if you do not need them val javaFxVersion = 15 diff --git a/src/main/java/it/unibo/model/GameManagerImpl.java b/src/main/java/it/unibo/model/GameManagerImpl.java index 181fb43..1ee4203 100644 --- a/src/main/java/it/unibo/model/GameManagerImpl.java +++ b/src/main/java/it/unibo/model/GameManagerImpl.java @@ -1,11 +1,17 @@ package it.unibo.model; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; +import java.util.Optional; import java.util.Random; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import it.unibo.common.Recipes; import it.unibo.common.card.CardType; @@ -18,6 +24,7 @@ import it.unibo.model.board.BoardImpl; import it.unibo.model.developmentcard.DevelopmentCards; import it.unibo.model.developmentcard.DevelopmentCardsImpl; +import it.unibo.model.mapgenerator.BeginnerGameMapGenerator; import it.unibo.model.mapgenerator.GameMapGenerator; import it.unibo.model.mapgenerator.RandomGameMapGenerator; import it.unibo.model.player.Player; @@ -35,9 +42,6 @@ * Implementation of GameManager. */ public final class GameManagerImpl implements GameManager { - private static final int DEFAULT_POINTS_TO_WIN = 10; - private static final int DEFAULT_BANK_RESOURCES = 19; - private final DevelopmentCards developmentCards; private final PropertyManager propertyManager; private final RoadManager roadManager; @@ -49,31 +53,21 @@ public final class GameManagerImpl implements GameManager { private final int pointsToWin; /** - * Game manager constructor. - * - * @param generator game map generator - * @param playersNames list of players' names - * @param pointsToWin points to win the game - * @param bankResourcesAmount initial amount for each resource of the bank + * @param playersNames list of players' names + * @see #GameManagerImpl(MapType,List,int) */ - public GameManagerImpl(final GameMapGenerator generator, final List playersNames, final int pointsToWin, - final int bankResourcesAmount) { + public GameManagerImpl(final List playersNames) { playersNames.forEach(name -> players.add(new PlayerImpl(name))); - this.pointsToWin = pointsToWin; this.developmentCards = new DevelopmentCardsImpl(); this.roadManager = new RoadManagerImpl(); this.propertyManager = new PropertyManagerImpl(); this.turnManager = new TurnManagerImpl(players); - this.resourceManager = new ResourceManagerImpl(players, bankResourcesAmount); - this.board = new BoardImpl(generator); - } + this.resourceManager = new ResourceManagerImpl(players); - /** - * @param playersNames list of players' names - */ - public GameManagerImpl(final List playersNames) { - this(new RandomGameMapGenerator(), playersNames, DEFAULT_POINTS_TO_WIN, DEFAULT_BANK_RESOURCES); + final GameSettings settings = new GameSettings("settings/settings.json"); + this.pointsToWin = settings.getPoints(); + this.board = new BoardImpl(settings.getMapGenerator()); } @Override @@ -339,4 +333,87 @@ private boolean isRoadNearToAnyPlayerProperty(final RoadPosition roadPosition, f }); } + /** + * Represents + */ + private class GameSettings { + + private enum MapType { + /** + * @see BeginnerGameMapGenerator + */ + BEGINNER, + /** + * @see RandomGameMapGenerator + */ + RANDOM + } + + private static final String DEFAULT_MAP_FIELD = "beginner"; + private static final MapType DEFAULT_MAP_TYPE = MapType.BEGINNER; + private static final Map fieldToMapType = Map.of(DEFAULT_MAP_FIELD, DEFAULT_MAP_TYPE, + "random", MapType.RANDOM); + private static final int DEFAULT_POINTS = 10; + + private int points; + private MapType mapType; + + /** + * Class which represents the settings of the game customized by players, which + * are the amount of points necessary to win and the type of map to play with. + * + * @param settingsPath path of settings file in CLASSPATH + */ + private GameSettings(final String settingsPath) { + try { + final ObjectMapper objectMapper = new ObjectMapper(); + final JsonNode settings = objectMapper.readTree(ClassLoader.getSystemResourceAsStream(settingsPath)); + final String MAP_FIELD_NAME = "map"; + final String POINTS_FIELD_NAME = "points"; + setMapType(Optional.ofNullable(settings.get(MAP_FIELD_NAME))); + setPoints(Optional.ofNullable(settings.get(POINTS_FIELD_NAME))); + } catch (IOException e) { + setDefaultMapType(); + setDefaultPoints(); + } + } + + private int getPoints() { + return points; + } + + private GameMapGenerator getMapGenerator() { + return switch (mapType) { + case RANDOM -> new RandomGameMapGenerator(); + default -> new BeginnerGameMapGenerator(); + }; + } + + private void setMapType(Optional selectedMap) { + if (selectedMap.isPresent()) { + mapType = fieldToMapType.getOrDefault( + selectedMap.get().asText(DEFAULT_MAP_FIELD).toLowerCase(Locale.US), + DEFAULT_MAP_TYPE); + } else { + setDefaultMapType(); + } + } + + private void setDefaultMapType() { + mapType = DEFAULT_MAP_TYPE; + } + + private void setPoints(Optional selectedPoints) { + if (selectedPoints.isPresent()) { + points = selectedPoints.get().asInt(DEFAULT_POINTS); + } else { + setDefaultPoints(); + } + } + + private void setDefaultPoints() { + points = DEFAULT_POINTS; + } + } + } diff --git a/src/main/java/it/unibo/model/resource/ResourceManagerImpl.java b/src/main/java/it/unibo/model/resource/ResourceManagerImpl.java index 9fbb961..45ff746 100644 --- a/src/main/java/it/unibo/model/resource/ResourceManagerImpl.java +++ b/src/main/java/it/unibo/model/resource/ResourceManagerImpl.java @@ -15,6 +15,7 @@ public final class ResourceManagerImpl implements ResourceManager { private final Map> allEntityResources = new HashMap<>(); private final ResourceOwner bank; static final int DISCARD_THRESHOLD = 7; + static final int DEFAULT_BANK_RESOURCES = 19; /** * Create the ResourceManager from the list of the resource owner(such as @@ -23,9 +24,9 @@ public final class ResourceManagerImpl implements ResourceManager { * @param players the list of players * @param bankResourcesAmount the amount of resources in the bank */ - public ResourceManagerImpl(final List players, final int bankResourcesAmount) { + public ResourceManagerImpl(final List players) { players.forEach(ro -> allEntityResources.put(ro, ro.getDefaultResources())); - bank = new BankImpl(bankResourcesAmount); + bank = new BankImpl(DEFAULT_BANK_RESOURCES); allEntityResources.put(bank, bank.getDefaultResources()); } diff --git a/src/main/resources/settings/settings.json b/src/main/resources/settings/settings.json new file mode 100644 index 0000000..f7a7555 --- /dev/null +++ b/src/main/resources/settings/settings.json @@ -0,0 +1,4 @@ +{ + "map": "beginner", + "victoryPoints": 10 +} \ No newline at end of file diff --git a/src/test/java/it/unibo/model/GameManagerTest.java b/src/test/java/it/unibo/model/GameManagerTest.java index d3785b2..5d6be39 100644 --- a/src/test/java/it/unibo/model/GameManagerTest.java +++ b/src/test/java/it/unibo/model/GameManagerTest.java @@ -30,9 +30,7 @@ void initializeGame() { void testProduceResources() { final Player player1 = new PlayerImpl("first"); final Player player2 = new PlayerImpl("second"); - final GameManager gameManager = new GameManagerImpl(new BeginnerGameMapGenerator(), - List.of(player1.getName(), player2.getName()), 10, - 19); + final GameManager gameManager = new GameManagerImpl(List.of(player1.getName(), player2.getName())); final TilePosition tile1 = new TilePositionImpl(1, 0); final TilePosition tile2 = new TilePositionImpl(3, 2); diff --git a/src/test/java/it/unibo/model/ResourceManagerTest.java b/src/test/java/it/unibo/model/ResourceManagerTest.java index cbb4570..73f32c9 100644 --- a/src/test/java/it/unibo/model/ResourceManagerTest.java +++ b/src/test/java/it/unibo/model/ResourceManagerTest.java @@ -30,7 +30,7 @@ class ResourceManagerTest { @BeforeEach void init() { - this.resourceManager = new ResourceManagerImpl(List.of(PLAYER1, PLAYER2, PLAYER3, PLAYER4), 19); + this.resourceManager = new ResourceManagerImpl(List.of(PLAYER1, PLAYER2, PLAYER3, PLAYER4)); } /**