|
Page 1 of 1 |
|
Posted: Sun, 7th Dec 2008 13:21 Post subject: A simple game of life in Python. Problems. |
|
 |
Code: | def checkNeighbours(living):
#funktion som kollar om cellen har grannar och bestämmer framtida bestånd
score = 0
#här behövs två matriser, en för att hålla reda på nuvarande generation och en för framtiden
living = living
future = living
#loop som går igenom alla celler i matrisen
for rad in range(1,19):
for plats in range(1,19):
if living[rad][plats]==1: #cell existerar på denna plats för tillfället
score = living[rad-1][plats-1] + living[rad-1][plats] + living[rad-1][plats+1] + living[rad][plats-1] + living[rad][plats+1] + living[rad+1][plats-1] + living[rad+1][plats] + living[rad+1][plats+1]
#lägg ihop närliggande celler
#000 m[n-1][p-1], m[n-1][p], m[n-1][p+1]
#0x0 m[n][p-1], m[n][p+1]
#000 m[n+1][p-1], m[n+1][p], m[n+1][p+1]
if score ==3:
future[rad][plats]=1
#cell kommer att existera på denna plats i nästa generation
elif score ==2:
future[rad][plats]=1
#cell kommer att existera på denna plats i nästa generation
else:
future[rad][plats]=0
#cell kommer inte att existera på denna plats i nästa generation
elif living[rad][plats]==0: #cell existerar inte på denna plats för tillfälet
score = living[rad-1][plats-1] + living[rad-1][plats] + living[rad-1][plats+1] + living[rad][plats-1] + living[rad][plats+1] + living[rad+1][plats-1] + living[rad+1][plats] + living[rad+1][plats+1]
#000 m[n-1][p-1], m[n-1][p], m[n-1][p+1]
#0x0 m[n][p+1], m[n][p+1]
#000 m[n+1][p-1], m[n+1][p], m[n+1][p+1]
if score ==3:
future[rad][plats]=1
#cell kommer att existera på denna plats i nästa generation
else:
future[rad][plats]=0
#cell kommer inte att existera på denna plats i nästa generation
else:
print 'fel!'
#nuvarande generation är inte intressant längre, returnerar framtida generation
return future |
Thats the function the problem is in. It doesn't count the neigbourghs right and I have no clue why. Any clues would be greatly appreciated. Thanks!
Here is the whole code Spoiler: | [code]# -*- coding: cp1252 -*-
def main():
#anropar funktion som skapar en matris
living = createMatrix()
#läs in fil, spara datan till matrisen
living = readFile(living)
userinput='blabla'
living[5][2]=1
living[6][3]=1
living[7][1]=1
living[7][2]=1
living[7][3]=1
userGeneration = input('How many generations between prints?')
#loop
while userinput !='q':
#skriv ut matris
printer(living)
#kontrollerar grannar
for n in range(0, userGeneration):
living = checkNeighbours(living)
userinput= raw_input('Enter to continue, q to quit')
#slut på loop
def createMatrix():
# funktion som skapar matriser
matrix = []
for i in range(0,20):
matrix.append([0]*20)
return matrix
return matrix
def readFile(living):
inlast = living
#funktion som läser av fil, exempelvis rymdskepp.txt och returnerar in datan i levande matris
return inlast
def checkNeighbours(living):
#funktion som kollar om cellen har grannar och bestämmer framtida bestånd
score = 0
#här behövs två matriser, en för att hålla reda på nuvarande generation och en för framtiden
living = living
future = living
#loop som går igenom alla celler i matrisen
for rad in range(1,19):
for plats in range(1,19):
if living[rad][plats]==1: #cell existerar på denna plats för tillfället
score = living[rad-1][plats-1] + living[rad-1][plats] + living[rad-1][plats+1] + living[rad][plats-1] + living[rad][plats+1] + living[rad+1][plats-1] + living[rad+1][plats] + living[rad+1][plats+1]
#lägg ihop närliggande celler
#000 m[n-1][p-1], m[n-1][p], m[n-1][p+1]
#0x0 m[n][p-1], m[n][p+1]
#000 m[n+1][p-1], m[n+1][p], m[n+1][p+1]
if score ==3:
future[rad][plats]=1
#cell kommer att existera på denna plats i nästa generation
elif score ==2:
future[rad][plats]=1
#cell kommer att existera på denna plats i nästa generation
else:
future[rad][plats]=0
#cell kommer inte att existera på denna plats i nästa generation
elif living[rad][plats]==0: #cell existerar inte på denna plats för tillfälet
score = living[rad-1][plats-1] + living[rad-1][plats] + living[rad-1][plats+1] + living[rad][plats-1] + living[rad][plats+1] + living[rad+1][plats-1] + living[rad+1][plats] + living[rad+1][plats+1]
#000 m[n-1][p-1], m[n-1][p], m[n-1][p+1]
#0x0 m[n][p+1], m[n][p+1]
#000 m[n+1][p-1], m[n+1][p], m[n+1][p+1]
if score ==3:
future[rad][plats]=1
#cell kommer att existera på denna plats i nästa generation
else:
future[rad][plats]=0
#cell kommer inte att existera på denna plats i nästa generation
else:
print 'fel!'
#nuvarande generation är inte intressant längre, returnerar framtida generation
return future
def printer(living):
living=living
for n in range(0,20):
print
for p in range(0,20):
if living[n][p]==1:
print 'X',
else:
print '.',
#funktion som skriver ut matrisen på skärmen
#skriv ut på något bra sätt, ascii
#huvudprogram
main()
[/code] |
|
|
Back to top |
|
 |
|
Posted: Sun, 7th Dec 2008 21:36 Post subject: |
|
 |
I'd help but Python... :/
Sense Amid Madness, Wit Amidst Folly
|
|
Back to top |
|
 |
|
Posted: Sun, 7th Dec 2008 22:04 Post subject: |
|
 |
TL;DR;
Blah, your code is badly written. (The total code isn't properly indented), but of the top of my head you do the following wrong:
"""
living = living
future = living
"""
What the hell do you think you are doing there? The first line doesn't do anything, and the second line makes a symlink to "living", not a copy. Since, from what I can gather, "living" is matrix - or, a list of lists - you need to deep copy it, or create a new matrix...
This is the game of life, right? I'm going to code a prototype [without looking at that trainwreck of a code] and get back to you. 
|
|
Back to top |
|
 |
|
Posted: Sun, 7th Dec 2008 22:49 Post subject: |
|
 |
here's my quick stab at game of life, generating a "glider" object (defined in the variable alive pairs)
http://pastebin.com/m2873e9c3
|
|
Back to top |
|
 |
|
Posted: Sun, 7th Dec 2008 23:16 Post subject: |
|
 |
PoorLeno wrote: | TL;DR;
Blah, your code is badly written. (The total code isn't properly indented), but of the top of my head you do the following wrong:
"""
living = living
future = living
""" |
I dunno really. I thought it was needed to do something like that when I call a function. Our book from school did it. Heh.
Either way, thanks for your help. Ill try to understand it but it will probably take a while. Thanks for your help!
|
|
Back to top |
|
 |
|
Posted: Sun, 7th Dec 2008 23:21 Post subject: |
|
 |
Which school, which book?
|
|
Back to top |
|
 |
|
|
Back to top |
|
 |
|
Posted: Sun, 7th Dec 2008 23:33 Post subject: |
|
 |
Hm, you've probably seen me in person then. I work at NADA. I'm going to bet it's either Open, Media or Industrial Economics. Open and Indek's p-uppgifter are due 10th or so december, if i'm not mistaken. I have that book next to me too. Heheh, small world.
I see you are doing this one : http://www.nada.kth.se/kurser/kth/2D1310/122.pdf
har har har
|
|
Back to top |
|
 |
|
Posted: Sun, 7th Dec 2008 23:36 Post subject: |
|
 |
|
|
Back to top |
|
 |
|
|
Back to top |
|
 |
Page 1 of 1 |
All times are GMT + 1 Hour |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group
|
|
 |
|