diff --git a/src/main/java/cz/jull/Game.java b/src/main/java/cz/jull/Game.java index 73f312d..88f876f 100644 --- a/src/main/java/cz/jull/Game.java +++ b/src/main/java/cz/jull/Game.java @@ -10,9 +10,12 @@ import cz.jull.tui.Cli; import cz.jull.tui.Menu; import cz.jull.tui.Strings; import lombok.Getter; +import lombok.Setter; +import org.w3c.dom.events.Event; import java.util.ArrayList; import java.util.List; +import java.util.Random; @Getter public class Game { @@ -29,7 +32,8 @@ public class Game { private final Strings strings; - private final List buildings = new ArrayList<>(); + @Setter + private List buildings = new ArrayList<>(); private Market market; @@ -37,10 +41,18 @@ public class Game { private int milkedToday = 0; + private Events currentEvent; + public Game(Strings strings) { this.strings = strings; } + private Events getRandomEvent() { + Random random = new Random(); + + return events.get(random.nextInt(events.size())); + } + public void play() { strings.print("welcome"); Cli.pressEnter(); @@ -48,6 +60,10 @@ public class Game { Menu menu = new Menu(this); for (int i = 0; i < 10; i++) { + currentEvent = getRandomEvent(); + strings.print("event", currentEvent); + currentEvent.getAction().accept(this); + menu.start(i); long wheatSeedsCount = buildings.stream().filter(item -> item == Item.WHEAT_SEEDS).count(); for (int j = 0; j < wheatSeedsCount; j++) { diff --git a/src/main/java/cz/jull/stats/Events.java b/src/main/java/cz/jull/stats/Events.java index 202f767..232ffa8 100644 --- a/src/main/java/cz/jull/stats/Events.java +++ b/src/main/java/cz/jull/stats/Events.java @@ -1,8 +1,45 @@ package cz.jull.stats; +import cz.jull.Game; +import cz.jull.Item; +import lombok.Getter; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + public enum Events { - PEACEFUL_ROUND, - FLOOD, - ATTACK, - FIRE + PEACEFUL_ROUND("Peaceful round. enjoy!", game -> { + }), + FLOOD("Flood", game -> { + game.getStrings().print("eventFlood"); + game.setBuildings(new ArrayList<>( + game.getBuildings().stream().filter( + building -> !List.of(Item.WHEAT_FARMLAND, Item.HOPS_FARMLAND, Item.VINEYARD).contains(building) + ).toList()) + ); + }), + ATTACK("Attack", game -> { + game.getStrings().print("eventAttack"); + game.getPlayer().setCoins(game.getPlayer().getCoins() - 100); + }), + FIRE("Fire", game -> { + game.getStrings().print("eventFire"); + game.getForestType().setSize(game.getForestType().getSize() - 10); + }); + + private final String name; + + @Getter + private final Consumer action; + + Events(String name, Consumer action) { + this.name = name; + this.action = action; + } + + @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 75d4b24..48a27db 100644 --- a/src/main/java/cz/jull/tui/Cli.java +++ b/src/main/java/cz/jull/tui/Cli.java @@ -40,6 +40,7 @@ public class Cli { Scanner scanner = new Scanner(System.in); int choice; + System.out.println(); System.out.println("Chose an option: "); for (int i = 0; i < options.size(); i++) { System.out.println((i + 1) + ". " + options.get(i)); diff --git a/src/main/java/cz/jull/tui/Strings.java b/src/main/java/cz/jull/tui/Strings.java index bbef677..18c8750 100644 --- a/src/main/java/cz/jull/tui/Strings.java +++ b/src/main/java/cz/jull/tui/Strings.java @@ -41,6 +41,10 @@ public class Strings { private String currentDay; private String cantMilkToMuch; private String noTrees; + private String event; + private String eventFire; + private String eventFlood; + private String eventAttack; /** * Loads strings from json file @@ -53,6 +57,7 @@ public class Strings { } /** + * Generated AI * Prints out a String based on the prop * @param prop The name of the field */ @@ -68,6 +73,7 @@ public class Strings { } /** + * Generated 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 diff --git a/strings.json b/strings.json index 7b4dde1..505ffcf 100644 --- a/strings.json +++ b/strings.json @@ -26,5 +26,9 @@ "stats": "Your stats: ", "currentDay": "Day: {0}", "cantMilkToMuch": "You milked the cow already 3 times today!", - "noTrees": "You don't have any trees left!" + "noTrees": "You don't have any trees left!", + "event": "Event for this day: {0}", + "eventFire": "Fire! You lost 10 trees...", + "eventFlood": "Flood! You lost All of your farmlands... If you had them.", + "eventAttack": "Attack! Your villagers got robbed and you lost 100 coins..." } \ No newline at end of file