Case Study: Game of Life Conway’s Game of Life A zero-player game for studying the evolution of simple organisms, using cellular automaton. Fig. An example of cellular automaton. Basic Rules • Each cell in the grid is either live or dead. • The user determines the initial positions of all live cells. • The game proceeds in rounds. In each round some new organisms born, and others die, according to predefined evolution rules. Basic Rules • Each cell in the grid is either live or dead. • The user determines the initial positions of all live cells. • The game proceeds in rounds. In each round some new organisms born, and others die, according to predefined evolution rules. 1st generation Basic Rules • Each cell in the grid is either live or dead. • The user determines the initial positions of all live cells. • The game proceeds in rounds. In each round some new organisms born, and others die, according to predefined evolution rules. 1st generation 2nd generation Basic Rules Neighbors: The neighbors of a cell are the cells that surround it. Basic Rules Neighbors: The neighbors of a cell are the cells that surround it. Basic Rules Neighbors: The neighbors of a cell are the cells that surround it. Basic Rules Neighbors: The neighbors of a cell are the cells that surround it. Evolution Rules 1. Any live cell with fewer than two live neighbors dies, as if caused by under-population. 2. Any live cell with two or three live neighbors lives on to the next generation. 3. Any live cell with more than three live neighbors dies, as if by overcrowding. 4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction. Program classes Game – Main class that creates the graphical user interface. Board – A class that represents the game board. Holds an array of ints and an array of buttons. Program classes Game – Main class that creates the graphical user interface. Board – A class that represents the game board. Holds an array of ints and an array of buttons. Model View Program classes Game – Main class that creates the graphical user interface. Board – A class that represents the game board. Holds an array of ints and an array of buttons. Model View ButtonPressed – an ActionListener for setting the initial position. public class Game extends JFrame implements ActionListener { private Board board; public Game(){ super("Game of Life"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.getContentPane().setLayout(new BorderLayout()); this.board = new Board(8); ImageIcon buttonIcon = new ImageIcon("next.gif"); JButton nextButton = new JButton("",buttonIcon); nextButton.addActionListener(this); this.getContentPane().add(nextButton, BorderLayout.NORTH); this.getContentPane().add(board, BorderLayout.CENTER); this.setSize(800,600); this.setVisible(true); } public void actionPerformed(ActionEvent e){ this.board.next(); } public static void main(String args[]){ Game game = new Game(); } } public class Board extends JPanel{ private final int N; private int [][]array; private JButton [][]buttons; public Board(int size){ super(new GridLayout(size,size)); N = size; JButton b; array = new int[N][N]; buttons = new JButton[N][N]; for (int i = 0; i < N; i++) for(int j = 0; j < N; j++){ array[i][j] = 0; b = new JButton(i + " , " + j); b.addActionListener(new ButtonPressed(i,j,array,b)); this.add(b); buttons[i][j] = b; } public void next(){ int [][]temp = new int[N + 2][N+2]; ImageIcon buttonIcon = new ImageIcon("button.gif"); for (int i =0; i < N + 2; i++) for (int j =0; j < N + 2; j++) temp[i][j] = 0; for (int i =1; i <= N ; i++) for (int j =1; j <= N ; j++) temp[i][j] = array[i-1][j-1]; for (int i =1; i <= N ; i++) for (int j =1; j <= N ; j++){ int cnt = 0; cnt = temp[i-1][j-1] + temp[i-1][j] + temp[i-1][j+1] + temp[i][j-1] + temp[i][j + 1] + temp[i+1][j-1] + temp[i+1][j] + temp[i+1][j+1]; if (cnt < 2 || cnt > 3){ array[i-1][j-1] = 0; buttons[i-1][j-1].setIcon(null); } else{ if(temp[i][j] == 1 || (temp[i][j] == 0 && cnt == 3)){ array[i-1][j-1] = 1; buttons[i-1][j-1].setIcon(buttonIcon); } } //end of for } //end of next() } //end of class Board public class ButtonPressed implements ActionListener { private int x,y; private int [][] array; private JButton button; public ButtonPressed(int x,int y,int[][] array, JButton button ){ this.x = x; this.y = y; this.array = array; this.button = button; } public void actionPerformed(ActionEvent e){ ImageIcon buttonIcon = new ImageIcon("button.gif"); if (array[x][y] == 0){ array[x][y] = 1; button.setIcon(buttonIcon); } else{ array[x][y] = 0; button.setIcon(null); } } }