Guest User

Untitled

a guest
Dec 10th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. //John Conway's Game of Life Clone - by Virgile Grob
  2.  
  3. static ArrayList<Cell> actors = new ArrayList<Cell>();
  4. final int[] adjOrdx = {-1, 0, 1, 1, 1, 0, -1, -1};
  5. final int[] adjOrdy = {-1, -1, -1, 0, 1, 1, 1, 0};
  6.  
  7. void setup() {
  8. fill(255, 100);
  9. rect(0,0,width,height);
  10. fill(0, 100);
  11. actors.add(new Cell(200, 200));
  12. actors.get(0).isAlive = true;
  13. actors.add(new Cell(196, 196));
  14. actors.get(1).isAlive = true;
  15. actors.add(new Cell(204, 196));
  16. actors.get(2).isAlive = true;
  17. actors.add(new Cell(200, 204));
  18. actors.get(3).isAlive = true;
  19. actors.add(new Cell(204, 200));
  20. actors.get(4).isAlive = true;
  21. frameRate(2);
  22. size(1200, 800);
  23. }
  24. void draw(){
  25. fill(255, 255);
  26. rect(0,0,width,height);
  27. fill(0, 100);
  28. if(actors.size() > 0){
  29. for(int a = 0; a<actors.size(); a++)
  30. rect(actors.get(a).location.x - 2, actors.get(a).location.y - 2, 4, 4);
  31. ArrayList<Cell> birth = willBeBorn();
  32. //System.out.println("There are " + birth.size() + " items in birth.");
  33. death();
  34. for(int b = 0; b<birth.size(); b++)
  35. actors.add(birth.get(b));
  36. }
  37. }
  38. class Cell{
  39. PVector location;
  40. boolean isAlive;
  41. public Cell(float xloc, float yloc){
  42. location = new PVector(xloc, yloc);
  43. isAlive = false;
  44. }
  45. public int getNumAdj(){
  46. int n = 0;
  47. for(int i=0; i<8; i++)
  48. for(int a=0; a<actors.size(); a++)
  49. if((actors.get(a).location.x == location.x+(adjOrdx[i]*4)) && (actors.get(a).location.y == location.y+(adjOrdy[i]*4)))
  50. n++;
  51. System.out.println(n);
  52. return n;
  53. }
  54. public boolean willSurvive(){
  55. if(getNumAdj() == 2 || getNumAdj() == 3)
  56. return true;
  57. return false;
  58. }
  59. public boolean isntAlreadyAlive(){
  60. for (int a = 0; a<actors.size(); a++)
  61. if(actors.get(a).location.x == location.x && actors.get(a).location.y == location.y)
  62. return false;
  63. return true;
  64. }
  65. public boolean isntAlreadyBorn(ArrayList<Cell> born){
  66. for (int b = 0; b<born.size(); b++)
  67. if(born.get(b).location.x == location.x && born.get(b).location.y == location.y)
  68. return false;
  69. return true;
  70. }
  71. public boolean isBorn(){
  72. if(getNumAdj() == 3 && isntAlreadyAlive())
  73. return true;
  74. return false;
  75. }
  76. public ArrayList<Cell> getAdjCells(){
  77. ArrayList<Cell> adj = new ArrayList<Cell>();
  78. for(int i=0; i<8; i++)
  79. adj.add(new Cell(location.x+(adjOrdx[i]*4), location.y+(adjOrdy[i]*4)));
  80. return adj;
  81. }
  82.  
  83.  
  84. }
  85. public static void death(){
  86. for(int a=0; a<actors.size(); a++){
  87. System.out.println(actors.get(a).willSurvive());
  88. if(!actors.get(a).willSurvive())
  89. actors.remove(a);
  90. }
  91. }
  92. public static ArrayList willBeBorn(){
  93. ArrayList<Cell> temp = new ArrayList<Cell>();
  94. for(int a = 0; a<actors.size(); a++){
  95. ArrayList<Cell> adjCells = actors.get(a).getAdjCells();
  96. for(int b = 0; b<adjCells.size(); b++)
  97. if(adjCells.get(b).isBorn() && adjCells.get(b).isntAlreadyBorn(temp)){
  98. adjCells.get(b).isAlive = true;
  99. temp.add(adjCells.get(b));
  100. }
  101. }
  102. return temp;
  103. }
Add Comment
Please, Sign In to add comment