python Implementation of the game of Life sample code of Game of Life

  • 2020-07-21 09:04:07
  • OfStack

The algorithm of life game is not explained much, baidu 1 is introduced everywhere.

Since most versions on the Internet are based on external libraries such as pygame and matlab, and 2-dimensional arrays are mostly implemented with numpy, which costs a lot to learn. Therefore, in my spare time, I write a version that does not rely on external libraries and is output by console.


# -*- coding: utf-8 -*- 
from time import sleep 
from copy import deepcopy 
 
WORLD_HIGH = 20 # The length of  
WORLD_WIDE = 40 # The width of the  
ALIVE_CON = 3 # Resurrection conditions  
KEEP_CON = 2 # tenure  
 
class Cell(object): 
  ''''' The cell object ''' 
  def __init__(self, pos): 
    ''''' Its coordinates x,y,  Has survived or not survived ''' 
    self.point, self.is_alive = pos, False 
    self.x, self.y = self.point 
   
  def setAlive(self): 
    self.is_alive = True 
     
  def setDied(self): 
    self.is_alive = False 
     
  def display(self): 
    if self.is_alive: 
      return '*' 
    return ' ' 
     
  def displayLinux(self): 
    ''''' in linux You can print black and white blocks in the environment ''' 
    if self.is_alive: 
      return '\033[0;37;47m \033[0m' 
    return '\033[0;30;40m \033[0m' 
     
class GameManager(object): 
  def __init__(self): 
    self.world = self.initWorld() 
    self.initAliveCell() 
    
  def initWorld(self): 
    world = [] 
    for pos_x in xrange(WORLD_WIDE): 
      column = [] 
      for pos_y in xrange(WORLD_HIGH): 
        column.append(Cell((pos_x, pos_y))) 
      world.append(column) 
    return world 
   
  def initAliveCell(self): 
    from random import choice 
    for high in self.world: 
      for cell in high: 
        if choice((0, 1)) == 0: 
          continue 
        cell.setAlive() 
   
  def getNeighbours(self, cell_obj): 
    alive_count = 0 
    for x_of in xrange(-1, 2): 
      for y_of in xrange(-1, 2): 
        c_x, c_y = cell_obj.x + x_of, cell_obj.y + y_of 
        if ((c_x, c_y) == cell_obj.point) or \ 
          (c_x < 0 or c_x >= WORLD_WIDE) or \ 
          (c_y < 0 or c_y >= WORLD_HIGH): 
          ''''' The point of exclusion and transgression ''' 
          continue 
        if self.world[c_x][c_y].is_alive: 
          alive_count += 1 
    return alive_count 
        
  def display(self): 
    print '='*WORLD_WIDE # Equal divide  
    for index in xrange(WORLD_HIGH): 
      print ''.join([high[index].displayLinux() for high in self.world]) 
    print '='*WORLD_WIDE 
 
  def gameStart(self): 
    while True: 
      self.display() 
      new_world = deepcopy(self.world) 
      for p_x, wide_list in enumerate(self.world): 
        for p_y, _ in enumerate(wide_list): 
          current_cell = new_world[p_x][p_y] 
          nei_num = self.getNeighbours(current_cell) 
          if nei_num == ALIVE_CON: 
            current_cell.setAlive() 
          elif nei_num != KEEP_CON: 
            current_cell.setDied()        
      self.world = new_world 
      sleep(0.2) 
 
if __name__ == '__main__': 
  world = GameManager() 
  try: 
    world.gameStart() 
  except KeyboardInterrupt: 
    ''''' To prevent ctrl+c Out of an error ''' 
    pass 

Related articles: