Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go l #21

Open
wants to merge 2 commits into
base: GoL
Choose a base branch
from
Open

Go l #21

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions GoL/gol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import gol_utils as g



world = g.create_world(alive=([(0,1),(1,1),(2,1)]))

def casillas_vecinas( pos, MAX):
"""Retorna Lista de casillas vecinas validas"""

xpos, ypos = pos

posiciones = [
(x,y)
for x in range(xpos-1, xpos+2) if x >= 0 and x <MAX
for y in range(ypos-1, ypos+2) if y >= 0 and y <MAX and not (x==xpos and y==ypos)
]
return posiciones

#print casillas_vecinas((0,0),MAX)


def numero_vecinos( world, pos):
"""Retorna el numero de vecions vivos de una casilla"""

xpos, ypos = pos

valores = [ world[p[0],p[1]] for p in casillas_vecinas(pos,world.shape[0]) ]
return sum(valores)


def evolve(world):
"""Recorre el mundo creando una nuevo"""

dim = world.shape[0]
nuevo_mundo = g.create_world( [

(x,y)
for x in range(0,dim)
for y in range(0,dim)
if ( (world[x,y] and numero_vecinos(world,(x,y))==2)
or numero_vecinos(world,(x,y))==3)

],
dim

)
return nuevo_mundo

#w1 = evolve(world)
#print w1

#print evolve(w1)










77 changes: 77 additions & 0 deletions GoL/golTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import unittest
import gol
import gol_utils as g

class TestGol(unittest.TestCase):
def setUp(self):
self.world = g.create_world(alive=([(0,1),(1,1),(2,1)]))

def test_vecinas_esquina_izquierda(self):
posiciones = gol.casillas_vecinas((0,0),3)
self.assertEqual(set([(0, 1), (1, 0), (1, 1)]),set(posiciones))

def test_vecinas_esquina_derecha(self):
posiciones = gol.casillas_vecinas((2,0),3)
self.assertEqual(set([(1, 0), (1, 1), (2, 1)]),set(posiciones))

def test_superior(self):
cuantos = gol.numero_vecinos(self.world,(0,1))
self.assertEqual(cuantos,1)

def test_esquina(self):
cuantos = gol.numero_vecinos(self.world,(0,0))
self.assertEqual(cuantos,2)

def test_medio(self):
cuantos = gol.numero_vecinos(self.world,(1,0))
self.assertEqual(cuantos,3)


class TestEndToEnd(unittest.TestCase):

def test_flipflop(self):

w1 = g.create_world(alive=([(0,1),(1,1),(2,1)]))
w2 = g.create_world(alive=([(1,0),(1,1),(1,2)]))

self.assertTrue((gol.evolve(w1) != w2).sum()==0)
self.assertTrue((gol.evolve(w2) != w1).sum() ==0)

def test_de_cuatro_estable(self):

w1 = g.create_world(
alive=([(1,1),(1,2),(2,1),(2,2)]),
MAX=4
)

w2 = gol.evolve(w1)
self.assertTrue((w2 != w1).sum()==0)

def test_beacon_de_6(self):

w1 = g.create_world(
alive=(
[
(1,1),(1,2),(2,1),(2,2),
(3,3),(3,4),(4,3),(4,4)
]),
MAX=6
)

w2 = gol.evolve(w1)

w2_ref = g.create_world(
alive=(
[
(1,1),(1,2),(2,1),
(3,4),(4,3),(4,4)
]),
MAX=6
)
self.assertTrue((w2 != w2_ref).sum()==0)
w3 = gol.evolve(w2)
self.assertTrue((w3 != w1).sum()==0)


if __name__ == "__main__":
unittest.main()
6 changes: 4 additions & 2 deletions GoL/gol_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import numpy as np
import matplotlib.pyplot as plt

def create_world(alive = None):
world = np.zeros((3,3), dtype=int)
def create_world(alive = None, MAX=3):
world = np.zeros((MAX,MAX), dtype=int)
if alive:
for position in alive:
world[position[0], position[1]] = 1
return world

def draw_world(world):
plt.imshow(world, cmap=plt.cm.Greys, interpolation='nearest')


94 changes: 94 additions & 0 deletions GoL/vida.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import gol
import gol_utils
import numpy as np
import random
import matplotlib.pyplot as plt
import matplotlib.animation as anim

# beacon
def get_beacon():
w = gol_utils.create_world(
alive=(
[
(1,1),(1,2),(2,1),(2,2),
(3,3),(3,4),(4,3),(4,4)
]),
MAX=6
)

# Para crear una matriz random
def ger_random():
return np.random.randint(0,2,(50,50))

def slider(posx,posy,flip=0,invertx=0,inverty=0,MAX=50):
res = [(2, 0), (3, 0), (0, 1), (1, 1), (3, 1), (4, 1), (0, 2), (1, 2), (2, 2), (3, 2), (1, 3), (2, 3)]

masx = max( x for x,y in res)
masy = max( y for x,y in res)

if flip:
res = [(y,-x+masx) for x,y in res]

def invx(v):
if invertx:
return masx -v
else:
return v
def invy(v):
if inverty:
return masy -v
else:
return v

res = [ (
invx(x)+posx,
invy(y)+posy
) for x,y in res]


return [ (x,y) for x,y in res if x in range(0,MAX) and y in range(0,MAX) ]

def multi_slider(num):

w = []
for a in range(num):
w += slider(
random.randint(0,50),
random.randint(0,50),
random.randint(0,1),
random.randint(0,1),
random.randint(0,1),
)
return gol_utils.create_world(w,50)


def simple_slider():
return gol_utils.create_world(
alive=(
[
(5,3),(6,3),
(3,4),(4,4), (6,4),(7,4),
(3,5),(4,5),(5,5),(6,5),
(4,6),(5,6),
]),
MAX=30
)

w = multi_slider(20)





fig = plt.figure()

def update(i):
global w
plt.clf()
gol_utils.draw_world(w)
fig.canvas.draw()
w = gol.evolve(w)


a = anim.FuncAnimation(fig, update, frames=3000, repeat=False)
plt.show()