-
Notifications
You must be signed in to change notification settings - Fork 0
Dev #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Dev #1
Changes from all commits
713ed05
53e7fab
1f635b2
c7c6d97
a7f3db5
8c2311a
1100e90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,23 @@ | ||
| # Island | ||
| ## Description | ||
|
|
||
| This program is a model of an island with variable parameters, consisting of an array of locations (for example, 100x20 cells). The locations will be filled with plants and animals. Animals can: | ||
|
|
||
| - There are plants and/or other animals (if there is suitable food in their location), | ||
|
|
||
| - Move around (to neighboring locations), | ||
|
|
||
| - Reproduce (if there is a pair in their location), | ||
|
|
||
| - Starve to death or be eaten. | ||
|
|
||
| ## Menu | ||
|  | ||
| You can very flexibly change the simulation settings in the provided menu | ||
|
|
||
|  | ||
|
|
||
| Also you can watch the simulation in the console | ||
|
|
||
|  | ||
|  | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package efanov; | ||
|
|
||
|
|
||
| import efanov.simulation.SimulationStarter; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| SimulationStarter simulationStarter = new SimulationStarter(); | ||
| simulationStarter.start(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package efanov.constants; | ||
|
|
||
| import efanov.simulation.SimulationSettings; | ||
|
|
||
| public class Constant { | ||
| public static final String RESOURCES_PROBABILITY_YAML = "/Users/effgang/dev/Island/src/efanov/resources/probability.yaml"; | ||
| public static final String ANIMAL_SETTINGS_PROPERTIES = "/Users/effgang/dev/Island/src/efanov/resources/animal-settings.properties"; | ||
| public static final int PLANT_COUNT_TO_ADD = 10; | ||
| public static final String END = "***Simulation is over***"; | ||
| public static final String DELIMITER = "-".repeat(20); | ||
| public static final String SIMULATION_STARTING = "Simulation starting..."; | ||
| public static final String CHANGE_DATA = """ | ||
| Would you like to change another parameter ? | ||
| 1 - Yes | ||
| 2 - No"""; | ||
| public static final String INVALID_ANSWER = "Invalid answer "; | ||
| public static final String WRITE_1_OR_2 = " please write 1 or 2"; | ||
| public static final String IT_S_NOT_A_NUMBER = "It's not a number: "; | ||
| public static final String ENTER_THE_PARAMETER_VALUE_FOR = "Please re-enter the parameter value for "; | ||
| public static final String WRITE_THE_PARAMETER_VALUE = "Write the parameter value: "; | ||
| public static final String WRITE_NUMBER_OF_PARAMETER = "Write parameter number: "; | ||
| public static final String GREETING = "We are glad to welcome you in our simulation, choose the parameters that you want to change"; | ||
| public static final String INVALID_NUMBER_PLEASE_TRY_AGAIN = "Invalid number, please try again"; | ||
| public static final String MAP_WIDTH = "Map width"; | ||
| public static final String MAP_HEIGHT = "Map height"; | ||
| public static final String COUNT_ENTITIES_IN_ONE_LOCATION = "Count entities in one location"; | ||
| public static final String LIFE_CYCLE_OF_SIMULATION = "Life cycle of simulation (tact)"; | ||
| public static final SimulationSettings settings = new SimulationSettings(); | ||
| public static final String START_MENU = """ | ||
| 1. Map width | ||
| 2. Map height | ||
| 3. Count entities in one location | ||
| 4. Life cycle of simulation (tact) | ||
| 5. Continue | ||
| """; | ||
| public static final String DATA = "Initial data:\n" + | ||
| "width -> " + settings.getWidth() + "\n" + | ||
| "height -> " + settings.getHeight() + "\n" + | ||
| "Count entities in one location -> " + settings.getEntityCountOnCage() + "\n" + | ||
| "Life cycle of simulation (tact) -> " + settings.getLifeCycleTact() + "\n"; | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package efanov.dialog; | ||
|
|
||
| import efanov.constants.Constant; | ||
| import efanov.simulation.SimulationSettings; | ||
|
|
||
| import java.util.InputMismatchException; | ||
| import java.util.Scanner; | ||
|
|
||
| public class UserDialog { | ||
| public boolean exit = false; | ||
| public String paramType = null; | ||
|
|
||
| public void showMenuAndChangeParameters(SimulationSettings settings) { | ||
| try (Scanner scanner = new Scanner(System.in)) { | ||
| System.out.println(Constant.DATA); | ||
| System.out.println(Constant.GREETING); | ||
| while (true) { | ||
| System.out.println(Constant.DELIMITER); | ||
| System.out.print(Constant.START_MENU); | ||
| System.out.println(Constant.DELIMITER); | ||
| System.out.print(Constant.WRITE_NUMBER_OF_PARAMETER); | ||
| int ans; | ||
| try { | ||
| ans = scanner.nextInt(); | ||
| if (ans < 0 || ans > 5) { | ||
| System.out.println(Constant.INVALID_NUMBER_PLEASE_TRY_AGAIN); | ||
| continue; | ||
| } | ||
| } catch (InputMismatchException e) { | ||
| String badValue = scanner.nextLine(); | ||
| System.out.println(Constant.IT_S_NOT_A_NUMBER + badValue); | ||
| continue; | ||
| } | ||
|
|
||
| if (ans != 5) { | ||
| System.out.print(Constant.WRITE_THE_PARAMETER_VALUE); | ||
| } | ||
| if (ans == 1) { | ||
| paramType = Constant.MAP_WIDTH; | ||
| } | ||
| if (ans == 2) { | ||
| paramType = Constant.MAP_HEIGHT; | ||
| } | ||
| if (ans == 3) { | ||
| paramType = Constant.COUNT_ENTITIES_IN_ONE_LOCATION; | ||
| } | ||
| if (ans == 4) { | ||
| paramType = Constant.LIFE_CYCLE_OF_SIMULATION; | ||
| } | ||
| if (ans == 5) { | ||
| break; | ||
| } | ||
|
Comment on lines
+22
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это можно вынести в отдельный сервис ввода данных, чтобы было более читаемо и был нормальный код. Плюс можно вынести в enum и сделать вот так |
||
|
|
||
| int param; | ||
| while (true) { | ||
| try { | ||
| param = scanner.nextInt(); | ||
| } catch (InputMismatchException e) { | ||
| String badValue = scanner.nextLine(); | ||
| System.out.println(Constant.IT_S_NOT_A_NUMBER + badValue + "\n" + Constant.ENTER_THE_PARAMETER_VALUE_FOR + paramType); | ||
| continue; | ||
| } | ||
| break; | ||
| } | ||
|
|
||
|
|
||
| switch (ans) { | ||
| case 1 -> settings.setWidth(param); | ||
| case 2 -> settings.setHeight(param); | ||
| case 3 -> settings.setEntityCountOnCage(param); | ||
| case 4 -> settings.setLifeCycleTact(param); | ||
| default -> System.out.println(Constant.INVALID_NUMBER_PLEASE_TRY_AGAIN); | ||
| } | ||
|
|
||
| System.out.println(Constant.CHANGE_DATA); | ||
|
|
||
| while (true) { | ||
| try { | ||
| int solution = scanner.nextInt(); | ||
| if (solution == 1) { | ||
| break; | ||
| } else if (solution == 2) { | ||
| exit = true; | ||
| break; | ||
| } else { | ||
| System.out.println(Constant.INVALID_NUMBER_PLEASE_TRY_AGAIN); | ||
| } | ||
|
|
||
| } catch (InputMismatchException e) { | ||
| String badValue = scanner.nextLine(); | ||
| System.out.println(Constant.INVALID_ANSWER + badValue + Constant.WRITE_1_OR_2); | ||
| } | ||
|
|
||
| } | ||
| if (exit) { | ||
| break; | ||
| } | ||
| } | ||
| } finally { | ||
| System.out.println(Constant.SIMULATION_STARTING); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package efanov.entities; | ||
|
|
||
| import efanov.entities.animals.Animal; | ||
| import efanov.entities.animals.herbivores.*; | ||
| import efanov.entities.animals.predators.*; | ||
| import efanov.entities.plants.Herb; | ||
| import efanov.properties.processing.Processor; | ||
|
|
||
| import java.lang.reflect.Constructor; | ||
| import java.lang.reflect.InvocationTargetException; | ||
|
|
||
| public class EntitiesFactory { | ||
| Processor processor = new Processor(); | ||
|
|
||
| public Entity createEntity(EntityType entityType) { | ||
| return switch (entityType) { | ||
| case WOLF -> getAnimal(entityType, Wolf.class); | ||
| case BEAR -> getAnimal(entityType, Bear.class); | ||
| case BOA -> getAnimal(entityType, Boa.class); | ||
| case FOX -> getAnimal(entityType, Fox.class); | ||
| case EAGLE -> getAnimal(entityType, Eagle.class); | ||
| case HORSE -> getAnimal(entityType, Horse.class); | ||
| case GOAT -> getAnimal(entityType, Goat.class); | ||
| case SHEEP -> getAnimal(entityType, Sheep.class); | ||
| case MOUSE -> getAnimal(entityType, Mouse.class); | ||
| case RABBIT -> getAnimal(entityType, Rabbit.class); | ||
| case DEER -> getAnimal(entityType, Deer.class); | ||
| case WILDBOAR -> getAnimal(entityType, WildBoar.class); | ||
| case DUCK -> getAnimal(entityType, Duck.class); | ||
| case BUFFALO -> getAnimal(entityType, Buffalo.class); | ||
| case CATERPILLAR -> getAnimal(entityType, Caterpillar.class); | ||
| case PLANT -> new Herb(processor.getEmojiFromFile(entityType.name()), | ||
| processor.getWeightFromFile(entityType.name()), | ||
| processor.getMaxCountFromFile(entityType.name())); | ||
| }; | ||
| } | ||
|
|
||
| private Animal getAnimal(EntityType entityType, Class<? extends Animal> clazz) { | ||
| Constructor<? extends Animal> constructor; | ||
| try { | ||
|
|
||
| constructor = clazz.getDeclaredConstructor(String.class, Double.class, Integer.class, Integer.class, Double.class); | ||
|
|
||
| return constructor.newInstance(processor.getEmojiFromFile(entityType.name()), | ||
| processor.getWeightFromFile(entityType.name()), | ||
| processor.getMaxCountFromFile(entityType.name()), | ||
| processor.getSpeedFromFile(entityType.name()), | ||
| processor.getSaturationFromFile(entityType.name())); | ||
| } catch (InstantiationException | IllegalAccessException | InvocationTargetException | | ||
| NoSuchMethodException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package efanov.entities; | ||
|
|
||
| import lombok.*; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @AllArgsConstructor(access = AccessLevel.PROTECTED) | ||
| @EqualsAndHashCode | ||
| public abstract class Entity { | ||
| private String emoji; | ||
| private double weight; | ||
| private int maxCountOnLocation; | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package efanov.entities; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public enum EntityType { | ||
| WOLF, | ||
| BOA, | ||
| FOX, | ||
| BEAR, | ||
| EAGLE, | ||
| HORSE, | ||
| DEER, | ||
| RABBIT, | ||
| MOUSE, | ||
| GOAT, | ||
| SHEEP, | ||
| WILDBOAR, | ||
| BUFFALO, | ||
| DUCK, | ||
| CATERPILLAR, | ||
| PLANT | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package efanov.entities.animals; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public enum Action { | ||
| EAT(30), | ||
| REPRODUCE(30), | ||
| MOVE(30), | ||
| SLEEP(10); | ||
|
|
||
| private final int actionChance; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package efanov.entities.animals; | ||
|
|
||
| import efanov.entities.Entity; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| import java.util.concurrent.ThreadLocalRandom; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| public abstract class Animal extends Entity { | ||
| private static final int BOUND = 100; | ||
| protected int speed; | ||
| protected double saturation; | ||
| protected double health; | ||
|
|
||
| public Animal(String emoji, Double weight, Integer maxCountOnLocation, Integer speed, Double saturation) { | ||
| super(emoji, weight, maxCountOnLocation); | ||
| this.speed = speed; | ||
| this.saturation = saturation; | ||
| this.health = saturation; | ||
| } | ||
|
|
||
| public abstract Animal reproduce(); | ||
|
|
||
| public void eat(Entity food) { | ||
| if (food.getWeight() + this.getHealth() >= this.getSaturation()) { | ||
| this.setHealth(this.getSaturation()); | ||
| } else { | ||
| double saturationAfterEating = this.getHealth() + food.getWeight(); | ||
| this.setHealth(saturationAfterEating); | ||
| } | ||
| } | ||
|
|
||
| public Direction chooseDirection() { | ||
| return Direction.values()[ThreadLocalRandom.current().nextInt(Direction.values().length)]; | ||
| } | ||
|
|
||
| public Action chooseAction() { | ||
| var action = Action.values()[ThreadLocalRandom.current().nextInt(Action.values().length)]; | ||
| var isActiveAction = ThreadLocalRandom.current().nextInt(BOUND) < action.getActionChance(); | ||
| return isActiveAction ? action : Action.SLEEP; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package efanov.entities.animals; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public enum Direction { | ||
| LEFT, | ||
| RIGHT, | ||
| UP, | ||
| DOWN | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package efanov.entities.animals; | ||
|
|
||
| import efanov.constants.Constant; | ||
| import lombok.Getter; | ||
| import org.yaml.snakeyaml.Yaml; | ||
|
|
||
| import java.io.FileInputStream; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.InputStream; | ||
| import java.util.Map; | ||
|
|
||
| @Getter | ||
| public class EatingMap { | ||
| InputStream inputStream; | ||
|
|
||
| { | ||
| try { | ||
| inputStream = new FileInputStream(Constant.RESOURCES_PROBABILITY_YAML); | ||
| } catch (FileNotFoundException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| Yaml yaml = new Yaml(); | ||
| Map<String, Map<String, Integer>> eatingProbability = yaml.load(inputStream); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package efanov.entities.animals.herbivores; | ||
|
|
||
| public class Buffalo extends Herbivore { | ||
|
|
||
| public Buffalo(String emoji, Double weight, Integer maxCountOnLocation, Integer speed, Double saturation) { | ||
| super(emoji, weight, maxCountOnLocation, speed, saturation); | ||
| } | ||
|
|
||
| @Override | ||
| public Buffalo reproduce() { | ||
| return new Buffalo(getEmoji(), getWeight(), getMaxCountOnLocation(), speed, saturation); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
за реадми лайк