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

View File

@@ -19,9 +19,9 @@ public enum Item implements Buildable {
HOPS_SEEDS("Hops seeds", true, 5), HOPS_SEEDS("Hops seeds", true, 5),
GRAPEVINE_SEEDS("Grapes seeds", true, 5), GRAPEVINE_SEEDS("Grapes seeds", true, 5),
VINEYARD("Vineyard", true, 100), VINEYARD("Vineyard", true, 7),
WHEAT_FARMLAND("Wheat farmland", true, 100), WHEAT_FARMLAND("Wheat farmland", true, 7),
HOPS_FARMLAND("Hops farmland", true, 100), HOPS_FARMLAND("Hops farmland", true, 7),
FENCE_WITH_COWS("Fence with cows", true, 100), FENCE_WITH_COWS("Fence with cows", true, 100),
@@ -39,7 +39,7 @@ public enum Item implements Buildable {
static { 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) { 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; return true;
} }
@@ -47,7 +47,7 @@ public enum Item implements Buildable {
return false; 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) { 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; return true;
} }
@@ -55,7 +55,7 @@ public enum Item implements Buildable {
return false; 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) { 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; return true;
} }
@@ -63,19 +63,7 @@ public enum Item implements Buildable {
return false; return false;
}; };
VINEYARD.function = game -> { WHEAT_FARMLAND.canBeBuiltFunction = 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;
}
if (game.getBuildings().stream().filter(building -> building == Item.WHEAT_FARMLAND).count() >= game.getWaterType().getFarmlandAndVineyardCount()) { if (game.getBuildings().stream().filter(building -> building == Item.WHEAT_FARMLAND).count() >= game.getWaterType().getFarmlandAndVineyardCount()) {
System.out.println(" "); // TODO System.out.println(" "); // TODO
return false; return false;
@@ -83,7 +71,23 @@ public enum Item implements Buildable {
return true; 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) { 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; return true;
} }
@@ -91,7 +95,7 @@ public enum Item implements Buildable {
return false; return false;
}; };
CHURCH.function = game -> { CHURCH.canBeBuiltFunction = game -> {
if (!game.getBuildings().contains(Item.CHURCH)) { if (!game.getBuildings().contains(Item.CHURCH)) {
return true; return true;
} }
@@ -99,7 +103,7 @@ public enum Item implements Buildable {
return false; return false;
}; };
VILLAGER_HOUSE.function = game -> { VILLAGER_HOUSE.canBeBuiltFunction = game -> {
if (game.getBuildings().contains(Item.CHURCH)) { if (game.getBuildings().contains(Item.CHURCH)) {
return true; return true;
} }
@@ -107,12 +111,7 @@ public enum Item implements Buildable {
return false; return false;
}; };
BAKERY.function = game -> { BAKERY.canBeBuiltFunction = game -> {
if (!game.getBuildings().contains(Item.VILLAGER_HOUSE)) {
System.out.println(" "); // TODO
return false;
}
if (!game.getBuildings().contains(Item.WHEAT_SEEDS)) { if (!game.getBuildings().contains(Item.WHEAT_SEEDS)) {
System.out.println(" "); // TODO System.out.println(" "); // TODO
return false; return false;
@@ -120,12 +119,7 @@ public enum Item implements Buildable {
return true; return true;
}; };
PUB.function = game -> { PUB.canBeBuiltFunction = game -> {
if (!game.getBuildings().contains(Item.VILLAGER_HOUSE)) {
System.out.println(" "); // TODO
return false;
}
if (!game.getBuildings().contains(Item.HOPS_SEEDS)) { if (!game.getBuildings().contains(Item.HOPS_SEEDS)) {
System.out.println(" "); // TODO System.out.println(" "); // TODO
return false; return false;
@@ -133,12 +127,7 @@ public enum Item implements Buildable {
return true; return true;
}; };
WINERY.function = game -> { WINERY.canBeBuiltFunction = game -> {
if (!game.getBuildings().contains(Item.VILLAGER_HOUSE)) {
System.out.println(" "); // TODO
return false;
}
if (!game.getBuildings().contains(Item.GRAPEVINE_SEEDS)) { if (!game.getBuildings().contains(Item.GRAPEVINE_SEEDS)) {
System.out.println(" "); // TODO System.out.println(" "); // TODO
return false; return false;
@@ -146,7 +135,25 @@ public enum Item implements Buildable {
return true; 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)) { if (!game.getBuildings().contains(Item.VILLAGER_HOUSE)) {
System.out.println(" "); // TODO System.out.println(" "); // TODO
return false; return false;
@@ -154,13 +161,18 @@ public enum Item implements Buildable {
return true; return true;
}; };
CHEESE_FACTORY.function = game -> { WHEAT_FARMLAND.canBeBoughtFunction = func;
if (!game.getBuildings().contains(Item.VILLAGER_HOUSE)) { HOPS_FARMLAND.canBeBoughtFunction = func;
System.out.println(" "); // TODO VINEYARD.canBeBoughtFunction = func;
return false;
} FENCE_WITH_COWS.canBeBoughtFunction = func;
return true;
}; BAKERY.canBeBoughtFunction = func;
PUB.canBeBoughtFunction = func;
WINERY.canBeBoughtFunction = func;
COAL_FACTORY.canBeBoughtFunction = func;
CHEESE_FACTORY.canBeBoughtFunction = func;
} }
@Getter @Getter
@@ -171,13 +183,17 @@ public enum Item implements Buildable {
@Getter @Getter
private final int price; 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) { Item(String name, boolean isPLaceable, int price) {
this.name = name; this.name = name;
this.isPLaceable = isPLaceable; this.isPLaceable = isPLaceable;
this.function = ignored -> isPLaceable; this.canBeBuiltFunction = ignored -> isPLaceable;
this.price = price; this.price = price;
this.canBeBoughtFunction = ignored -> true;
} }
@Override @Override
@@ -185,6 +201,11 @@ public enum Item implements Buildable {
if (!isPLaceable) { if (!isPLaceable) {
return false; 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; package cz.jull;
import cz.jull.exceptions.ItemNotAvailableException;
public class Main { public class Main {
public static void main(String[] args) { 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.Game;
import cz.jull.Item; import cz.jull.Item;
import cz.jull.Player; import cz.jull.Player;
import cz.jull.exceptions.ItemNotAvailableException;
import cz.jull.surroundings.PathType; import cz.jull.surroundings.PathType;
import cz.jull.surroundings.SoilType; import cz.jull.surroundings.SoilType;
import cz.jull.surroundings.WaterType; import cz.jull.surroundings.WaterType;
@@ -21,53 +22,23 @@ public class Market {
player.getInventory().remove(item); 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)) { if (availableItems.contains(item)) {
game.getPlayer().getInventory().add(item); game.getPlayer().getInventory().add(item);
game.getPlayer().setCoins(game.getPlayer().getCoins() - item.getPrice()); game.getPlayer().setCoins(game.getPlayer().getCoins() - item.getPrice());
} else { } 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++) { 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; return;
} }
throw new ItemNotAvailableException("Item is not available");
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");
} }
} }