diff --git a/src/main/java/cz/jull/Game.java b/src/main/java/cz/jull/Game.java index f5d45cf..c155f84 100644 --- a/src/main/java/cz/jull/Game.java +++ b/src/main/java/cz/jull/Game.java @@ -1,10 +1,11 @@ package cz.jull; import cz.jull.market.Market; -import cz.jull.surroundings.ForestType; -import cz.jull.surroundings.PathType; -import cz.jull.surroundings.SoilType; -import cz.jull.surroundings.WaterType; +import cz.jull.stats.Events; +import cz.jull.stats.surroundings.ForestType; +import cz.jull.stats.surroundings.PathType; +import cz.jull.stats.surroundings.SoilType; +import cz.jull.stats.surroundings.WaterType; import cz.jull.tui.Strings; import lombok.Getter; @@ -26,7 +27,7 @@ public class Game { @Getter private WaterType waterType; - private Strings strings; + private final Strings strings; @Getter private final List buildings = new ArrayList<>(); @@ -34,12 +35,14 @@ public class Game { @Getter private Market market; + private final List events = new ArrayList<>(); + public Game(Strings strings) { this.strings = strings; } public void play() { - System.out.println(strings.getWelcome()); + strings.print("welcome"); generateStats(); for (int i = 0; i < 10; i++) { @@ -49,20 +52,35 @@ public class Game { public void generateStats() { forestType = ForestType.getRandom(); - System.out.println("forest: " + forestType); + strings.print("forestType", forestType); soilType = SoilType.getRandom(); - System.out.println("soil: " + soilType); + strings.print("soilType", soilType); pathType = PathType.getRandom(); - System.out.println("path: " + pathType); + strings.print("pathType", pathType); waterType = WaterType.getRandom(); - System.out.println("water: " + waterType); + strings.print("waterType", waterType); if (waterType == WaterType.WELL) { buildings.add(Item.WELL); } + player.setCoins(1000); market = new Market(this); + + events.add(Events.PEACEFUL_ROUND); + + if (forestType == ForestType.LARGE) { + events.add(Events.FIRE); + } + + if (waterType == WaterType.RIVER) { + events.add(Events.FLOOD); + } + + if (pathType == PathType.MAIN_PATH) { + events.add(Events.ATTACK); + } } public void build(Item item) { diff --git a/src/main/java/cz/jull/Item.java b/src/main/java/cz/jull/Item.java index 9f0c191..0967238 100644 --- a/src/main/java/cz/jull/Item.java +++ b/src/main/java/cz/jull/Item.java @@ -4,7 +4,13 @@ import lombok.Getter; import java.util.function.Function; - +/** + * This enum is managing all items whether the is item is placeable + * and the appropriate price. + *

+ * Some enum constants have their own functions for determining if the item can be built or not. + * The same thing applies for buying. + */ public enum Item implements Buildable { AXE("Axe", false, 5), @@ -174,6 +180,9 @@ public enum Item implements Buildable { CHEESE_FACTORY.canBeBoughtFunction = func; } + /** + * Human-readable name + */ @Getter private final String name; @@ -182,8 +191,14 @@ public enum Item implements Buildable { @Getter private final int price; + /** + * Function that determines if the item can be built or not + */ private Function canBeBuiltFunction; + /** + * Function that determines if the item can be bought or not + */ @Getter private Function canBeBoughtFunction; @@ -195,6 +210,11 @@ public enum Item implements Buildable { this.canBeBoughtFunction = ignored -> true; } + /** + * Function that executes function that determines if the item can be built or not + * @param game + * @return Boolean if the item can be built + */ @Override public boolean canBuild(Game game) { if (!isPlaceable) { @@ -203,6 +223,10 @@ public enum Item implements Buildable { return canBeBuiltFunction.apply(game); } + /** + * Returns human-readable name + * @return Human-readable name + */ @Override public String toString() { return name; diff --git a/src/main/java/cz/jull/Main.java b/src/main/java/cz/jull/Main.java index f2e09c3..3339f14 100644 --- a/src/main/java/cz/jull/Main.java +++ b/src/main/java/cz/jull/Main.java @@ -5,8 +5,14 @@ import cz.jull.tui.Strings; import java.io.IOException; public class Main { - public static void main(String[] args) throws IOException { - Strings strings = Strings.load(); + public static void main(String[] args) { + Strings strings = null; + try { + strings = Strings.load(); + } catch (IOException e) { + System.out.println("Error: Locale string could not be loaded."); + System.exit(1); + } Game game = new Game(strings); game.play(); } diff --git a/src/main/java/cz/jull/market/Market.java b/src/main/java/cz/jull/market/Market.java index 773f3e3..c38c882 100644 --- a/src/main/java/cz/jull/market/Market.java +++ b/src/main/java/cz/jull/market/Market.java @@ -4,8 +4,8 @@ import cz.jull.Game; import cz.jull.Item; import cz.jull.exceptions.ItemNotAvailableException; import cz.jull.exceptions.NotSufficientsCoinsException; -import cz.jull.surroundings.PathType; -import cz.jull.surroundings.SoilType; +import cz.jull.stats.surroundings.PathType; +import cz.jull.stats.surroundings.SoilType; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/cz/jull/stats/Events.java b/src/main/java/cz/jull/stats/Events.java new file mode 100644 index 0000000..202f767 --- /dev/null +++ b/src/main/java/cz/jull/stats/Events.java @@ -0,0 +1,8 @@ +package cz.jull.stats; + +public enum Events { + PEACEFUL_ROUND, + FLOOD, + ATTACK, + FIRE +} diff --git a/src/main/java/cz/jull/surroundings/ForestType.java b/src/main/java/cz/jull/stats/surroundings/ForestType.java similarity index 66% rename from src/main/java/cz/jull/surroundings/ForestType.java rename to src/main/java/cz/jull/stats/surroundings/ForestType.java index 3d1fce9..b3a6935 100644 --- a/src/main/java/cz/jull/surroundings/ForestType.java +++ b/src/main/java/cz/jull/stats/surroundings/ForestType.java @@ -1,4 +1,4 @@ -package cz.jull.surroundings; +package cz.jull.stats.surroundings; import lombok.AllArgsConstructor; import lombok.Getter; @@ -8,9 +8,11 @@ import java.util.Random; @AllArgsConstructor public enum ForestType { - SMALL(25, 1), - MEDIUM(50, 3), - LARGE(100, 5); + SMALL("Small", 25, 1), + MEDIUM("Medium",50, 3), + LARGE("Large", 100, 5); + + private String name; @Getter @Setter @@ -25,4 +27,9 @@ public enum ForestType { return types[random.nextInt(types.length)]; } + + @Override + public String toString() { + return name; + } } diff --git a/src/main/java/cz/jull/stats/surroundings/PathType.java b/src/main/java/cz/jull/stats/surroundings/PathType.java new file mode 100644 index 0000000..b1fa266 --- /dev/null +++ b/src/main/java/cz/jull/stats/surroundings/PathType.java @@ -0,0 +1,27 @@ +package cz.jull.stats.surroundings; + +import java.util.Random; + +public enum PathType { + DEAD_END("Dead end"), + SIDE_PATH("Side path"), + MAIN_PATH("Main path"),; + + private final String name; + + PathType(String name) { + this.name = name; + } + + public static PathType getRandom() { + Random random = new Random(); + PathType[] types = values(); + + return types[random.nextInt(types.length)]; + } + + @Override + public String toString() { + return name; + } +} diff --git a/src/main/java/cz/jull/stats/surroundings/SoilType.java b/src/main/java/cz/jull/stats/surroundings/SoilType.java new file mode 100644 index 0000000..6c5ee13 --- /dev/null +++ b/src/main/java/cz/jull/stats/surroundings/SoilType.java @@ -0,0 +1,27 @@ +package cz.jull.stats.surroundings; + +import java.util.Random; + +public enum SoilType { + STONY("Stony"), + LESS_FERTILE("Moderately fertile"), + FERTILE("Fertile"),; + + private final String name; + + public static SoilType getRandom() { + Random random = new Random(); + SoilType[] types = values(); + + return types[random.nextInt(types.length)]; + } + + SoilType(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } +} diff --git a/src/main/java/cz/jull/surroundings/WaterType.java b/src/main/java/cz/jull/stats/surroundings/WaterType.java similarity index 64% rename from src/main/java/cz/jull/surroundings/WaterType.java rename to src/main/java/cz/jull/stats/surroundings/WaterType.java index 76b3f8b..4d9c468 100644 --- a/src/main/java/cz/jull/surroundings/WaterType.java +++ b/src/main/java/cz/jull/stats/surroundings/WaterType.java @@ -1,4 +1,4 @@ -package cz.jull.surroundings; +package cz.jull.stats.surroundings; import lombok.AllArgsConstructor; import lombok.Getter; @@ -7,9 +7,11 @@ import java.util.Random; @AllArgsConstructor public enum WaterType { - WELL(5), - POND(10), - RIVER(15); + WELL("Well",5), + POND("Pond",10), + RIVER("River",15); + + private final String name; @Getter private final int farmlandAndVineyardCount; @@ -20,4 +22,9 @@ public enum WaterType { return types[random.nextInt(types.length)]; } + + @Override + public String toString() { + return name; + } } diff --git a/src/main/java/cz/jull/surroundings/PathType.java b/src/main/java/cz/jull/surroundings/PathType.java deleted file mode 100644 index 23d3d72..0000000 --- a/src/main/java/cz/jull/surroundings/PathType.java +++ /dev/null @@ -1,16 +0,0 @@ -package cz.jull.surroundings; - -import java.util.Random; - -public enum PathType { - DEAD_END, - SIDE_PATH, - MAIN_PATH; - - public static PathType getRandom() { - Random random = new Random(); - PathType[] types = values(); - - return types[random.nextInt(types.length)]; - } -} diff --git a/src/main/java/cz/jull/surroundings/SoilType.java b/src/main/java/cz/jull/surroundings/SoilType.java deleted file mode 100644 index 78c7127..0000000 --- a/src/main/java/cz/jull/surroundings/SoilType.java +++ /dev/null @@ -1,16 +0,0 @@ -package cz.jull.surroundings; - -import java.util.Random; - -public enum SoilType { - STONY, - LESS_FERTILE, - FERTILE; - - public static SoilType getRandom() { - Random random = new Random(); - SoilType[] types = values(); - - return types[random.nextInt(types.length)]; - } -} diff --git a/src/main/java/cz/jull/tui/Strings.java b/src/main/java/cz/jull/tui/Strings.java index b4a186c..a57b961 100644 --- a/src/main/java/cz/jull/tui/Strings.java +++ b/src/main/java/cz/jull/tui/Strings.java @@ -5,20 +5,58 @@ import lombok.Getter; import java.io.File; import java.io.IOException; +import java.text.MessageFormat; +/** + * This class manages all strings in the game + */ @Getter public class Strings { private String welcome; + private String forestType; + private String soilType; + private String pathType; + private String waterType; + /** + * Loads strings from json file + * @return Instance of {@link Strings} + * @throws IOException Is thrown when loading the strings failed + */ public static Strings load() throws IOException { ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.readValue(new File("./strings.json"), Strings.class); } - public void test(String prop) throws NoSuchFieldException { - var field = this.getClass().getDeclaredField(prop); - field.setAccessible(true); - + /** + * Prints out a String based on the prop + * @param prop The name of the field + */ + public void print(String prop) { + try { + var field = this.getClass().getDeclaredField(prop); + field.setAccessible(true); + Object value = field.get(this); + System.out.println(value); + } catch (NoSuchFieldException | IllegalAccessException e) { + System.out.println("Internal error: Could not found string"); + } + } + /** + * Prints out a template with string inserted based on the prop + * @param prop The name of the field + * @param values Values for the template + */ + public void print(String prop, Object... values) { + try { + var field = this.getClass().getDeclaredField(prop); + field.setAccessible(true); + Object value = field.get(this); + String result = MessageFormat.format(value.toString(), values); + System.out.println(result); + } catch (NoSuchFieldException | IllegalAccessException e) { + System.out.println("Internal error: Could not found string"); + } } } diff --git a/strings.json b/strings.json index 5e24f8e..9f8a11c 100644 --- a/strings.json +++ b/strings.json @@ -1,3 +1,7 @@ { - "welcome": "Welcome" + "welcome": "Welcome", + "forestType": "Your forest size: {0}", + "soilType": "Your soil type size: {0}", + "pathType": "Your path type: {0}", + "waterType": "Your water type: {0}" } \ No newline at end of file