Advertisement
Guest User

Untitled

a guest
Dec 4th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.24 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3.  
  4. import javax.swing.JFrame;
  5. import javax.swing.JOptionPane;
  6. import javax.swing.Timer;
  7.  
  8. public class GoLWorldMainCS {
  9.  
  10.     public static void main(String[] args) throws InterruptedException {
  11.         JFrame frame = new JFrame();
  12.  
  13.         GoLWorldCS terrain = new GoLWorldCS(Integer.parseInt(JOptionPane.showInputDialog("Enter rows: ")), Integer.parseInt(JOptionPane.showInputDialog("Enter cols: ")));
  14.         terrain.setGenerations(Integer.parseInt(JOptionPane.showInputDialog("Enter generations: ")));
  15.  
  16.         frame.setSize(700, 700);
  17.         frame.setTitle("Game of Life");
  18.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19.         frame.add(terrain);
  20.         frame.setVisible(true);
  21.  
  22.         if (terrain.getRow() > 60 || terrain.getRow() < 1 || terrain.getCol() > 60 || terrain.getCol() < 1 || terrain.getGenerations() < 1) {
  23.             System.out.println("Error: Rows and Columns must be 1-60, and Generations must be greater than 0.");
  24.         } else {
  25.             terrain.setCellSize((600 / terrain.getRow()), (600 / terrain.getCol()));
  26.  
  27.             terrain.setCells(terrain.getC(), terrain.getR(), 10);
  28.            
  29.             Timer t = new Timer(250, new ActionListener() {
  30.                
  31.                 @Override
  32.                 public void actionPerformed(ActionEvent e) {
  33.                     terrain.markAlive();
  34.                     terrain.repaint();
  35.                 }
  36.             });
  37.            
  38.             t.start();
  39.         }
  40.     }
  41. }
  42.  
  43.  
  44. import java.awt.Color;
  45. import java.awt.Graphics;
  46. import java.util.Random;
  47.  
  48. import javax.swing.JPanel;
  49.  
  50. public class GoLWorldCS extends JPanel {
  51.  
  52.     private static final long serialVersionUID = -2833280938880400310L;
  53.     int maxRow;
  54.     int maxCol;
  55.     int gen;
  56.     int buckets[][];
  57.     int r = 0, c = 0, l = 1;
  58.     int xDist, yDist;
  59.  
  60.     public GoLWorldCS(int maxRow, int maxCol) {
  61.         this.maxRow = maxRow;
  62.         this.maxCol = maxCol;
  63.         this.buckets = new int[maxRow][maxCol];
  64.         for (int i = 0; i < buckets.length * 20; i++) {
  65.             buckets[new Random().nextInt(buckets.length)][new Random().nextInt(buckets.length)] = new Random().nextInt(2);
  66.         }
  67.     }
  68.  
  69.     public void clearGrid() {
  70.         for (int i = 0; i < buckets.length; i++) {
  71.             for (int j = 0; j < buckets[i].length; j++) {
  72.                 buckets[i][j] = 0;
  73.             }
  74.         }
  75.     }
  76.  
  77.     public void markAlive() {
  78.         int[][] tempBucket = new int[buckets.length][buckets.length];
  79.         for (int i = 0; i < buckets.length; i++) {
  80.             for (int j = 0; j < buckets[i].length; j++) {
  81.                 int numAliveNeighbors = countAliveNeighbors(i, j);
  82.                 if (buckets[i][j] == 1) {
  83.                     tempBucket[i][j] = checkIfDead(numAliveNeighbors, i, j);
  84.                 } else {
  85.                     tempBucket[i][j] = checkIfAlive(numAliveNeighbors, i, j);
  86.                 }
  87.             }
  88.         }
  89.         buckets = tempBucket;
  90.     }
  91.  
  92.     private int checkIfAlive(int numAliveNeighbors, int i, int j) {
  93.         if (numAliveNeighbors == 3) {
  94.             return 1;
  95.         }
  96.         return 0;
  97.     }
  98.  
  99.     private int checkIfDead(int numAliveNeighbors, int i, int j) {
  100.         if (numAliveNeighbors < 2 || numAliveNeighbors > 3) {
  101.             return 0;
  102.         }
  103.         return 1;
  104.     }
  105.  
  106.     private int countAliveNeighbors(int i, int j) {
  107.         int count = 0;
  108.         if (i - 1 >= 0) {
  109.             count += countForRow(i - 1, j);
  110.         }
  111.         count += countForRow(i, j);
  112.         if (i + 1 < buckets.length) {
  113.             count += countForRow(i + 1, j);
  114.         }
  115.         return count;
  116.     }
  117.  
  118.     private int countForRow(int i, int j) {
  119.         int count = 0;
  120.         if (j - 1 >= 0 && buckets[i][j - 1] == 1) {
  121.             count++;
  122.         }
  123.         if (buckets[i][j] == 1) {
  124.             count++;
  125.         }
  126.         if (j + 1 < buckets[i].length && buckets[i][j + 1] == 1) {
  127.             count++;
  128.         }
  129.         return count;
  130.     }
  131.  
  132.     @Override
  133.     public void paintComponent(Graphics g) {
  134.         super.paintComponent(g);
  135.         g.setColor(Color.blue);
  136.  
  137.         for (int i = 0; i < buckets.length; i++) {
  138.             for (int j = 0; j < buckets[i].length; j++) {
  139.                 if (buckets[i][j] != 0) {
  140.                     g.fillOval((i) * xDist + 50, (j) * yDist + 50, xDist, yDist);
  141.                 }
  142.             }
  143.         }
  144.     }
  145.  
  146.     public void setCellSize(int width, int height) {
  147.         this.xDist = width;
  148.         this.yDist = height;
  149.     }
  150.  
  151.     public void setGenerations(int gens) {
  152.         this.gen = gens;
  153.     }
  154.  
  155.     public void setCells(int R, int C, int L) {
  156.         this.r = R;
  157.         this.c = C;
  158.         this.l = L;
  159.     }
  160.  
  161.     public int getRow() {
  162.         return maxRow;
  163.     }
  164.  
  165.     public int getCol() {
  166.         return maxCol;
  167.     }
  168.  
  169.     public int getGenerations() {
  170.         return gen;
  171.     }
  172.  
  173.     public int getR() {
  174.         return r;
  175.     }
  176.  
  177.     public int getC() {
  178.         return c;
  179.     }
  180.  
  181.     public int getL() {
  182.         return l;
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement