Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class g
- {
- final static int M = 75, N = 25;
- final static char DOT = '.';
- public static void init(char[][] grid)
- {
- for(int r = 0; r <= N + 1 ; r++)
- {
- for(int c = 0; c <= M + 1; c++)
- {
- grid[r][c] = DOT;
- }
- }
- }
- public static void read(char[][] grid)
- {
- Scanner kbd = null;;
- File f = new File("C:\\Users\\Usiel\\Desktop\\life1.dat");
- String s;
- try
- {
- kbd = new Scanner(f);
- }catch (Exception e){}
- for(int r = 1; r <= N; r++)
- {
- s = kbd.next();
- for(int c = 1; c <= M; c++)
- {
- grid[r][c] =s.charAt(c-1);
- }
- }
- }
- public static char[][] newgrid(char[][] grid)
- {
- char[][] grid2 = new char[N+2][M + 2];
- for(int row = 1; row <= N; row++)
- {
- for (int col = 1; col <= M; col++)
- {
- if (grid[row][col] == 'X' && neighbor(grid, row, col)==2)
- {
- grid2[row][col] = 'X';
- }
- else if (grid[row][col] == 'X' && neighbor(grid, row, col)==3)
- {
- grid2[row][col] = 'X';
- }
- else if (grid[row][col] == '.' && neighbor(grid, row, col)==3)
- {
- grid2[row][col] = 'X';
- }
- else
- {
- grid2[row][col] = '.';
- }
- }
- }
- return grid2;
- }
- public static int neighbor(char[][] grid, int row, int col)
- {
- int count = 0;
- for(int r = row -1; r <= row+ 1; r++)
- {
- for( int c = col-1; c<= col+1; c++)
- {
- if( grid[r][c] == 'X' )
- {
- count++;
- }
- }
- }
- if(grid[row][col] =='X') count = count -1;
- return count;
- }
- public static boolean isBlank(char[][] grid )
- {
- for(int r = 1; r <= N; r++)
- {
- for( int c = 1; c<= M; c++)
- {
- if( grid[r][c] == 'X' )
- {
- return false;
- }
- }
- }
- return true;
- }
- public static boolean stable(char[][] grid, char[][] grid2)
- {
- for(int r = 1; r <= N; r++)
- {
- for( int c = 1; c<= M; c++)
- {
- if( grid[r][c] != grid2[r][c] )
- {
- return false;
- }
- }
- }
- return true;
- }
- public static void print(char[][] grid)
- {
- for(int r = 1; r <= N ; r++)
- {
- for(int c = 1; c <= M; c++)
- {
- System.out.print( grid[r][c]);
- }
- System.out.println();
- }
- }
- public static int pause()
- {
- System.out.println("1 to continue, 0 to exit");
- int temp = 2;
- Scanner wait = new Scanner(System.in);
- temp = wait.nextInt();
- while (temp != 1 && temp != 0);
- return temp;
- }
- public static void main(String[] args)
- {
- int exit = 1;
- char[][] grid = new char[N+2][M + 2];
- init(grid);
- read(grid);
- print(grid);
- System.out.println();
- exit = pause();
- char[][] grid2 = new char[N+2][M + 2];
- grid2 = newgrid(grid);
- if (exit != 0 && !isBlank(grid))
- {
- print(grid2);
- System.out.println();
- exit = pause();
- while (exit != 0 && !isBlank(grid) && !isBlank(grid2) && !stable(grid, grid2))
- {
- grid = newgrid(grid2);
- print(grid);
- System.out.println();
- exit = pause();
- if (exit != 0 && !isBlank(grid) && !isBlank(grid2) && !stable(grid, grid2))
- {
- grid2 = newgrid(grid);
- print(grid2);
- System.out.println();
- exit = pause();
- }
- }
- }
- if (stable(grid, grid2))
- {
- System.out.println("The environment is stable and will not change.");
- }
- if (isBlank(grid) || isBlank(grid2))
- {
- System.out.println("The environment is empty.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement