Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.91 KB | None | 0 0
  1. import java.io.FileNotFoundException;
  2. import java.lang.annotation.ElementType;
  3. import java.lang.annotation.Target;
  4. import java.lang.invoke.MethodHandles;
  5. import java.util.ArrayList;
  6. import java.util.stream.Collectors;
  7. import java.util.stream.Stream;
  8. import java.util.Arrays;
  9. import java.util.HashMap;
  10. import java.util.HashSet;
  11. import java.lang.reflect.*;
  12. import java.util.Random;
  13. import java.util.Scanner;
  14. import java.util.stream.Collectors.*;
  15.  
  16. enum ActionType {
  17. CREATE, DELETE, MOVE, REPRODUCE, PRINT, TURN_ROUND
  18. }
  19. class Controller {
  20.  
  21. boolean quit;
  22. Dispatcher dispatcher;
  23.  
  24.  
  25. ArrayList<Action> actions;
  26.  
  27. Controller(Dispatcher dispatcher){
  28. this.dispatcher = dispatcher;
  29. this.quit = false;
  30. }
  31. Controller() {
  32. this.quit = false;
  33.  
  34. }
  35.  
  36. void startup(Config config){
  37. for(Action action : config.actions){
  38. this.dispatcher.dispatch(action);
  39. }
  40. }
  41. void show(){
  42. this.dispatcher.dispatch(new Action(ActionType.PRINT));
  43. }
  44.  
  45.  
  46. void makeMove(){
  47. this.dispatcher.dispatch(new Action(ActionType.TURN_ROUND));
  48. }
  49.  
  50.  
  51.  
  52.  
  53.  
  54. }
  55. abstract class DefaultConfig{
  56. int gridSize = 25;
  57. int plantsCount = 60;
  58. int carnivoreCount = 15;
  59. int herbivoreCount = 25;
  60. ArrayList<Action> actions;
  61.  
  62. }
  63. class Config extends DefaultConfig{
  64.  
  65. Config(){
  66.  
  67. this.actions = new ArrayList<Action>();
  68.  
  69. }
  70.  
  71.  
  72. void setActions(OrganismFactory factory, ZoneManager zones){
  73.  
  74. for(int i =0; i< plantsCount; i++){
  75.  
  76. this.actions.add( new Action( factory.getPlant(), zones.getRandom("free") ) );
  77.  
  78. }
  79.  
  80. for(int i=0; i<carnivoreCount; i++){
  81. this.actions.add(new Action(factory.getCarnivore(), zones.getRandom("free")));
  82. }
  83.  
  84. for(int i=0; i< herbivoreCount; i++){
  85. this.actions.add(new Action(factory.getHerbivore(), zones.getRandom("free")));
  86. }
  87.  
  88. }
  89.  
  90. }
  91.  
  92.  
  93.  
  94.  
  95. abstract class JavaWorldGenerator{
  96.  
  97.  
  98.  
  99.  
  100. static int generate() {
  101. int offset = 1;
  102. return (0 - offset) + (int) (Math.random() * ((offset - (0 - offset)) + 1));
  103. }
  104. static int generate(int offset) {
  105. return (0 - offset) + (int) (Math.random() * ((offset - (0 - offset)) + 1));
  106. }
  107. static int generate(int min, int max) {
  108. Random r = new Random();
  109.  
  110.  
  111. if (min > max) {
  112. return generate(max, min);
  113.  
  114. }
  115.  
  116. return r.nextInt((max - min) + 1) + min;
  117. }
  118.  
  119.  
  120.  
  121.  
  122. }
  123. class RandomGenerator extends JavaWorldGenerator {
  124.  
  125.  
  126. private RandomGenerator(){
  127.  
  128. }
  129.  
  130.  
  131.  
  132.  
  133.  
  134. }
  135. // REQUIRED : RANDOMG GENERATOR, ZONEMANAGER, ORGANISMSMANAGER, DISPATCHER
  136.  
  137. public class World{
  138.  
  139. public static void main(String[] args){
  140.  
  141.  
  142.  
  143. Dispatcher dispatcher = new Dispatcher();
  144. Controller controller = new Controller(dispatcher);
  145. ZoneManager zoneManager = new ZoneManager(dispatcher);
  146.  
  147. OrganismManager organismManager = new OrganismManager(dispatcher);
  148.  
  149. OrganismFactory factory = new OrganismFactory();
  150.  
  151. factory.supply(Grass.getSupply());
  152. factory.supply(Tree.getSupply());
  153. factory.supply(Wolf.getSupply());
  154. factory.supply(Sheep.getSupply());
  155.  
  156. Config config = new Config();
  157. config.setActions(factory, zoneManager);
  158. controller.startup(config);
  159.  
  160.  
  161.  
  162.  
  163. // organismManager.organisms.stream().forEach(el->{System.out.println(el.zone);});
  164.  
  165.  
  166. // while(true){
  167.  
  168.  
  169. // System.out.print("\033[H\033[2J");
  170. // zoneManager.printOnly();
  171. // try {
  172. // Thread.sleep(2000);
  173. // } catch (Exception e) {
  174.  
  175. // }
  176.  
  177. // }
  178.  
  179.  
  180.  
  181.  
  182. }
  183.  
  184.  
  185. }
  186.  
  187.  
  188. class Clock extends Thread{
  189.  
  190. }
  191.  
  192. class Zone {
  193. char x;
  194. int y;
  195. boolean isOccupied ;
  196. boolean hasAnimal;
  197. boolean hasPlant;
  198. Animal animal;
  199. Plant plant;
  200. Organism organism;
  201. Zone(char x, int y) {
  202. this.x = x;
  203. this.y = y;
  204. this.isOccupied = false;
  205. this.hasAnimal = false;
  206. this.hasPlant = false;
  207. this.animal = null;
  208. this.plant = null;
  209. this.organism = null;
  210. }
  211. String appear(){
  212. if(this.organism!= null){
  213.  
  214. if(this.y<10) return String.format(this.organism.id+" ");
  215. else return String.format(this.organism.id+" ");
  216. }
  217. else{
  218. return this.toString();
  219. }
  220. };
  221.  
  222. String showOrganism(){
  223. if(this.organism!=null){
  224. if(this.y<10) return String.format(this.organism.id+ " ");
  225. else return String.format(this.organism.id+" ");
  226. }
  227. else{
  228. return " ";
  229. }
  230. }
  231. void subscribe(Organism organism){
  232. this.isOccupied = true;
  233. this.organism = organism;
  234.  
  235. }
  236. @Override
  237. public String toString() {
  238. // TODO Auto-generated method stub
  239. if (this.y < 10)
  240. return String.format("%d%s ", y, x);
  241. else
  242. return String.format("%d%s ", y, x);
  243. }
  244.  
  245. String Alert(){
  246. String ANSI_RED = "\u001B[31m";
  247. String ANSI_GREEN = "\u001B[32m";
  248.  
  249. String ANSI_YELLOW = "\u001b[33m";
  250. String ANSI_BLUE = "\u001b[34m";
  251. String ANSI_MAGENTA = "\u001b[35m";
  252. String ANSI_CYAN = "\u001b[36m";
  253. String ANSI_WHITE = "\u001b[37m";
  254.  
  255.  
  256. String ANSI_RESET = "\u001B[0m";
  257. if(this.y <10)
  258. return ANSI_RED + y + x +" "+ ANSI_RESET;
  259. return ANSI_RED + y + x +" "+ ANSI_RESET;
  260. }
  261.  
  262. int[][] getMatrix() {
  263. int[][] result = { { 1, 2, 3 }, { 12, 3, 4 }, { 1, 2, 3 } };
  264. return result;
  265. }
  266.  
  267. }
  268. class ZoneManager {
  269. ArrayList<Zone> zones;
  270. ArrayList<Zone> empty;
  271. Dispatcher dispatcher;
  272.  
  273. int GRIDSIZE = 25;
  274.  
  275. ZoneManager(Dispatcher dispatcher) {
  276. this.dispatcher = dispatcher;
  277. this.dispatcher.subscribe(this);
  278.  
  279. this.zones = new ArrayList<Zone>();
  280. this.empty = new ArrayList<Zone>();
  281.  
  282. for (int y = GRIDSIZE; y > 0; y--)
  283. {
  284.  
  285. for (int x = 65; x <= 65 + GRIDSIZE; x++)
  286. {
  287. Zone content = new Zone((char) x, y);
  288. this.zones.add(content);
  289. this.empty.add(content);
  290. this.dispatcher.subscribe(content);
  291. }
  292. }
  293. }
  294. void printOrganisms(){
  295. for (Zone el : this.zones) {
  296.  
  297. System.out.print(el.appear());
  298. if ((int) el.x - 65 == this.GRIDSIZE)
  299. System.out.println();
  300.  
  301. }
  302. }
  303. void printOnly(){
  304. for (Zone el : this.zones) {
  305.  
  306. System.out.print(el.showOrganism());
  307. if ((int) el.x - 65 == this.GRIDSIZE)
  308. System.out.println();
  309.  
  310. }
  311. }
  312.  
  313.  
  314. Zone getZone(Zone zone){
  315.  
  316.  
  317. return this.zones.stream().filter(x-> x.hashCode()==zone.hashCode() ).collect(Collectors.toCollection(ArrayList::new)).get(0) ;
  318. }
  319.  
  320. void subscribe(Organism organism, Zone zone){
  321.  
  322. getZone(zone).subscribe(organism);
  323.  
  324.  
  325.  
  326.  
  327.  
  328. }
  329.  
  330. Zone getRandom(String extra){
  331. return this.empty.get(RandomGenerator.generate(0, GRIDSIZE * GRIDSIZE));
  332. }
  333.  
  334. Zone getRandom() {
  335.  
  336. return this.zones.get(RandomGenerator.generate(0, GRIDSIZE * GRIDSIZE));
  337. }
  338.  
  339. Zone selectRandom(){
  340. Zone zone = this.zones.get(RandomGenerator.generate(0,GRIDSIZE* GRIDSIZE) ) ;
  341. for (Zone el : this.zones) {
  342.  
  343. if(zone.hashCode() == el.hashCode() ){
  344. System.out.print(el.Alert());
  345. }
  346. else{
  347. System.out.print(el);
  348. }
  349. if ((int) el.x - 65 == this.GRIDSIZE)
  350. System.out.println();
  351.  
  352. }
  353. return zone;
  354. }
  355.  
  356. ArrayList<Zone> selectRandom(int count){
  357. ArrayList<Zone> selected = new ArrayList<Zone>();
  358. for(int i=0; i<count; i++){
  359. Zone zone = this.zones.get(RandomGenerator.generate(0, GRIDSIZE * GRIDSIZE));
  360. selected.add(zone);
  361. }
  362. for ( Zone el : this.zones){
  363.  
  364. if ( selected.contains(el) ){
  365.  
  366. System.out.print(el.Alert() );
  367.  
  368. }
  369. else{
  370. System.out.print(el);
  371. }
  372. if ((int) el.x - 65 == this.GRIDSIZE)
  373. System.out.println();
  374. }
  375. return selected;
  376. }
  377. void printRows() {
  378. int count = 0;
  379. for (Zone el : this.zones) {
  380.  
  381. System.out.print(el);
  382. if ((int) el.x - 65 == this.GRIDSIZE)
  383. System.out.println();
  384.  
  385. }
  386. }
  387. }
  388. class OrganismManager{
  389. ArrayList<Organism> organisms;
  390.  
  391. ArrayList<Animal> animalSpecies;
  392. ArrayList<Plant> plantSpecies;
  393. Dispatcher dispatcher ;
  394. OrganismManager(){
  395. }
  396. OrganismManager(Dispatcher dispatcher){
  397. this.dispatcher = dispatcher;
  398. this.dispatcher.subscribe(this);
  399. this.animalSpecies = new ArrayList<Animal>();
  400. this.plantSpecies = new ArrayList<Plant>();
  401. this.organisms = new ArrayList<Organism>();
  402. }
  403. void add(Organism organism){
  404. this.organisms.add(organism);
  405. }
  406. void makeMove(){
  407. for(Organism organism : this.organisms){
  408. organism.makeMove();
  409. }
  410.  
  411. }
  412.  
  413.  
  414.  
  415. }
  416. abstract class Organism {
  417. static String ANSI_RED = "\u001B[31m";
  418. static String ANSI_GREEN = "\u001B[32m";
  419. static Digestion digestion;
  420. static String ANSI_YELLOW = "\u001b[33m";
  421. static String ANSI_BLUE = "\u001b[34m";
  422. static String ANSI_MAGENTA = "\u001b[35m";
  423. static String ANSI_CYAN = "\u001b[36m";
  424. static String ANSI_WHITE = "\u001b[37m";
  425.  
  426. static String ANSI_RESET = "\u001B[0m";
  427. static HashMap<String, String> colors = new HashMap<String, String>();
  428.  
  429. static{
  430. colors.put("red", ANSI_RED);
  431. colors.put("green", ANSI_GREEN);
  432. colors.put("yellow", ANSI_YELLOW);
  433. colors.put("blue", ANSI_BLUE);
  434. colors.put("magenta", ANSI_MAGENTA);
  435. colors.put("cyan", ANSI_CYAN);
  436. colors.put("white", ANSI_WHITE);
  437. colors.put("reset", ANSI_RESET);
  438.  
  439. }
  440.  
  441.  
  442.  
  443.  
  444. //static Digestion digestion;
  445. String id;
  446.  
  447. Zone zone;
  448. Dispatcher dispatcher;
  449. static ArrayList<Organism> prototypes;
  450.  
  451. Organism(Dispatcher dispatcher){
  452. this.dispatcher = dispatcher;
  453. this.dispatcher.subscribe(this);
  454. }
  455.  
  456. void setZone(Zone zone){
  457.  
  458. this.zone = zone;
  459. }
  460. Organism(){
  461.  
  462. }
  463. void makeMove(){
  464. System.out.println(this);
  465. }
  466.  
  467.  
  468.  
  469. String getId(){
  470. return this.id;
  471. }
  472.  
  473. static int range;
  474.  
  475.  
  476.  
  477.  
  478.  
  479. }
  480. abstract class Animal extends Organism {
  481.  
  482.  
  483.  
  484. Animal(Dispatcher dispatcher ){
  485.  
  486. super(dispatcher );
  487.  
  488.  
  489.  
  490.  
  491. }
  492. Animal(){}
  493.  
  494.  
  495. void occupy(Zone zone){
  496. this.zone = zone;
  497. }
  498.  
  499.  
  500. }
  501. abstract class Carnivore extends Animal{
  502. static Digestion digestion = Digestion.CARNIVORE;
  503. static String classColor = colors.get("red");
  504.  
  505. Carnivore(){
  506. super();
  507.  
  508. }
  509. Carnivore(Dispatcher dispatcher){
  510. super(dispatcher);
  511.  
  512. }
  513.  
  514. static int getRange() {
  515. return range;
  516. }
  517.  
  518. static Digestion getDigestion() {
  519. return digestion;
  520. }
  521.  
  522. }
  523.  
  524. abstract class Herbivore extends Animal {
  525. static Digestion digestion = Digestion.HERBIVORE;
  526. static String classColor = colors.get("blue");
  527. Herbivore(){
  528. super();
  529.  
  530. }
  531. Herbivore(Dispatcher dispatcher){
  532. super(dispatcher);
  533.  
  534. }
  535.  
  536. static int getRange() {
  537. return range;
  538. }
  539.  
  540. static Digestion getDigestion() {
  541. return digestion;
  542. }
  543.  
  544. }
  545. abstract class Plant extends Organism implements Listener{
  546. static Digestion digestion = Digestion.NONE;
  547. static String classColor = colors.get("green");
  548. Plant(){
  549.  
  550. }
  551. Plant(Dispatcher dispatcher){
  552. super(dispatcher);
  553.  
  554. }
  555.  
  556. static int getRange() {
  557. return range;
  558. }
  559.  
  560. static Digestion getDigestion() {
  561. return digestion;
  562. }
  563. }
  564.  
  565.  
  566. //@FunctionalInterface
  567. interface Supplier<T>{
  568. T get(Zone zone);
  569. }
  570.  
  571. enum Digestion{
  572.  
  573.  
  574. CARNIVORE,
  575. HERBIVORE,
  576. NONE
  577.  
  578.  
  579. }
  580.  
  581. class Supply{
  582.  
  583. Supplier<Organism> supplier;
  584. Digestion digestion;
  585. int range;
  586. String className;
  587. Supply(Supplier<Organism> supplier, Digestion digestion, int range, String className){
  588.  
  589. this.supplier = supplier;
  590. this.digestion = digestion;
  591. this.range = range;
  592. this.className = className;
  593.  
  594.  
  595.  
  596. }
  597.  
  598. }
  599.  
  600.  
  601. class Wolf extends Carnivore{
  602.  
  603.  
  604.  
  605.  
  606. static int range = 4;
  607.  
  608. Wolf(Dispatcher dispatcher){
  609. super(dispatcher );
  610. this.range = 4;
  611. this.id = classColor + "WF" + colors.get("reset");
  612.  
  613. }
  614.  
  615. Wolf() {
  616. this.id = classColor + "WF" + colors.get("reset");
  617. this.range = 4;
  618. System.out.println();
  619.  
  620. }
  621. Wolf(Zone zone){
  622.  
  623. this.zone = zone;
  624. this.zone.subscribe(this);
  625. this.id = classColor + "WF" + colors.get("reset");
  626. }
  627.  
  628.  
  629. private static Supplier<Organism> getSupplier(){
  630.  
  631. Supplier<Organism> mkWolf = (Zone zone) -> new Wolf(zone);
  632. return mkWolf;
  633. }
  634.  
  635. static Supply getSupply(){
  636. return new Supply(getSupplier(), getDigestion(), getRange(), MethodHandles.lookup().lookupClass().getSimpleName() );
  637. }
  638.  
  639.  
  640. }
  641. class Sheep extends Herbivore{
  642. int range =2 ;
  643.  
  644. Sheep(){
  645. this.id = classColor + "SP" + colors.get("reset");
  646. this.range = 2;
  647. //System.out.print("Creating sheep");
  648. }
  649. Sheep(Zone zone){
  650. this.zone = zone;
  651. this.id = classColor + "SP" + colors.get("reset");
  652. this.zone.subscribe(this);
  653. }
  654.  
  655.  
  656. private static Supplier<Organism> getSupplier() {
  657.  
  658. Supplier<Organism> mkSheep = (Zone zone) -> new Sheep(zone);
  659. return mkSheep;
  660. }
  661.  
  662. static Supply getSupply() {
  663. return new Supply(getSupplier(), getDigestion(), getRange(), MethodHandles.lookup().lookupClass().getSimpleName() );
  664. }
  665.  
  666. }
  667.  
  668. class Grass extends Plant {
  669. int range = 0;
  670. Grass(){
  671. this.id = classColor + "GS" + colors.get("reset");
  672. this.range = 0;
  673. }
  674. Grass(Dispatcher dispatcher) {
  675. super(dispatcher);
  676. this.id = classColor + "GS" + colors.get("reset");
  677. this.range = 0;
  678. }
  679. Grass(Zone zone){
  680. this.zone = zone;
  681. this.zone.subscribe(this);
  682. this.id = classColor + "GS" + colors.get("reset");
  683. }
  684.  
  685.  
  686. static Supplier<Organism> getSupplier() {
  687.  
  688. Supplier<Organism> mkGrass = (Zone zone) -> new Grass(zone);
  689. return mkGrass;
  690. }
  691.  
  692. static Supply getSupply() {
  693. return new Supply(getSupplier(), getDigestion(), getRange(), MethodHandles.lookup().lookupClass().getSimpleName() );
  694. }
  695.  
  696.  
  697. }
  698.  
  699. class Tree extends Plant{
  700. int range = 0;
  701. Tree(){
  702. this.id = classColor + "TR" + colors.get("reset");
  703. this.range = 0;
  704. }
  705. Tree(Dispatcher dispatcher){
  706. super(dispatcher);
  707. this.id = classColor + "TR" + colors.get("reset");
  708. this.range = 0;
  709.  
  710. }
  711. Tree(Zone zone){
  712. this.zone = zone;
  713. this.zone.subscribe(this);
  714. this.id = classColor + "TR" + colors.get("reset");
  715. }
  716.  
  717.  
  718. static public Supplier<Organism> getSupplier() {
  719.  
  720. Supplier<Organism> mkTree = (Zone zone) -> new Tree(zone);
  721. return mkTree;
  722. }
  723.  
  724. static Supply getSupply() {
  725. return new Supply(getSupplier(), getDigestion(), getRange(), MethodHandles.lookup().lookupClass().getSimpleName() );
  726. }
  727. }
  728. interface Listener{
  729.  
  730. }
  731.  
  732. interface AbstractFactory<Type>{
  733. Type create(String AnimalType);
  734.  
  735. }
  736.  
  737.  
  738.  
  739. class Dispatcher {
  740. ArrayList<Organism> organisms;
  741.  
  742. ArrayList<Zone> zones;
  743. Action actions;
  744. ZoneManager zoneManager;
  745. OrganismManager organismManager;
  746. Dispatcher() {
  747.  
  748. this.actions = new Action(this);
  749. this.organisms = new ArrayList<Organism>();
  750.  
  751. this.zones = new ArrayList<Zone>();
  752.  
  753. }
  754. void subscribe(ZoneManager zoneManager){
  755. this.zoneManager = zoneManager;
  756. }
  757. void subscribe(OrganismManager organismManager){
  758. this.organismManager = organismManager;
  759. }
  760.  
  761. void subscribe(Organism organism) {
  762.  
  763. this.organisms.add(organism);
  764. }
  765.  
  766.  
  767. void subscribe(Zone zone) {
  768. this.zones.add(zone);
  769. }
  770.  
  771. void dispatch(Action action){
  772.  
  773. switch(action.type){
  774. case CREATE:
  775. this.organismManager.add(action.supply.supplier.get(action.zone) );
  776. break;
  777. case PRINT:
  778. this.zoneManager.printOnly();
  779. break;
  780. case TURN_ROUND:
  781. this.organismManager.makeMove();
  782. break;
  783. default:
  784. break;
  785.  
  786. }
  787.  
  788.  
  789.  
  790. }
  791.  
  792.  
  793.  
  794.  
  795.  
  796.  
  797.  
  798.  
  799. }
  800.  
  801.  
  802. class Action {
  803. Supplier<Organism> supplier;
  804. Supply supply;
  805.  
  806. static boolean isInitialized;
  807. Organism organism;
  808. static Dispatcher dispatcher;
  809. Zone zone;
  810. ActionType type;
  811. Action() {
  812.  
  813. };
  814. Action(Dispatcher dispatcherInstance) {
  815. isInitialized = true;
  816. dispatcher = dispatcherInstance;
  817.  
  818. }
  819.  
  820.  
  821. Action(Supply supply, Zone zone){
  822. this.type = ActionType.CREATE;
  823. this.supply = supply;
  824. this.zone = zone;
  825. }
  826. Action(ActionType actionType){
  827. this.type= actionType;
  828. }
  829.  
  830. }
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837. class OrganismFactory{
  838.  
  839. ArrayList<Supply> supplies;
  840.  
  841. OrganismFactory(){
  842.  
  843. this.supplies = new ArrayList<Supply>();
  844.  
  845. }
  846.  
  847. void supply(Supply supply){
  848. this.supplies.add(supply);
  849.  
  850. };
  851. private Supply get(Digestion digestion){
  852.  
  853. int count = this.supplies.stream().filter(el->{return el.digestion== digestion;} ).collect(Collectors.toCollection(ArrayList::new)).size();
  854.  
  855. return this.supplies.stream().filter(el->{ return el.digestion== digestion; }).collect(Collectors.toCollection(ArrayList::new) ).get(JavaWorldGenerator.generate(0, count-1) );
  856.  
  857. }
  858. Supply getPlant(){
  859.  
  860. return this.get(Digestion.NONE);
  861.  
  862. };
  863.  
  864. Supply getCarnivore(){
  865.  
  866. return this.get(Digestion.CARNIVORE);
  867. }
  868.  
  869. Supply getHerbivore(){
  870. return this.get(Digestion.HERBIVORE);
  871. }
  872.  
  873.  
  874.  
  875.  
  876.  
  877. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement