Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileNotFoundException;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Target;
- import java.lang.invoke.MethodHandles;
- import java.util.ArrayList;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.lang.reflect.*;
- import java.util.Random;
- import java.util.Scanner;
- import java.util.stream.Collectors.*;
- enum ActionType {
- CREATE, DELETE, MOVE, REPRODUCE, PRINT, TURN_ROUND
- }
- class Controller {
- boolean quit;
- Dispatcher dispatcher;
- ArrayList<Action> actions;
- Controller(Dispatcher dispatcher){
- this.dispatcher = dispatcher;
- this.quit = false;
- }
- Controller() {
- this.quit = false;
- }
- void startup(Config config){
- for(Action action : config.actions){
- this.dispatcher.dispatch(action);
- }
- }
- void show(){
- this.dispatcher.dispatch(new Action(ActionType.PRINT));
- }
- void makeMove(){
- this.dispatcher.dispatch(new Action(ActionType.TURN_ROUND));
- }
- }
- abstract class DefaultConfig{
- int gridSize = 25;
- int plantsCount = 60;
- int carnivoreCount = 15;
- int herbivoreCount = 25;
- ArrayList<Action> actions;
- }
- class Config extends DefaultConfig{
- Config(){
- this.actions = new ArrayList<Action>();
- }
- void setActions(OrganismFactory factory, ZoneManager zones){
- for(int i =0; i< plantsCount; i++){
- this.actions.add( new Action( factory.getPlant(), zones.getRandom("free") ) );
- }
- for(int i=0; i<carnivoreCount; i++){
- this.actions.add(new Action(factory.getCarnivore(), zones.getRandom("free")));
- }
- for(int i=0; i< herbivoreCount; i++){
- this.actions.add(new Action(factory.getHerbivore(), zones.getRandom("free")));
- }
- }
- }
- abstract class JavaWorldGenerator{
- static int generate() {
- int offset = 1;
- return (0 - offset) + (int) (Math.random() * ((offset - (0 - offset)) + 1));
- }
- static int generate(int offset) {
- return (0 - offset) + (int) (Math.random() * ((offset - (0 - offset)) + 1));
- }
- static int generate(int min, int max) {
- Random r = new Random();
- if (min > max) {
- return generate(max, min);
- }
- return r.nextInt((max - min) + 1) + min;
- }
- }
- class RandomGenerator extends JavaWorldGenerator {
- private RandomGenerator(){
- }
- }
- // REQUIRED : RANDOMG GENERATOR, ZONEMANAGER, ORGANISMSMANAGER, DISPATCHER
- public class World{
- public static void main(String[] args){
- Dispatcher dispatcher = new Dispatcher();
- Controller controller = new Controller(dispatcher);
- ZoneManager zoneManager = new ZoneManager(dispatcher);
- OrganismManager organismManager = new OrganismManager(dispatcher);
- OrganismFactory factory = new OrganismFactory();
- factory.supply(Grass.getSupply());
- factory.supply(Tree.getSupply());
- factory.supply(Wolf.getSupply());
- factory.supply(Sheep.getSupply());
- Config config = new Config();
- config.setActions(factory, zoneManager);
- controller.startup(config);
- // organismManager.organisms.stream().forEach(el->{System.out.println(el.zone);});
- // while(true){
- // System.out.print("\033[H\033[2J");
- // zoneManager.printOnly();
- // try {
- // Thread.sleep(2000);
- // } catch (Exception e) {
- // }
- // }
- }
- }
- class Clock extends Thread{
- }
- class Zone {
- char x;
- int y;
- boolean isOccupied ;
- boolean hasAnimal;
- boolean hasPlant;
- Animal animal;
- Plant plant;
- Organism organism;
- Zone(char x, int y) {
- this.x = x;
- this.y = y;
- this.isOccupied = false;
- this.hasAnimal = false;
- this.hasPlant = false;
- this.animal = null;
- this.plant = null;
- this.organism = null;
- }
- String appear(){
- if(this.organism!= null){
- if(this.y<10) return String.format(this.organism.id+" ");
- else return String.format(this.organism.id+" ");
- }
- else{
- return this.toString();
- }
- };
- String showOrganism(){
- if(this.organism!=null){
- if(this.y<10) return String.format(this.organism.id+ " ");
- else return String.format(this.organism.id+" ");
- }
- else{
- return " ";
- }
- }
- void subscribe(Organism organism){
- this.isOccupied = true;
- this.organism = organism;
- }
- @Override
- public String toString() {
- // TODO Auto-generated method stub
- if (this.y < 10)
- return String.format("%d%s ", y, x);
- else
- return String.format("%d%s ", y, x);
- }
- String Alert(){
- String ANSI_RED = "\u001B[31m";
- String ANSI_GREEN = "\u001B[32m";
- String ANSI_YELLOW = "\u001b[33m";
- String ANSI_BLUE = "\u001b[34m";
- String ANSI_MAGENTA = "\u001b[35m";
- String ANSI_CYAN = "\u001b[36m";
- String ANSI_WHITE = "\u001b[37m";
- String ANSI_RESET = "\u001B[0m";
- if(this.y <10)
- return ANSI_RED + y + x +" "+ ANSI_RESET;
- return ANSI_RED + y + x +" "+ ANSI_RESET;
- }
- int[][] getMatrix() {
- int[][] result = { { 1, 2, 3 }, { 12, 3, 4 }, { 1, 2, 3 } };
- return result;
- }
- }
- class ZoneManager {
- ArrayList<Zone> zones;
- ArrayList<Zone> empty;
- Dispatcher dispatcher;
- int GRIDSIZE = 25;
- ZoneManager(Dispatcher dispatcher) {
- this.dispatcher = dispatcher;
- this.dispatcher.subscribe(this);
- this.zones = new ArrayList<Zone>();
- this.empty = new ArrayList<Zone>();
- for (int y = GRIDSIZE; y > 0; y--)
- {
- for (int x = 65; x <= 65 + GRIDSIZE; x++)
- {
- Zone content = new Zone((char) x, y);
- this.zones.add(content);
- this.empty.add(content);
- this.dispatcher.subscribe(content);
- }
- }
- }
- void printOrganisms(){
- for (Zone el : this.zones) {
- System.out.print(el.appear());
- if ((int) el.x - 65 == this.GRIDSIZE)
- System.out.println();
- }
- }
- void printOnly(){
- for (Zone el : this.zones) {
- System.out.print(el.showOrganism());
- if ((int) el.x - 65 == this.GRIDSIZE)
- System.out.println();
- }
- }
- Zone getZone(Zone zone){
- return this.zones.stream().filter(x-> x.hashCode()==zone.hashCode() ).collect(Collectors.toCollection(ArrayList::new)).get(0) ;
- }
- void subscribe(Organism organism, Zone zone){
- getZone(zone).subscribe(organism);
- }
- Zone getRandom(String extra){
- return this.empty.get(RandomGenerator.generate(0, GRIDSIZE * GRIDSIZE));
- }
- Zone getRandom() {
- return this.zones.get(RandomGenerator.generate(0, GRIDSIZE * GRIDSIZE));
- }
- Zone selectRandom(){
- Zone zone = this.zones.get(RandomGenerator.generate(0,GRIDSIZE* GRIDSIZE) ) ;
- for (Zone el : this.zones) {
- if(zone.hashCode() == el.hashCode() ){
- System.out.print(el.Alert());
- }
- else{
- System.out.print(el);
- }
- if ((int) el.x - 65 == this.GRIDSIZE)
- System.out.println();
- }
- return zone;
- }
- ArrayList<Zone> selectRandom(int count){
- ArrayList<Zone> selected = new ArrayList<Zone>();
- for(int i=0; i<count; i++){
- Zone zone = this.zones.get(RandomGenerator.generate(0, GRIDSIZE * GRIDSIZE));
- selected.add(zone);
- }
- for ( Zone el : this.zones){
- if ( selected.contains(el) ){
- System.out.print(el.Alert() );
- }
- else{
- System.out.print(el);
- }
- if ((int) el.x - 65 == this.GRIDSIZE)
- System.out.println();
- }
- return selected;
- }
- void printRows() {
- int count = 0;
- for (Zone el : this.zones) {
- System.out.print(el);
- if ((int) el.x - 65 == this.GRIDSIZE)
- System.out.println();
- }
- }
- }
- class OrganismManager{
- ArrayList<Organism> organisms;
- ArrayList<Animal> animalSpecies;
- ArrayList<Plant> plantSpecies;
- Dispatcher dispatcher ;
- OrganismManager(){
- }
- OrganismManager(Dispatcher dispatcher){
- this.dispatcher = dispatcher;
- this.dispatcher.subscribe(this);
- this.animalSpecies = new ArrayList<Animal>();
- this.plantSpecies = new ArrayList<Plant>();
- this.organisms = new ArrayList<Organism>();
- }
- void add(Organism organism){
- this.organisms.add(organism);
- }
- void makeMove(){
- for(Organism organism : this.organisms){
- organism.makeMove();
- }
- }
- }
- abstract class Organism {
- static String ANSI_RED = "\u001B[31m";
- static String ANSI_GREEN = "\u001B[32m";
- static Digestion digestion;
- static String ANSI_YELLOW = "\u001b[33m";
- static String ANSI_BLUE = "\u001b[34m";
- static String ANSI_MAGENTA = "\u001b[35m";
- static String ANSI_CYAN = "\u001b[36m";
- static String ANSI_WHITE = "\u001b[37m";
- static String ANSI_RESET = "\u001B[0m";
- static HashMap<String, String> colors = new HashMap<String, String>();
- static{
- colors.put("red", ANSI_RED);
- colors.put("green", ANSI_GREEN);
- colors.put("yellow", ANSI_YELLOW);
- colors.put("blue", ANSI_BLUE);
- colors.put("magenta", ANSI_MAGENTA);
- colors.put("cyan", ANSI_CYAN);
- colors.put("white", ANSI_WHITE);
- colors.put("reset", ANSI_RESET);
- }
- //static Digestion digestion;
- String id;
- Zone zone;
- Dispatcher dispatcher;
- static ArrayList<Organism> prototypes;
- Organism(Dispatcher dispatcher){
- this.dispatcher = dispatcher;
- this.dispatcher.subscribe(this);
- }
- void setZone(Zone zone){
- this.zone = zone;
- }
- Organism(){
- }
- void makeMove(){
- System.out.println(this);
- }
- String getId(){
- return this.id;
- }
- static int range;
- }
- abstract class Animal extends Organism {
- Animal(Dispatcher dispatcher ){
- super(dispatcher );
- }
- Animal(){}
- void occupy(Zone zone){
- this.zone = zone;
- }
- }
- abstract class Carnivore extends Animal{
- static Digestion digestion = Digestion.CARNIVORE;
- static String classColor = colors.get("red");
- Carnivore(){
- super();
- }
- Carnivore(Dispatcher dispatcher){
- super(dispatcher);
- }
- static int getRange() {
- return range;
- }
- static Digestion getDigestion() {
- return digestion;
- }
- }
- abstract class Herbivore extends Animal {
- static Digestion digestion = Digestion.HERBIVORE;
- static String classColor = colors.get("blue");
- Herbivore(){
- super();
- }
- Herbivore(Dispatcher dispatcher){
- super(dispatcher);
- }
- static int getRange() {
- return range;
- }
- static Digestion getDigestion() {
- return digestion;
- }
- }
- abstract class Plant extends Organism implements Listener{
- static Digestion digestion = Digestion.NONE;
- static String classColor = colors.get("green");
- Plant(){
- }
- Plant(Dispatcher dispatcher){
- super(dispatcher);
- }
- static int getRange() {
- return range;
- }
- static Digestion getDigestion() {
- return digestion;
- }
- }
- //@FunctionalInterface
- interface Supplier<T>{
- T get(Zone zone);
- }
- enum Digestion{
- CARNIVORE,
- HERBIVORE,
- NONE
- }
- class Supply{
- Supplier<Organism> supplier;
- Digestion digestion;
- int range;
- String className;
- Supply(Supplier<Organism> supplier, Digestion digestion, int range, String className){
- this.supplier = supplier;
- this.digestion = digestion;
- this.range = range;
- this.className = className;
- }
- }
- class Wolf extends Carnivore{
- static int range = 4;
- Wolf(Dispatcher dispatcher){
- super(dispatcher );
- this.range = 4;
- this.id = classColor + "WF" + colors.get("reset");
- }
- Wolf() {
- this.id = classColor + "WF" + colors.get("reset");
- this.range = 4;
- System.out.println();
- }
- Wolf(Zone zone){
- this.zone = zone;
- this.zone.subscribe(this);
- this.id = classColor + "WF" + colors.get("reset");
- }
- private static Supplier<Organism> getSupplier(){
- Supplier<Organism> mkWolf = (Zone zone) -> new Wolf(zone);
- return mkWolf;
- }
- static Supply getSupply(){
- return new Supply(getSupplier(), getDigestion(), getRange(), MethodHandles.lookup().lookupClass().getSimpleName() );
- }
- }
- class Sheep extends Herbivore{
- int range =2 ;
- Sheep(){
- this.id = classColor + "SP" + colors.get("reset");
- this.range = 2;
- //System.out.print("Creating sheep");
- }
- Sheep(Zone zone){
- this.zone = zone;
- this.id = classColor + "SP" + colors.get("reset");
- this.zone.subscribe(this);
- }
- private static Supplier<Organism> getSupplier() {
- Supplier<Organism> mkSheep = (Zone zone) -> new Sheep(zone);
- return mkSheep;
- }
- static Supply getSupply() {
- return new Supply(getSupplier(), getDigestion(), getRange(), MethodHandles.lookup().lookupClass().getSimpleName() );
- }
- }
- class Grass extends Plant {
- int range = 0;
- Grass(){
- this.id = classColor + "GS" + colors.get("reset");
- this.range = 0;
- }
- Grass(Dispatcher dispatcher) {
- super(dispatcher);
- this.id = classColor + "GS" + colors.get("reset");
- this.range = 0;
- }
- Grass(Zone zone){
- this.zone = zone;
- this.zone.subscribe(this);
- this.id = classColor + "GS" + colors.get("reset");
- }
- static Supplier<Organism> getSupplier() {
- Supplier<Organism> mkGrass = (Zone zone) -> new Grass(zone);
- return mkGrass;
- }
- static Supply getSupply() {
- return new Supply(getSupplier(), getDigestion(), getRange(), MethodHandles.lookup().lookupClass().getSimpleName() );
- }
- }
- class Tree extends Plant{
- int range = 0;
- Tree(){
- this.id = classColor + "TR" + colors.get("reset");
- this.range = 0;
- }
- Tree(Dispatcher dispatcher){
- super(dispatcher);
- this.id = classColor + "TR" + colors.get("reset");
- this.range = 0;
- }
- Tree(Zone zone){
- this.zone = zone;
- this.zone.subscribe(this);
- this.id = classColor + "TR" + colors.get("reset");
- }
- static public Supplier<Organism> getSupplier() {
- Supplier<Organism> mkTree = (Zone zone) -> new Tree(zone);
- return mkTree;
- }
- static Supply getSupply() {
- return new Supply(getSupplier(), getDigestion(), getRange(), MethodHandles.lookup().lookupClass().getSimpleName() );
- }
- }
- interface Listener{
- }
- interface AbstractFactory<Type>{
- Type create(String AnimalType);
- }
- class Dispatcher {
- ArrayList<Organism> organisms;
- ArrayList<Zone> zones;
- Action actions;
- ZoneManager zoneManager;
- OrganismManager organismManager;
- Dispatcher() {
- this.actions = new Action(this);
- this.organisms = new ArrayList<Organism>();
- this.zones = new ArrayList<Zone>();
- }
- void subscribe(ZoneManager zoneManager){
- this.zoneManager = zoneManager;
- }
- void subscribe(OrganismManager organismManager){
- this.organismManager = organismManager;
- }
- void subscribe(Organism organism) {
- this.organisms.add(organism);
- }
- void subscribe(Zone zone) {
- this.zones.add(zone);
- }
- void dispatch(Action action){
- switch(action.type){
- case CREATE:
- this.organismManager.add(action.supply.supplier.get(action.zone) );
- break;
- case PRINT:
- this.zoneManager.printOnly();
- break;
- case TURN_ROUND:
- this.organismManager.makeMove();
- break;
- default:
- break;
- }
- }
- }
- class Action {
- Supplier<Organism> supplier;
- Supply supply;
- static boolean isInitialized;
- Organism organism;
- static Dispatcher dispatcher;
- Zone zone;
- ActionType type;
- Action() {
- };
- Action(Dispatcher dispatcherInstance) {
- isInitialized = true;
- dispatcher = dispatcherInstance;
- }
- Action(Supply supply, Zone zone){
- this.type = ActionType.CREATE;
- this.supply = supply;
- this.zone = zone;
- }
- Action(ActionType actionType){
- this.type= actionType;
- }
- }
- class OrganismFactory{
- ArrayList<Supply> supplies;
- OrganismFactory(){
- this.supplies = new ArrayList<Supply>();
- }
- void supply(Supply supply){
- this.supplies.add(supply);
- };
- private Supply get(Digestion digestion){
- int count = this.supplies.stream().filter(el->{return el.digestion== digestion;} ).collect(Collectors.toCollection(ArrayList::new)).size();
- return this.supplies.stream().filter(el->{ return el.digestion== digestion; }).collect(Collectors.toCollection(ArrayList::new) ).get(JavaWorldGenerator.generate(0, count-1) );
- }
- Supply getPlant(){
- return this.get(Digestion.NONE);
- };
- Supply getCarnivore(){
- return this.get(Digestion.CARNIVORE);
- }
- Supply getHerbivore(){
- return this.get(Digestion.HERBIVORE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement