A simple Game of Life implementation in Java
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.3 KiB

  1. package edu.stuy.goldfish;
  2. import edu.stuy.goldfish.rules.*;
  3. public class Goldfish {
  4. private static final String[] RULES = {"Conway", "Conway4", "Life Without Death", "Brian's Brain"};
  5. private Grid _grid;
  6. private Render _render;
  7. public Goldfish() {
  8. int width = 512;
  9. int height = 512;
  10. _grid = new Grid(width, height);
  11. _render = new Render(width, height, _grid, RULES);
  12. }
  13. public void run() {
  14. setup(_render.rule);
  15. int runs = 0;
  16. double average = 0;
  17. while (true) {
  18. if (_render.reset) {
  19. setup(_render.rule);
  20. _render.reset = false;
  21. }
  22. if (!_render.paused) {
  23. long start = System.currentTimeMillis();
  24. String rule = _render.rule;
  25. if (rule.equals("Conway"))
  26. _grid = Conway.run(_grid);
  27. else if (rule.equals("Conway4"))
  28. _grid = Conway4.run(_grid);
  29. else if (rule.equals("Life Without Death"))
  30. _grid = LifeWithoutDeath.run(_grid);
  31. else if (rule.equals("Brian's Brain"))
  32. _grid = BriansBrain.run(_grid);
  33. long diff = System.currentTimeMillis() - start;
  34. average = (diff + (average * runs)) / (runs + 1);
  35. runs++;
  36. if (runs % 30 == 0)
  37. System.out.println(runs + ": " + average);
  38. }
  39. _render.setGrid(_grid);
  40. _render.run();
  41. _render.sleep();
  42. }
  43. }
  44. public static int getMaxStates(String rule) {
  45. if (rule.equals("Conway"))
  46. return Conway.states;
  47. else if (rule.equals("Conway4"))
  48. return Conway4.states;
  49. else if (rule.equals("Life Without Death"))
  50. return LifeWithoutDeath.states;
  51. else if (rule.equals("Brian's Brain"))
  52. return BriansBrain.states;
  53. return 2;
  54. }
  55. private void setup(String rule) {
  56. if (rule.equals("Conway")) {
  57. int[][] glidergun = {
  58. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
  59. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0},
  60. {0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
  61. {0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
  62. {1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  63. {1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0},
  64. {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
  65. {0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  66. {0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
  67. };
  68. for (int i = 0; i < 36; i++) {
  69. for (int j = 0; j < 9; j++) {
  70. _grid.getPatch(i + 2, j + 2).setState(glidergun[j][i]);
  71. }
  72. }
  73. } else if (rule.equals("Life Without Death")) {
  74. int[][] pattern = {
  75. {1,1,1,1,0,1},
  76. {1,0,1,1,1,1}
  77. };
  78. for (int i = 0; i < 6; i++) {
  79. for (int j = 0; j < 2; j++) {
  80. _grid.getPatch(i + ((_grid.getHeight() - 6) / 2),
  81. j + ((_grid.getWidth() - 2) / 2)).setState(pattern[j][i]);
  82. }
  83. }
  84. } else if (rule.equals("Brian's Brain")) {
  85. int[][] pattern = {
  86. {2,0,2,0,2},
  87. {2,0,2,0,2},
  88. {0,1,0,1,0}
  89. };
  90. for (int i = 0; i < 5; i++) {
  91. for (int j = 0; j < 3; j++) {
  92. _grid.getPatch(i + ((_grid.getHeight() - 5) / 2),
  93. j + ((_grid.getWidth() - 3) / 2)).setState(pattern[j][i]);
  94. }
  95. }
  96. }
  97. }
  98. public static void main(String[] args) {
  99. Goldfish g = new Goldfish();
  100. g.run();
  101. }
  102. }