feat: Another string implementing and class Menu

This commit is contained in:
2025-05-21 10:35:29 +02:00
parent 1e898cc1f7
commit e603d9452c
5 changed files with 86 additions and 10 deletions

View File

@@ -6,33 +6,31 @@ import cz.jull.stats.surroundings.ForestType;
import cz.jull.stats.surroundings.PathType; import cz.jull.stats.surroundings.PathType;
import cz.jull.stats.surroundings.SoilType; import cz.jull.stats.surroundings.SoilType;
import cz.jull.stats.surroundings.WaterType; import cz.jull.stats.surroundings.WaterType;
import cz.jull.tui.Cli;
import cz.jull.tui.Menu;
import cz.jull.tui.Strings; import cz.jull.tui.Strings;
import lombok.Getter; import lombok.Getter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Game {
@Getter @Getter
public class Game {
private final Player player = new Player(); private final Player player = new Player();
private ForestType forestType; private ForestType forestType;
@Getter
private SoilType soilType; private SoilType soilType;
@Getter
private PathType pathType; private PathType pathType;
@Getter
private WaterType waterType; private WaterType waterType;
private final Strings strings; private final Strings strings;
@Getter
private final List<Item> buildings = new ArrayList<>(); private final List<Item> buildings = new ArrayList<>();
@Getter
private Market market; private Market market;
private final List<Events> events = new ArrayList<>(); private final List<Events> events = new ArrayList<>();
@@ -43,9 +41,12 @@ public class Game {
public void play() { public void play() {
strings.print("welcome"); strings.print("welcome");
Cli.pressEnter();
generateStats(); generateStats();
for (int i = 0; i < 10; i++) { Menu menu = new Menu(this);
for (int i = 0; i < 10; i++) {
menu.start();
} }
// TODO the whole game // TODO the whole game
} }
@@ -63,8 +64,10 @@ public class Game {
if (waterType == WaterType.WELL) { if (waterType == WaterType.WELL) {
buildings.add(Item.WELL); buildings.add(Item.WELL);
} }
System.out.println();
player.setCoins(1000); player.setCoins(1000);
strings.print("coins", player.getCoins());
market = new Market(this); market = new Market(this);

View File

@@ -6,10 +6,12 @@ import cz.jull.exceptions.ItemNotAvailableException;
import cz.jull.exceptions.NotSufficientsCoinsException; import cz.jull.exceptions.NotSufficientsCoinsException;
import cz.jull.stats.surroundings.PathType; import cz.jull.stats.surroundings.PathType;
import cz.jull.stats.surroundings.SoilType; import cz.jull.stats.surroundings.SoilType;
import lombok.Getter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@Getter
public class Market { public class Market {
private final List<Item> availableItems = new ArrayList<>(List.of( private final List<Item> availableItems = new ArrayList<>(List.of(
Item.AXE, Item.AXE,
@@ -24,6 +26,7 @@ public class Market {
game.getPlayer().setCoins(game.getPlayer().getCoins() + item.getPrice()); game.getPlayer().setCoins(game.getPlayer().getCoins() + item.getPrice());
game.getPlayer().getInventory().remove(item); game.getPlayer().getInventory().remove(item);
game.getStrings().print("marketSellMessage", item);
} }
public void buyItem(Game game, Item item) throws ItemNotAvailableException, NotSufficientsCoinsException { public void buyItem(Game game, Item item) throws ItemNotAvailableException, NotSufficientsCoinsException {
@@ -35,10 +38,10 @@ public class Market {
System.out.println(" "); //TODO System.out.println(" "); //TODO
return; return;
} }
System.out.println(availableItems);
if (availableItems.contains(item)) { if (availableItems.contains(item)) {
game.getPlayer().getInventory().add(item); game.getPlayer().getInventory().add(item);
game.getPlayer().setCoins(game.getPlayer().getCoins() - item.getPrice()); game.getPlayer().setCoins(game.getPlayer().getCoins() - item.getPrice());
game.getStrings().print("marketBuyMessage", item);
} else { } else {
if (List.of(Item.WHEAT_SEEDS, Item.HOPS_SEEDS, Item.GRAPEVINE_SEEDS, Item.WHEAT_FARMLAND, Item.HOPS_FARMLAND, Item.VINEYARD).contains(item)) { 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 < game.getWaterType().getFarmlandAndVineyardCount() ; i++) { for (int i = 0; i < game.getWaterType().getFarmlandAndVineyardCount() ; i++) {

View File

@@ -0,0 +1,58 @@
package cz.jull.tui;
import cz.jull.Game;
import cz.jull.Item;
import lombok.RequiredArgsConstructor;
import java.util.List;
@RequiredArgsConstructor
public class Menu {
private final Game game;
public void start() {
int index = Cli.selectOptionIndex(List.of(
"See your inventory",
"See what you have built",
"Go to Market",
"See your stats" ));
switch (index) {
case 0 -> {
}
case 1 -> {
for (Item building : game.getBuildings()) {
System.out.println(building);
}
}
case 2 -> {
game.getStrings().print("marketGreeting");
System.out.println();
int marketOption = Cli.selectOptionIndex(List.of(
"Buy Item",
"Sell Item",
"Go back"
));
switch (marketOption) {
case 0 -> {
game.getStrings().print("marketBuy");
List<String> availableItems = game.getMarket().getAvailableItems().stream().map(Item::toString).toList();
int itemIndex = Cli.selectOptionIndex(availableItems);
Item item = game.getMarket().getAvailableItems().get(itemIndex);
game.getMarket().buyItem(game, item);
}
case 1 -> {
game.getStrings().print("marketSell");
List<String> inventory = game.getPlayer().getInventory().stream().map(Item::toString).toList();
int itemIndex = Cli.selectOptionIndex(inventory);
Item item = game.getMarket().getAvailableItems().get(itemIndex);
game.getMarket().sellItem(game, item);
}
case 2 -> {
start();
}
}
}
}
}
}

View File

@@ -17,6 +17,12 @@ public class Strings {
private String soilType; private String soilType;
private String pathType; private String pathType;
private String waterType; private String waterType;
private String coins;
private String marketGreeting;
private String marketBuy;
private String marketBuyMessage;
private String marketSell;
private String marketSellMessage;
/** /**
* Loads strings from json file * Loads strings from json file

View File

@@ -1,7 +1,13 @@
{ {
"welcome": "Welcome", "welcome": "Welcome player! ",
"forestType": "Your forest size: {0}", "forestType": "Your forest size: {0}",
"soilType": "Your soil type size: {0}", "soilType": "Your soil type size: {0}",
"pathType": "Your path type: {0}", "pathType": "Your path type: {0}",
"waterType": "Your water type: {0}" "waterType": "Your water type: {0}",
"coins": "Amount of Coins: {0}",
"marketGreeting": "Welcome to Market!",
"marketBuy": "Pick which item you want to buy",
"marketBuyMessage": "You bought: {0}",
"marketSell": "Pick which item you want to sell",
"marketSellMessage": "You sold: {0}"
} }