feat: a lot of changes

This commit is contained in:
2025-05-27 09:07:10 +02:00
parent 30c272bb9f
commit 3d6283dd77
6 changed files with 76 additions and 23 deletions

View File

@@ -3,7 +3,7 @@
<option name="myName" value="Project Default" /> <option name="myName" value="Project Default" />
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true"> <inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<Languages> <Languages>
<language minSize="68" name="Java" /> <language minSize="69" name="Java" />
</Languages> </Languages>
</inspection_tool> </inspection_tool>
</profile> </profile>

View File

@@ -35,6 +35,8 @@ public class Game {
private final List<Events> events = new ArrayList<>(); private final List<Events> events = new ArrayList<>();
private int milkedToday = 0;
public Game(Strings strings) { public Game(Strings strings) {
this.strings = strings; this.strings = strings;
} }
@@ -46,9 +48,26 @@ public class Game {
Menu menu = new Menu(this); Menu menu = new Menu(this);
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
menu.start(); menu.start(i);
long wheatSeedsCount = buildings.stream().filter(item -> item == Item.WHEAT_SEEDS).count();
for (int j = 0; j < wheatSeedsCount; j++) {
buildings.remove(Item.WHEAT_SEEDS);
buildings.add(Item.WHEAT);
}
long hopsSeedsCount = buildings.stream().filter(item -> item == Item.HOPS_SEEDS).count();
for (int k = 0; k < hopsSeedsCount; k++) {
buildings.remove(Item.HOPS_SEEDS);
buildings.add(Item.HOPS);
}
long grapevineSeedsCount = buildings.stream().filter(item -> item == Item.GRAPEVINE_SEEDS).count();
for (int l = 0; l < grapevineSeedsCount; l++) {
buildings.remove(Item.GRAPEVINE_SEEDS);
buildings.add(Item.GRAPEVINE);
}
milkedToday = 0;
} }
// TODO the whole game
} }
public void generateStats() { public void generateStats() {
@@ -109,6 +128,10 @@ public class Game {
} }
public void cutTrees() { public void cutTrees() {
if (forestType.getSize() <= 0 ) {
strings.print("noTrees");
return;
}
long axeCount = player.getInventory().stream().filter(item -> item == Item.AXE).count(); long axeCount = player.getInventory().stream().filter(item -> item == Item.AXE).count();
if (axeCount < forestType.getAxesNeeded()) { if (axeCount < forestType.getAxesNeeded()) {
strings.print("cantCutTrees", forestType.getAxesNeeded()); strings.print("cantCutTrees", forestType.getAxesNeeded());
@@ -124,22 +147,34 @@ public class Game {
public void harvest(Item item) { public void harvest(Item item) {
switch (item) { switch (item) {
case WHEAT_FARMLAND -> { case WHEAT_FARMLAND -> {
if (waterType.getFarmlandAndVineyardCount() > 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.getFarmlandAndVineyardCount() ; i++) {
buildings.remove(Item.WHEAT_SEEDS); buildings.remove(Item.WHEAT);
player.getInventory().add(Item.WHEAT); player.getInventory().add(Item.WHEAT);
} }
} }
case HOPS_FARMLAND -> { case HOPS_FARMLAND -> {
if (waterType.getFarmlandAndVineyardCount() > 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.getFarmlandAndVineyardCount() ; i++) {
buildings.remove(Item.HOPS_SEEDS); buildings.remove(Item.HOPS);
player.getInventory().add(Item.HOPS); player.getInventory().add(Item.HOPS);
} }
} }
case VINEYARD -> { case VINEYARD -> {
if (waterType.getFarmlandAndVineyardCount() > 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.getFarmlandAndVineyardCount() ; i++) {
buildings.remove(Item.GRAPEVINE_SEEDS); buildings.remove(Item.GRAPEVINE);
player.getInventory().add(Item.GRAPEVINE); player.getInventory().add(Item.GRAPEVINE);
} }
} }
@@ -147,11 +182,16 @@ public class Game {
} }
public void milkCow() { public void milkCow() {
if (milkedToday >= 3) {
strings.print("cantMilkToMuch");
return;
}
if (!buildings.contains(Item.FENCE_WITH_COWS)) { if (!buildings.contains(Item.FENCE_WITH_COWS)) {
strings.print("cantMilkCows"); strings.print("cantMilkCows");
return; return;
} }
milkedToday++;
player.getInventory().add(Item.MILK); player.getInventory().add(Item.MILK);
} }

View File

@@ -54,7 +54,7 @@ public class Cli {
if (choice >= 1 && choice <= options.size()) { if (choice >= 1 && choice <= options.size()) {
return choice - 1; return choice - 1;
} else { } else {
System.out.println("Invalid option, choose a number between 1 and" + options.size()); System.out.println("Invalid option, choose a number between 1 and " + options.size());
System.out.println(); System.out.println();
} }
} else { } else {

View File

@@ -30,52 +30,55 @@ public class Menu {
} }
} }
public void start() { public void start(int day) {
int index = Cli.selectOptionIndex(List.of( int index = Cli.selectOptionIndex(List.of(
"See your inventory", "See your inventory",
"See what you have built", "See what you have built",
"Go to Market", "Go to Market",
"See your stats" )); "See your stats",
"Next Day"));
switch (index) { switch (index) {
case 0 -> { case 0 -> {
List<Item> inventory = game.getPlayer().getInventory(); List<Item> inventory = game.getPlayer().getInventory();
Map<Item, Integer> grouped = group(inventory); Map<Item, Integer> grouped = group(inventory);
List<String> mapped = new ArrayList<>(grouped.keySet().stream().map(item -> grouped.get(item) + "x " + item).toList()); List<Item> keys = grouped.keySet().stream().toList();
List<String> mapped = new ArrayList<>(keys.stream().map(item -> grouped.get(item) + "x " + item).toList());
mapped.add("Go back"); mapped.add("Go back");
int itemIndex = Cli.selectOptionIndex(mapped); int itemIndex = Cli.selectOptionIndex(mapped);
if (itemIndex == mapped.size() - 1) { if (itemIndex == mapped.size() - 1) {
start(); start(day);
return; return;
} }
Item item = game.getPlayer().getInventory().get(itemIndex); Item item = keys.get(itemIndex);
String art = loadResource("item_art/" + item.name() + ".txt"); String art = loadResource("item_art/" + item.name() + ".txt");
System.out.println(art); System.out.println(art);
System.out.println(); System.out.println();
List<Action> actions = item.getActions(); List<Action> actions = item.getActions();
actionMenu(item, actions); actionMenu(item, actions, day);
} }
case 1 -> { case 1 -> {
List<Item> buildings = game.getBuildings(); List<Item> buildings = game.getBuildings();
Map<Item, Integer> grouped = group(buildings); Map<Item, Integer> grouped = group(buildings);
List<Item> keys = grouped.keySet().stream().toList();
List<String> mapped = new ArrayList<>(grouped.keySet().stream().map(item -> grouped.get(item) + "x " + item).toList()); List<String> mapped = new ArrayList<>(grouped.keySet().stream().map(item -> grouped.get(item) + "x " + item).toList());
mapped.add("Go back"); mapped.add("Go back");
int itemIndex = Cli.selectOptionIndex(mapped); int itemIndex = Cli.selectOptionIndex(mapped);
if (itemIndex == buildings.size() - 1) { if (itemIndex == mapped.size() - 1) {
start(); start(day);
return; return;
} }
Item item = game.getBuildings().get(itemIndex); Item item = keys.get(itemIndex);
String art = loadResource("item_art/" + item.name() + ".txt"); String art = loadResource("item_art/" + item.name() + ".txt");
System.out.println(art); System.out.println(art);
System.out.println(); System.out.println();
List<Action> actions = item.getBuildActions(); List<Action> actions = item.getBuildActions();
actionMenu(item, actions); actionMenu(item, actions, day);
} }
case 2 -> { case 2 -> {
game.getStrings().print("marketGreeting"); game.getStrings().print("marketGreeting");
@@ -105,7 +108,7 @@ public class Menu {
Item item = game.getMarket().getAvailableItems().get(itemIndex); Item item = game.getMarket().getAvailableItems().get(itemIndex);
game.getMarket().sellItem(game, item); game.getMarket().sellItem(game, item);
} }
case 2 -> start(); case 2 -> start(day);
} }
} }
case 3 -> { case 3 -> {
@@ -121,15 +124,20 @@ public class Menu {
game.getStrings().print("forestSize", game.getForestType().getSize()); game.getStrings().print("forestSize", game.getForestType().getSize());
System.out.println(); System.out.println();
} }
case 4 -> {
game.getStrings().print("currentDay", day + 2);
return;
} }
} }
start(day);
}
private void actionMenu(Item item, List<Action> actions) { private void actionMenu(Item item, List<Action> actions, int day) {
List<String> actionStrings = new ArrayList<>(actions.stream().map(Action::toString).toList()); List<String> actionStrings = new ArrayList<>(actions.stream().map(Action::toString).toList());
actionStrings.add("Go back"); actionStrings.add("Go back");
int actionIndex = Cli.selectOptionIndex(actionStrings); int actionIndex = Cli.selectOptionIndex(actionStrings);
if (actionIndex == actionStrings.size() - 1) { if (actionIndex == actionStrings.size() - 1) {
start(); start(day);
return; return;
} }
Action action = actions.get(actionIndex); Action action = actions.get(actionIndex);

View File

@@ -38,7 +38,9 @@ public class Strings {
private String cantBuy; private String cantBuy;
private String forestSize; private String forestSize;
private String stats; private String stats;
private String currentDay;
private String cantMilkToMuch;
private String noTrees;
/** /**
* Loads strings from json file * Loads strings from json file

View File

@@ -23,5 +23,8 @@
"cantProduce": "You can`t produce when you dont have {0}", "cantProduce": "You can`t produce when you dont have {0}",
"cantSell": "You can`t sell this item! You don`t have it!", "cantSell": "You can`t sell this item! You don`t have it!",
"forestSize": "The size of your forest is {0} trees", "forestSize": "The size of your forest is {0} trees",
"stats": "Your stats: " "stats": "Your stats: ",
"currentDay": "Day: {0}",
"cantMilkToMuch": "You milked the cow already 3 times today!",
"noTrees": "You don't have any trees left!"
} }