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.

Goldfish.java 2.5 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /* Main method for running the simulator. */
  14. public void run() {
  15. setup(_render.rule);
  16. while (true) {
  17. if (_render.reset) {
  18. _render.acquireLock(0);
  19. setup(_render.rule);
  20. _render.releaseLock(0);
  21. _render.reset = false;
  22. }
  23. if (!_render.paused) {
  24. _render.acquireLock(0);
  25. doLogic(_render.rule);
  26. _render.releaseLock(0);
  27. }
  28. _render.setGrid(_grid);
  29. _render.run();
  30. _render.sleep();
  31. }
  32. }
  33. /* Advance the grid one step using the given algorithm. */
  34. private void doLogic(String rule) {
  35. if (rule.equals("Conway"))
  36. _grid = Conway.run(_grid);
  37. else if (rule.equals("Conway4"))
  38. _grid = Conway4.run(_grid);
  39. else if (rule.equals("Life Without Death"))
  40. _grid = LifeWithoutDeath.run(_grid);
  41. else if (rule.equals("Brian's Brain"))
  42. _grid = BriansBrain.run(_grid);
  43. }
  44. /* Return the max number of states a patch can be in for the given rule. */
  45. public static int getMaxStates(String rule) {
  46. if (rule.equals("Conway"))
  47. return Conway.states;
  48. else if (rule.equals("Conway4"))
  49. return Conway4.states;
  50. else if (rule.equals("Life Without Death"))
  51. return LifeWithoutDeath.states;
  52. else if (rule.equals("Brian's Brain"))
  53. return BriansBrain.states;
  54. return 2;
  55. }
  56. /* Generate a default shape for the grid based on the given rule. */
  57. private void setup(String rule) {
  58. if (rule.equals("Conway"))
  59. Conway.setup(_grid);
  60. else if (rule.equals("Conway4"))
  61. Conway4.setup(_grid);
  62. else if (rule.equals("Life Without Death"))
  63. LifeWithoutDeath.setup(_grid);
  64. else if (rule.equals("Brian's Brain"))
  65. BriansBrain.setup(_grid);
  66. }
  67. public static void main(String[] args) {
  68. Goldfish g = new Goldfish();
  69. g.run();
  70. }
  71. }