From 2cbc692470c18d4cb7429ad1939b979588e2951f Mon Sep 17 00:00:00 2001 From: jull Date: Fri, 30 May 2025 08:16:54 +0200 Subject: [PATCH] feat: a lot of changes --- src/main/java/cz/jull/Game.java | 58 ++++++++++++++++--- src/main/java/cz/jull/Player.java | 3 + src/main/java/cz/jull/items/Item.java | 2 + src/main/java/cz/jull/stats/Events.java | 15 +++++ .../cz/jull/stats/surroundings/WaterType.java | 10 +++- src/main/java/cz/jull/tui/Cli.java | 12 ++++ src/main/java/cz/jull/tui/Menu.java | 53 ++++++++++++----- src/main/java/cz/jull/tui/Strings.java | 7 ++- 8 files changed, 137 insertions(+), 23 deletions(-) diff --git a/src/main/java/cz/jull/Game.java b/src/main/java/cz/jull/Game.java index aced9eb..24d4262 100644 --- a/src/main/java/cz/jull/Game.java +++ b/src/main/java/cz/jull/Game.java @@ -17,6 +17,9 @@ import java.util.ArrayList; import java.util.List; import java.util.Random; +/** + * Class that manages the whole game + */ @Getter public class Game { @@ -47,6 +50,9 @@ public class Game { this.strings = strings; } + /** + * Function that starts the game + */ public void play() { strings.print("welcome"); Cli.pressEnter(); @@ -99,6 +105,9 @@ public class Game { } } + /** + * Function that generates the main stats for the player and the game + */ public void generateStats() { forestType = ForestType.getRandom(); strings.print("forestType", forestType); @@ -143,6 +152,11 @@ public class Game { return events.get(random.nextInt(events.size())); } + /** + * Function that builds an item from players inventory + * + * @param item Item that is being build + */ public void build(Item item) { if (!player.getInventory().contains(item)) { strings.print("cantBuild"); @@ -151,7 +165,7 @@ public class Game { if (item.canBuild(this)) { if (List.of(Item.WHEAT_SEEDS, Item.HOPS_SEEDS, Item.GRAPEVINE_SEEDS, Item.WHEAT_FARMLAND, Item.HOPS_FARMLAND, Item.VINEYARD).contains(item)) { - for (int i = 0; i < waterType.getFarmlandAndVineyardCount() ; i++) { + for (int i = 0; i < waterType.getFarmlandCount() ; i++) { buildings.add(item); player.getInventory().remove(item); } @@ -162,6 +176,9 @@ public class Game { } } + /** + * Function that gives player wood based on how large is players forest + */ public void cutTrees() { if (forestType.getSize() <= 0 ) { strings.print("noTrees"); @@ -179,36 +196,55 @@ public class Game { } } + /** + * Function that picks an item for harvesting and puts teh produce in inventory + * + * @param item Item that is being harvested + */ public void harvest(Item item) { + Item item2 = switch (item) { + + } + + if (waterType.getFarmlandCount() > buildings.stream().filter(item1 -> item1 == item2).count()) { + System.out.println("You can't harvest"); + return; + } + for (int i = 0; i < waterType.getFarmlandCount() ; i++) { + buildings.remove(item2); + player.getInventory().add(item2); + } + + switch (item) { case WHEAT_FARMLAND -> { - if (waterType.getFarmlandAndVineyardCount() > buildings.stream().filter(item1 -> item1 == Item.WHEAT).count()) { + if (waterType.getFarmlandCount() > buildings.stream().filter(item1 -> item1 == Item.WHEAT).count()) { System.out.println("You can't harvest"); return; } - for (int i = 0; i < waterType.getFarmlandAndVineyardCount() ; i++) { + for (int i = 0; i < waterType.getFarmlandCount() ; i++) { buildings.remove(Item.WHEAT); player.getInventory().add(Item.WHEAT); } } case HOPS_FARMLAND -> { - if (waterType.getFarmlandAndVineyardCount() > buildings.stream().filter(item1 -> item1 == Item.HOPS).count()) { + if (waterType.getFarmlandCount() > buildings.stream().filter(item1 -> item1 == Item.HOPS).count()) { System.out.println("You can't harvest"); return; } - for (int i = 0; i < waterType.getFarmlandAndVineyardCount() ; i++) { + for (int i = 0; i < waterType.getFarmlandCount() ; i++) { buildings.remove(Item.HOPS); player.getInventory().add(Item.HOPS); } } case VINEYARD -> { - if (waterType.getFarmlandAndVineyardCount() > buildings.stream().filter(item1 -> item1 == Item.GRAPEVINE).count()) { + if (waterType.getFarmlandCount() > buildings.stream().filter(item1 -> item1 == Item.GRAPEVINE).count()) { System.out.println("You can't harvest"); return; } - for (int i = 0; i < waterType.getFarmlandAndVineyardCount() ; i++) { + for (int i = 0; i < waterType.getFarmlandCount() ; i++) { buildings.remove(Item.GRAPEVINE); player.getInventory().add(Item.GRAPEVINE); } @@ -216,6 +252,9 @@ public class Game { } } + /** + * Function that milks the cow + */ public void milkCow() { if (milkedToday >= 3) { strings.print("cantMilkToMuch"); @@ -230,6 +269,11 @@ public class Game { player.getInventory().add(Item.MILK); } + /** + * Function that takes item that you want to produce and then gives you coins based on the produced item and its amount + * + * @param item Item that is produced + */ public void produce(Item item) { if (!buildings.contains(item)) { strings.print("cantProduce", item); diff --git a/src/main/java/cz/jull/Player.java b/src/main/java/cz/jull/Player.java index f2c5815..94cb3fa 100644 --- a/src/main/java/cz/jull/Player.java +++ b/src/main/java/cz/jull/Player.java @@ -7,6 +7,9 @@ import lombok.Setter; import java.util.ArrayList; import java.util.List; +/** + * This class contains coins and inventory of the player + */ @Getter @Setter public class Player { diff --git a/src/main/java/cz/jull/items/Item.java b/src/main/java/cz/jull/items/Item.java index c119607..7213140 100644 --- a/src/main/java/cz/jull/items/Item.java +++ b/src/main/java/cz/jull/items/Item.java @@ -192,6 +192,7 @@ public enum Item implements Buildable { /** * Function that executes function that determines if the item can be built or not + * * @param game The game instance * @return Boolean if the item can be built */ @@ -205,6 +206,7 @@ public enum Item implements Buildable { /** * Returns human-readable name + * * @return Human-readable name */ @Override diff --git a/src/main/java/cz/jull/stats/Events.java b/src/main/java/cz/jull/stats/Events.java index 77c3dfc..7769de2 100644 --- a/src/main/java/cz/jull/stats/Events.java +++ b/src/main/java/cz/jull/stats/Events.java @@ -8,6 +8,10 @@ import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; +/** + * Enum that manages all events in the game, + * and it contains their names and actions + */ public enum Events { PEACEFUL_ROUND("Peaceful round. enjoy!", game -> { }), @@ -28,8 +32,14 @@ public enum Events { game.getForestType().setSize(game.getForestType().getSize() - 10); }); + /** + * Human-readable name + */ private final String name; + /** + * Function that contains an action for an event + */ @Getter private final Consumer action; @@ -38,6 +48,11 @@ public enum Events { this.action = action; } + /** + * Returns human-readable name + * + * @return Human-readable name + */ @Override public String toString() { return name; diff --git a/src/main/java/cz/jull/stats/surroundings/WaterType.java b/src/main/java/cz/jull/stats/surroundings/WaterType.java index 4d9c468..fe2b0ec 100644 --- a/src/main/java/cz/jull/stats/surroundings/WaterType.java +++ b/src/main/java/cz/jull/stats/surroundings/WaterType.java @@ -11,10 +11,13 @@ public enum WaterType { POND("Pond",10), RIVER("River",15); + /** + * Human-readable name + */ private final String name; @Getter - private final int farmlandAndVineyardCount; + private final int farmlandCount; public static WaterType getRandom() { Random random = new Random(); @@ -23,6 +26,11 @@ public enum WaterType { return types[random.nextInt(types.length)]; } + /** + * Returns human-readable name + * + * @return Human-readable name + */ @Override public String toString() { return name; diff --git a/src/main/java/cz/jull/tui/Cli.java b/src/main/java/cz/jull/tui/Cli.java index 63f21d6..33119eb 100644 --- a/src/main/java/cz/jull/tui/Cli.java +++ b/src/main/java/cz/jull/tui/Cli.java @@ -4,12 +4,24 @@ import java.io.Console; import java.util.List; import java.util.Scanner; +/** + * Class that manages command line + */ public class Cli { + /** + * Function that waits before user presses enter + */ public static void pressEnter() { Console console = System.console(); console.readLine(); } + /** + * Function that gives user a numbered options that can be chosen + * + * @param options The options + * @return The choice - 1 so it doesn't start with zero + */ public static int selectOptionIndex(List options) { Scanner scanner = new Scanner(System.in); int choice; diff --git a/src/main/java/cz/jull/tui/Menu.java b/src/main/java/cz/jull/tui/Menu.java index a7eee38..055ae30 100644 --- a/src/main/java/cz/jull/tui/Menu.java +++ b/src/main/java/cz/jull/tui/Menu.java @@ -15,23 +15,18 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +/** + * Class that manages the menu logic + */ @RequiredArgsConstructor public class Menu { private final Game game; - public static String loadResource(String fileName) { - try (InputStream inputStream = Menu.class.getClassLoader().getResourceAsStream(fileName)) { - if (inputStream == null) { - return "File not found: " + fileName; - } - - byte[] bytes = inputStream.readAllBytes(); - return new String(bytes, StandardCharsets.UTF_8); - } catch (IOException e) { - return null; - } - } - + /** + * Function that starts the menu + * + * @param day Game round + */ public void start(int day) { int index = Cli.selectOptionIndex(List.of( "See your inventory", @@ -147,6 +142,13 @@ public class Menu { start(day); } + /** + * Function that applies the action on the item + * + * @param item Item for which the action will be applied + * @param actions Actions for the item + * @param day Game round + */ private void actionMenu(Item item, List actions, int day) { List actionStrings = new ArrayList<>(actions.stream().map(Action::toString).toList()); actionStrings.add("Go back"); @@ -159,6 +161,12 @@ public class Menu { action.execute(game, item); } + /** + * Function that groups the same items together + * + * @param items Items that are being grouped + * @return The grouped items + */ private Map group(List items) { Map group = new HashMap<>(); for (Item item : items) { @@ -170,4 +178,23 @@ public class Menu { } return group; } + + /** + * Function that loads resource from resources + * + * @param fileName The file that is being loaded + * @return The resource + */ + public static String loadResource(String fileName) { + try (InputStream inputStream = Menu.class.getClassLoader().getResourceAsStream(fileName)) { + if (inputStream == null) { + return "File not found: " + fileName; + } + + byte[] bytes = inputStream.readAllBytes(); + return new String(bytes, StandardCharsets.UTF_8); + } catch (IOException e) { + return null; + } + } } diff --git a/src/main/java/cz/jull/tui/Strings.java b/src/main/java/cz/jull/tui/Strings.java index bdfe3dc..567521a 100644 --- a/src/main/java/cz/jull/tui/Strings.java +++ b/src/main/java/cz/jull/tui/Strings.java @@ -49,6 +49,7 @@ public class Strings { /** * Loads strings from json file + * * @return Instance of {@link Strings} * @throws IOException Is thrown when loading the strings failed */ @@ -59,8 +60,9 @@ public class Strings { } /** - * Generated AI + * Generated by AI * Prints out a String based on the prop + * * @param prop The name of the field */ public void print(String prop) { @@ -75,8 +77,9 @@ public class Strings { } /** - * Generated AI + * Generated by AI * Prints out a template with string inserted based on the prop + * * @param prop The name of the field * @param values Values for the template */