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.

41 lines
1.3 KiB

  1. package edu.stuy.goldfish.rules;
  2. import edu.stuy.goldfish.Grid;
  3. import edu.stuy.goldfish.Patch;
  4. public class BriansBrain extends RuleSet {
  5. public static int states = 3;
  6. public static Grid run(Grid g) {
  7. Grid newGrid = new Grid(g.getWidth(), g.getHeight(), false);
  8. for (int i = 0; i < g.getWidth(); i++) {
  9. for (int j = 0; j < g.getHeight(); j++) {
  10. Patch orig = g.getPatch(i, j);
  11. Patch p = g.getPatch(i, j).clone(newGrid);
  12. if (orig.getState() == 1) p.setState(0); // Dying cells die.
  13. else if (orig.getState() == 2) p.setState(1); // Make living cells dying.
  14. else {
  15. int numAlive = orig.get8Neighbors(2, 3);
  16. if (orig.getState() == 0 && numAlive == 2) p.setState(2);
  17. }
  18. newGrid.setPatch(i,j,p);
  19. }
  20. }
  21. return newGrid;
  22. }
  23. public static void setup(Grid g) {
  24. int[][] pattern = {
  25. {2,0,2,0,2},
  26. {2,0,2,0,2},
  27. {0,1,0,1,0}
  28. };
  29. for (int i = 0; i < 5; i++) {
  30. for (int j = 0; j < 3; j++) {
  31. g.getPatch(i + ((g.getWidth() - 5) / 2),
  32. j + ((g.getHeight() - 3) / 2)).setState(pattern[j][i]);
  33. }
  34. }
  35. }
  36. }