rendered paste bodyimport java.util.Random;public class Gol{ protected boolean alive; protected boolean dead; private static int xDim; private static int yDim; private static Cell[][] grid; private static Cell[][] tempGrid; public int type; public Gol(int xDim, int yDim) { this.xDim = xDim; this.yDim = yDim; initMatrix(); } public void setLife(int k) { //if(grid.length != this.yDim) //throw new IllegalArgumentException("Das Array abstraktesSpielfeld muß exakt dieselben Abmessungen haben wie das Spielfeld."); for(int i = 0; i < xDim; i++) { //if(grid[i].length != this.xDim) //throw new IllegalArgumentException("Das Array abstraktesSpielfeld muß exakt dieselben Abmessungen haben wie das Spielfeld."); for(int j = 0; j < yDim; j++) { type = generateLife(); if(type >= 0 && type < k ) { grid[i][j].setAlive(false); } else { grid[i][j].setAlive(true); } } } } private int generateLife() { Random rn = new Random(); int number = rn.nextInt(10); return number; } private void initMatrix() { this.grid = new Cell[yDim][xDim]; for(int i = 0; i < yDim; i++) { for(int j = 0; j < xDim; j++) { this.grid[i][j] = new Cell(j, i); } } } public void playLifecycles(int n) { System.out.println("StartGrid:"); printMatrix(); for(int i = 0; i < n; i++) { System.out.println("Generation " + (i + 1) + ":"); nextGeneration(); printMatrix(); } } private boolean checkRighttolive(int r, int c) { int neighbourCount = 0; int tmp_i = 0; int tmp_k = 0; //int cC = 0; //System.out.println("Row: " + r + ", Col " + c); for(int i = -1; i <= 1; i++) { for(int k = -1; k <= 1; k++) { if(r+i > this.xDim) tmp_i = 0; else if(r+i < 0) tmp_i = this.xDim-1; else tmp_i = r+i; if(c+k > this.xDim-1) tmp_k = 0; else if(c+k < 0) tmp_k = this.xDim-1; else tmp_k = c+k; if(tmp_i == r && tmp_k == c) { //System.out.println("Checking myself (" + r + "," + c +")"); continue; } //System.out.println(cC++ + ": Checking (" + tmp_i + ", " + tmp_k + ")"); if(this.grid[tmp_i][tmp_k].isAlive() == true) neighbourCount++; if(neighbourCount > 3) return false; } } //System.out.println("Neighbours: " + neighbourCount); //System.out.println("Row " + r); //System.out.println("Col " + c); //System.out.println("hmmm" + this.world[r][c]); if(this.grid[r][c].isAlive() == true) { if(neighbourCount == 2 || neighbourCount == 3) { this.livingCells++; return true; } } else { if(neighbourCount == 3) { this.livingCells++; return true; } } return false; } /* public static boolean isAlive(int xPos, int yPos) { System.out.println("xpos:"+xPos+" "+yPos); System.out.println("xdim:"+xDim+" "+yDim); if(xPos < 0 && yPos < 0 && grid[xDim-1][yDim-1].alive == true) { return true; } else if( xPos >= xDim && yPos >= yDim && grid[0][0].alive == true) { return true; } else if(xPos < 0 && grid[xDim-1][yPos].alive == true) { return true; } else if(xPos >= xDim && grid[0][yPos].alive == true) { return true; } else if(yPos < 0 && grid[xPos][yDim-1].alive == true) { return true; } else if(yPos >= yDim && grid[xPos][0].alive == true) { return true; } else { return false; } }*/ public void nextGeneration() { this.tempGrid = new Cell[yDim][xDim]; for(int i = 0; i < this.yDim; i++) { for(int j = 0; j < this.xDim; j++) { this.tempGrid[i][j] = this.grid[i][j]; } } // Nächste Runde berechnen for(int i = 0; i < this.yDim; i++) { for(int j = 0; j < this.xDim; j++) { if(this.tempGrid[i][j].nextValue() == true) this.grid[i][j].setAlive(true); else this.grid[i][j].setAlive(false); } } } public void printMatrix() { System.out.println(); for(int i = 0; i < this.yDim; i++) { for(int j = 0; j < this.xDim; j++) { System.out.print(grid[i][j]); } System.out.println(); } System.out.println(); } }