Advertisement
Guest User

wawawa

a guest
Oct 24th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. class AnEntity {
  2. static int lastID = -1;
  3. AWorld residence;
  4. String species;
  5. char symbol;
  6. int id;
  7. int xPos;
  8. int yPos;
  9. int energy;
  10.  
  11. public String getSpecies() {
  12. return species;
  13. }
  14.  
  15. public void setSpecies(String species) {
  16. this.species = species;
  17. }
  18.  
  19. public char getSymbol(){
  20. return symbol;
  21. }
  22.  
  23. public void setsymbol(char symbol)
  24. {
  25. this.symbol = symbol;
  26. }
  27.  
  28. public int getId() {
  29. return id;
  30. }
  31.  
  32. public void setId(int id) {
  33. this.id = id;
  34. }
  35.  
  36. public int getX() {
  37. return xPos;
  38. }
  39.  
  40. public void setX(int xPos) {
  41. this.xPos = xPos;
  42. }
  43.  
  44. public int getY() {
  45. return yPos;
  46. }
  47.  
  48. public void setY(int yPos) {
  49. this.yPos = yPos;
  50. }
  51.  
  52. public int getEnergy() {
  53. return energy;
  54. }
  55.  
  56. public void setEnergy(int energy) {
  57. this.energy = energy;
  58. }
  59.  
  60. public String toString() {
  61. return species + "(" + xPos + "," + yPos + ")";
  62. }
  63.  
  64. public String toText() {
  65. return id + " " + toString() + " Energy: " + energy;
  66. }
  67.  
  68.  
  69. AnEntity() {
  70. species = null;
  71. id = -1;
  72. xPos = -1;
  73. yPos = -1;
  74. energy = -1;
  75. };
  76.  
  77. AnEntity(String _species, char _symbol, int _energy, int _xPos, int _yPos) {
  78. species = _species;
  79. symbol = _symbol;
  80. energy = _energy;
  81. xPos = _xPos;
  82. yPos = _yPos;
  83. lastID++;
  84. id = lastID;
  85. }
  86.  
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93. class AWorld {
  94. int xSize;
  95. int ySize;
  96. int currentEntities;
  97. AnEntity entities[];
  98. char[] symbolArray;
  99.  
  100. public int getX() {
  101. return xSize;
  102. }
  103.  
  104. public void setX(int xSize) {
  105. this.xSize = xSize;
  106. symbolArray = new char[xSize*ySize];
  107. }
  108.  
  109. public int getY() {
  110. return ySize;
  111. }
  112.  
  113. public void setY(int ySize) {
  114. this.ySize = ySize;
  115. symbolArray = new char[xSize*ySize];
  116. }
  117.  
  118. public int getSize() {
  119. return entities.length;
  120. }
  121.  
  122. public boolean setSize(int size) {
  123. if(size > xSize * ySize)
  124. {
  125. System.out.println("Maximum number of entities exceeds the size of the world.");
  126. return false;
  127. }
  128. entities = new AnEntity[size];
  129. return true;
  130. }
  131.  
  132. public char[] getSymbolArray() {
  133. for(int i = 0; i < symbolArray.length; i++)
  134. {
  135. symbolArray[i] = ' ';
  136. }
  137. for (int i = 0; i < currentEntities; i++)
  138. {
  139. if(!(entities[i].getY() < 0 || entities[i].getX() < 0))
  140. {
  141. symbolArray[ (xSize * entities[i].getY()) + entities[i].getX()] = entities[i].getSymbol();
  142.  
  143. }
  144. }
  145. return symbolArray;
  146. }
  147.  
  148. public boolean addEntity(String species, char symbol, int energy, int x, int y) {
  149. if(currentEntities < entities.length) {
  150. if(x < xSize && y < ySize) {
  151. AnEntity entityToAdd = new AnEntity(species, symbol, energy, x, y);
  152. entities[currentEntities] = entityToAdd;
  153. currentEntities++;
  154. return true;
  155. }
  156. else
  157. {
  158. System.out.println("Co-ordinates of entity outside of maximum bounds!");
  159. }
  160. }
  161. else
  162. {
  163. System.out.println("Entities limit reached! Cannot add any more entities.");
  164. }
  165. return false;
  166. }
  167.  
  168. public String[] getList() {
  169. String list[] = new String[currentEntities];
  170. for (int i = 0; i < currentEntities; i++)
  171. {
  172. list[i] = entities[i].toText();
  173. }
  174. return list;
  175. }
  176.  
  177. public boolean fromText(String text) {
  178. String[] input = text.split(" ", 5);
  179. int x = Integer.parseInt(input[0]);
  180. int y = Integer.parseInt(input[1]);
  181. int foodNumber = (Integer.parseInt(input[2])*100)/(x*y);
  182. int obstNumber = (Integer.parseInt(input[3])*100)/(x*y);
  183. String[] input2 = input[4].split(" ");
  184. int total = 0;
  185. for (int i = 1; i < input2.length; i+=2)
  186. {
  187. total += Integer.parseInt(input2[i]);
  188. }
  189. total += foodNumber;
  190. total += obstNumber;
  191. if(total > entities.length)
  192. {
  193. System.out.println("This would require " +total+ " entities, whereas the maximum number that can be created is " + entities.length);
  194. return false;
  195. }
  196. xSize = x;
  197. ySize = y;
  198. for (int i = 0; i < foodNumber; i++)
  199. {
  200. addEntity("food", 'f', 5, -1, -1);
  201. }
  202. for (int i = 0; i < obstNumber; i++)
  203. {
  204. addEntity("obstacle", 'O', -1, -1, -1);
  205. }
  206. for (int i = 0; i < input2.length; i+=2)
  207. {
  208. for (int j = 0; j < Integer.parseInt(input2[i+1]); j++)
  209. {
  210. addEntity(input2[i], input2[i].charAt(0), 0, -1, -1);
  211. }
  212. }
  213. return true;
  214. }
  215.  
  216. public
  217.  
  218. AWorld() {
  219. xSize = 0;
  220. ySize = 0;
  221. currentEntities = 0;
  222. entities = null;
  223. symbolArray = null;
  224. }
  225.  
  226. AWorld(int x, int y, int size) {
  227. xSize = x;
  228. ySize = y;
  229. currentEntities = 0;
  230. symbolArray = new char[x*y];
  231.  
  232. if(size <= x*y) {
  233. entities = new AnEntity[size];
  234. }
  235. else {
  236. entities = new AnEntity[x*y];
  237. System.out.println("Maximum number of entities exceeds the size of the world. Size has been set to the maximum, " + entities.length);
  238. }
  239. }
  240.  
  241. }
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256. import javax.swing.JOptionPane;
  257.  
  258. class AnInterface {
  259. AWorld theWorld;
  260. public void addEntity(){
  261. String inputHolder = JOptionPane.showInputDialog("Please enter the entity species name, symbol, energy, x position, and y position; separated with spaces.");
  262. String[] input = inputHolder.split(" ");
  263. theWorld.addEntity(input[0], input[1].charAt(0), Integer.parseInt(input[2]), Integer.parseInt(input[3]), Integer.parseInt(input[4]));
  264. }
  265.  
  266. public void configure(){
  267. String input = JOptionPane.showInputDialog("Please enter x size, y size, %food, %obstacles, and species plus the number");
  268. theWorld.fromText(input);
  269. }
  270.  
  271. public void display(){
  272. char symbolArray[] = theWorld.getSymbolArray();
  273. String toPrint = "/";
  274. for (int i = 0; i < theWorld.getX(); i++)
  275. {
  276. toPrint += "-";
  277. }
  278. toPrint += "\\\n";
  279. for(int i = 0; i < theWorld.getY(); i++)
  280. {
  281. toPrint += "|";
  282. toPrint += ( new String( symbolArray, i*theWorld.getX(),theWorld.getX() ) );
  283. toPrint += "|\n";
  284. }
  285. toPrint += "\\";
  286. for (int i = 0; i < theWorld.getX(); i++)
  287. {
  288. toPrint += "-";
  289. }
  290. toPrint += "/\n";
  291. System.out.print(toPrint);
  292. }
  293.  
  294. public void list(){
  295. String printList[] = theWorld.getList();
  296. for (int i = 0; i < printList.length; i++)
  297. {
  298. System.out.println(printList[i]);
  299. }
  300. }
  301. public static void main(String[] args)
  302. {
  303. AnInterface iface = new AnInterface();
  304. iface.theWorld = new AWorld(20,20,25);
  305. iface.configure();
  306. iface.list();
  307. }
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement