Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.12 KB | None | 0 0
  1. package packageName;
  2.  
  3. public class Gameoflife {
  4.  
  5. private static Boolean[][] world = new Boolean[10][10];
  6.  
  7. public static void main(String[] args) throws InterruptedException {
  8. populate();
  9. begin_simulation();
  10. }
  11.  
  12. public static void begin_simulation() throws InterruptedException {
  13. // uses a loop to simulate time passing and to apply rules at each interval for
  14. // movement, new life, expired life
  15. for (int x = 0; x < 20; x++) { // simulate 7 rounds
  16. displayGrid(); // show the two dimensional array in the console window
  17. next_round(); // you will need to create this method and figure you what you want to happen
  18. // (apply the game rules) at each iteration
  19. Thread.sleep(500);
  20. }
  21. }
  22.  
  23. public static void populate() {
  24. // blanket fill all as false
  25. for (int x = 0; x < world.length; x++) { // moves through columns {
  26. for (int y = 0; y < world.length; y++) { // moves through rows {
  27. world[x][y] = false;
  28. }
  29. }
  30.  
  31. // custom values
  32. world[3][4] = true;
  33. world[4][4] = true;
  34. world[5][4] = true;
  35.  
  36. }
  37.  
  38. private static void next_round() {
  39. Boolean[][] newWorld = new Boolean[world.length][world[0].length];
  40.  
  41. for (int x = 0; x < world.length; x++) { // moves through columns {
  42. for (int y = 0; y < world.length; y++) { // moves through rows {
  43. // calculate number of living adjacent cells
  44. int number = 0;
  45.  
  46. if (y > 0) {
  47. if (x > 0) {
  48. if (world[x - 1][y - 1])
  49. number++;
  50. }
  51.  
  52. if (world[x][y - 1])
  53. number++;
  54.  
  55. if (x < world[0].length - 1) {
  56. if (world[x + 1][y - 1])
  57. number++;
  58. }
  59. }
  60.  
  61. // CENTRE VALUES
  62. // if not at left border
  63. if (x > 0) {
  64. // centre left
  65. if (world[x - 1][y])
  66. number++;
  67. }
  68.  
  69. // if not at right border
  70. if (x < world[0].length - 1) {
  71. // centre right
  72. if (world[x + 1][y])
  73. number++;
  74. world[x][y] = true;
  75. }
  76.  
  77. // BOTTOM VALUES
  78. // if not at bottom border
  79. if (y < world.length - 1) {
  80. // if not at left border
  81. if (x > 0) {
  82. // top left
  83. if (world[x - 1][y + 1])
  84. number++;
  85. }
  86.  
  87. // top middle
  88. if (world[x][y + 1])
  89. number++;
  90.  
  91. // if not at right border
  92. if (x < world[0].length - 1) {
  93. if (world[x + 1][y + 1])
  94. number++;
  95. }
  96. }
  97.  
  98. // if the cell is alive
  99. if (world[x][y]) {
  100. // any cell with fewer than 2 neighbours dies
  101. if (number < 2)
  102. newWorld[x][y] = false;
  103. // any cell with 2 or 3 neighbours survives
  104. if (number == 2 || number == 3)
  105. newWorld[x][y] = true;
  106. // any cell with more than 3 neighbours dies
  107. if (number > 3)
  108. newWorld[x][y] = false;
  109. } else {
  110. // if a dead cell has 3 neighbours it becomes alive
  111. if (number == 3)
  112. newWorld[x][y] = true;
  113. else
  114. newWorld[x][y] = false;
  115. }
  116. if ((x < 9) && (y < 9)) {
  117. if (world[x][y] && world[x + 1][y]) {
  118. world[x][y + 1] = true;
  119. world[x+1][y] = true;
  120. }
  121. }
  122. }
  123. }
  124. world = newWorld;
  125. }
  126.  
  127. public static void displayGrid() {
  128. // creates output in console window
  129. for (int x = 0; x < world.length; x++) { // moves through columns {
  130. for (int y = 0; y < world.length; y++) // moves through rows {
  131. {
  132. if (world[x][y])
  133. System.out.print("v");
  134. else
  135. System.out.print(".");
  136. }
  137. System.out.println("\n");
  138. }
  139.  
  140. System.out.println(""); // move down to next line after row is finished printing
  141. //
  142. System.out.println(""); // insert a blank line after the entire 2d arrays has been shown
  143. System.out.println("--------------------"); // add some dashes
  144. System.out.println(""); // one more blank line
  145. }
  146.  
  147. }
  148.  
  149. package packageName;
  150.  
  151. public class Gameoflife {
  152.  
  153. private static Boolean[][] world = new Boolean[10][10];
  154.  
  155. public static void main(String[] args) throws InterruptedException {
  156. populate();
  157. begin_simulation();
  158. }
  159.  
  160. public static void begin_simulation() throws InterruptedException {
  161. // uses a loop to simulate time passing and to apply rules at each interval for
  162. // movement, new life, expired life
  163. for (int x = 0; x < 20; x++) { // simulate 7 rounds
  164. displayGrid(); // show the two dimensional array in the console window
  165. next_round(); // you will need to create this method and figure you what you want to happen
  166. // (apply the game rules) at each iteration
  167. Thread.sleep(500);
  168. }
  169. }
  170.  
  171. public static void populate() {
  172. // blanket fill all as false
  173. for (int x = 0; x < world.length; x++) { // moves through columns {
  174. for (int y = 0; y < world.length; y++) { // moves through rows {
  175. world[x][y] = false;
  176. }
  177. }
  178.  
  179. // custom values
  180. world[3][4] = true;
  181. world[4][4] = true;
  182. world[5][4] = true;
  183.  
  184. }
  185.  
  186. private static void next_round() {
  187. Boolean[][] newWorld = new Boolean[world.length][world[0].length];
  188.  
  189. for (int x = 0; x < world.length; x++) { // moves through columns {
  190. for (int y = 0; y < world.length; y++) { // moves through rows {
  191. // calculate number of living adjacent cells
  192. int number = 0;
  193.  
  194. if (y > 0) {
  195. if (x > 0) {
  196. if (world[x - 1][y - 1])
  197. number++;
  198. }
  199.  
  200. if (world[x][y - 1])
  201. number++;
  202.  
  203. if (x < world[0].length - 1) {
  204. if (world[x + 1][y - 1])
  205. number++;
  206. }
  207. }
  208.  
  209. // CENTRE VALUES
  210. // if not at left border
  211. if (x > 0) {
  212. // centre left
  213. if (world[x - 1][y])
  214. number++;
  215. }
  216.  
  217. // if not at right border
  218. if (x < world[0].length - 1) {
  219. // centre right
  220. if (world[x + 1][y])
  221. number++;
  222. world[x][y] = true;
  223. }
  224.  
  225. // BOTTOM VALUES
  226. // if not at bottom border
  227. if (y < world.length - 1) {
  228. // if not at left border
  229. if (x > 0) {
  230. // top left
  231. if (world[x - 1][y + 1])
  232. number++;
  233. }
  234.  
  235. // top middle
  236. if (world[x][y + 1])
  237. number++;
  238.  
  239. // if not at right border
  240. if (x < world[0].length - 1) {
  241. if (world[x + 1][y + 1])
  242. number++;
  243. }
  244. }
  245.  
  246. // if the cell is alive
  247. if (world[x][y]) {
  248. // any cell with fewer than 2 neighbours dies
  249. if (number < 2)
  250. newWorld[x][y] = false;
  251. // any cell with 2 or 3 neighbours survives
  252. if (number == 2 || number == 3)
  253. newWorld[x][y] = true;
  254. // any cell with more than 3 neighbours dies
  255. if (number > 3)
  256. newWorld[x][y] = false;
  257. } else {
  258. // if a dead cell has 3 neighbours it becomes alive
  259. if (number == 3)
  260. newWorld[x][y] = true;
  261. else
  262. newWorld[x][y] = false;
  263. }
  264. if ((x < 9) && (y < 9)) {
  265. if (world[x][y] && world[x + 1][y]) {
  266. world[x][y + 1] = true;
  267. world[x+1][y] = true;
  268. }
  269. }
  270. }
  271. }
  272. world = newWorld;
  273. }
  274.  
  275. public static void displayGrid() {
  276. // creates output in console window
  277. for (int x = 0; x < world.length; x++) { // moves through columns {
  278. for (int y = 0; y < world.length; y++) // moves through rows {
  279. {
  280. if (world[x][y])
  281. System.out.print("v");
  282. else
  283. System.out.print(".");
  284. }
  285. System.out.println("\n");
  286. }
  287.  
  288. System.out.println(""); // move down to next line after row is finished printing
  289. //
  290. System.out.println(""); // insert a blank line after the entire 2d arrays has been shown
  291. System.out.println("--------------------"); // add some dashes
  292. System.out.println(""); // one more blank line
  293. }
  294.  
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement