A simple Game of Life implementation in Java
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

Goldfish.java 2.2 KiB

hace 11 años
hace 11 años
hace 11 años
hace 11 años
hace 11 años
hace 11 años
hace 11 años
hace 11 años
hace 11 años
hace 11 años
hace 11 años
hace 11 años
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. }