Python - Ayuda con codigo similar a "Game of Life"

 
Vista:
sin imagen de perfil

Ayuda con codigo similar a "Game of Life"

Publicado por Fernando (1 intervención) el 11/10/2013 05:59:06
Hola, estoy escribiendo un código usando la librería pygame, la idea es simular un proceso usando un método llamado de grano grueso para 2 dimensiones , el código es similar al de "Game Of Life", el algoritmo es el siguiente:
1.- hacer una matriz de rango nxn, donde todos los elementos son números aleatorios entre 0 y 9
2.- seleccionar al azar un elemento de la matriz y encontrar sus vecinos, es decir los elementos que rodean al elemento seleccionado
3.- contador 1: elementos "vecinos" iguales al elemento seleccionado en (2) se agregan al contador 1
4.- el elemento al azar seleccionado se cambia por otro elemento aleatorio entre 0 y 9 manteniendo sus "vecinos" anteriores.
5.- contador 2: elementos "vecinos" iguales al elemento de (4) dado se agregan al contador 2.
6.- si el contador 2 es mayor que el contador 1 se cambia el elemento de (2) por e elemento de (4)
7.- luego del cambio mostrar la matriz de nxn "modificada"
8.- repetir el proceso desde (2)

La implementación la hice en Fortran pero quiero realizar una simulación para que se visualice el proceso físico (bueno así me dijo el profesor). Les dejo la implementación hasta donde e llegado. Espero que me puedan ayudar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import pygame, sys
from pygame.locals import *
import random
 
FPS = 10
n = 600
cs = 10
 
WINDOWWIDTH = n
WINDOWHEIGHT = n
CELLSIZE = cs
 
assert WINDOWWIDTH % CELLSIZE == 0, "NO ES MULTIPLO DEL TAMANO DE LA CELDA"
assert WINDOWHEIGHT % CELLSIZE == 0, "NO ES MULTIPLO DEL TAMANO DE LA CELDA"
#tamaño de la celda
CELLWIDTH = WINDOWWIDTH / CELLSIZE
CELLHEIGHT = WINDOWHEIGHT / CELLSIZE
#anadimos colores 
BLACK =    (0,  0,  0)
WHITE =    (255,255,255)
DARKGRAY = (40, 40, 40)
GREEN =    (0, 255, 0)
RED =      (255, 0 ,0)
BLUE =     (0, 0, 255)
YELLOW =    (255, 255, 0)
SKYBLUE =  (0, 255, 255)
PURPLE =   (255, 0, 255)
GRAY =     (100, 100, 100)
#creamos las lineas que separan cada celda
def drawGrid():
    for x in range(0, WINDOWWIDTH, CELLSIZE):
        pygame.draw.line(DISPLAYSURF, GRAY, (x,0),(x,WINDOWHEIGHT))
    for y in range (0, WINDOWHEIGHT, CELLSIZE):
        pygame.draw.line(DISPLAYSURF, GRAY, (0,y), (WINDOWWIDTH, y))
#los casilleros lo llenamos con colores para cada caso
def colourGrid(item, Dict):
    x = item[0]
    y = item[1]
    y = y * CELLSIZE
    x = x * CELLSIZE
    if Dict[item] == 0:
        pygame.draw.rect(DISPLAYSURF, BLACK, (x, y, CELLSIZE, CELLSIZE))
    if Dict[item] == 1:
        pygame.draw.rect(DISPLAYSURF, WHITE, (x, y, CELLSIZE, CELLSIZE))
    if Dict[item] == 2:
    	pygame.draw.rect(DISPLAYSURF, DARKGRAY, (x, y,CELLSIZE, CELLSIZE))
    if Dict[item] == 3:
    	pygame.draw.rect(DISPLAYSURF, GREEN, (x, y,CELLSIZE, CELLSIZE))
    if Dict[item] == 4:
    	pygame.draw.rect(DISPLAYSURF, RED, (x, y,CELLSIZE, CELLSIZE))
    if Dict[item] == 5:
    	pygame.draw.rect(DISPLAYSURF, BLUE, (x, y,CELLSIZE, CELLSIZE))
    if Dict[item] == 6:
    	pygame.draw.rect(DISPLAYSURF, YELLOW, (x, y,CELLSIZE, CELLSIZE))
    if Dict[item] == 7:
    	pygame.draw.rect(DISPLAYSURF, SKYBLUE, (x, y,CELLSIZE, CELLSIZE))
    if Dict[item] == 8:
    	pygame.draw.rect(DISPLAYSURF, PURPLE, (x, y,CELLSIZE, CELLSIZE))
    if Dict[item] == 9:
    	pygame.draw.rect(DISPLAYSURF, GRAY, (x, y,CELLSIZE, CELLSIZE))
    return None
#creando el diccionario para las celdas
def blankGrid():
    gridDict = {}
    for y in range (CELLHEIGHT):
        for x in range (CELLWIDTH):
            gridDict[x,y] = 0
    return gridDict
#aqui se asigna los valores para cada celda
def startingGridRandom(Dict):
    for item in lifeDict:
        Dict[item] = random.randint(0,9)
    return Dict
#aqui comienza el paso 2 aun por terminar
def getNeighbours(item,Dict):
	neighbours = 0
	newneighbours = 0
 	a = random.randint(1, CELLWIDTH-1)
	b = random.randint(1, CELLHEIGHT-1)
	c = random.randint(0,9)
	checkCell = (a+item[0], b+item[1])
	if Dict[checkCell] == Dict[item]:
		for x in range(a-1,a+1):
			for y in range(b-1,b+1):
				if x !=a and y!=b:
					neighbours+= 1
				else:
					neighbours+= 0
 
	if Dict[c] == Dict[checkCell]:
		for x in range(a-1,a+1):
			for y in range(b-1,b+1):
				if x!=a and y!=b:
					newneighbours += 1
				else:
					newneighbours += 0
 
	if newneighbours > neighbours:
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder