Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordaedalus <daedalus2027@gmail.com>2009-08-31 02:01:58 (GMT)
committer daedalus <daedalus2027@gmail.com>2009-08-31 02:01:58 (GMT)
commit15bc7b04a3f11004cb4a69403262dacd81e56807 (patch)
treecc9cb63e2a92e42e8f377853d0247562e2803715
parentee552fcbdad3c3ab82934db4a0f51f17ea6cf579 (diff)
added a life game
-rw-r--r--data/misc/lifegame89
1 files changed, 89 insertions, 0 deletions
diff --git a/data/misc/lifegame b/data/misc/lifegame
new file mode 100644
index 0000000..3b80584
--- /dev/null
+++ b/data/misc/lifegame
@@ -0,0 +1,89 @@
+# -*- coding: utf-8 -*-
+# Bibliotecas
+import copy, os, time, random
+
+def cargartablero(filas,columnas):
+ tablero=[]
+ columna = [0]*columnas
+ celulas=0
+#cargamos casilleros vacios
+ for i in range(filas):
+ columna = [0]* columnas
+ tablero.append(columna)
+#cargamos las celulas
+ for x in range(filas):
+ for y in range(columnas):
+ celula = random.randint(0,random.randint(0,1))
+ tablero[x][y] = celula
+ return tablero
+
+def dibujartablero(tablero):
+ filas=len(tablero)
+ columnas=len(tablero[1])
+ for x in range(filas):
+ for y in range(columnas):
+ if tablero[x][y]==0:
+ print ".",
+ else:
+ print "o",
+ print "\n",
+
+def contarvecinos(tablero,x,y):
+ vecinos=0
+ filas=len(tablero)
+ columnas=len(tablero[1])
+
+ if x < filas-1 and tablero[x+1][y]==1:
+ vecinos = vecinos + 1
+ if x > 0 and tablero[x-1][y]==1:
+ vecinos = vecinos + 1
+ if y < columnas-1 and tablero[x][y+1]==1:
+ vecinos = vecinos + 1
+ if y > 0 and tablero[x][y-1]==1:
+ vecinos = vecinos + 1
+ if x < filas-1 and y < columnas-1 and tablero[x+1][y+1]==1:
+ vecinos = vecinos + 1
+ if x > 0 and y > 0 and tablero[x-1][y-1]==1:
+ vecinos = vecinos + 1
+ if x > 0 and y < columnas-1 and tablero[x-1][y+1]==1:
+ vecinos = vecinos + 1
+ if x < filas-1 and y > 0 and tablero[x+1][y-1]==1:
+ vecinos = vecinos + 1
+
+ return vecinos
+
+def iteracion(tablero):
+ filas=len(tablero)
+ columnas=len(tablero[1])
+ vecinos=0
+ for x in range(filas):
+ for y in range(columnas):
+ if tablero[x][y] == 1:
+ vecinos = contarvecinos(tablero,x,y)
+ if vecinos >= 3 and vecinos <= 6:
+ xpos = x + random.randint(-1,1)
+ if xpos <= 0:
+ xpos = 1
+ if xpos >= filas:
+ xpos = filas-1
+ ypos = y + random.randint(-1,1)
+ if ypos <= 0:
+ ypos = 1
+ if ypos >= columnas:
+ ypos = columnas-1
+ tablero[xpos][ypos]=1
+ else:
+ tablero[x][y]=0
+
+def bucleprincipal(pulsos):
+ pulso=0
+ tablero = cargartablero(30,30)
+ while pulso < pulsos:
+ os.system('clear')
+ print "Pulso: ",pulso
+ iteracion(tablero)
+ dibujartablero(tablero)
+ pulso = pulso + 1
+ time.sleep(0.2)
+bucleprincipal(130)
+