import java.util.Scanner;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.awt.*;
import java.awt.event.*;
public class life {
public int[][] grid;
public int[][] grid2;
int currentGeneration = 0;
int x, y;
String filename;
public int count = 0;
public static void main(String[] args) throws FileNotFoundException {
life lifegrid = new life(6, 10, "testLab8.lif");
lifegrid.show();
}
life(int x, int y, String filename) throws FileNotFoundException {
this.grid = new int[x][y];
File file = new File(filename);
Scanner keyboard = new Scanner(file);
String line;
int lines = 0;
while (keyboard.hasNextLine()) {
line = keyboard.nextLine();
int i = 0;
for (i = 0; i < line.length(); i++) {
if (line.charAt(i) == '*') {
grid[lines][i] = 1;
} else {
grid[lines][i] = 0;
}
}
lines++;
}
}
public int getX() {return grid[0].length;}
public int getY() {return grid.length;}
public int getCell(int y, int x) {return grid[x][y];}
public void setCell(int y, int x, int v) {grid[x][y] = v;}
public void show()
{
for (int x = 0; x < getY(); x++) {
for (int y = 0; y < getX(); y++) {
System.out.print(grid[x][y]);
/* if (getCell(x, y) == 1){
livingCells.setColor(Color.black);
livingCells.fillRect(x * 4, y * 4, 4, 4);
}
else {
livingCells.setColor(Color.white);
livingCells.fillRect(x * 4, y * 4, 4, 4);
}*/
}
}
}
public int neighbours(int x, int y) {
int count = 0;
if (x > 0){
if (grid[x][y] == grid[x-1][y])
count++;
}
if (x < x-1){
if (grid[x][y] == grid[x+1][y])
count++;
}
if (grid[x][y] == grid[x][y-1]){
count++;
}
if (grid[x][y] == grid[x][y+1]){
count++;
}
if ((x > 0) && (y > 0)){
if (grid[x][y] == grid[x-1][y-1])
count++;
}
if (x > 0){
if (grid[x][y] == grid[x-1][y+1])
count++;
}
if (x < x-1){
if (grid[x][y] == grid[x+1][y-1])
count++;
}
if ((x < 0) && (y < 0)){
if (grid[x][y] == grid[x+1][y+1])
count++;
}
return count;
}
public void run()
{
for (int x = 1; x < getY(); x++){
for (int y = 1; y < getX(); y++){
if (neighbours (y, x) == 3){
if (neighbours (y, x) < 2 || neighbours(x, y) > 3){
setCell(y, x, 1);
}
else {
setCell(x, y, 0);
}
}
else {
if (neighbours(x, y) ==3) {
setCell(x, y, 1);
}
else {
setCell(x, y, 0);
}
}
}
}
for (int i = 0; i < getY(); i++) {
for (int j = 0; j < getX(); j++){
grid[y][x] = grid2[y][x];
}
}
currentGeneration++;
}
}