Advertisement
Guest User

Untitled

a guest
May 24th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.67 KB | None | 0 0
  1. class Life {
  2.  
  3. int UNDERPOP_LIM;
  4. int OVERPOP_LIM;
  5. int REPRODUCE_NUM;
  6.  
  7. int LOOPS_PER_MS;
  8.  
  9. int xMax;
  10. int yMax;
  11. int[] field;
  12.  
  13. public static void main(String[] a) {
  14. Life l;
  15. int unused;
  16.  
  17. l = new Life();
  18. l.init();
  19.  
  20. while (true) {
  21. l.printField();
  22. l.update();
  23. unused = io.read();
  24. }
  25.  
  26. }
  27.  
  28. public boolean init() {
  29. int[] lineLenA;
  30. int lineLen;
  31.  
  32. lineLenA = new int[1];
  33.  
  34. /* "Static" variables */
  35. UNDERPOP_LIM = 2;
  36. OVERPOP_LIM = 3;
  37. REPRODUCE_NUM = 3;
  38.  
  39. LOOPS_PER_MS = 225000;
  40.  
  41. /* Instance variables */
  42. field = this.field(lineLenA);
  43. lineLen = lineLenA[0];
  44.  
  45. xMax = lineLen - 1;
  46. yMax = field.length / lineLen - 1;
  47.  
  48. return true;
  49. }
  50.  
  51. /* Change this!
  52. * (might want to write a script
  53. * to autogenerate every assignment...)
  54. *
  55. * lineLen is "pass by reference",
  56. * and we modify it to return (e.g. time_t *time)
  57. */
  58. public int[] field(int[] lineLen) {
  59. int[] field;
  60.  
  61. field = new int[100];
  62. lineLen[0] = 10;
  63.  
  64. field[0] = 0;
  65. field[1] = 0;
  66. field[2] = 1;
  67. field[3] = 0;
  68. field[4] = 0;
  69. field[5] = 0;
  70. field[6] = 0;
  71. field[7] = 0;
  72. field[8] = 0;
  73. field[9] = 0;
  74. field[10] = 1;
  75. field[11] = 0;
  76. field[12] = 1;
  77. field[13] = 0;
  78. field[14] = 0;
  79. field[15] = 0;
  80. field[16] = 0;
  81. field[17] = 0;
  82. field[18] = 0;
  83. field[19] = 0;
  84. field[20] = 0;
  85. field[21] = 1;
  86. field[22] = 1;
  87. field[23] = 0;
  88. field[24] = 0;
  89. field[25] = 0;
  90. field[26] = 0;
  91. field[27] = 0;
  92. field[28] = 0;
  93. field[29] = 0;
  94. field[30] = 0;
  95. field[31] = 0;
  96. field[32] = 0;
  97. field[33] = 0;
  98. field[34] = 0;
  99. field[35] = 0;
  100. field[36] = 0;
  101. field[37] = 0;
  102. field[38] = 0;
  103. field[39] = 0;
  104. field[40] = 0;
  105. field[41] = 0;
  106. field[42] = 0;
  107. field[43] = 0;
  108. field[44] = 0;
  109. field[45] = 0;
  110. field[46] = 0;
  111. field[47] = 0;
  112. field[48] = 0;
  113. field[49] = 0;
  114. field[50] = 0;
  115. field[51] = 0;
  116. field[52] = 0;
  117. field[53] = 0;
  118. field[54] = 0;
  119. field[55] = 0;
  120. field[56] = 0;
  121. field[57] = 0;
  122. field[58] = 0;
  123. field[59] = 0;
  124. field[60] = 0;
  125. field[61] = 0;
  126. field[62] = 0;
  127. field[63] = 0;
  128. field[64] = 0;
  129. field[65] = 0;
  130. field[66] = 0;
  131. field[67] = 0;
  132. field[68] = 0;
  133. field[69] = 0;
  134. field[70] = 0;
  135. field[71] = 0;
  136. field[72] = 0;
  137. field[73] = 0;
  138. field[74] = 0;
  139. field[75] = 0;
  140. field[76] = 0;
  141. field[77] = 0;
  142. field[78] = 0;
  143. field[79] = 0;
  144. field[80] = 0;
  145. field[81] = 0;
  146. field[82] = 0;
  147. field[83] = 0;
  148. field[84] = 0;
  149. field[85] = 0;
  150. field[86] = 0;
  151. field[87] = 0;
  152. field[88] = 0;
  153. field[89] = 0;
  154. field[90] = 0;
  155. field[91] = 0;
  156. field[92] = 0;
  157. field[93] = 0;
  158. field[94] = 0;
  159. field[95] = 0;
  160. field[96] = 0;
  161. field[97] = 0;
  162. field[98] = 0;
  163. field[99] = 0;
  164.  
  165. return field;
  166.  
  167. }
  168.  
  169. public boolean update() {
  170. int i;
  171. int cur;
  172. int neighN;
  173. boolean goodPop;
  174. int[] newField;
  175.  
  176. newField = new int[field.length];
  177.  
  178. i = 0;
  179. while (i < field.length) {
  180. cur = field[i];
  181. neighN = this.getLiveNeighborN(i);
  182.  
  183.  
  184. // Live cell
  185. if (!(cur < 1)) {
  186. goodPop = this.ge(neighN,UNDERPOP_LIM) && this.le(neighN,OVERPOP_LIM);
  187. if (!goodPop) {
  188. newField[i] = 0;
  189. } else {
  190. newField[i] = field[i];
  191. }
  192. }
  193. // Dead cell
  194. else {
  195. if (this.eq(neighN,REPRODUCE_NUM)) {
  196. newField[i] = 1;
  197. } else {
  198. newField[i] = field[i];
  199. }
  200. }
  201.  
  202. i = i + 1;
  203. }
  204.  
  205. field = newField;
  206. return true;
  207.  
  208. }
  209.  
  210. public boolean printField() {
  211.  
  212. int i;
  213. int j;
  214.  
  215. i = 0;
  216. j = 0;
  217. while (i < field.length) {
  218. if (this.gt(j,xMax)) {
  219. io.println();
  220. j = 0;
  221. }
  222. else {}
  223. io.println(field[i]);
  224.  
  225. i = i + 1;
  226. j = j + 1;
  227. }
  228.  
  229. io.println();
  230. io.println();
  231. return true;
  232.  
  233. }
  234.  
  235. public int trIdx(int x, int y) {
  236. return x + (xMax + 1) * y;
  237. }
  238.  
  239. public int[] cartIdx(int absPos) {
  240. int x;
  241. int y;
  242. int xLim;
  243. int[] ret;
  244.  
  245. xLim = xMax + 1;
  246.  
  247. y = absPos / xLim;
  248. x = absPos - y * xLim;
  249.  
  250. ret = new int[2];
  251. ret[0] = x;
  252. ret[1] = y;
  253.  
  254. return ret;
  255.  
  256. }
  257.  
  258. public int[] getNeighborCoords(int absPos) {
  259. int x;
  260. int y;
  261.  
  262. int upX;
  263. int upY;
  264. int downX;
  265. int downY;
  266.  
  267. int[] cart;
  268. int[] ret;
  269.  
  270. cart = this.cartIdx(absPos);
  271. x = cart[0];
  272. y = cart[1];
  273.  
  274. if (x < xMax) {
  275. downX = x + 1;
  276. if (this.gt(x,0))
  277. upX = x - 1;
  278. else
  279. upX = xMax;
  280. } else {
  281. downX = 0;
  282. upX = x - 1;
  283. }
  284.  
  285. if (y < yMax) {
  286. downY = y + 1;
  287. if (this.gt(y,0))
  288. upY = y - 1;
  289. else
  290. upY = yMax;
  291. } else {
  292. downY = 0;
  293. upY = y - 1;
  294. }
  295.  
  296. ret = new int[8];
  297. // Clockwise from N
  298. ret[0] = this.trIdx(x, upY);
  299. ret[1] = this.trIdx(upX, upY);
  300. ret[2] = this.trIdx(upX, y);
  301. ret[3] = this.trIdx(upX, downY);
  302. ret[4] = this.trIdx(x, downY);
  303. ret[5] = this.trIdx(downX, downY);
  304. ret[6] = this.trIdx(downX, y);
  305. ret[7] = this.trIdx(downX, upY);
  306.  
  307. return ret;
  308.  
  309. }
  310.  
  311. public int getLiveNeighborN(int absPos) {
  312. int[] neigh;
  313. int i;
  314. int ret;
  315.  
  316. ret = 0;
  317.  
  318. neigh = this.getNeighborCoords(absPos);
  319.  
  320. i = 0;
  321. while (i < neigh.length) {
  322. if (this.ne(field[neigh[i]],0))
  323. ret = ret + 1;
  324. else {
  325. }
  326.  
  327. i = i + 1;
  328. }
  329.  
  330. return ret;
  331. }
  332.  
  333. public boolean busyWait(int ms) {
  334. int i;
  335. int n;
  336.  
  337. n = ms * LOOPS_PER_MS;
  338.  
  339. // Try optimizing this away!
  340. i = 0;
  341. while (i < n) {
  342. i = i + 1;
  343. }
  344.  
  345. return true;
  346. }
  347.  
  348. public boolean eq(int a, int b) {
  349. return (!this.lt(a, b) && !this.lt(b, a));
  350. }
  351.  
  352. public boolean ne(int a, int b) {
  353. return (!this.eq(a, b));
  354. }
  355.  
  356. public boolean lt(int a, int b) {
  357. return (a < b);
  358. }
  359.  
  360. public boolean le(int a, int b) {
  361. return !(!this.lt(a, b) && !this.eq(a, b));
  362. }
  363.  
  364. public boolean gt(int a, int b) {
  365. return (!this.le(a, b));
  366. }
  367.  
  368. public boolean ge(int a, int b) {
  369. return !(!this.gt(a, b) && !this.eq(a, b));
  370. }
  371.  
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement