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.

75 lines
2.2 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. doLogic(_render.rule);
  23. _render.releaseLock(0);
  24. }
  25. _render.setGrid(_grid);
  26. _render.run();
  27. _render.sleep();
  28. }
  29. }
  30. private void doLogic(String rule) {
  31. if (rule.equals("Conway"))
  32. _grid = Conway.run(_grid);
  33. else if (rule.equals("Conway4"))
  34. _grid = Conway4.run(_grid);
  35. else if (rule.equals("Life Without Death"))
  36. _grid = LifeWithoutDeath.run(_grid);
  37. else if (rule.equals("Brian's Brain"))
  38. _grid = BriansBrain.run(_grid);
  39. }
  40. public static int getMaxStates(String rule) {
  41. if (rule.equals("Conway"))
  42. return Conway.states;
  43. else if (rule.equals("Conway4"))
  44. return Conway4.states;
  45. else if (rule.equals("Life Without Death"))
  46. return LifeWithoutDeath.states;
  47. else if (rule.equals("Brian's Brain"))
  48. return BriansBrain.states;
  49. return 2;
  50. }
  51. private void setup(String rule) {
  52. if (rule.equals("Conway"))
  53. Conway.setup(_grid);
  54. else if (rule.equals("Conway4"))
  55. Conway4.setup(_grid);
  56. else if (rule.equals("Life Without Death"))
  57. LifeWithoutDeath.setup(_grid);
  58. else if (rule.equals("Brian's Brain"))
  59. BriansBrain.setup(_grid);
  60. }
  61. public static void main(String[] args) {
  62. Goldfish g = new Goldfish();
  63. g.run();
  64. }
  65. }