LifeTest first step passed

This commit is contained in:
Moritz Neumeier 2021-12-17 10:19:08 +01:00
parent f6187ef27f
commit 2e9764cb45
2 changed files with 0 additions and 113 deletions

View File

@ -1,113 +0,0 @@
public class Simulation {
int width;
int height;
int[][] board;
public Simulation(int width, int height) {
this.width = width;
this.height = height;
this.board = new int[width][height];
}
public void printBoard() {
System.out.println("---");
for (int y = 0; y < height; y++) {
String line = "|";
for (int x = 0; x < width; x++) {
if (this.board[x][y] == 0) {
line += ".";
} else {
line += "*";
}
}
line += "|";
System.out.println(line);
}
System.out.println("---\n");
}
public void setAlive(int x, int y) {
this.board[x][y] = 1;
}
public void setDead(int x, int y) {
this.board[x][y] = 0;
}
public int countAliveNeighbours(int x, int y) {
int count = 0;
count += getState(x - 1, y - 1);
count += getState(x, y - 1);
count += getState(x + 1, y - 1);
count += getState(x - 1, y);
count += getState(x + 1, y);
count += getState(x - 1, y + 1);
count += getState(x, y + 1);
count += getState(x + 1, y + 1);
return count;
}
public int getState(int x, int y) {
if (x < 0 || x >= width) {
return 0;
}
if (y < 0 || y >= height) {
return 0;
}
return this.board[x][y];
}
public void step() {
int[][] newBoard = new int[width][height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int aliveNeighbours = countAliveNeighbours(x, y);
if (getState(x, y) == 1) {
if (aliveNeighbours < 2) {
newBoard[x][y] = 0;
} else if (aliveNeighbours == 2 || aliveNeighbours == 3) {
newBoard[x][y] = 1;
} else if (aliveNeighbours > 3) {
newBoard[x][y] = 0;
}
} else {
if (aliveNeighbours == 3) {
newBoard[x][y] = 1;
}
}
}
}
this.board = newBoard;
}
public static void main(String[] args) {
Simulation simulation = new Simulation(8, 5);
simulation.setAlive(2, 2);
simulation.setAlive(3, 2);
simulation.setAlive(4, 2);
simulation.printBoard();
simulation.step();
simulation.printBoard();
simulation.step();
simulation.printBoard();
}
}