feat: a lot of changes

This commit is contained in:
2025-05-30 08:16:54 +02:00
parent 07c955b571
commit 2cbc692470
8 changed files with 137 additions and 23 deletions

View File

@@ -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);

View File

@@ -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 {

View File

@@ -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

View File

@@ -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<Game> 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;

View File

@@ -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;

View File

@@ -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<String> options) {
Scanner scanner = new Scanner(System.in);
int choice;

View File

@@ -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<Action> actions, int day) {
List<String> 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<Item, Integer> group(List<Item> items) {
Map<Item, Integer> 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;
}
}
}

View File

@@ -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
*/