Advertisement
Guest User

Colonies GAME OF LIFE CLASS

a guest
Oct 24th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. package gameoflife;
  2.  
  3. import java.util.*;
  4.  
  5. public class Colonies {
  6.  
  7.     private String name;
  8.  
  9.     private Set<Coor> cells = new HashSet();
  10.  
  11.     public Colonies(java.lang.String name) {
  12.         colonyNumber= getNextColonyNumber();
  13.         this.name = name;
  14.     }
  15.     private int colonyNumber;
  16. private static int nextColonyNumber= 0;
  17.     public static int getNextColonyNumber(){
  18.        
  19.         return nextColonyNumber++;
  20.     }
  21.  
  22.    
  23.     public int getColonyNumber(){
  24.         return colonyNumber;
  25.     }
  26.  
  27.     public java.lang.String getColonyName() {
  28.         return name;
  29.     }
  30.     private int gennumb = 0;
  31.  
  32.     public int getGenerationNumber() {
  33.  
  34.        
  35.         return gennumb;
  36.     }
  37.  
  38.     public void setCellAlive(int x, int y) {
  39.         Coor c = new Coor(x, y);
  40.         cells.add(c);
  41.     }
  42.  
  43.     public void setCellDead(int x, int y) {
  44.         Coor c = new Coor(x, y);
  45.         cells.remove(c);
  46.     }
  47.  
  48.     public void resetColony() {
  49.         cells.clear();
  50.  
  51.     }
  52.  
  53.     public int getNumberCells() {
  54.          return cellse.size();
  55.     }
  56.  
  57.     public boolean isCellAlive(int x, int y) {
  58.         Coor c = new Coor(x, y);
  59.         return cells.contains(c);
  60.  
  61.     }
  62.     private Set<Coor> cellse = new HashSet();
  63.     private Set<Coor> nextgenCell = new HashSet();
  64.  
  65.     public void evolve() {
  66.         gennumb++;
  67.         cellse.clear();
  68.         nextgenCell.clear();
  69.         for (Coor c : cells) {
  70.             Coor a = new Coor(c.x - 1, c.y - 1);
  71.             Coor b = new Coor(c.x + 1, c.y + 1);
  72.             Coor t = new Coor(c.x + 1, c.y - 1);
  73.             Coor d = new Coor(c.x - 1, c.y + 1);
  74.             Coor e = new Coor(c.x + 1, c.y + 0);
  75.             Coor f = new Coor(c.x - 1, c.y + 0);
  76.             Coor g = new Coor(c.x + 0, c.y - 1);
  77.             Coor h = new Coor(c.x + 0, c.y + 1);
  78.             Coor i = new Coor(c.x + 0, c.y + 0);
  79.             cellse.add(a);
  80.             cellse.add(b);
  81.             cellse.add(t);
  82.             cellse.add(d);
  83.             cellse.add(e);
  84.             cellse.add(f);
  85.             cellse.add(g);
  86.             cellse.add(h);
  87.             cellse.add(i);
  88.         }
  89.         for (Coor c : cellse) {
  90.             int counter = 0;
  91.             if (isCellAlive(c.x - 1, c.y - 1)) {
  92.                 counter++;
  93.             }
  94.             if (isCellAlive(c.x + 1, c.y + 1)) {
  95.                 counter++;
  96.             }
  97.             if (isCellAlive(c.x + 1, c.y - 1)) {
  98.                 counter++;
  99.             }
  100.             if (isCellAlive(c.x - 1, c.y + 1)) {
  101.                 counter++;
  102.             }
  103.             if (isCellAlive(c.x + 1, c.y + 0)) {
  104.                 counter++;
  105.             }
  106.             if (isCellAlive(c.x - 1, c.y + 0)) {
  107.                 counter++;
  108.             }
  109.             if (isCellAlive(c.x + 0, c.y - 1)) {
  110.                 counter++;
  111.             }
  112.             if (isCellAlive(c.x + 0, c.y + 1)) {
  113.                 counter++;
  114.             }
  115.             if (isCellAlive(c.x, c.y)) {
  116.                 if (counter == 3) {
  117.                     nextgenCell.add(c);
  118.                 }
  119.                 if (counter == 2) {
  120.                     nextgenCell.add(c);
  121.                 }
  122.  
  123.             } else {
  124.                 if (counter == 3) {
  125.                     nextgenCell.add(c);
  126.                 }
  127.  
  128.             }
  129.  
  130.         }
  131.         cells.clear();
  132.         for (Coor c : nextgenCell) {
  133.             cells.add(c);
  134.         }
  135.     }
  136.  
  137.     @Override
  138.     public java.lang.String toString() {
  139.         return singleRow(20, 20) + "generation number: " + gennumb;
  140.  
  141.     }
  142.  
  143.     public String helperXAndY(int x, int y) {
  144.         if (isCellAlive(x, y)) {
  145.             return "*";
  146.         } else {
  147.             return " ";
  148.  
  149.         }
  150.  
  151.     }
  152.  
  153.     public String singleRow(int xSize, int ySize) {
  154.         String b = " ";
  155.         for (int y = ySize - 1; y >= 0; y--) {
  156.             for (int x = 0; x < xSize; x++) {
  157.                 b += (helperXAndY(x, y));
  158.             }
  159.             b += "\n";
  160.         }
  161.         return b;
  162.     }
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement