Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. package packageName;
  2.  
  3. public class Gameoflife {
  4.  
  5. private static Boolean[][] world = new Boolean[20][20];
  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 < 100; 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. world[x][y] = true;
  74. }
  75.  
  76. // BOTTOM VALUES
  77. // if not at bottom border
  78. if (y < world.length - 1) {
  79. // if not at left border
  80. if (x > 0) {
  81. // top left
  82. if (world[x - 1][y + 1])
  83. number++;
  84. }
  85.  
  86. // top middle
  87. if (world[x][y + 1])
  88. number++;
  89.  
  90. // if not at right border
  91. if (x < world[0].length - 1) {
  92. if (world[x + 1][y + 1])
  93. number++;
  94. }
  95. }
  96.  
  97. // if the cell is alive
  98. if (world[x][y]) {
  99. // any cell with fewer than 2 neighbours dies
  100. if (number < 2)
  101. newWorld[x][y] = false;
  102. // any cell with 2 or 3 neighbours survives
  103. if (number == 2 || number == 3)
  104. newWorld[x][y] = true;
  105. // any cell with more than 3 neighbours dies
  106. if (number > 3)
  107. newWorld[x][y] = false;
  108. } else {
  109. // if a dead cell has 3 neighbours it becomes alive
  110. if (number == 3)
  111. newWorld[x][y] = true;
  112. else
  113. newWorld[x][y] = false;
  114. }
  115. if ((x < 18) && (y < 18)) {
  116. if (world[x][y] && world[x + 1][y]) {
  117. world[x][y + 1] = true;
  118. world[x+1][y] = true;
  119. }
  120. }
  121. }
  122. }
  123. world = newWorld;
  124. }
  125.  
  126. public static void displayGrid() {
  127. // creates output in console window
  128. for (int x = 0; x < world.length; x++) { // moves through columns {
  129. for (int y = 0; y < world.length; y++) // moves through rows {
  130. {
  131. if (world[x][y])
  132. System.out.print("v");
  133. else
  134. System.out.print(".");
  135. }
  136. System.out.println("\n");
  137. }
  138.  
  139. System.out.println(""); // move down to next line after row is finished printing
  140. //
  141. System.out.println(""); // insert a blank line after the entire 2d arrays has been shown
  142. System.out.println("--------------------"); // add some dashes
  143. System.out.println(""); // one more blank line
  144. }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement