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.

86 lignes
2.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. //Thread[] threads = new Thread[g.getWidth()];
  8. Grid newGrid = new Grid(g.getWidth(), g.getHeight());
  9. for (int i = 0; i < g.getWidth(); i++) {
  10. //Conway.rowThread rt = new Conway.rowThread(g, newGrid, i);
  11. //Thread t = new Thread(rt);
  12. //rt.setThread(t);
  13. //threads[i] = t;
  14. //t.run();
  15. for (int j = 0; j < g.getHeight(); j++) {
  16. Patch[] neighbors = g.getPatch(i, j).get8Neighbors();
  17. int numAlive = 0;
  18. for (Patch neighbor : neighbors)
  19. if (neighbor.getState() == 1) numAlive++;
  20. Patch p = g.getPatch(i,j).clone(newGrid);
  21. if (numAlive < 2) {
  22. p.setState(0); //Dies by underpopulation
  23. }
  24. if (numAlive > 3) {
  25. p.setState(0); //Dies by overpopulation
  26. }
  27. if (numAlive == 3)
  28. p.setState(1); //Born with 3 neighbors.
  29. newGrid.setPatch(i,j,p);
  30. }
  31. }
  32. //boolean running = true;
  33. //while (running) {
  34. //running = false;
  35. //for (Thread t : threads) {
  36. //if (t.isAlive()) running = true;
  37. //}
  38. //}
  39. return newGrid;
  40. }
  41. //public static class rowThread implements Runnable {
  42. //private Grid _original, _newGrid;
  43. //private int _rowNum;
  44. //private Thread _thread;
  45. //public rowThread (Grid orig, Grid newGrid, int rowNum) {
  46. //_original = orig;
  47. //_newGrid = newGrid;
  48. //_rowNum = rowNum;
  49. //}
  50. //public void setThread (Thread t) {
  51. //_thread = t;
  52. //}
  53. //public void run () {
  54. //for (int j = 0; j < _original.getHeight(); j++) {
  55. //Patch[] neighbors = _original.getPatch(_rowNum, j).get8Neighbors();
  56. //int numAlive = 0;
  57. //for (Patch p : neighbors)
  58. //if (p.getState() == 1) numAlive++;
  59. //Patch p = _original.getPatch(_rowNum,j).clone(_newGrid);
  60. //if (numAlive < 2) {
  61. //p.setState(0); //Dies by underpopulation
  62. //}
  63. //if (numAlive > 3) {
  64. //p.setState(0); //Dies by overpopulation
  65. //}
  66. //if (numAlive == 3)
  67. //p.setState(1); //Born with 3 neighbors.
  68. //_newGrid.setPatch(_rowNum,j,p);
  69. //}
  70. //try {
  71. //_thread.join();
  72. //} catch (Exception e) {
  73. //e.printStackTrace();
  74. //}
  75. //}
  76. //}
  77. }