feat: added a lot of changes

This commit is contained in:
2025-05-05 11:01:36 +02:00
parent bafa77db70
commit 49bdbc989f
5 changed files with 118 additions and 90 deletions

View File

@@ -28,13 +28,18 @@ public class Game {
@Getter
private final List<Item> buildings = new ArrayList<>();
@Getter
private final Market market = new Market(this);
public void generateStats() {
forestType = ForestType.getRandom();
System.out.println("forest: " + forestType);
soilType = SoilType.getRandom();
System.out.println("soil: " + soilType);
pathType = PathType.getRandom();
System.out.println("path: " + pathType);
waterType = WaterType.getRandom();
System.out.println("water: " + waterType);
if (waterType == WaterType.WELL) {
buildings.add(Item.WELL);

View File

@@ -19,9 +19,9 @@ public enum Item implements Buildable {
HOPS_SEEDS("Hops seeds", true, 5),
GRAPEVINE_SEEDS("Grapes seeds", true, 5),
VINEYARD("Vineyard", true, 100),
WHEAT_FARMLAND("Wheat farmland", true, 100),
HOPS_FARMLAND("Hops farmland", true, 100),
VINEYARD("Vineyard", true, 7),
WHEAT_FARMLAND("Wheat farmland", true, 7),
HOPS_FARMLAND("Hops farmland", true, 7),
FENCE_WITH_COWS("Fence with cows", true, 100),
@@ -39,7 +39,7 @@ public enum Item implements Buildable {
static {
WHEAT_SEEDS.function = game -> {
WHEAT_SEEDS.canBeBuiltFunction = game -> {
if (game.getBuildings().stream().filter(building -> building == Item.WHEAT_FARMLAND).count() >= game.getBuildings().stream().filter(building -> building == Item.WHEAT_SEEDS || building == Item.HOPS_SEEDS).count() + 1) {
return true;
}
@@ -47,7 +47,7 @@ public enum Item implements Buildable {
return false;
};
HOPS_SEEDS.function = game -> {
HOPS_SEEDS.canBeBuiltFunction = game -> {
if (game.getBuildings().stream().filter(building -> building == Item.WHEAT_FARMLAND).count() >= game.getBuildings().stream().filter(building -> building == Item.WHEAT_SEEDS || building == Item.HOPS_SEEDS).count() + 1) {
return true;
}
@@ -55,7 +55,7 @@ public enum Item implements Buildable {
return false;
};
GRAPEVINE_SEEDS.function = game -> {
GRAPEVINE_SEEDS.canBeBuiltFunction = game -> {
if (game.getBuildings().stream().filter(building -> building == Item.VINEYARD).count() >= game.getBuildings().stream().filter(building -> building == Item.GRAPEVINE_SEEDS || building == Item.HOPS_SEEDS).count() + 1) {
return true;
}
@@ -63,19 +63,7 @@ public enum Item implements Buildable {
return false;
};
VINEYARD.function = game -> {
if (game.getBuildings().stream().filter(building -> building == Item.VINEYARD).count() >= game.getWaterType().getFarmlandAndVineyardCount()) {
return true;
}
System.out.println(" "); //TODO
return false;
};
WHEAT_FARMLAND.function = game -> {
if (!game.getBuildings().contains(Item.VILLAGER_HOUSE)) {
System.out.println(" "); // TODO
return false;
}
WHEAT_FARMLAND.canBeBuiltFunction = game -> {
if (game.getBuildings().stream().filter(building -> building == Item.WHEAT_FARMLAND).count() >= game.getWaterType().getFarmlandAndVineyardCount()) {
System.out.println(" "); // TODO
return false;
@@ -83,7 +71,23 @@ public enum Item implements Buildable {
return true;
};
FENCE_WITH_COWS.function = game -> {
VINEYARD.canBeBuiltFunction = game -> {
if (game.getBuildings().stream().filter(building -> building == Item.VINEYARD).count() >= game.getWaterType().getFarmlandAndVineyardCount()) {
return true;
}
System.out.println(" "); //TODO
return false;
};
HOPS_FARMLAND.canBeBuiltFunction = game -> {
if (game.getBuildings().stream().filter(building -> building == Item.HOPS_FARMLAND).count() >= game.getWaterType().getFarmlandAndVineyardCount()) {
return true;
}
System.out.println(" "); //TODO
return false;
};
FENCE_WITH_COWS.canBeBuiltFunction = game -> {
if (game.getBuildings().stream().filter(building -> building == Item.VILLAGER_HOUSE).count() >= (game.getBuildings().stream().filter(building -> building == Item.FENCE_WITH_COWS).count() + 1) * 3) {
return true;
}
@@ -91,7 +95,7 @@ public enum Item implements Buildable {
return false;
};
CHURCH.function = game -> {
CHURCH.canBeBuiltFunction = game -> {
if (!game.getBuildings().contains(Item.CHURCH)) {
return true;
}
@@ -99,7 +103,7 @@ public enum Item implements Buildable {
return false;
};
VILLAGER_HOUSE.function = game -> {
VILLAGER_HOUSE.canBeBuiltFunction = game -> {
if (game.getBuildings().contains(Item.CHURCH)) {
return true;
}
@@ -107,12 +111,7 @@ public enum Item implements Buildable {
return false;
};
BAKERY.function = game -> {
if (!game.getBuildings().contains(Item.VILLAGER_HOUSE)) {
System.out.println(" "); // TODO
return false;
}
BAKERY.canBeBuiltFunction = game -> {
if (!game.getBuildings().contains(Item.WHEAT_SEEDS)) {
System.out.println(" "); // TODO
return false;
@@ -120,12 +119,7 @@ public enum Item implements Buildable {
return true;
};
PUB.function = game -> {
if (!game.getBuildings().contains(Item.VILLAGER_HOUSE)) {
System.out.println(" "); // TODO
return false;
}
PUB.canBeBuiltFunction = game -> {
if (!game.getBuildings().contains(Item.HOPS_SEEDS)) {
System.out.println(" "); // TODO
return false;
@@ -133,12 +127,7 @@ public enum Item implements Buildable {
return true;
};
WINERY.function = game -> {
if (!game.getBuildings().contains(Item.VILLAGER_HOUSE)) {
System.out.println(" "); // TODO
return false;
}
WINERY.canBeBuiltFunction = game -> {
if (!game.getBuildings().contains(Item.GRAPEVINE_SEEDS)) {
System.out.println(" "); // TODO
return false;
@@ -146,7 +135,25 @@ public enum Item implements Buildable {
return true;
};
COAL_FACTORY.function = game -> {
CHEESE_FACTORY.canBeBuiltFunction = game -> {
if (!game.getBuildings().contains(Item.FENCE_WITH_COWS)) {
System.out.println(" "); // TODO
return false;
}
return true;
};
}
static {
VILLAGER_HOUSE.canBeBoughtFunction = game -> {
if (!game.getBuildings().contains(Item.CHURCH)) {
System.out.println(" "); //TODO
return false;
}
return true;
};
Function<Game, Boolean> func = game -> {
if (!game.getBuildings().contains(Item.VILLAGER_HOUSE)) {
System.out.println(" "); // TODO
return false;
@@ -154,13 +161,18 @@ public enum Item implements Buildable {
return true;
};
CHEESE_FACTORY.function = game -> {
if (!game.getBuildings().contains(Item.VILLAGER_HOUSE)) {
System.out.println(" "); // TODO
return false;
}
return true;
};
WHEAT_FARMLAND.canBeBoughtFunction = func;
HOPS_FARMLAND.canBeBoughtFunction = func;
VINEYARD.canBeBoughtFunction = func;
FENCE_WITH_COWS.canBeBoughtFunction = func;
BAKERY.canBeBoughtFunction = func;
PUB.canBeBoughtFunction = func;
WINERY.canBeBoughtFunction = func;
COAL_FACTORY.canBeBoughtFunction = func;
CHEESE_FACTORY.canBeBoughtFunction = func;
}
@Getter
@@ -171,13 +183,17 @@ public enum Item implements Buildable {
@Getter
private final int price;
private Function<Game, Boolean> function;
private Function<Game, Boolean> canBeBuiltFunction;
@Getter
private Function<Game, Boolean> canBeBoughtFunction;
Item(String name, boolean isPLaceable, int price) {
this.name = name;
this.isPLaceable = isPLaceable;
this.function = ignored -> isPLaceable;
this.canBeBuiltFunction = ignored -> isPLaceable;
this.price = price;
this.canBeBoughtFunction = ignored -> true;
}
@Override
@@ -185,6 +201,11 @@ public enum Item implements Buildable {
if (!isPLaceable) {
return false;
}
return function.apply(game);
return canBeBuiltFunction.apply(game);
}
@Override
public String toString() {
return name;
}
}

View File

@@ -1,7 +1,31 @@
package cz.jull;
import cz.jull.exceptions.ItemNotAvailableException;
public class Main {
public static void main(String[] args) {
Game game = new Game();
game.generateStats();
System.out.println(game.getPlayer().getCoins());
try {
game.getMarket().buyItem(game, Item.CHURCH);
} catch (ItemNotAvailableException e) {
throw new RuntimeException(e);
}
System.out.println(game.getPlayer().getCoins());
System.out.println(game.getPlayer().getInventory());
try {
game.getMarket().buyItem(game, Item.WHEAT_FARMLAND);
} catch (ItemNotAvailableException e) {
throw new RuntimeException(e);
}
System.out.println(game.getPlayer().getInventory());
System.out.println(game.getPlayer().getCoins());
}
}

View File

@@ -0,0 +1,7 @@
package cz.jull.exceptions;
public class ItemNotAvailableException extends RuntimeException {
public ItemNotAvailableException(String message) {
super(message);
}
}

View File

@@ -3,6 +3,7 @@ package cz.jull.market;
import cz.jull.Game;
import cz.jull.Item;
import cz.jull.Player;
import cz.jull.exceptions.ItemNotAvailableException;
import cz.jull.surroundings.PathType;
import cz.jull.surroundings.SoilType;
import cz.jull.surroundings.WaterType;
@@ -21,53 +22,23 @@ public class Market {
player.getInventory().remove(item);
}
public void buyItem(Game game, Item item) throws Exception {
public void buyItem(Game game, Item item) throws ItemNotAvailableException {
if (!item.getCanBeBoughtFunction().apply(game)) {
System.out.println(" "); //TODO
return;
}
if (availableItems.contains(item)) {
game.getPlayer().getInventory().add(item);
game.getPlayer().setCoins(game.getPlayer().getCoins() - item.getPrice());
} else {
if (item == Item.WHEAT_SEEDS) {
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++) {
game.getPlayer().getInventory().add(Item.WHEAT_SEEDS);
game.getPlayer().getInventory().add(item);
game.getPlayer().setCoins(game.getPlayer().getCoins() - item.getPrice());
}
return;
}
if (item == Item.HOPS_SEEDS) {
for (int i = 0; i < game.getWaterType().getFarmlandAndVineyardCount() ; i++) {
game.getPlayer().getInventory().add(Item.HOPS_SEEDS);
}
return;
}
if (item == Item.GRAPEVINE_SEEDS) {
for (int i = 0; i < game.getWaterType().getFarmlandAndVineyardCount() ; i++) {
game.getPlayer().getInventory().add(Item.GRAPEVINE_SEEDS);
}
return;
}
if (item == Item.WHEAT_FARMLAND) {
for (int i = 0; i < game.getWaterType().getFarmlandAndVineyardCount() ; i++) {
game.getPlayer().getInventory().add(Item.WHEAT_FARMLAND);
}
return;
}
if (item == Item.HOPS_FARMLAND) {
for (int i = 0; i < game.getWaterType().getFarmlandAndVineyardCount() ; i++) {
game.getPlayer().getInventory().add(Item.HOPS_FARMLAND);
}
return;
}
if (item == Item.VINEYARD) {
for (int i = 0; i < game.getWaterType().getFarmlandAndVineyardCount() ; i++) {
game.getPlayer().getInventory().add(Item.VINEYARD);
}
return;
}
throw new Exception("Item is not available");
throw new ItemNotAvailableException("Item is not available");
}
}