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.

96 lines
3.2 KiB

  1. package edu.stuy.goldfish;
  2. import java.util.Random;
  3. import edu.stuy.goldfish.rules.*;
  4. public class Goldfish {
  5. private static final String[] RULES = {"Conway", "Conway4", "Life Without Death", "Brian's Brain"};
  6. private Grid _grid;
  7. private Render _render;
  8. private Random random = new Random();
  9. public Goldfish() {
  10. int width = 128;
  11. int height = 128;
  12. _grid = new Grid(width, height);
  13. _render = new Render(width, height, _grid, RULES);
  14. }
  15. public void run() {
  16. setup(_render.rule);
  17. while (true) {
  18. if (_render.reset) {
  19. setup(_render.rule);
  20. _render.reset = false;
  21. }
  22. if (!_render.paused) {
  23. String rule = _render.rule;
  24. if (rule.equals("Conway"))
  25. _grid = Conway.run(_grid);
  26. else if (rule.equals("Conway4"))
  27. _grid = Conway4.run(_grid);
  28. else if (rule.equals("Life Without Death"))
  29. _grid = LifeWithoutDeath.run(_grid);
  30. else if (rule.equals("Brian's Brain"))
  31. _grid = BriansBrain.run(_grid);
  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),j+((_grid.getWidth()-2)/2)).setState(pattern[j][i]);
  75. }
  76. }
  77. } else if (rule.equals("Brian's Brain")) {
  78. _render.randomize();
  79. }
  80. }
  81. public static void main(String[] args) {
  82. Goldfish g = new Goldfish();
  83. g.run();
  84. }
  85. }