Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.45 KB | None | 0 0
  1. package assignment4;
  2. /* CRITTERS Main.java
  3. * EE422C Project 4 submission by
  4. * Varun Sridhar
  5. * vks377
  6. * 16220
  7. * Ramakrishna Sai Annaluru
  8. * rsa662
  9. * 16236
  10. * Slip days used: 0
  11. * Spring 2017
  12. */
  13.  
  14.  
  15. import java.lang.reflect.*;
  16. import java.util.Iterator;
  17. import java.util.List;
  18.  
  19. /* see the PDF for descriptions of the methods and fields in this class
  20. * you may add fields, methods or inner classes to Critter ONLY if you make your additions private
  21. * no new public, protected or default-package code or data can be added to Critter
  22. */
  23.  
  24.  
  25. public abstract class Critter {
  26. private static String myPackage;
  27. private static List<Critter> population = new java.util.ArrayList<Critter>();
  28. private static List<Critter> babies = new java.util.ArrayList<Critter>();
  29. private static int timestep = 0;
  30.  
  31. // Gets the package name. This assumes that Critter and its subclasses are all in the same package.
  32. static {
  33. myPackage = Critter.class.getPackage().toString().split(" ")[1];
  34. }
  35.  
  36. private static java.util.Random rand = new java.util.Random();
  37. public static int getRandomInt(int max) {
  38. return rand.nextInt(max);
  39. }
  40.  
  41. public static void setSeed(long new_seed) {
  42. rand = new java.util.Random(new_seed);
  43. }
  44.  
  45.  
  46. /* a one-character long string that visually depicts your critter in the ASCII interface */
  47. public String toString() { return ""; }
  48.  
  49. private int energy = 0;
  50. protected int getEnergy() { return energy; }
  51.  
  52. private int x_coord;
  53. private int y_coord;
  54.  
  55. private boolean moved = false;
  56. private boolean inFight = false;
  57.  
  58. /**
  59. * Move critter 1 step in specified direction
  60. * @param direction the specified direction
  61. */
  62. protected final void walk(int direction) {
  63. energy -= Params.walk_energy_cost; // decrement the critter's energy
  64. int nextX;
  65. int nextY;
  66. if(!moved) { // if the critter hasn't already moved in this time step
  67. if(direction == 0) { // move the critter in the specified direction
  68. if(!inFight || (inFight && !isOccupied((x_coord + 1) % Params.world_width, y_coord)))
  69. x_coord = (x_coord + 1) % Params.world_width;
  70. }
  71. else if(direction == 1) {
  72. nextY = y_coord - 1;
  73. if(nextY < 0)
  74. nextY += Params.world_height;
  75.  
  76. if(!inFight || (inFight && !isOccupied((x_coord + 1) % Params.world_width, nextY))) {
  77. x_coord = (x_coord + 1) % Params.world_width;
  78. y_coord--;
  79. if(y_coord < 0)
  80. y_coord += Params.world_height;
  81. }
  82. }
  83. else if(direction == 2) {
  84. nextY = y_coord - 1;
  85. if(nextY < 0)
  86. nextY += Params.world_height;
  87.  
  88. if(!inFight || (inFight && !isOccupied(x_coord, nextY))) {
  89. y_coord--;
  90. if(y_coord < 0)
  91. y_coord += Params.world_height;
  92. }
  93. }
  94. else if(direction == 3) {
  95. nextX = x_coord - 1;
  96. if(nextX < 0)
  97. nextX += Params.world_width;
  98. nextY = y_coord - 1;
  99. if(nextY < 0)
  100. nextY += Params.world_height;
  101.  
  102. if(!inFight || (inFight && !isOccupied(nextX, nextY))) {
  103. x_coord--;
  104. if(x_coord < 0)
  105. x_coord += Params.world_width;
  106. y_coord--;
  107. if(y_coord < 0)
  108. y_coord += Params.world_height;
  109. }
  110. }
  111. else if(direction == 4) {
  112. nextX = x_coord - 1;
  113. if(nextX < 0)
  114. nextX += Params.world_width;
  115.  
  116. if(!inFight || (inFight && !isOccupied(nextX, y_coord))) {
  117. x_coord--;
  118. if(x_coord < 0)
  119. x_coord += Params.world_width;
  120. }
  121. }
  122. else if(direction == 5) {
  123. nextX = x_coord - 1;
  124. if(nextX < 0)
  125. nextX += Params.world_width;
  126.  
  127. if(!inFight || (inFight && !isOccupied(nextX, (y_coord + 1) % Params.world_height))) {
  128. x_coord--;
  129. if(x_coord < 0)
  130. x_coord += Params.world_width;
  131. y_coord = (y_coord + 1) % Params.world_height;
  132. }
  133. }
  134. else if(direction == 6) {
  135. if(!inFight || (inFight && !isOccupied(x_coord, (y_coord + 1) & Params.world_height)))
  136. y_coord = (y_coord + 1) % Params.world_height;
  137. }
  138. else {
  139. nextY = y_coord - 1;
  140. if(nextY < 0)
  141. nextY += Params.world_height;
  142.  
  143. if(!inFight || (inFight && !isOccupied((x_coord + 1) % Params.world_height, nextY))) {
  144. x_coord = (x_coord + 1) % Params.world_width;
  145. y_coord--;
  146. if(y_coord < 0)
  147. y_coord += Params.world_height;
  148. }
  149. }
  150. }
  151. moved = true; // set moved flag
  152. }
  153. /**
  154. * Move Critter two steps based on direction
  155. * @param direction 1 of 8 directional ways to go based on normal directions
  156. */
  157. protected final void run(int direction) {
  158. int nextX;
  159. int nextY;
  160. energy -= Params.run_energy_cost; // decrement the critter's energy
  161. if(!moved) { // if the critter hasn't already moved in this time step
  162. if(direction == 0) { // move the critter in the specified direction
  163. if(!inFight || (inFight && !isOccupied((x_coord + 2) % Params.world_width, y_coord)))
  164. x_coord = (x_coord + 2) % Params.world_width;
  165. }
  166. else if(direction == 1) {//move the critter in next direction, 7 or so things that look exactly like this
  167. nextY = y_coord - 2;
  168. if(nextY < 0)
  169. nextY += Params.world_height;
  170.  
  171. if(!inFight || (inFight && !isOccupied((x_coord + 2) % Params.world_width, nextY))) {
  172. x_coord = (x_coord + 2) % Params.world_width;//Taking care of fight characteristics
  173. y_coord -= 2;
  174. if(y_coord < 0)
  175. y_coord += Params.world_height;
  176. }
  177. }
  178. else if(direction == 2) {
  179. nextY = y_coord - 2;
  180. if(nextY < 0)
  181. nextY += Params.world_height;
  182.  
  183. if(!inFight || (inFight && !isOccupied(x_coord, nextY))) {
  184. y_coord -= 2;
  185. if(y_coord < 0)
  186. y_coord += Params.world_height;
  187. }
  188. }
  189. else if(direction == 3) {
  190. nextX = x_coord - 2;
  191. if(nextX < 0)
  192. nextX += Params.world_width;
  193. nextY = y_coord - 2;
  194. if(nextY < 0)
  195. nextY += Params.world_height;
  196.  
  197. if(!inFight || (inFight && !isOccupied(nextX, nextY))) {
  198. x_coord -= 2;
  199. if(x_coord < 0)
  200. x_coord += Params.world_width;
  201. y_coord -= 2;
  202. if(y_coord < 0)
  203. y_coord += Params.world_height;
  204. }
  205. }
  206. else if(direction == 4) {
  207. nextX = x_coord - 2;
  208. if(nextX < 0)
  209. nextX += Params.world_width;
  210.  
  211. if(!inFight || (inFight && !isOccupied(nextX, y_coord))) {
  212. x_coord -= 2;
  213. if(x_coord < 0)
  214. x_coord += Params.world_width;
  215. }
  216. }
  217. else if(direction == 5) {
  218. nextX = x_coord - 2;
  219. if(nextX < 0)
  220. nextX += Params.world_width;
  221.  
  222. if(!inFight || (inFight && !isOccupied(nextX, (y_coord + 2) % Params.world_height))) {
  223. x_coord -= 2;
  224. if(x_coord < 0)
  225. x_coord += Params.world_width;
  226. y_coord = (y_coord + 2) % Params.world_height;
  227. }
  228. }
  229. else if(direction == 6) {
  230. if(!inFight || (inFight && !isOccupied(x_coord, (y_coord + 2) % Params.world_height))) {
  231. y_coord = (y_coord + 2) % Params.world_height;
  232. }
  233. }
  234. else {
  235. nextY = y_coord - 2;
  236. if(nextY < 0)
  237. nextY += Params.world_height;
  238.  
  239. if(!inFight || (inFight && !isOccupied((x_coord + 2) % Params.world_width, nextY))) {
  240. x_coord = (x_coord + 2) % Params.world_width;
  241. y_coord -= 2;
  242. if(y_coord < 0)
  243. y_coord += Params.world_height;
  244. }
  245. }
  246. }
  247. moved = true; // set moved flag
  248. }
  249.  
  250. /**
  251. * Create new critter and place near old critter
  252. * @param offspring Before calling this method, create an offspring to send to reproduce
  253. * @param direction place Critter 1 position away in the direction stated
  254. */
  255. protected final void reproduce(Critter offspring, int direction) {
  256. if(getEnergy() >= Params.min_reproduce_energy) {
  257. offspring.energy = getEnergy() / 2;
  258. energy = (int)Math.ceil(0.5 * (double)getEnergy());
  259. if(direction == 0) {
  260. offspring.x_coord = x_coord + 1;
  261. offspring.y_coord = y_coord;
  262. }
  263. else if(direction == 1) {
  264. offspring.x_coord = x_coord + 1;
  265. offspring.y_coord = y_coord - 1;
  266. }
  267. else if(direction == 2) {
  268. offspring.x_coord = x_coord;
  269. offspring.y_coord = y_coord - 1;
  270. }
  271. else if(direction == 3) {
  272. offspring.x_coord = x_coord - 1;
  273. offspring.y_coord = y_coord - 1;
  274. }
  275. else if(direction == 4) {
  276. offspring.x_coord = x_coord - 1;
  277. offspring.y_coord = y_coord;
  278. }
  279. else if(direction == 5) {
  280. offspring.x_coord = x_coord - 1;
  281. offspring.y_coord = y_coord + 1;
  282. }
  283. else if(direction == 6) {
  284. offspring.x_coord = x_coord;
  285. offspring.y_coord = y_coord + 1;
  286. }
  287. else {
  288. offspring.x_coord = x_coord + 1;
  289. offspring.y_coord = y_coord + 1;
  290. }
  291. babies.add(offspring);
  292. }
  293. }
  294.  
  295. public abstract void doTimeStep();
  296. public abstract boolean fight(String oponent);
  297.  
  298. /**
  299. * create and initialize a Critter subclass.
  300. * critter_class_name must be the unqualified name of a concrete subclass of Critter, if not,
  301. * an InvalidCritterException must be thrown.
  302. * (Java weirdness: Exception throwing does not work properly if the parameter has lower-case instead of
  303. * upper. For example, if craig is supplied instead of Craig, an error is thrown instead of
  304. * an Exception.)
  305. * @param critter_class_name
  306. * @throws InvalidCritterException
  307. */
  308. public static void makeCritter(String critter_class_name) throws InvalidCritterException {
  309. Class<?> myCritter = null;
  310. Constructor<?> myConstructor = null;
  311. Object instanceOfMyCritter = null;
  312.  
  313. try {
  314. myCritter = Class.forName(myPackage + "." + critter_class_name); // Class object of specified name
  315. } catch(ClassNotFoundException e) {
  316. throw new InvalidCritterException(critter_class_name);
  317. }
  318.  
  319. try {
  320. myConstructor = myCritter.getConstructor(); // No parameter constructor object
  321. instanceOfMyCritter = myConstructor.newInstance(); // Create new object using constructor
  322. } catch(InstantiationException e) {
  323. throw new InvalidCritterException(critter_class_name);
  324. } catch (IllegalAccessException e) {
  325. throw new InvalidCritterException(critter_class_name);
  326. } catch (NoSuchMethodException e) {
  327. throw new InvalidCritterException(critter_class_name);
  328. } catch (IllegalArgumentException e) {
  329. throw new InvalidCritterException(critter_class_name);
  330. } catch (InvocationTargetException e) {
  331. throw new InvalidCritterException(critter_class_name);
  332. }
  333.  
  334.  
  335. Critter newCritter = (Critter)instanceOfMyCritter;
  336. newCritter.energy = Params.start_energy; // start energy of new Critter
  337. newCritter.x_coord = getRandomInt(Params.world_width); // set x coordinate of the new Critter
  338. newCritter.y_coord = getRandomInt(Params.world_height); // set y coordinate of the new Critter
  339.  
  340. population.add(newCritter); // add new Critter to the list of Critters
  341. }
  342.  
  343. /**
  344. * Gets a list of critters of a specific type.
  345. * @param critter_class_name What kind of Critter is to be listed. Unqualified class name.
  346. * @return List of Critters.
  347. * @throws InvalidCritterException
  348. */
  349. public static List<Critter> getInstances(String critter_class_name) throws InvalidCritterException {
  350. List<Critter> result = new java.util.ArrayList<Critter>(); // list to store critters of the given type
  351. Class<?> myCritter = null; // class variable
  352.  
  353. try {
  354. myCritter = Class.forName(myPackage + "." + critter_class_name); // assigns specific class to variable
  355. }
  356. catch(ClassNotFoundException e) {
  357. throw new InvalidCritterException(critter_class_name); // throws exception if class does not exist
  358. }
  359.  
  360. for(Critter crit: population) { // iterate through all critters
  361. if(myCritter.isInstance(crit)) // if that critter is an instance of the given type
  362. result.add(crit); // add it to the list
  363. }
  364.  
  365. return result; // return the list of critters
  366. }
  367.  
  368. /**
  369. * Prints out how many Critters of each type there are on the board.
  370. * @param critters List of Critters.
  371. */
  372. public static void runStats(List<Critter> critters) {
  373. System.out.print("" + critters.size() + " critters as follows -- ");
  374. java.util.Map<String, Integer> critter_count = new java.util.HashMap<String, Integer>();
  375. for (Critter crit : critters) {
  376. String crit_string = crit.toString();
  377. Integer old_count = critter_count.get(crit_string);
  378. if (old_count == null) {
  379. critter_count.put(crit_string, 1);
  380. } else {
  381. critter_count.put(crit_string, old_count.intValue() + 1);
  382. }
  383. }
  384. String prefix = "";
  385. for (String s : critter_count.keySet()) {
  386. System.out.print(prefix + s + ":" + critter_count.get(s));
  387. prefix = ", ";
  388. }
  389. System.out.println();
  390. }
  391.  
  392. /* the TestCritter class allows some critters to "cheat". If you want to
  393. * create tests of your Critter model, you can create subclasses of this class
  394. * and then use the setter functions contained here.
  395. *
  396. * NOTE: you must make sure that the setter functions work with your implementation
  397. * of Critter. That means, if you're recording the positions of your critters
  398. * using some sort of external grid or some other data structure in addition
  399. * to the x_coord and y_coord functions, then you MUST update these setter functions
  400. * so that they correctly update your grid/data structure.
  401. */
  402. static abstract class TestCritter extends Critter {
  403. protected void setEnergy(int new_energy_value) {
  404. super.energy = new_energy_value;
  405. }
  406.  
  407. protected void setX_coord(int new_x_coord) {
  408. super.x_coord = new_x_coord;
  409. }
  410.  
  411. protected void setY_coord(int new_y_coord) {
  412. super.y_coord = new_y_coord;
  413. }
  414.  
  415. protected int getX_coord() {
  416. return super.x_coord;
  417. }
  418.  
  419. protected int getY_coord() {
  420. return super.y_coord;
  421. }
  422.  
  423.  
  424. /*
  425. * This method getPopulation has to be modified by you if you are not using the population
  426. * ArrayList that has been provided in the starter code. In any case, it has to be
  427. * implemented for grading tests to work.
  428. */
  429. protected static List<Critter> getPopulation() {
  430. return population;
  431. }
  432.  
  433. /*
  434. * This method getBabies has to be modified by you if you are not using the babies
  435. * ArrayList that has been provided in the starter code. In any case, it has to be
  436. * implemented for grading tests to work. Babies should be added to the general population
  437. * at either the beginning OR the end of every timestep.
  438. */
  439. protected static List<Critter> getBabies() {
  440. return babies;
  441. }
  442. }
  443.  
  444. /**
  445. * Clear the world of all critters, dead and alive
  446. */
  447. public static void clearWorld() {
  448. // Complete this method.
  449. population.clear();
  450. }
  451.  
  452. public static void worldTimeStep() {
  453. //increment timestep
  454. timestep++; // increment timestep
  455. for(Critter c: population) // invoke doTimeStep on every living critter in the collection
  456. c.doTimeStep(); //First Instruction. World time step should do every time step
  457.  
  458. //ENCOUNTERS
  459. for(Critter c: population) {// for all the critters
  460. for(Critter d: population) {// and anyone they'd fight
  461. if(c != d && c.x_coord == d.x_coord && c.y_coord == d.y_coord && c.getEnergy() > 0 && d.getEnergy() > 0) {//if in same location
  462. c.inFight = true;//and if strong enough
  463. d.inFight = true;//
  464. boolean cFight = c.fight(d.toString());//convert to word
  465. boolean dFight = d.fight(c.toString());//convert to word
  466. if(c.x_coord == d.x_coord && c.y_coord == d.y_coord && c.getEnergy() > 0 && d.getEnergy() > 0) {//check again
  467. int cRoll = 0;
  468. int dRoll = 0;
  469. if(cFight)
  470. cRoll = getRandomInt(c.getEnergy());//roll to see who wins
  471. if(dFight)
  472. dRoll = getRandomInt(d.getEnergy());
  473. if(cRoll > dRoll) {
  474. c.energy += (d.getEnergy() / 2);//whoever loses energy goes to 0, others is 1.5 times
  475. d.energy = 0;
  476. }
  477. else {
  478. d.energy += (c.getEnergy() / 2);
  479. c.energy = 0;
  480. }
  481. }
  482. }
  483. }
  484. }
  485.  
  486. for(Critter c: population)
  487. c.energy -= Params.rest_energy_cost;
  488.  
  489. Iterator<Critter> i = population.iterator();//
  490. while(i.hasNext()) {
  491. if(i.next().getEnergy() <= 0)
  492. i.remove();
  493. }
  494.  
  495. for(Critter c: population) {
  496. c.moved = false;
  497. c.inFight = false;
  498. }
  499.  
  500. for(int j = 0; j < Params.refresh_algae_count; j++) {
  501. Critter algae = new Algae();
  502. algae.energy = Params.start_energy;
  503. algae.x_coord = getRandomInt(Params.world_width);
  504. algae.y_coord = getRandomInt(Params.world_height);
  505. population.add(algae);
  506. }
  507.  
  508. population.addAll(babies);
  509. babies.clear();
  510. }
  511. /**
  512. * Displays the world in the specific graph method
  513. */
  514. public static void displayWorld() {
  515. // Complete this method.
  516. boolean printed = false;
  517. System.out.print("+");
  518. for(int i = 0; i < Params.world_width; i++)
  519. System.out.print("-");
  520. System.out.println("+");
  521. for(int i = 0; i < Params.world_height; i++) {
  522. System.out.print("|");
  523. for(int j = 0; j < Params.world_width; j++) {
  524. for(Critter c: population) {
  525. if(c.x_coord == j && c.y_coord == i && !printed) {
  526. System.out.print(c);
  527. printed = true;
  528. }
  529. }
  530. if(!printed)
  531. System.out.print(" ");
  532. printed = false;
  533. }
  534. System.out.println("|");
  535. }
  536. System.out.print("+");//final row
  537. for(int i = 0; i < Params.world_width; i++)
  538. System.out.print("-");
  539. System.out.println("+");
  540. }
  541. /**
  542. *
  543. * @param x coordinate
  544. * @param y coordinate
  545. * @return whether that location is occupied
  546. */
  547. private boolean isOccupied(int x, int y) {
  548. for(Critter c: population) {
  549. if(c.x_coord == x && c.y_coord == y)//used to check if occupied
  550. return true;
  551. }
  552. return false;
  553. }
  554. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement