"Game of Life" - 2D und 3D
Verfasst: Montag 26. März 2007, 14:45
Hi!
Mir war mal wieder Langweilig, da dachte ich mir, ich könnte mal ein Game of Life Script schreiben ... Lasst mich dazusagen, dass es ziemlich viel Leistung braucht ...
2D-Version:
3D-Version:
Und jetzt, stürzt euch mal drauf
Kann man was verbessern?
MfG,
Craven
Mir war mal wieder Langweilig, da dachte ich mir, ich könnte mal ein Game of Life Script schreiben ... Lasst mich dazusagen, dass es ziemlich viel Leistung braucht ...
2D-Version:
Code: Alles auswählen
import time
class game_of_life:
def __init__(self, a = 100, b = 100, r = 100):
self.living_pixel = [[3,3], [3,4], [3,5]]
self.next_gen = []
x = 1
y = 1
i = 1
t1 = time.time()
while i < r:
while x < a:
while y < b:
self.check_pixel(x, y)
y = y + 1
y = 1
x = x + 1
self.living_pixel = self.next_gen
self.next_gen = []
x = 1
i = i + 1
print self.living_pixel
t2 = time.time()
#print round(t2 - t1, 5)
raw_input()
def check_pixel(self, x, y):
if [x, y] in self.living_pixel:
#print x,y,"is a living pixel"
self.is_living_pixel(x, y)
else:
#print x,y,"is a dead pixel"
self.is_dead_pixel(x, y)
def is_living_pixel(self, x, y):
neighbours = self.count_neighbours(x, y)
#print x,y,"has",neighbours,"neighbours"
##if neighbours != 2 and neighbours != 3:
## #print "--> Pixel dies"
## #self.living_pixel.remove([x, y])
##else:
## self.next_gen.append([x, y])
if neighbours == 2 or neighbours == 3:
#print "--> Pixel stays alive"
self.next_gen.append([x, y])
def is_dead_pixel(self, x, y):
neighbours = self.count_neighbours(x, y)
#print x,y,"has",neighbours,"neighbours"
if neighbours == 3:
#print "--> Pixel spawns"
self.next_gen.append([x, y])
def count_neighbours(self, x, y):
i = 0
if [x-1, y-1] in self.living_pixel:
i = i + 1
if [x-1, y] in self.living_pixel:
i = i + 1
if [x-1, y+1] in self.living_pixel:
i = i + 1
if [x, y-1] in self.living_pixel:
i = i + 1
##if [x, y] in self.living_pixel:
## i = i + 1
if [x, y+1] in self.living_pixel:
i = i + 1
if [x+1, y-1] in self.living_pixel:
i = i + 1
if [x+1, y] in self.living_pixel:
i = i + 1
if [x+1, y+1] in self.living_pixel:
i = i + 1
return i
a = game_of_life()
Code: Alles auswählen
import time
class game_of_life:
def __init__(self, a = 10, b = 10, c = 10, r = 10):
self.living_pixel = [[3,3,3], [3,4,3], [3,5,3]]
self.next_gen = []
x = 1
y = 1
z = 1
i = 1
t1 = time.time()
while i < r:
while x < a:
while y < b:
while z < c:
self.check_pixel(x, y, z)
z = z + 1
z = 1
y = y + 1
y = 1
x = x + 1
self.living_pixel = self.next_gen
self.next_gen = []
x = 1
i = i + 1
#print self.living_pixel
t2 = time.time()
#print round(t2 - t1, 5)
raw_input()
def check_pixel(self, x, y, z):
if [x, y, z] in self.living_pixel:
#print x,y,z,"is a living pixel"
self.is_living_pixel(x, y, z)
else:
#print x,y,z,"is a dead pixel"
self.is_dead_pixel(x, y, z)
def is_living_pixel(self, x, y, z):
neighbours = self.count_neighbours(x, y, z)
#print x,y,z,"has",neighbours,"neighbours"
##if neighbours != 2 and neighbours != 3:
## #print "--> Pixel dies"
## #self.living_pixel.remove([x, y, z])
##else:
## self.next_gen.append([x, y, z])
if neighbours == 2 or neighbours == 3:
#print "--> Pixel stays alive"
self.next_gen.append([x, y, z])
def is_dead_pixel(self, x, y, z):
neighbours = self.count_neighbours(x, y, z)
#print x,y,z,"has",neighbours,"neighbours"
if neighbours == 3:
#print "--> Pixel spawns"
self.next_gen.append([x, y, z])
def count_neighbours(self, x, y, z):
i = 0
if [x-1, y-1, z-1] in self.living_pixel:
i = i + 1
if [x-1, y-1, z] in self.living_pixel:
i = i + 1
if [x-1, y-1, z+1] in self.living_pixel:
i = i + 1
if [x-1, y, z-1] in self.living_pixel:
i = i + 1
if [x-1, y, z] in self.living_pixel:
i = i + 1
if [x-1, y, z+1] in self.living_pixel:
i = i + 1
if [x-1, y+1, z-1] in self.living_pixel:
i = i + 1
if [x-1, y+1, z] in self.living_pixel:
i = i + 1
if [x-1, y+1, z+1] in self.living_pixel:
i = i + 1
if [x, y-1, z-1] in self.living_pixel:
i = i + 1
if [x, y-1, z] in self.living_pixel:
i = i + 1
if [x, y-1, z+1] in self.living_pixel:
i = i + 1
if [x, y, z-1] in self.living_pixel:
i = i + 1
##if [x, y, z] in self.living_pixel:
## i = i + 1
if [x, y, z+1] in self.living_pixel:
i = i + 1
if [x, y+1, z-1] in self.living_pixel:
i = i + 1
if [x, y+1, z] in self.living_pixel:
i = i + 1
if [x, y+1, z+1] in self.living_pixel:
i = i + 1
if [x+1, y-1, z-1] in self.living_pixel:
i = i + 1
if [x+1, y-1, z] in self.living_pixel:
i = i + 1
if [x+1, y-1, z+1] in self.living_pixel:
i = i + 1
if [x+1, y, z-1] in self.living_pixel:
i = i + 1
if [x+1, y, z] in self.living_pixel:
i = i + 1
if [x+1, y, z+1] in self.living_pixel:
i = i + 1
if [x+1, y+1, z-1] in self.living_pixel:
i = i + 1
if [x+1, y+1, z] in self.living_pixel:
i = i + 1
if [x+1, y+1, z+1] in self.living_pixel:
i = i + 1
return i
a = game_of_life()
Kann man was verbessern?
MfG,
Craven