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.

67 lines
2.1 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();
  15. while (true) {
  16. if (!_render.paused) {
  17. String rule = _render.rule;
  18. if (rule.equals("Conway"))
  19. _grid = Conway.run(_grid);
  20. else if (rule.equals("Conway4"))
  21. _grid = Conway4.run(_grid);
  22. else if (rule.equals("Life Without Death"))
  23. _grid = LifeWithoutDeath.run(_grid);
  24. else if (rule.equals("Brian's Brain"))
  25. _grid = BriansBrain.run(_grid);
  26. }
  27. _render.setGrid(_grid);
  28. _render.run();
  29. _render.sleep();
  30. }
  31. }
  32. public static int getMaxStates(String rule) {
  33. if (rule.equals("Conway"))
  34. return Conway.states;
  35. else if (rule.equals("Conway4"))
  36. return Conway4.states;
  37. else if (rule.equals("Life Without Death"))
  38. return LifeWithoutDeath.states;
  39. else if (rule.equals("Brian's Brain"))
  40. return BriansBrain.states;
  41. return 2;
  42. }
  43. private void setup() {
  44. for (int i = 0; i < _grid.getWidth(); i += 16) {
  45. for (int j = 0; j < _grid.getHeight(); j += 16) {
  46. _grid.getPatch(i + 1, j + 0).setState(1);
  47. _grid.getPatch(i + 2, j + 1).setState(1);
  48. _grid.getPatch(i + 2, j + 2).setState(1);
  49. _grid.getPatch(i + 1, j + 2).setState(1);
  50. _grid.getPatch(i + 0, j + 2).setState(1);
  51. }
  52. }
  53. }
  54. public static void main(String[] args) {
  55. Goldfish g = new Goldfish();
  56. g.run();
  57. }
  58. }