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.

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