This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| import java.io.*; | |
| import javax.swing.*; | |
| import java.awt.*; | |
| import java.lang.*; | |
| // | |
| public class GameOfLifeDraft2 extends JFrame | |
| { | |
| /* initial world read it and print it | |
| */ | |
| public GameOfLifeDraft2() | |
| { | |
| super("Game of Life"); | |
| setSize(760, 420); | |
| JPanel panel = new JPanel(); | |
| panel.setLayout( new GridLayout(1,2)); | |
| Test test1 = new Test(); | |
| Test test2 = new Test(); | |
| test1.setBackground(Color.green); | |
| test2.setBackground(Color.red); | |
| panel.add(test1); | |
| panel.add(test2); | |
| setContentPane(panel); | |
| setVisible(true); | |
| Temp one = new Temp(test1); | |
| Temp two = new Temp(test2); | |
| one.start(); | |
| two.start(); | |
| } | |
| public static void main(String[] asd) | |
| { | |
| new GameOfLifeDraft2(); | |
| System.out.println(); | |
| } | |
| } | |
| class Temp extends Thread | |
| { | |
| Test anim; | |
| public Temp(Test anim) | |
| { | |
| this.anim = anim; | |
| } | |
| public void run()//fro each instance of test begin will be executed | |
| { | |
| anim.begin(); | |
| } | |
| } | |
| class Test extends JPanel | |
| { | |
| final static int NROW = 25, NCOL = 47; | |
| final static char DOT = '.'; | |
| static char[][] grid = new char[NROW+2][NCOL + 2]; | |
| static char[][] nextgrid = new char[NROW+2][NCOL + 2]; | |
| boolean sameFlag; | |
| boolean blankFlag; | |
| public static void init(char[][] grid, char[][] nextgrid) | |
| { | |
| for(int row = 0; row <= NROW+1; row++) | |
| { | |
| for(int col = 0; col <= NCOL+1; col++) | |
| { | |
| grid[row][col] = DOT; | |
| nextgrid[row][col] = DOT; | |
| } | |
| } | |
| } | |
| public static void pause() | |
| { | |
| try | |
| { | |
| Thread.currentThread().sleep(1000L); | |
| } | |
| catch(InterruptedException e) { | |
| } | |
| } | |
| public void begin() | |
| { | |
| init(grid, nextgrid); | |
| read(grid); | |
| repaint(); //calls paintComponent | |
| pause(); | |
| while(sameFlag == true && blankFlag == false) | |
| { | |
| nextGen(grid, nextgrid); | |
| } | |
| } | |
| public static void read(char[][] grid) | |
| { | |
| Scanner world = new Scanner(System.in); | |
| System.out.println("Type in the Worlds you want to create"); | |
| String fileName = world.nextLine(); | |
| { | |
| try | |
| { | |
| world = new Scanner(new File(fileName)); | |
| }catch(Exception ex){ | |
| System.out.println("Problem creating scanner"); | |
| }; | |
| for(int row = 1; row <= NROW ; row++) | |
| { | |
| String s = world.next(); | |
| for(int col = 1; col <= NCOL ; col++) | |
| { | |
| grid[row][col] = s.charAt(col -1); | |
| } | |
| } | |
| } | |
| } | |
| public void print(Graphics g) | |
| { | |
| int x, y; | |
| y = 20; | |
| for(int row = 1; row <= NROW ; row++) | |
| { | |
| x = 20; | |
| for(int col = 1; col <= NCOL ; col++) | |
| { | |
| g.drawString (""+ grid[row][col], x, y ); | |
| x = x +7; | |
| } | |
| y = y + 15; | |
| } | |
| } | |
| public static int neighbors(char[][] grid, int r, int c) | |
| { | |
| //counts the # of closest neighbvors that are X's | |
| int count = 0; | |
| for(int row = r-1; row <= r+1 ; row++) | |
| { | |
| for(int col = c-1; col <= c+1 ; col++) | |
| { | |
| if(grid[row][col] == 'X') | |
| { | |
| count++; | |
| } | |
| } | |
| } | |
| if(grid[r][c] =='X') | |
| { | |
| count = count -1; | |
| } | |
| return count; | |
| } | |
| public static void nextGen(char [][] grid,char [][] nextgrid ) | |
| { | |
| for(int row = 1; row <= NROW; row++) | |
| { | |
| for(int col = 1; col <= NCOL; col++) | |
| { | |
| if (grid[row][col] == 'X') | |
| { | |
| int count = 0; | |
| { | |
| if (grid[row][col-1]=='X') | |
| count = count + 1; | |
| if (grid[row][col + 1] =='X') | |
| count = count+1; | |
| if (grid [row-1][col] == 'X') | |
| count = count + 1; | |
| if (grid[row-1][col - 1] =='X') | |
| count = count + 1; | |
| if (grid [row -1][col + 1]=='X') | |
| count = count + 1; | |
| if (grid [row+1][col-1]=='X') | |
| count = count + 1; | |
| if(grid [row + 1][col]=='X') | |
| count = count + 1; | |
| if (grid[row+1][col+1]=='X') | |
| count = count + 1; | |
| } | |
| if (count == 2 || count == 3) | |
| { | |
| nextgrid[row][col] = 'X'; | |
| } | |
| else | |
| nextgrid[row][col]=DOT; | |
| } | |
| if (grid[row][col] == DOT) | |
| { | |
| int count1 = 0; | |
| { | |
| if (grid[row][col-1]=='X') | |
| count1 = count1 + 1; | |
| if (grid[row][col + 1] =='X') | |
| count1 = count1+1; | |
| if (grid [row-1][col] == 'X') | |
| count1 = count1 + 1; | |
| if (grid[row-1][col - 1] =='X') | |
| count1 = count1 + 1; | |
| if (grid [row -1][col + 1]=='X') | |
| count1 = count1 + 1; | |
| if (grid [row+1][col-1]=='X') | |
| count1 = count1 + 1; | |
| if(grid [row + 1][col]=='X') | |
| count1 = count1 + 1; | |
| if (grid[row+1][col+1]=='X') | |
| count1 = count1 + 1; | |
| } | |
| if (count1 == 3) | |
| nextgrid[row][col] = 'X'; | |
| } | |
| } | |
| } | |
| } | |
| public static void copy(char [][] grid,char [][] nextgrid) | |
| { | |
| for (int i=0; i < NROW + 1; i++) | |
| { | |
| for(int j=0; j < NCOL + 1; j++) | |
| { | |
| grid[i][j]=nextgrid[i][j]; | |
| } | |
| } | |
| } | |
| public static boolean isEmpty(char [][] grid,char [][] nextgrid) | |
| { | |
| boolean blankFlag = true; | |
| for (int i=0; i < NROW + 1; i++) | |
| { | |
| for(int j=0; j < NCOL + 1; j++) | |
| { | |
| if(grid[i][j] != DOT) | |
| { | |
| blankFlag = false; | |
| } | |
| } | |
| } | |
| return blankFlag; | |
| } | |
| public static boolean isSame(char [][] grid,char [][] nextgrid) | |
| { | |
| boolean sameFlag = false; | |
| for (int i=0; i < NROW + 1; i++) | |
| { | |
| for(int j=0; j < NCOL + 1; j++) | |
| { | |
| if( grid[i][j] == nextgrid[i][j]) | |
| { | |
| sameFlag = true; | |
| break; | |
| } | |
| } | |
| } | |
| return sameFlag; | |
| } | |
| public void paintComponent(Graphics g) | |
| { | |
| super.paintComponent(g);//erases panel Contents | |
| g.setColor(Color.black); | |
| if(sameFlag == false && blankFlag == false) | |
| { | |
| print(g);//or whatever method you use to print the world | |
| } | |
| else | |
| { | |
| if(sameFlag == true) | |
| { | |
| g.drawString("The worlds are repeating!", 10, 250); | |
| } | |
| if (blankFlag == true) | |
| { | |
| g.drawString("The world is blank!", 10, 250); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment