Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class krehal2lab13
- {
- public static void main(String args[])
- {
- int xsize, ysize = -1;
- int xpos, ypos = -1;
- File f = new File (args[0]);
- Scanner sc = null;
- try
- {
- sc = new Scanner (f);
- }
- catch (FileNotFoundException fnfe)
- {
- System.out.println ("File did not exist");
- return;
- }
- xsize = sc.nextInt();
- ysize = sc.nextInt();
- // create the grid of 25 rows and 35 columns
- GridDisplay disp = new GridDisplay(xsize, ysize);
- int[][] currentGen = new int[xsize][ysize];
- int[][] nextGen = new int[xsize][ysize];
- for(int i = 0; i < xsize; i++)
- {
- for(int j = 0; j < ysize; j++)
- {
- currentGen[i][j] = 0;
- nextGen[i][j] = 0;
- disp.setColor(i, j, Color.WHITE);
- }
- }
- // loop until all integers are read from the file
- while (sc.hasNextInt())
- {
- xpos = sc.nextInt();
- ypos = sc.nextInt();
- currentGen[xpos][ypos] = 1;
- }
- for(int i = 0; i < xsize; i++)
- {
- for(int j = 0; j < ysize; j++)
- {
- if(currentGen[i][j] == 0)
- disp.setColor(i, j, Color.WHITE);
- else if (currentGen[i][j] == 1)
- disp.setColor(i, j, Color.BLACK);
- }
- }
- while(true)
- {
- for(int i = 0; i < xsize; i++)
- {
- for(int j = 0; j < ysize; j++)
- {
- if(nextGen[i][j] == 0)
- disp.setColor(i, j, Color.WHITE);
- else if (nextGen[i][j] == 1)
- disp.setColor(i, j, Color.BLACK);
- }
- }
- setNextGen(currentGen, nextGen, xsize, ysize);
- mySleep(500);
- System.out.println("hi");
- }
- }
- // puts the current thread to sleep for some number of milliseconds to allow for "animation"
- public static void mySleep( int milliseconds)
- {
- try
- {
- Thread.sleep(milliseconds);
- }
- catch (InterruptedException ie)
- {
- }
- }
- public static void setNextGen(int[][] currentGen, int[][] nextGen, int xsize, int ysize)
- {
- for(int i = 0; i < xsize; i++)
- {
- for(int j = 0; j < ysize; j++)
- {
- int aliveNeighbors = 0;
- /*
- 1. Any live cell with less than two live neighbors dies, as if caused by under-population.
- 2. Any live cell with two or three live neighbors lives on to the next generation.
- 3. Any live cell with more than three live neighbors dies, as if by overcrowding.
- 4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
- */
- //if it is a corner cell
- //top left corner
- if(i == 0 && j == 0)
- {
- if(currentGen[i][j+1] == 1)
- aliveNeighbors++;
- if(currentGen[i+1][j] == 1)
- aliveNeighbors++;
- if(currentGen[i+1][j+1] == 1)
- aliveNeighbors++;
- }
- //top right corner
- else if(i == 0 && j == ysize - 1)
- {
- if(currentGen[i][j-1] == 1)
- aliveNeighbors++;
- if(currentGen[i+1][j] == 1)
- aliveNeighbors++;
- if(currentGen[i+1][j-1] == 1)
- aliveNeighbors++;
- }
- //bottom left corner
- else if(i == xsize - 1 && j == 0)
- {
- if(currentGen[i-1][j] == 1)
- aliveNeighbors++;
- if(currentGen[i-1][j+1] == 1)
- aliveNeighbors++;
- if(currentGen[i][j+1] == 1)
- aliveNeighbors++;
- }
- //bottom right corner
- else if(i == xsize - 1 && j == ysize - 1)
- {
- if(currentGen[i][j-1] == 1)
- aliveNeighbors++;
- if(currentGen[i-1][j-1] ==1)
- aliveNeighbors++;
- if(currentGen[i-1][j] == 1)
- aliveNeighbors++;
- }
- //else if it is a top row cell
- else if(i == 0)
- {
- if(currentGen[i][j-1] == 1)
- aliveNeighbors++;
- if(currentGen[i+1][j-1] == 1)
- aliveNeighbors++;
- if(currentGen[i+1][j] == 1)
- aliveNeighbors++;
- if(currentGen[i+1][j+1] == 1)
- aliveNeighbors++;
- if(currentGen[i][j+1] == 1)
- aliveNeighbors++;
- }
- //else if it is a bottom row cell
- else if(i == xsize - 1)
- {
- if(currentGen[i][j-1] == 1)
- aliveNeighbors++;
- if(currentGen[i-1][j-1] == 1)
- aliveNeighbors++;
- if(currentGen[i-1][j] == 1)
- aliveNeighbors++;
- if(currentGen[i-1][j+1] == 1)
- aliveNeighbors++;
- if(currentGen[i][j+1] == 1)
- aliveNeighbors++;
- }
- //else if it is a left column cell
- else if(j == 0)
- {
- if(currentGen[i-1][j] == 1)
- aliveNeighbors++;
- if(currentGen[i-1][j+1] == 1)
- aliveNeighbors++;
- if(currentGen[i][j+1] == 1)
- aliveNeighbors++;
- if(currentGen[i+1][j+1] == 1)
- aliveNeighbors++;
- if(currentGen[i+1][j] == 1)
- aliveNeighbors++;
- }
- //else if it is a right column cell
- else if(j == ysize - 1)
- {
- if(currentGen[i-1][j] == 1)
- aliveNeighbors++;
- if(currentGen[i-1][j-1] == 1)
- aliveNeighbors++;
- if(currentGen[i][j-1] == 1)
- aliveNeighbors++;
- if(currentGen[i+1][j-1] == 1)
- aliveNeighbors++;
- if(currentGen[i+1][j] == 1)
- aliveNeighbors++;
- }
- //else it is a center cell
- else
- {
- if(currentGen[i-1][j-1] == 1)
- aliveNeighbors++;
- if(currentGen[i-1][j] == 1)
- aliveNeighbors++;
- if(currentGen[i-1][j+1] == 1)
- aliveNeighbors++;
- if(currentGen[i][j-1] == 1)
- aliveNeighbors++;
- if(currentGen[i][j+1] == 1)
- aliveNeighbors++;
- if(currentGen[i+1][j-1] == 1)
- aliveNeighbors++;
- if(currentGen[i+1][j] == 1)
- aliveNeighbors++;
- if(currentGen[i+1][j+1] == 1)
- aliveNeighbors++;
- }
- //If the cell is alive
- if(currentGen[i][j] == 1)
- {
- if(aliveNeighbors < 2)
- nextGen[i][j] = 0;
- else if(aliveNeighbors == 2 || aliveNeighbors == 3)
- nextGen[i][j] = 1;
- else if(aliveNeighbors > 3)
- nextGen[i][j] = 0;
- }
- //If the cell is dead
- else
- {
- if(aliveNeighbors == 3)
- nextGen[i][j] = 1;
- }
- }
- }
- //copy everything from nextGen to currentGen, since we are printing from currentGen
- for(int x = 0; x < xsize; x++)
- {
- for(int y = 0; y < ysize; y++)
- {
- currentGen[x][y] = nextGen[x][y];
- }
- }
- //return nextGen;
- }
- }
- class GridDisplay extends JFrame
- {
- private JLabel labels[];
- private Container container;
- private GridLayout grid1;
- int rowCount;
- int colCount;
- // set up GUI
- public GridDisplay(int rows, int cols)
- {
- super( "GridDisplay for CS211" );
- setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
- // set up grid layout struture of the display
- rowCount = rows;
- colCount = cols;
- grid1 = new GridLayout( rows, cols );
- container = getContentPane();
- container.setLayout( grid1 );
- // create and add buttons
- labels = new JLabel[ rows * cols ];
- for ( int count = 0; count < labels.length; count++ ) {
- labels[ count ] = new JLabel( " " );
- labels[count].setOpaque(true);
- container.add( labels[ count ] );
- }
- // set up the size of the window and show it
- setSize( cols * 15 , rows * 15 );
- setVisible( true );
- } // end constructor GridLayoutDemo
- // display the given char in the (row,col) position of the display
- public void setChar (int row, int col, char c)
- {
- if ((row >= 0 && row < rowCount) && (col >= 0 && col < colCount) )
- {
- int pos = row * colCount + col;
- labels [pos].setText("" + c);
- }
- }
- // display the given color in the (row,col) position of the display
- public void setColor (int row, int col, Color c)
- {
- if ((row >= 0 && row < rowCount) && (col >= 0 && col < colCount) )
- {
- int pos = row * colCount + col;
- labels [pos].setBackground(c);
- }
- }
- } // end class GridDisplay
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement