feat: strings implementing and docs
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
package cz.jull;
|
package cz.jull;
|
||||||
|
|
||||||
import cz.jull.market.Market;
|
import cz.jull.market.Market;
|
||||||
import cz.jull.surroundings.ForestType;
|
import cz.jull.stats.Events;
|
||||||
import cz.jull.surroundings.PathType;
|
import cz.jull.stats.surroundings.ForestType;
|
||||||
import cz.jull.surroundings.SoilType;
|
import cz.jull.stats.surroundings.PathType;
|
||||||
import cz.jull.surroundings.WaterType;
|
import cz.jull.stats.surroundings.SoilType;
|
||||||
|
import cz.jull.stats.surroundings.WaterType;
|
||||||
import cz.jull.tui.Strings;
|
import cz.jull.tui.Strings;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
@@ -26,7 +27,7 @@ public class Game {
|
|||||||
@Getter
|
@Getter
|
||||||
private WaterType waterType;
|
private WaterType waterType;
|
||||||
|
|
||||||
private Strings strings;
|
private final Strings strings;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final List<Item> buildings = new ArrayList<>();
|
private final List<Item> buildings = new ArrayList<>();
|
||||||
@@ -34,12 +35,14 @@ public class Game {
|
|||||||
@Getter
|
@Getter
|
||||||
private Market market;
|
private Market market;
|
||||||
|
|
||||||
|
private final List<Events> events = new ArrayList<>();
|
||||||
|
|
||||||
public Game(Strings strings) {
|
public Game(Strings strings) {
|
||||||
this.strings = strings;
|
this.strings = strings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void play() {
|
public void play() {
|
||||||
System.out.println(strings.getWelcome());
|
strings.print("welcome");
|
||||||
generateStats();
|
generateStats();
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
|
|
||||||
@@ -49,20 +52,35 @@ public class Game {
|
|||||||
|
|
||||||
public void generateStats() {
|
public void generateStats() {
|
||||||
forestType = ForestType.getRandom();
|
forestType = ForestType.getRandom();
|
||||||
System.out.println("forest: " + forestType);
|
strings.print("forestType", forestType);
|
||||||
soilType = SoilType.getRandom();
|
soilType = SoilType.getRandom();
|
||||||
System.out.println("soil: " + soilType);
|
strings.print("soilType", soilType);
|
||||||
pathType = PathType.getRandom();
|
pathType = PathType.getRandom();
|
||||||
System.out.println("path: " + pathType);
|
strings.print("pathType", pathType);
|
||||||
waterType = WaterType.getRandom();
|
waterType = WaterType.getRandom();
|
||||||
System.out.println("water: " + waterType);
|
strings.print("waterType", waterType);
|
||||||
|
|
||||||
if (waterType == WaterType.WELL) {
|
if (waterType == WaterType.WELL) {
|
||||||
buildings.add(Item.WELL);
|
buildings.add(Item.WELL);
|
||||||
}
|
}
|
||||||
|
|
||||||
player.setCoins(1000);
|
player.setCoins(1000);
|
||||||
|
|
||||||
market = new Market(this);
|
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) {
|
public void build(Item item) {
|
||||||
|
|||||||
@@ -4,7 +4,13 @@ import lombok.Getter;
|
|||||||
|
|
||||||
import java.util.function.Function;
|
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 {
|
public enum Item implements Buildable {
|
||||||
AXE("Axe", false, 5),
|
AXE("Axe", false, 5),
|
||||||
|
|
||||||
@@ -174,6 +180,9 @@ public enum Item implements Buildable {
|
|||||||
CHEESE_FACTORY.canBeBoughtFunction = func;
|
CHEESE_FACTORY.canBeBoughtFunction = func;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Human-readable name
|
||||||
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
@@ -182,8 +191,14 @@ public enum Item implements Buildable {
|
|||||||
@Getter
|
@Getter
|
||||||
private final int price;
|
private final int price;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function that determines if the item can be built or not
|
||||||
|
*/
|
||||||
private Function<Game, Boolean> canBeBuiltFunction;
|
private Function<Game, Boolean> canBeBuiltFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function that determines if the item can be bought or not
|
||||||
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
private Function<Game, Boolean> canBeBoughtFunction;
|
private Function<Game, Boolean> canBeBoughtFunction;
|
||||||
|
|
||||||
@@ -195,6 +210,11 @@ public enum Item implements Buildable {
|
|||||||
this.canBeBoughtFunction = ignored -> true;
|
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
|
@Override
|
||||||
public boolean canBuild(Game game) {
|
public boolean canBuild(Game game) {
|
||||||
if (!isPlaceable) {
|
if (!isPlaceable) {
|
||||||
@@ -203,6 +223,10 @@ public enum Item implements Buildable {
|
|||||||
return canBeBuiltFunction.apply(game);
|
return canBeBuiltFunction.apply(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns human-readable name
|
||||||
|
* @return Human-readable name
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return name;
|
return name;
|
||||||
|
|||||||
@@ -5,8 +5,14 @@ import cz.jull.tui.Strings;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) {
|
||||||
Strings strings = Strings.load();
|
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 game = new Game(strings);
|
||||||
game.play();
|
game.play();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import cz.jull.Game;
|
|||||||
import cz.jull.Item;
|
import cz.jull.Item;
|
||||||
import cz.jull.exceptions.ItemNotAvailableException;
|
import cz.jull.exceptions.ItemNotAvailableException;
|
||||||
import cz.jull.exceptions.NotSufficientsCoinsException;
|
import cz.jull.exceptions.NotSufficientsCoinsException;
|
||||||
import cz.jull.surroundings.PathType;
|
import cz.jull.stats.surroundings.PathType;
|
||||||
import cz.jull.surroundings.SoilType;
|
import cz.jull.stats.surroundings.SoilType;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|||||||
8
src/main/java/cz/jull/stats/Events.java
Normal file
8
src/main/java/cz/jull/stats/Events.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package cz.jull.stats;
|
||||||
|
|
||||||
|
public enum Events {
|
||||||
|
PEACEFUL_ROUND,
|
||||||
|
FLOOD,
|
||||||
|
ATTACK,
|
||||||
|
FIRE
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package cz.jull.surroundings;
|
package cz.jull.stats.surroundings;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
@@ -8,9 +8,11 @@ import java.util.Random;
|
|||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public enum ForestType {
|
public enum ForestType {
|
||||||
SMALL(25, 1),
|
SMALL("Small", 25, 1),
|
||||||
MEDIUM(50, 3),
|
MEDIUM("Medium",50, 3),
|
||||||
LARGE(100, 5);
|
LARGE("Large", 100, 5);
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@@ -25,4 +27,9 @@ public enum ForestType {
|
|||||||
|
|
||||||
return types[random.nextInt(types.length)];
|
return types[random.nextInt(types.length)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
27
src/main/java/cz/jull/stats/surroundings/PathType.java
Normal file
27
src/main/java/cz/jull/stats/surroundings/PathType.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
27
src/main/java/cz/jull/stats/surroundings/SoilType.java
Normal file
27
src/main/java/cz/jull/stats/surroundings/SoilType.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package cz.jull.surroundings;
|
package cz.jull.stats.surroundings;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
@@ -7,9 +7,11 @@ import java.util.Random;
|
|||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public enum WaterType {
|
public enum WaterType {
|
||||||
WELL(5),
|
WELL("Well",5),
|
||||||
POND(10),
|
POND("Pond",10),
|
||||||
RIVER(15);
|
RIVER("River",15);
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final int farmlandAndVineyardCount;
|
private final int farmlandAndVineyardCount;
|
||||||
@@ -20,4 +22,9 @@ public enum WaterType {
|
|||||||
|
|
||||||
return types[random.nextInt(types.length)];
|
return types[random.nextInt(types.length)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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)];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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)];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,20 +5,58 @@ import lombok.Getter;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class manages all strings in the game
|
||||||
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
public class Strings {
|
public class Strings {
|
||||||
private String welcome;
|
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 {
|
public static Strings load() throws IOException {
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
return objectMapper.readValue(new File("./strings.json"), Strings.class);
|
return objectMapper.readValue(new File("./strings.json"), Strings.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void test(String prop) throws NoSuchFieldException {
|
/**
|
||||||
var field = this.getClass().getDeclaredField(prop);
|
* Prints out a String based on the prop
|
||||||
field.setAccessible(true);
|
* @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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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}"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user