Guest User

Untitled

a guest
May 20th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.awt.Color;
  7.  
  8.  
  9. class GameOfLife {
  10.     public static void main(String[] args) throws FileNotFoundException {
  11.         String fileToLoad = "";
  12.         int width = 0, height = 0, squareSize = 10;
  13.         // 8, 3, Test.txt
  14.         Scanner keyboard = new Scanner(System.in);
  15.        
  16.         System.out.println("What file would you like to load into the program?");
  17.         fileToLoad = keyboard.next();
  18.         System.out.println("What is the width of the grid in this file?");
  19.         width = keyboard.nextInt();
  20.         System.out.println("What is the height of the grid in this file?");
  21.         height = keyboard.nextInt();
  22.        
  23.         Grid life  = new Grid(width, height,fileToLoad);       
  24.         LifeFrame frame = new LifeFrame();
  25.         LifeGlob glob = new LifeGlob(life, squareSize);
  26.         JButton button = new JButton("Next Generation");
  27.         button.addActionListener(new GenerationListener(life, frame, width, height));
  28.         frame.add(glob, BorderLayout.CENTER);
  29.         frame.add(button, BorderLayout.SOUTH);
  30.         frame.pack();
  31.     }
  32. }
  33.  
  34. class GenerationListener implements ActionListener {
  35.     Grid c;
  36.     LifeFrame f;
  37.     int width = 0, height = 0;
  38.    
  39.      GenerationListener(Grid c, LifeFrame f, int width, int height) {
  40.          this.c = c;
  41.          this.f = f;
  42.          this.width = width;
  43.          this.height = height;
  44.      }
  45.  
  46.      public void actionPerformed(ActionEvent e) {
  47.          int[][] newGen = new int[width][height];
  48.         int input = 0;
  49.         for (int i = 0; i < height; i ++) {
  50.             for (int j = 0; j < width; j ++) {
  51.                     if (c.neighbours(j, i) == 2) {
  52.                         newGen[j][i] = 1;
  53.                     } else if (c.neighbours(j, i) <= 1) {
  54.                         newGen[j][i] = 0;
  55.                     } else if (c.neighbours(j, i) >= 3) {
  56.                         newGen[j][i] = 0;
  57.                     }
  58.                 }
  59.             }
  60.             for (int k = 0; k < height; k ++) {
  61.                 for (int l = 0; l < width; l ++) {
  62.                     input = newGen[l][k];
  63.                     c.setCell(l, k, input);
  64.                 }
  65.             }
  66.             f.repaint();
  67.         }
  68. }
  69.    
  70.  
  71. class LifeGlob extends JComponent {
  72.     private Grid c;
  73.     private int squareSize;
  74.     private Color map[] = {Color.BLUE, Color.GREEN};
  75.    
  76.     LifeGlob(Grid c, int squareSize) {
  77.         this.c = c; this.squareSize = squareSize;
  78.         setPreferredSize(new Dimension(c.getX() * squareSize, c.getY() * squareSize));
  79.     }
  80.    
  81.     protected void paintComponent(Graphics g) {
  82.         super.paintComponent(g);
  83.  
  84.         for (int y = 0; y < c.getY(); y++) {
  85.             for (int x = 0; x < c.getX(); x++) {
  86.                 if (c.getCell(x, y) == 0) {
  87.                     g.setColor(map[0]);
  88.                     g.fillRect(x * squareSize,y * squareSize, squareSize, squareSize);
  89.                 } else {
  90.                     g.setColor(map[1]);
  91.                     g.fillRect(x * squareSize,y * squareSize, squareSize, squareSize);
  92.                 }              
  93.             }
  94.         }
  95.     }
  96. }
  97.  
  98. class LifeFrame extends JFrame {
  99.   LifeFrame()  {
  100.     setTitle("Game of Life");
  101.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  102.     setVisible(true);
  103.   }
  104. }
  105.  
  106. class Grid {   
  107.     int[][] grid;
  108.    
  109.     Grid(int x, int y, String inputFileName) throws FileNotFoundException {
  110.         grid = new int[x][y];
  111.         File f = new File(inputFileName);
  112.         Scanner sc = new Scanner(f);
  113.         for (int j = 0; j < y; j++) {
  114.             if (sc.hasNextLine()) {
  115.                 String line = sc.nextLine();
  116.                 for (int i = 0; i < x; i++) {
  117.                     if (line.charAt(i) == '*') {
  118.                         grid[i][j] = 1;
  119.                     } else {
  120.                         grid[i][j] = 0;
  121.                     }
  122.                 }
  123.             }
  124.         }
  125.                
  126.     }  
  127.    
  128.     public int getX() {return grid[0].length; }
  129.     public int getY() {return grid.length; }
  130.     public int getCell(int x, int y) {return grid[x][y];}
  131.     public void setCell(int x, int y, int input) {grid[x][y] = input; }
  132.    
  133.     public int neighbours(int x, int y) {
  134.         int populated = 0;
  135.         try { if (grid[x - 1][y + 1] == 1) { populated ++; } } catch(ArrayIndexOutOfBoundsException e) {}
  136.         try { if (grid[x][y + 1] == 1) { populated ++; } }catch(ArrayIndexOutOfBoundsException e) {}
  137.         try { if (grid[x + 1][y + 1] == 1) { populated ++; } } catch(ArrayIndexOutOfBoundsException e) {}
  138.         try { if (grid[x - 1][y] == 1) { populated ++; } } catch(ArrayIndexOutOfBoundsException e) {}
  139.         try { if (grid[x + 1][y] == 1) { populated ++; } } catch(ArrayIndexOutOfBoundsException e) {}
  140.         try { if (grid[x - 1][y - 1] == 1) { populated ++; } } catch(ArrayIndexOutOfBoundsException e) {}
  141.         try { if (grid[x][y - 1] == 1) { populated ++; } } catch(ArrayIndexOutOfBoundsException e) {}
  142.         try { if (grid[x + 1][y - 1] == 1) { populated ++; } } catch(ArrayIndexOutOfBoundsException e) {}
  143.         return populated;
  144.     }
  145. }
Add Comment
Please, Sign In to add comment