This is based on John Conways Game of Life, a simple yet awesome code that simulates life.
Rules are simple, if a cell has less then 1 or more then 4 living cells next to it, the cell dies.
An empty cell with exactly 3 living cells next to it becomes alive itself.
My code seems to murder the whole scene... I'm probably wrong somewhere in lines 49 to 70.
package igrazivota;
import java.util.Random;
import java.util.Scanner;
public class IgraZivota {
public static void main(String[] args) {
int iterationNumber;
int chance;
int limit;
int counter = 0;
int row = 10;
int col = 10;
boolean[][] matrix = new boolean[row][col];
Random R = new Random();
Scanner unos = new Scanner(System.in);
System.out.println("Life factor: (0-many; 100-few)");
limit = Integer.parseInt(unos.nextLine());
for (int i = 1; i < row; i++) {
for (int j = 1; j < col; j++) {
chance = R.nextInt(100) + 1;
if (chance > limit) {
matrix[i][j] = true;
}
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (matrix[i][j] == false) {
System.out.print("_ ");
} else if (matrix[i][j] == true) {
System.out.print("X ");
}
}
System.out.println();
}
System.out.println("Number of iterations:");
iterationNumber = Integer.parseInt(unos.nextLine());
for (int o = 0; o < iterationNumber; o++) {
for (int i = 1; i < row; i++) {
for (int j = 1; j < col; j++) {
for (int ii = i - 1; ii < i + 1; ii++) {
for (int jj = j - 1; jj < j + 1; jj++) {
if (matrix[ii][jj] == true) {
counter++;
}
}
}
if (counter == 2 || counter == 3) {
matrix[i][j] = true;
}
if (counter <= 1 || counter >= 4) {
matrix[i][j] = false;
}
if (counter == 3) {
matrix[i][j] = true;
}
counter = 0;
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (matrix[i][j] == false) {
System.out.print("_ ");
} else if (matrix[i][j] == true) {
System.out.print("X ");
}
}
System.out.println();
}
System.out.println("///////////////////");
}
}
}