A simple Game of Life implementation in Java
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. String rule = _render.rule;
  22. if (rule.equals("Conway"))
  23. _grid = Conway.run(_grid);
  24. else if (rule.equals("Conway4"))
  25. _grid = Conway4.run(_grid);
  26. else if (rule.equals("Life Without Death"))
  27. _grid = LifeWithoutDeath.run(_grid);
  28. else if (rule.equals("Brian's Brain"))
  29. _grid = BriansBrain.run(_grid);
  30. }
  31. _render.setGrid(_grid);
  32. _render.run();
  33. _render.sleep();
  34. }
  35. }
  36. public static int getMaxStates(String rule) {
  37. if (rule.equals("Conway"))
  38. return Conway.states;
  39. else if (rule.equals("Conway4"))
  40. return Conway4.states;
  41. else if (rule.equals("Life Without Death"))
  42. return LifeWithoutDeath.states;
  43. else if (rule.equals("Brian's Brain"))
  44. return BriansBrain.states;
  45. return 2;
  46. }
  47. private void setup(String rule) {
  48. if (rule.equals("Conway")) {
  49. int[][] glidergun = {
  50. {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},
  51. {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},
  52. {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},
  53. {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},
  54. {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},
  55. {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},
  56. {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},
  57. {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},
  58. {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}
  59. };
  60. for (int i = 0; i < 36; i++) {
  61. for (int j = 0; j < 9; j++) {
  62. _grid.getPatch(i + 2, j + 2).setState(glidergun[j][i]);
  63. }
  64. }
  65. } else if (rule.equals("Life Without Death")) {
  66. int[][] pattern = {
  67. {1,1,1,1,0,1},
  68. {1,0,1,1,1,1}
  69. };
  70. for (int i = 0; i < 6; i++) {
  71. for (int j = 0; j < 2; j++) {
  72. _grid.getPatch(i + ((_grid.getHeight() - 6) / 2),
  73. j + ((_grid.getWidth() - 2) / 2)).setState(pattern[j][i]);
  74. }
  75. }
  76. } else if (rule.equals("Brian's Brain")) {
  77. int[][] pattern = {
  78. {2,0,2,0,2},
  79. {2,0,2,0,2},
  80. {0,1,0,1,0}
  81. };
  82. for (int i = 0; i < 5; i++) {
  83. for (int j = 0; j < 3; j++) {
  84. _grid.getPatch(i + ((_grid.getHeight() - 5) / 2),
  85. j + ((_grid.getWidth() - 3) / 2)).setState(pattern[j][i]);
  86. }
  87. }
  88. }
  89. }
  90. public static void main(String[] args) {
  91. Goldfish g = new Goldfish();
  92. g.run();
  93. }
  94. }