ich bin noch ein blutiger Anfänger in Sachen Python und Programmierung generell. Da mir für sinnvolle Programme die Ideen und vermutliche auch die Fähigkeiten fehlen, habe ich heute aus Spaß an der Freude ein Programm geschrieben, das Conways 'Spiel des Lebens' spielt (wer es nicht kennt: http://de.wikipedia.org/wiki/Conways_Spiel_des_Lebens ). Damit sich das Problem der fehlenden Fähigkeit ändert, würde ich mich freuen, wenn ihr an dem Programm alles kritisiert, was es zu kritisieren gibt (inklusive schlechtem Stil etc.).
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf8 -*-
import string
import time
def printlife(life, length): #zeigt das momentane Biotop
for i in range(0, length * length):
if life[i][0] == 0:
print ' ',
elif life[i][0] == 1:
print 'o',
if (i + 1) % length == 0:
print
def getvalue(life, position, length): #zählt die lebenden Zellen in der Nachbarschaft
if position % length == 0:
if position == 0:
return (life[1][0] + life[length][0] +
life[1 + length][0])
elif position == length * (length - 1):
return (life[position - length][0] +
life[position + 1 - length][0] +
life[position + 1][0])
return (life[position - length][0] +
life[position + 1 - length][0] +
life[position + length][0] +
life[position + 1 + length][0] +
life[position + 1][0])
elif (position + 1) % length == 0:
if position == length - 1:
return (life[position - 1][0] +
life[position + length - 1][0] +
life[position + length][0])
elif position == (length * length) - 1:
return (life[position - length - 1][0] +
life[position - length][0] +
life[position - 1][0])
return (life[position - length - 1][0] +
life[position - length][0] +
life[position - 1][0] +
life[position - 1 + length][0] +
life[position + length][0])
if position < length:
return (life[position - 1][0] +
life[position + 1][0] +
life[position - 1 + length][0] +
life[position + length][0] +
life[position + length + 1][0])
elif position > length * (length - 1):
return (life[position - length - 1][0] +
life[position - length][0] +
life[position - length + 1][0] +
life[position - 1][0] +
life[position + 1][0])
return (life[position - 1 - length][0] + life[position - length][0] +
life[position + 1 - length][0] +
life[position - 1][0] +
life[position + 1][0] +
life[position + length - 1][0] +
life[position + length][0] +
life[position + length + 1][0])
def evolve(life, length): #entwickelt die nächste Generation
for position in range(0, length * length - 1):
if getvalue(life, position, length) < 2:
life[position].append(0)
elif getvalue(life, position, length) == 2:
if life[position][0] == 0:
life[position].append(0)
elif life[position][0] == 1:
life[position].append(1)
elif getvalue(life, position, length) == 3:
life[position].append(1)
elif getvalue(life, position, length) > 3:
life[position].append(0)
for position in range(0, length * length- 1):
del life[position][0]
return life
def main():
length = int(raw_input('Welche Seitenlaenge soll das Biotop haben? '))
life = []
for i in range(0, length * length):
life.append([0])
answer = None
while answer != '' and answer != 'nein':
answer = raw_input('Zelle zum Leben erwecken? Wenn ja, welche?'
'(Koordinaten) ')
if answer != 'nein' and answer != '':
life[int(string.split(answer, ',')[0]) - 1 + \
(int(string.split(answer, ',')[1]) - 1) * length][0] = 1
nextanswer = int(raw_input('Spiel einfach laufen lassen (1) \
oder x-te Generation ansehen(x eingeben)? '))
if nextanswer == 1:
while True:
printlife(life, length)
evolve(life, length)
time.sleep(1)
print
elif nextanswer > 1:
for generation in range(1, nextanswer):
evolve(life, length)
printlife(life, length)
main()