The Game of Life Code

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

import import import import import import import import import import import import import import import

import import import import import

java.awt.BorderLayout; java.awt.Color; java.awt.Dimension; java.awt.FlowLayout; java.awt.GridLayout; java.awt.event.ActionEvent; java.awt.event.ActionListener; java.io.File; java.io.FileNotFoundException; java.io.PrintStream; java.util.ArrayList; java.util.Scanner; javax.swing.JButton; javax.swing.JFrame; javax.swing.JLabel; javax.swing.JOptionPane; javax.swing.JPanel; javax.swing.JScrollPane; javax.swing.JTextArea; javax.swing.JTextField;

public class RunGameOfLife implements ActionListener{ public static void main(String[] args) { RunGameOfLife window = new RunGameOfLife(); } private private private private private private private private private private private JFrame frame; JButton newe; JButton next1; JButton next10; JButton next100; JButton play; JButton pause; JButton stop; int height; int width; ArrayList<JButton> buttonArray;

//Creates the main text editing window when called public RunGameOfLife () { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("The Game of Life"); frame.setLayout(new BorderLayout()); height = 10; width = 25; JPanel center = new JPanel(new GridLayout(height, width)); buttonArray = new ArrayList<JButton>(); for (int i = 0; i < (height * width); i++){ JButton b = new JButton(); b.setPreferredSize(new Dimension(25, 25));

b.addActionListener(this); b.setBackground(Color.white); buttonArray.add(b); center.add(b); } JPanel south = new JPanel(new FlowLayout()); newe = new JButton("New"); newe.addActionListener(this); south.add(newe); next1 = new JButton("Next"); next1.addActionListener(this); south.add(next1); next10 = new JButton("Next Ten"); next10.addActionListener(this); south.add(next10); next100 = new JButton("Next Hundred"); next100.addActionListener(this); south.add(next100); play = new JButton("Play"); newe.addActionListener(this); south.add(play); pause = new JButton("Pause"); pause.addActionListener(this); south.add(pause); stop = new JButton("Stop"); stop.addActionListener(this); south.add(stop); frame.add(center, BorderLayout.CENTER); frame.add(south, BorderLayout.SOUTH); frame.setSize(frame.getPreferredSize()); frame.setVisible(true);

//Called when any button is clicked. Redirects to appropriate methods for each button and handles exceptions public void actionPerformed(ActionEvent event) { JButton button = (JButton) event.getSource(); if(button.equals(newe)){ for (int i = 0; i < buttonArray.size(); i++){ buttonArray.get(i).setBackground(Color.white); stop(); } } else if(button.equals(next1)){ next(); } else if(button.equals(next10)){ for (int i = 0; i < 10; i ++){ next(); } } else if(button.equals(next100)){ for (int i = 0; i < 100; i ++){

next(); } else if(button.equals(play)){ for(;;){ next(); } } else if(button.equals(pause)){ } else if(button.equals(stop)){ stop(); } else{ ((JButton) event.getSource()).setBackground(Color.black); } } private void stop() { // TODO Auto-generated method stub } private void next() { ArrayList<Boolean> shouldBeAlive = new ArrayList<Boolean>(); for (int i = 0; i < buttonArray.size(); i++){ int aliveNeighbors = 0; if (i - 1 >= 0){ if (buttonArray.get(i 1).getBackground().equals(Color.black)){ aliveNeighbors ++; } } if (i + 1 < buttonArray.size()){ if (buttonArray.get(i + 1).getBackground().equals(Color.black)){ aliveNeighbors ++; } } if (i - width >= 0){ if (buttonArray.get(i width).getBackground().equals(Color.black)){ aliveNeighbors ++; } } if (i + width < buttonArray.size()){ if (buttonArray.get(i + width).getBackground().equals(Color.black)){ aliveNeighbors ++; } } if (buttonArray.get(i).getBackground().equals(Color.white)){ }

if (aliveNeighbors >= 3){ shouldBeAlive.set(i, true); } else { shouldBeAlive.set(i, false); }

else if (buttonArray.get(i).getBackground().equals(Color.black)){ if (aliveNeighbors < 2 || aliveNeighbors > 3){ shouldBeAlive.set(i, false); } else if (aliveNeighbors == 2 || aliveNeighbors == 3){ shouldBeAlive.set(i, true); } } } } }

You might also like