Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gameoflife;
- import java.util.*;
- public class Colonies {
- private String name;
- private Set<Coor> cells = new HashSet();
- public Colonies(java.lang.String name) {
- colonyNumber= getNextColonyNumber();
- this.name = name;
- }
- private int colonyNumber;
- private static int nextColonyNumber= 0;
- public static int getNextColonyNumber(){
- return nextColonyNumber++;
- }
- public int getColonyNumber(){
- return colonyNumber;
- }
- public java.lang.String getColonyName() {
- return name;
- }
- private int gennumb = 0;
- public int getGenerationNumber() {
- return gennumb;
- }
- public void setCellAlive(int x, int y) {
- Coor c = new Coor(x, y);
- cells.add(c);
- }
- public void setCellDead(int x, int y) {
- Coor c = new Coor(x, y);
- cells.remove(c);
- }
- public void resetColony() {
- cells.clear();
- }
- public int getNumberCells() {
- return cellse.size();
- }
- public boolean isCellAlive(int x, int y) {
- Coor c = new Coor(x, y);
- return cells.contains(c);
- }
- private Set<Coor> cellse = new HashSet();
- private Set<Coor> nextgenCell = new HashSet();
- public void evolve() {
- gennumb++;
- cellse.clear();
- nextgenCell.clear();
- for (Coor c : cells) {
- Coor a = new Coor(c.x - 1, c.y - 1);
- Coor b = new Coor(c.x + 1, c.y + 1);
- Coor t = new Coor(c.x + 1, c.y - 1);
- Coor d = new Coor(c.x - 1, c.y + 1);
- Coor e = new Coor(c.x + 1, c.y + 0);
- Coor f = new Coor(c.x - 1, c.y + 0);
- Coor g = new Coor(c.x + 0, c.y - 1);
- Coor h = new Coor(c.x + 0, c.y + 1);
- Coor i = new Coor(c.x + 0, c.y + 0);
- cellse.add(a);
- cellse.add(b);
- cellse.add(t);
- cellse.add(d);
- cellse.add(e);
- cellse.add(f);
- cellse.add(g);
- cellse.add(h);
- cellse.add(i);
- }
- for (Coor c : cellse) {
- int counter = 0;
- if (isCellAlive(c.x - 1, c.y - 1)) {
- counter++;
- }
- if (isCellAlive(c.x + 1, c.y + 1)) {
- counter++;
- }
- if (isCellAlive(c.x + 1, c.y - 1)) {
- counter++;
- }
- if (isCellAlive(c.x - 1, c.y + 1)) {
- counter++;
- }
- if (isCellAlive(c.x + 1, c.y + 0)) {
- counter++;
- }
- if (isCellAlive(c.x - 1, c.y + 0)) {
- counter++;
- }
- if (isCellAlive(c.x + 0, c.y - 1)) {
- counter++;
- }
- if (isCellAlive(c.x + 0, c.y + 1)) {
- counter++;
- }
- if (isCellAlive(c.x, c.y)) {
- if (counter == 3) {
- nextgenCell.add(c);
- }
- if (counter == 2) {
- nextgenCell.add(c);
- }
- } else {
- if (counter == 3) {
- nextgenCell.add(c);
- }
- }
- }
- cells.clear();
- for (Coor c : nextgenCell) {
- cells.add(c);
- }
- }
- @Override
- public java.lang.String toString() {
- return singleRow(20, 20) + "generation number: " + gennumb;
- }
- public String helperXAndY(int x, int y) {
- if (isCellAlive(x, y)) {
- return "*";
- } else {
- return " ";
- }
- }
- public String singleRow(int xSize, int ySize) {
- String b = " ";
- for (int y = ySize - 1; y >= 0; y--) {
- for (int x = 0; x < xSize; x++) {
- b += (helperXAndY(x, y));
- }
- b += "\n";
- }
- return b;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement