associação pythonbrasil[11] django zope/plone planet Início Logado como (Entrar)

GameOfLife

Implementação do Conway's Game of Life em Python, que eu chamei de PyOrganism. Adaptado e melhorado da versão em C++ escrita por Mithun Aiyswaryan.

   1 #!/usr/bin/python
   2 import platform
   3 import random
   4 import math
   5 import time
   6 import sys
   7 import os
   8 
   9 clear = ('clear','cls')[platform.system() == 'Windows']
  10 
  11 class Life:
  12     def __init__(self):
  13         self._life = []
  14         self._flag = 1
  15         self._seed = 0
  16         self._number_of_generations = 0
  17         self._game_world = 0
  18         self._screen_length = 40
  19         self._delay_seconds = -1
  20     
  21     def initialize(self):
  22         random.seed(self._seed)
  23         self.init_world()
  24         
  25         for i in range(self._number_of_generations):
  26             os.system(clear)
  27             self.print_world()
  28             self.evolve()
  29             time.sleep(self._delay_seconds)
  30     
  31     
  32     def exit(self):
  33         self.draw_line('=')
  34         raw_input("Press <enter> to exit...\n")
  35         sys.exit(0)
  36 
  37         
  38     def header(self):
  39         self.draw_line('=')
  40         print "PyOrganysm - Python Implementation of Conway's Game of Life"
  41         print "Writen by Danillo Souza <danillo012@gmail.com>"
  42         self.draw_line('=')
  43         print ">>> Rules Of The Game"
  44         self.draw_line('~')
  45         print "1. An organism is born in an Empty-Cell when it has exactly 3 neighbors"
  46         print "2. An organism dies from starvation if it has fewer than 2 neighbors"
  47         print "3. An organism dies of overcrowding if it has more than 3 neighbors"
  48         print "4. Otherwise, the organism survives!"
  49         self.draw_line('~')
  50         
  51     
  52     def setup(self):
  53         while self._seed == 0:
  54             self._seed = int(raw_input("Enter a number to initiate the world: "))
  55         
  56         while self._number_of_generations == 0:
  57             self._number_of_generations = int(raw_input("Enter the number of generations: "))
  58         
  59         while self._game_world == 0:
  60             self._game_world = int(raw_input("Enter the size of the array: "))
  61             self._game_world = (30, self._game_world)[self._game_world >= 4]
  62         
  63         while self._delay_seconds < 0:
  64             self._delay_seconds = int(raw_input("Enter the delay time(in seconds): "))
  65         
  66         self._seed = int(math.fabs(self._seed))
  67         self._number_of_generations = int(math.fabs(self._number_of_generations))
  68         self._game_world = int(math.fabs(self._game_world))
  69         self._delay_seconds = int(math.fabs(self._delay_seconds))
  70         self._life = [['' for i in range(self._game_world)] for i in range(self._game_world)]    
  71     
  72     def draw_line(self, char):
  73         for i in range(self._screen_length):
  74             print char,
  75         
  76         print
  77             
  78             
  79     def init_world(self):
  80         for y in range(self._game_world):
  81             for x in range(self._game_world):
  82                 z = random.randint(1, 10)
  83                 if z < 5:
  84                     self._life[y][x] = ' '
  85                 else:
  86                     self._life[y][x] = '#'
  87                     
  88                     
  89     def evolve(self):
  90         double_array = [['' for i in range(self._game_world)] for i in range(self._game_world)]
  91         
  92         for i in range(self._game_world):
  93             for j in range(self._game_world):
  94                 x = self.count_neighbors(i, j)
  95                 
  96                 if self._life[i][j] == '#':
  97                     if (x == 2) or (x == 3):
  98                         double_array[i][j] = '#'
  99                     else:
 100                         double_array[i][j] = ' '
 101                 else:
 102                     if x == 3:
 103                         double_array[i][j] = '#'
 104                     else:
 105                         double_array[i][j] = ' '
 106                         
 107         self._life = double_array
 108         
 109     def print_world(self):
 110         for x in range(self._game_world):
 111             tmp = ''
 112             for y in range(self._game_world):
 113                 tmp += self._life[x][y] + ' '
 114             
 115             print tmp
 116             tmp = ''
 117     
 118     def count_neighbors(self, y, x):
 119         sum = 0
 120         
 121         if (x - 1) >= 0:
 122             for i in range(y-1, y+2):
 123                 if (i >= 0) and (i < self._game_world):
 124                     if self._life[i][x-1] == '#':
 125                         sum += 1
 126                         
 127         if (x + 1) < self._game_world:
 128             for i in range(y-1, y+2):
 129                 if (i >= 0) and (i < self._game_world):
 130                     if self._life[i][x+1] == '#':
 131                         sum += 1
 132                         
 133         if (y - 1) >= 0:
 134             if self._life[y-1][x] == '#':
 135                 sum += 1
 136         
 137         if (y + 1) < self._game_world:
 138             if self._life[y+1][x] == '#':
 139                 sum += 1
 140                 
 141         return sum
 142 
 143             
 144     def go(self):
 145         self.header()
 146         self.setup()
 147         self.initialize()
 148         self.exit()
 149             
 150             
 151 if __name__ == '__main__':
 152     life = Life()
 153     life.go()