Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.Timer;
- public class GoLWorldMainCS {
- public static void main(String[] args) throws InterruptedException {
- JFrame frame = new JFrame();
- GoLWorldCS terrain = new GoLWorldCS(Integer.parseInt(JOptionPane.showInputDialog("Enter rows: ")), Integer.parseInt(JOptionPane.showInputDialog("Enter cols: ")));
- terrain.setGenerations(Integer.parseInt(JOptionPane.showInputDialog("Enter generations: ")));
- frame.setSize(700, 700);
- frame.setTitle("Game of Life");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(terrain);
- frame.setVisible(true);
- if (terrain.getRow() > 60 || terrain.getRow() < 1 || terrain.getCol() > 60 || terrain.getCol() < 1 || terrain.getGenerations() < 1) {
- System.out.println("Error: Rows and Columns must be 1-60, and Generations must be greater than 0.");
- } else {
- terrain.setCellSize((600 / terrain.getRow()), (600 / terrain.getCol()));
- terrain.setCells(terrain.getC(), terrain.getR(), 10);
- Timer t = new Timer(250, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- terrain.markAlive();
- terrain.repaint();
- }
- });
- t.start();
- }
- }
- }
- import java.awt.Color;
- import java.awt.Graphics;
- import java.util.Random;
- import javax.swing.JPanel;
- public class GoLWorldCS extends JPanel {
- private static final long serialVersionUID = -2833280938880400310L;
- int maxRow;
- int maxCol;
- int gen;
- int buckets[][];
- int r = 0, c = 0, l = 1;
- int xDist, yDist;
- public GoLWorldCS(int maxRow, int maxCol) {
- this.maxRow = maxRow;
- this.maxCol = maxCol;
- this.buckets = new int[maxRow][maxCol];
- for (int i = 0; i < buckets.length * 20; i++) {
- buckets[new Random().nextInt(buckets.length)][new Random().nextInt(buckets.length)] = new Random().nextInt(2);
- }
- }
- public void clearGrid() {
- for (int i = 0; i < buckets.length; i++) {
- for (int j = 0; j < buckets[i].length; j++) {
- buckets[i][j] = 0;
- }
- }
- }
- public void markAlive() {
- int[][] tempBucket = new int[buckets.length][buckets.length];
- for (int i = 0; i < buckets.length; i++) {
- for (int j = 0; j < buckets[i].length; j++) {
- int numAliveNeighbors = countAliveNeighbors(i, j);
- if (buckets[i][j] == 1) {
- tempBucket[i][j] = checkIfDead(numAliveNeighbors, i, j);
- } else {
- tempBucket[i][j] = checkIfAlive(numAliveNeighbors, i, j);
- }
- }
- }
- buckets = tempBucket;
- }
- private int checkIfAlive(int numAliveNeighbors, int i, int j) {
- if (numAliveNeighbors == 3) {
- return 1;
- }
- return 0;
- }
- private int checkIfDead(int numAliveNeighbors, int i, int j) {
- if (numAliveNeighbors < 2 || numAliveNeighbors > 3) {
- return 0;
- }
- return 1;
- }
- private int countAliveNeighbors(int i, int j) {
- int count = 0;
- if (i - 1 >= 0) {
- count += countForRow(i - 1, j);
- }
- count += countForRow(i, j);
- if (i + 1 < buckets.length) {
- count += countForRow(i + 1, j);
- }
- return count;
- }
- private int countForRow(int i, int j) {
- int count = 0;
- if (j - 1 >= 0 && buckets[i][j - 1] == 1) {
- count++;
- }
- if (buckets[i][j] == 1) {
- count++;
- }
- if (j + 1 < buckets[i].length && buckets[i][j + 1] == 1) {
- count++;
- }
- return count;
- }
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.setColor(Color.blue);
- for (int i = 0; i < buckets.length; i++) {
- for (int j = 0; j < buckets[i].length; j++) {
- if (buckets[i][j] != 0) {
- g.fillOval((i) * xDist + 50, (j) * yDist + 50, xDist, yDist);
- }
- }
- }
- }
- public void setCellSize(int width, int height) {
- this.xDist = width;
- this.yDist = height;
- }
- public void setGenerations(int gens) {
- this.gen = gens;
- }
- public void setCells(int R, int C, int L) {
- this.r = R;
- this.c = C;
- this.l = L;
- }
- public int getRow() {
- return maxRow;
- }
- public int getCol() {
- return maxCol;
- }
- public int getGenerations() {
- return gen;
- }
- public int getR() {
- return r;
- }
- public int getC() {
- return c;
- }
- public int getL() {
- return l;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement