Labels

Tuesday, March 24, 2015

Conway's Game of Life

From wiki
The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:
  1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by overcrowding.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed—births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the preceding one). The rules continue to be applied repeatedly to create further generations.

The class interface is as follows.

class GameOfLife:
def __init__(self, board_width):
""" Your code here """

def set_alive(self, row, column):
""" Your code here """

def set_dead(self, row, column):
        """ Your code here """.


def step(self):
  """ Your code here """.
# more stuffs if you want to add.

Naive Thinking: Follow the rule to accomplish these functions. To update the board, I need a temporary board to store next state information.
 
 public class GameOfLife{  
      private int board[][];  
      GameOfLife(){init(10,10);}  
      GameOfLife(int row, int column){init(row, column);}  
      public void init(int L, int W){  
           assert L > 0;  
           assert W > 0;  
           board = new int[L][W];  
      }  
      public void set_alive(int row, int column){  
           if(!isInitialized()) return;  
           if(row > board.length && column > board[0].length) return;  
           board[row][column] = 1;  
      }  
      public void set_dead(int row, int column){  
           if(!isInitialized()) return;  
           if(row > board.length && column > board[0].length) return;  
           board[row][column] = 0;  
      }  
      public void step(){  
           if(!isInitialized()) return;  
           int temp[][] = new int[board.length][board[0].length];  
           // make a temporary board to store next state  
           for(int i = 0; i < board.length;i++)  
                for(int j = 0;j < board[0].length;j++)  
                     if(board[i][j]==1){  
                          if(getAliveNeighbor(i,j) < 2 || getAliveNeighbor(i,j) > 3)  
                               temp[i][j] = 0;  
                          else  
                               temp[i][j] = 1;  
                     }else if(getAliveNeighbor(i,j) == 3)  
                          temp[i][j] = 1;  
           // copy the temp board to main board  
           for(int i = 0;i < board.length;i++)  
                for(int j = 0;j < board[0].length;j++)  
                     board[i][j] = temp[i][j];  
      }  
      private int getAliveNeighbor(int x, int y){  
           int sum = 0;  
           for(int i = -1;i <= 1;i++)  
                for(int j = -1;j <= 1;j++)  
                     if(x+i >= 0 && x+i < board.length && y+j >= 0 && y+j < board[0].length && !(i==0 && j==0))  
                          sum += board[x+i][y+j]==1?1:0;  
           return sum;  
      }  
      private boolean isInitialized(){return !(board == null || board.length == 0);}  
      public void print(){  
           if(!isInitialized()) return;  
           for(int i = 0;i < board.length;i++){  
                for(int j = 0;j < board[0].length;j++)  
                     System.out.print(board[i][j]+" ");  
                System.out.println();  
           }  
           System.out.println();  
      }  
 }  

To test the game:

 public class Test{  
      public static void main(String args[]){  
           GameOfLife g = new GameOfLife();  
           for(int i = 0;i < 10;i++)  
                for(int j = 0;j < 10;j++)  
                     if(j%2==0 && i%2==0 || i%3==0) g.set_alive(i,j);  
           int loop = 5;  
           for(int i = 0;i < loop;i++){  
                g.print();  
                g.step();  
           }  
      }  
 }  

Output:

 1 1 1 1 1 1 1 1 1 1   
 0 0 0 0 0 0 0 0 0 0   
 1 0 1 0 1 0 1 0 1 0   
 1 1 1 1 1 1 1 1 1 1   
 1 0 1 0 1 0 1 0 1 0   
 0 0 0 0 0 0 0 0 0 0   
 1 1 1 1 1 1 1 1 1 1   
 0 0 0 0 0 0 0 0 0 0   
 1 0 1 0 1 0 1 0 1 0   
 1 1 1 1 1 1 1 1 1 1   
   
 0 1 1 1 1 1 1 1 1 0   
 1 0 0 0 0 0 0 0 0 1   
 1 0 1 0 1 0 1 0 1 1   
 1 0 0 0 0 0 0 0 0 1   
 1 0 1 0 1 0 1 0 1 1   
 1 0 0 0 0 0 0 0 0 1   
 0 1 1 1 1 1 1 1 1 0   
 1 0 0 0 0 0 0 0 0 1   
 1 0 1 0 1 0 1 0 1 1   
 1 0 1 0 1 0 1 0 1 1   
   
 0 1 1 1 1 1 1 1 1 0   
 1 0 0 0 0 0 0 0 0 1   
 1 0 0 0 0 0 0 0 1 1   
 1 0 0 0 0 0 0 0 0 0   
 1 0 0 0 0 0 0 0 1 1   
 1 0 0 0 0 0 0 0 0 1   
 1 1 1 1 1 1 1 1 1 1   
 1 0 0 0 0 0 0 0 0 1   
 1 0 0 0 0 0 0 0 0 0   
 0 0 0 0 0 0 0 0 1 1   
   
 0 1 1 1 1 1 1 1 1 0   
 1 0 1 1 1 1 1 0 0 1   
 1 1 0 0 0 0 0 0 1 1   
 1 1 0 0 0 0 0 0 0 0   
 1 1 0 0 0 0 0 0 1 1   
 1 0 1 1 1 1 1 0 0 0   
 1 0 1 1 1 1 1 1 0 1   
 1 0 1 1 1 1 1 1 0 1   
 0 0 0 0 0 0 0 0 1 1   
 0 0 0 0 0 0 0 0 0 0   
   
 0 1 0 0 0 0 0 1 1 0   
 1 0 0 0 0 0 0 0 0 1   
 0 0 0 1 1 1 0 0 1 1   
 0 0 1 0 0 0 0 0 0 0   
 0 0 0 1 1 1 0 0 0 0   
 1 0 0 0 0 0 0 0 0 1   
 1 0 0 0 0 0 0 0 0 0   
 0 0 1 0 0 0 0 0 0 1   
 0 0 0 1 1 1 1 1 1 1   
 0 0 0 0 0 0 0 0 0 0   

No comments:

Post a Comment