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.

98 lines
3.4 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. //System.out.println(numAlive);
  21. Patch p = g.getPatch(i,j).clone(newGrid);
  22. if (numAlive < 2) {
  23. p.setState(0); //Dies by underpopulation
  24. //System.out.println("ded by under");
  25. }
  26. if (numAlive > 3) {
  27. //System.out.println("ded by over");
  28. p.setState(0); //Dies by overpopulation
  29. }
  30. if (numAlive == 3)
  31. p.setState(1); //Born with 3 neighbors.
  32. newGrid.setPatch(i,j,p);
  33. }
  34. }
  35. System.out.println(g);
  36. System.out.println("-----");
  37. System.out.println(newGrid);
  38. //boolean running = true;
  39. //while (running) {
  40. //running = false;
  41. //for (Thread t : threads) {
  42. //if (t.isAlive()) running = true;
  43. //}
  44. //}
  45. return newGrid;
  46. }
  47. //public static class rowThread implements Runnable {
  48. //private Grid _original, _newGrid;
  49. //private int _rowNum;
  50. //private Thread _thread;
  51. //public rowThread (Grid orig, Grid newGrid, int rowNum) {
  52. //_original = orig;
  53. //_newGrid = newGrid;
  54. //_rowNum = rowNum;
  55. //}
  56. //public void setThread (Thread t) {
  57. //_thread = t;
  58. //}
  59. //public void run () {
  60. //for (int j = 0; j < _original.getHeight(); j++) {
  61. //Patch[] neighbors = _original.getPatch(_rowNum, j).get8Neighbors();
  62. //int numAlive = 0;
  63. //System.out.println(_original.getPatch(_rowNum, j).getX() + "," + _original.getPatch(_rowNum, j).getY());
  64. //for (Patch p : neighbors) {
  65. //System.out.print(p.getState());
  66. //if (p.getState() == 1) {
  67. //numAlive++;
  68. //}
  69. //}
  70. //System.out.println();
  71. //Patch p = _original.getPatch(_rowNum,j).clone();
  72. //if (numAlive < 2) {
  73. //p.setState(0); //Dies by underpopulation
  74. //}
  75. //if (numAlive > 3) {
  76. //p.setState(0); //Dies by overpopulation
  77. //}
  78. //if (numAlive == 3)
  79. //p.setState(1); //Born with 3 neighbors.
  80. //_newGrid.setPatch(_rowNum,j,p);
  81. //}
  82. //try {
  83. //_thread.join();
  84. //} catch (Exception e) {
  85. //e.printStackTrace();
  86. //}
  87. //}
  88. //}
  89. }