feat: a lot of changes
This commit is contained in:
@@ -17,6 +17,9 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class that manages the whole game
|
||||||
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
public class Game {
|
public class Game {
|
||||||
|
|
||||||
@@ -47,6 +50,9 @@ public class Game {
|
|||||||
this.strings = strings;
|
this.strings = strings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function that starts the game
|
||||||
|
*/
|
||||||
public void play() {
|
public void play() {
|
||||||
strings.print("welcome");
|
strings.print("welcome");
|
||||||
Cli.pressEnter();
|
Cli.pressEnter();
|
||||||
@@ -99,6 +105,9 @@ public class Game {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function that generates the main stats for the player and the game
|
||||||
|
*/
|
||||||
public void generateStats() {
|
public void generateStats() {
|
||||||
forestType = ForestType.getRandom();
|
forestType = ForestType.getRandom();
|
||||||
strings.print("forestType", forestType);
|
strings.print("forestType", forestType);
|
||||||
@@ -143,6 +152,11 @@ public class Game {
|
|||||||
return events.get(random.nextInt(events.size()));
|
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) {
|
public void build(Item item) {
|
||||||
if (!player.getInventory().contains(item)) {
|
if (!player.getInventory().contains(item)) {
|
||||||
strings.print("cantBuild");
|
strings.print("cantBuild");
|
||||||
@@ -151,7 +165,7 @@ public class Game {
|
|||||||
|
|
||||||
if (item.canBuild(this)) {
|
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)) {
|
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);
|
buildings.add(item);
|
||||||
player.getInventory().remove(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() {
|
public void cutTrees() {
|
||||||
if (forestType.getSize() <= 0 ) {
|
if (forestType.getSize() <= 0 ) {
|
||||||
strings.print("noTrees");
|
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) {
|
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) {
|
switch (item) {
|
||||||
case WHEAT_FARMLAND -> {
|
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");
|
System.out.println("You can't harvest");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < waterType.getFarmlandAndVineyardCount() ; i++) {
|
for (int i = 0; i < waterType.getFarmlandCount() ; i++) {
|
||||||
buildings.remove(Item.WHEAT);
|
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()) {
|
if (waterType.getFarmlandCount() > buildings.stream().filter(item1 -> item1 == Item.HOPS).count()) {
|
||||||
System.out.println("You can't harvest");
|
System.out.println("You can't harvest");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < waterType.getFarmlandAndVineyardCount() ; i++) {
|
for (int i = 0; i < waterType.getFarmlandCount() ; i++) {
|
||||||
buildings.remove(Item.HOPS);
|
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()) {
|
if (waterType.getFarmlandCount() > buildings.stream().filter(item1 -> item1 == Item.GRAPEVINE).count()) {
|
||||||
System.out.println("You can't harvest");
|
System.out.println("You can't harvest");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < waterType.getFarmlandAndVineyardCount() ; i++) {
|
for (int i = 0; i < waterType.getFarmlandCount() ; i++) {
|
||||||
buildings.remove(Item.GRAPEVINE);
|
buildings.remove(Item.GRAPEVINE);
|
||||||
player.getInventory().add(Item.GRAPEVINE);
|
player.getInventory().add(Item.GRAPEVINE);
|
||||||
}
|
}
|
||||||
@@ -216,6 +252,9 @@ public class Game {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function that milks the cow
|
||||||
|
*/
|
||||||
public void milkCow() {
|
public void milkCow() {
|
||||||
if (milkedToday >= 3) {
|
if (milkedToday >= 3) {
|
||||||
strings.print("cantMilkToMuch");
|
strings.print("cantMilkToMuch");
|
||||||
@@ -230,6 +269,11 @@ public class Game {
|
|||||||
player.getInventory().add(Item.MILK);
|
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) {
|
public void produce(Item item) {
|
||||||
if (!buildings.contains(item)) {
|
if (!buildings.contains(item)) {
|
||||||
strings.print("cantProduce", item);
|
strings.print("cantProduce", item);
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ import lombok.Setter;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class contains coins and inventory of the player
|
||||||
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class Player {
|
public class Player {
|
||||||
|
|||||||
@@ -192,6 +192,7 @@ public enum Item implements Buildable {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Function that executes function that determines if the item can be built or not
|
* Function that executes function that determines if the item can be built or not
|
||||||
|
*
|
||||||
* @param game The game instance
|
* @param game The game instance
|
||||||
* @return Boolean if the item can be built
|
* @return Boolean if the item can be built
|
||||||
*/
|
*/
|
||||||
@@ -205,6 +206,7 @@ public enum Item implements Buildable {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns human-readable name
|
* Returns human-readable name
|
||||||
|
*
|
||||||
* @return Human-readable name
|
* @return Human-readable name
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -8,6 +8,10 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enum that manages all events in the game,
|
||||||
|
* and it contains their names and actions
|
||||||
|
*/
|
||||||
public enum Events {
|
public enum Events {
|
||||||
PEACEFUL_ROUND("Peaceful round. enjoy!", game -> {
|
PEACEFUL_ROUND("Peaceful round. enjoy!", game -> {
|
||||||
}),
|
}),
|
||||||
@@ -28,8 +32,14 @@ public enum Events {
|
|||||||
game.getForestType().setSize(game.getForestType().getSize() - 10);
|
game.getForestType().setSize(game.getForestType().getSize() - 10);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Human-readable name
|
||||||
|
*/
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function that contains an action for an event
|
||||||
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
private final Consumer<Game> action;
|
private final Consumer<Game> action;
|
||||||
|
|
||||||
@@ -38,6 +48,11 @@ public enum Events {
|
|||||||
this.action = action;
|
this.action = action;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns human-readable name
|
||||||
|
*
|
||||||
|
* @return Human-readable name
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return name;
|
return name;
|
||||||
|
|||||||
@@ -11,10 +11,13 @@ public enum WaterType {
|
|||||||
POND("Pond",10),
|
POND("Pond",10),
|
||||||
RIVER("River",15);
|
RIVER("River",15);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Human-readable name
|
||||||
|
*/
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final int farmlandAndVineyardCount;
|
private final int farmlandCount;
|
||||||
|
|
||||||
public static WaterType getRandom() {
|
public static WaterType getRandom() {
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
@@ -23,6 +26,11 @@ public enum WaterType {
|
|||||||
return types[random.nextInt(types.length)];
|
return types[random.nextInt(types.length)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns human-readable name
|
||||||
|
*
|
||||||
|
* @return Human-readable name
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return name;
|
return name;
|
||||||
|
|||||||
@@ -4,12 +4,24 @@ import java.io.Console;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class that manages command line
|
||||||
|
*/
|
||||||
public class Cli {
|
public class Cli {
|
||||||
|
/**
|
||||||
|
* Function that waits before user presses enter
|
||||||
|
*/
|
||||||
public static void pressEnter() {
|
public static void pressEnter() {
|
||||||
Console console = System.console();
|
Console console = System.console();
|
||||||
console.readLine();
|
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) {
|
public static int selectOptionIndex(List<String> options) {
|
||||||
Scanner scanner = new Scanner(System.in);
|
Scanner scanner = new Scanner(System.in);
|
||||||
int choice;
|
int choice;
|
||||||
|
|||||||
@@ -15,23 +15,18 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class that manages the menu logic
|
||||||
|
*/
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class Menu {
|
public class Menu {
|
||||||
private final Game game;
|
private final Game game;
|
||||||
|
|
||||||
public static String loadResource(String fileName) {
|
/**
|
||||||
try (InputStream inputStream = Menu.class.getClassLoader().getResourceAsStream(fileName)) {
|
* Function that starts the menu
|
||||||
if (inputStream == null) {
|
*
|
||||||
return "File not found: " + fileName;
|
* @param day Game round
|
||||||
}
|
*/
|
||||||
|
|
||||||
byte[] bytes = inputStream.readAllBytes();
|
|
||||||
return new String(bytes, StandardCharsets.UTF_8);
|
|
||||||
} catch (IOException e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void start(int day) {
|
public void start(int day) {
|
||||||
int index = Cli.selectOptionIndex(List.of(
|
int index = Cli.selectOptionIndex(List.of(
|
||||||
"See your inventory",
|
"See your inventory",
|
||||||
@@ -147,6 +142,13 @@ public class Menu {
|
|||||||
start(day);
|
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) {
|
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");
|
||||||
@@ -159,6 +161,12 @@ public class Menu {
|
|||||||
action.execute(game, item);
|
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) {
|
private Map<Item, Integer> group(List<Item> items) {
|
||||||
Map<Item, Integer> group = new HashMap<>();
|
Map<Item, Integer> group = new HashMap<>();
|
||||||
for (Item item : items) {
|
for (Item item : items) {
|
||||||
@@ -170,4 +178,23 @@ public class Menu {
|
|||||||
}
|
}
|
||||||
return group;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ public class Strings {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads strings from json file
|
* Loads strings from json file
|
||||||
|
*
|
||||||
* @return Instance of {@link Strings}
|
* @return Instance of {@link Strings}
|
||||||
* @throws IOException Is thrown when loading the strings failed
|
* @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
|
* Prints out a String based on the prop
|
||||||
|
*
|
||||||
* @param prop The name of the field
|
* @param prop The name of the field
|
||||||
*/
|
*/
|
||||||
public void print(String prop) {
|
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
|
* Prints out a template with string inserted based on the prop
|
||||||
|
*
|
||||||
* @param prop The name of the field
|
* @param prop The name of the field
|
||||||
* @param values Values for the template
|
* @param values Values for the template
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user