A simple Game of Life implementation in Java
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

47 lignes
1.9 KiB

  1. package edu.stuy.goldfish.rules;
  2. import edu.stuy.goldfish.Grid;
  3. import edu.stuy.goldfish.Patch;
  4. public class Conway extends RuleSet {
  5. public static int states = 2;
  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. int numAlive = orig.get8Neighbors(1, 4);
  12. Patch p = orig.clone(newGrid);
  13. if (numAlive < 2)
  14. p.setState(0); // Dies by underpopulation
  15. else if (numAlive > 3)
  16. p.setState(0); // Dies by overpopulation
  17. else if (numAlive == 3)
  18. p.setState(1); // Born with 3 neighbors
  19. newGrid.setPatch(i, j, p);
  20. }
  21. }
  22. return newGrid;
  23. }
  24. public static void setup(Grid g) {
  25. int[][] glidergun = {
  26. {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},
  27. {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},
  28. {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},
  29. {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},
  30. {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},
  31. {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},
  32. {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},
  33. {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},
  34. {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}
  35. };
  36. for (int i = 0; i < 36; i++) {
  37. for (int j = 0; j < 9; j++) {
  38. g.getPatch(i + 2, j + 2).setState(glidergun[j][i]);
  39. }
  40. }
  41. }
  42. }