feat: strings implementing and docs

This commit is contained in:
2025-05-19 19:19:06 +02:00
parent 683813145a
commit 1e898cc1f7
13 changed files with 194 additions and 60 deletions

View File

@@ -1,10 +1,11 @@
package cz.jull;
import cz.jull.market.Market;
import cz.jull.surroundings.ForestType;
import cz.jull.surroundings.PathType;
import cz.jull.surroundings.SoilType;
import cz.jull.surroundings.WaterType;
import cz.jull.stats.Events;
import cz.jull.stats.surroundings.ForestType;
import cz.jull.stats.surroundings.PathType;
import cz.jull.stats.surroundings.SoilType;
import cz.jull.stats.surroundings.WaterType;
import cz.jull.tui.Strings;
import lombok.Getter;
@@ -26,7 +27,7 @@ public class Game {
@Getter
private WaterType waterType;
private Strings strings;
private final Strings strings;
@Getter
private final List<Item> buildings = new ArrayList<>();
@@ -34,12 +35,14 @@ public class Game {
@Getter
private Market market;
private final List<Events> events = new ArrayList<>();
public Game(Strings strings) {
this.strings = strings;
}
public void play() {
System.out.println(strings.getWelcome());
strings.print("welcome");
generateStats();
for (int i = 0; i < 10; i++) {
@@ -49,20 +52,35 @@ public class Game {
public void generateStats() {
forestType = ForestType.getRandom();
System.out.println("forest: " + forestType);
strings.print("forestType", forestType);
soilType = SoilType.getRandom();
System.out.println("soil: " + soilType);
strings.print("soilType", soilType);
pathType = PathType.getRandom();
System.out.println("path: " + pathType);
strings.print("pathType", pathType);
waterType = WaterType.getRandom();
System.out.println("water: " + waterType);
strings.print("waterType", waterType);
if (waterType == WaterType.WELL) {
buildings.add(Item.WELL);
}
player.setCoins(1000);
market = new Market(this);
events.add(Events.PEACEFUL_ROUND);
if (forestType == ForestType.LARGE) {
events.add(Events.FIRE);
}
if (waterType == WaterType.RIVER) {
events.add(Events.FLOOD);
}
if (pathType == PathType.MAIN_PATH) {
events.add(Events.ATTACK);
}
}
public void build(Item item) {

View File

@@ -4,7 +4,13 @@ import lombok.Getter;
import java.util.function.Function;
/**
* This enum is managing all items whether the is item is placeable
* and the appropriate price.
* <p>
* Some enum constants have their own functions for determining if the item can be built or not.
* The same thing applies for buying.
*/
public enum Item implements Buildable {
AXE("Axe", false, 5),
@@ -174,6 +180,9 @@ public enum Item implements Buildable {
CHEESE_FACTORY.canBeBoughtFunction = func;
}
/**
* Human-readable name
*/
@Getter
private final String name;
@@ -182,8 +191,14 @@ public enum Item implements Buildable {
@Getter
private final int price;
/**
* Function that determines if the item can be built or not
*/
private Function<Game, Boolean> canBeBuiltFunction;
/**
* Function that determines if the item can be bought or not
*/
@Getter
private Function<Game, Boolean> canBeBoughtFunction;
@@ -195,6 +210,11 @@ public enum Item implements Buildable {
this.canBeBoughtFunction = ignored -> true;
}
/**
* Function that executes function that determines if the item can be built or not
* @param game
* @return Boolean if the item can be built
*/
@Override
public boolean canBuild(Game game) {
if (!isPlaceable) {
@@ -203,6 +223,10 @@ public enum Item implements Buildable {
return canBeBuiltFunction.apply(game);
}
/**
* Returns human-readable name
* @return Human-readable name
*/
@Override
public String toString() {
return name;

View File

@@ -5,8 +5,14 @@ import cz.jull.tui.Strings;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
Strings strings = Strings.load();
public static void main(String[] args) {
Strings strings = null;
try {
strings = Strings.load();
} catch (IOException e) {
System.out.println("Error: Locale string could not be loaded.");
System.exit(1);
}
Game game = new Game(strings);
game.play();
}

View File

@@ -4,8 +4,8 @@ import cz.jull.Game;
import cz.jull.Item;
import cz.jull.exceptions.ItemNotAvailableException;
import cz.jull.exceptions.NotSufficientsCoinsException;
import cz.jull.surroundings.PathType;
import cz.jull.surroundings.SoilType;
import cz.jull.stats.surroundings.PathType;
import cz.jull.stats.surroundings.SoilType;
import java.util.ArrayList;
import java.util.List;

View File

@@ -0,0 +1,8 @@
package cz.jull.stats;
public enum Events {
PEACEFUL_ROUND,
FLOOD,
ATTACK,
FIRE
}

View File

@@ -1,4 +1,4 @@
package cz.jull.surroundings;
package cz.jull.stats.surroundings;
import lombok.AllArgsConstructor;
import lombok.Getter;
@@ -8,9 +8,11 @@ import java.util.Random;
@AllArgsConstructor
public enum ForestType {
SMALL(25, 1),
MEDIUM(50, 3),
LARGE(100, 5);
SMALL("Small", 25, 1),
MEDIUM("Medium",50, 3),
LARGE("Large", 100, 5);
private String name;
@Getter
@Setter
@@ -25,4 +27,9 @@ public enum ForestType {
return types[random.nextInt(types.length)];
}
@Override
public String toString() {
return name;
}
}

View File

@@ -0,0 +1,27 @@
package cz.jull.stats.surroundings;
import java.util.Random;
public enum PathType {
DEAD_END("Dead end"),
SIDE_PATH("Side path"),
MAIN_PATH("Main path"),;
private final String name;
PathType(String name) {
this.name = name;
}
public static PathType getRandom() {
Random random = new Random();
PathType[] types = values();
return types[random.nextInt(types.length)];
}
@Override
public String toString() {
return name;
}
}

View File

@@ -0,0 +1,27 @@
package cz.jull.stats.surroundings;
import java.util.Random;
public enum SoilType {
STONY("Stony"),
LESS_FERTILE("Moderately fertile"),
FERTILE("Fertile"),;
private final String name;
public static SoilType getRandom() {
Random random = new Random();
SoilType[] types = values();
return types[random.nextInt(types.length)];
}
SoilType(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}

View File

@@ -1,4 +1,4 @@
package cz.jull.surroundings;
package cz.jull.stats.surroundings;
import lombok.AllArgsConstructor;
import lombok.Getter;
@@ -7,9 +7,11 @@ import java.util.Random;
@AllArgsConstructor
public enum WaterType {
WELL(5),
POND(10),
RIVER(15);
WELL("Well",5),
POND("Pond",10),
RIVER("River",15);
private final String name;
@Getter
private final int farmlandAndVineyardCount;
@@ -20,4 +22,9 @@ public enum WaterType {
return types[random.nextInt(types.length)];
}
@Override
public String toString() {
return name;
}
}

View File

@@ -1,16 +0,0 @@
package cz.jull.surroundings;
import java.util.Random;
public enum PathType {
DEAD_END,
SIDE_PATH,
MAIN_PATH;
public static PathType getRandom() {
Random random = new Random();
PathType[] types = values();
return types[random.nextInt(types.length)];
}
}

View File

@@ -1,16 +0,0 @@
package cz.jull.surroundings;
import java.util.Random;
public enum SoilType {
STONY,
LESS_FERTILE,
FERTILE;
public static SoilType getRandom() {
Random random = new Random();
SoilType[] types = values();
return types[random.nextInt(types.length)];
}
}

View File

@@ -5,20 +5,58 @@ import lombok.Getter;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
/**
* This class manages all strings in the game
*/
@Getter
public class Strings {
private String welcome;
private String forestType;
private String soilType;
private String pathType;
private String waterType;
/**
* Loads strings from json file
* @return Instance of {@link Strings}
* @throws IOException Is thrown when loading the strings failed
*/
public static Strings load() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(new File("./strings.json"), Strings.class);
}
public void test(String prop) throws NoSuchFieldException {
var field = this.getClass().getDeclaredField(prop);
field.setAccessible(true);
/**
* Prints out a String based on the prop
* @param prop The name of the field
*/
public void print(String prop) {
try {
var field = this.getClass().getDeclaredField(prop);
field.setAccessible(true);
Object value = field.get(this);
System.out.println(value);
} catch (NoSuchFieldException | IllegalAccessException e) {
System.out.println("Internal error: Could not found string");
}
}
/**
* Prints out a template with string inserted based on the prop
* @param prop The name of the field
* @param values Values for the template
*/
public void print(String prop, Object... values) {
try {
var field = this.getClass().getDeclaredField(prop);
field.setAccessible(true);
Object value = field.get(this);
String result = MessageFormat.format(value.toString(), values);
System.out.println(result);
} catch (NoSuchFieldException | IllegalAccessException e) {
System.out.println("Internal error: Could not found string");
}
}
}

View File

@@ -1,3 +1,7 @@
{
"welcome": "Welcome"
"welcome": "Welcome",
"forestType": "Your forest size: {0}",
"soilType": "Your soil type size: {0}",
"pathType": "Your path type: {0}",
"waterType": "Your water type: {0}"
}