A simple game of life in Python. Problems.
Page 1 of 1
Trolldeg




Posts: 508
Location: Sweden
PostPosted: 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. Mad Any clues would be greatly appreciated. Thanks!
Here is the whole code
 Spoiler:
 
Back to top
tainted4ever
VIP Member



Posts: 11335

PostPosted: Sun, 7th Dec 2008 21:36    Post subject:
I'd help but Python... :/


Sense Amid Madness, Wit Amidst Folly
Back to top
PoorLeno




Posts: 999
Location: Sweden
PostPosted: 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. Very Happy


Back to top
PoorLeno




Posts: 999
Location: Sweden
PostPosted: 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
Trolldeg




Posts: 508
Location: Sweden
PostPosted: 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
PoorLeno




Posts: 999
Location: Sweden
PostPosted: Sun, 7th Dec 2008 23:21    Post subject:
Which school, which book?


Back to top
Trolldeg




Posts: 508
Location: Sweden
PostPosted: Sun, 7th Dec 2008 23:25    Post subject:
PoorLeno wrote:
Which school, which book?


KTH, Starting out with Python by Tony Gaddis.

I've even had this code go through some of our teachers who said "Yeah this is probably a good way to do it on". Guess not. Rolling Eyes
Back to top
PoorLeno




Posts: 999
Location: Sweden
PostPosted: 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. Wink 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
Trolldeg




Posts: 508
Location: Sweden
PostPosted: Sun, 7th Dec 2008 23:36    Post subject:
PoorLeno wrote:
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. Wink I have that book next to me too. Heheh, small world.


Yeah, you are probably the one who tricked me into doing it like this. Laughing

And yeah, it's due this week. Wink
Back to top
Trolldeg




Posts: 508
Location: Sweden
PostPosted: Mon, 8th Dec 2008 01:34    Post subject:
Alright, think I understand most of your code now. Going to write a new one for myself with my newfound "knowledge".

Ill probably run into some problems though so Ill be back. Wink

Thanks for your help so far!
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
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