Gonway's Game of Life

A forum for general discussion of the Python programming language.

Gonway's Game of Life

Postby matsinator » Wed Mar 06, 2013 1:06 pm

We have an assignement in Python programming and we can't figure it out on our own, can you help us forum?
Our job is to create a game of life, using these four rules:

Any live cell with fewer than two live neighbours dies, as if caused by under-population.
Any live cell with two or three live neighbours lives on to the next generation.
Any live cell with more than three live neighbours dies, as if by overcrowding.
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

This has to act like a torus to make it even more difficult
Please help us

This is what we've got so far:

Code: Select all
TIME = 10

seed = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]

from Tkinter import *

import random
import time

live=[]
lives=0
e=0

for e in range (1,TIME):
    a=len(seed)
    for e in seed:
        b= len(e)

# load the initial data
def load():
    for y in range(-1,a):
        for x in range(-1,b):
            live[x][y] = 0
            temp[x][y] = 0
            cell[x][y] = canvas.create_oval((x*10, y*10, x*10+10, y*10+10), outline="gray", fill="black")

def frame():
    process()
    draw()
    root.after(100, frame)

   
# Apply the 4 rules to the matrix
def process():
    for y in range(0,a-1):
        for x in range(0,b-1):
            lives = live_neighbors(x,y)
            # Rule 1 - Any live cell with fewer than two live neighbours dies, as if caused by under-population.
            if live[x][y] == 1 and lives < 2: temp[x][y] = 0
            # Rule 2 - Any live cell with two or three live neighbours lives on to the next generation.
            if live[x][y] == 1 and (lives == 2 or lives == 3): temp[x][y] = 1
            # Rule 3 - Any live cell with more than three live neighbours dies, as if by overcrowding.
            if live[x][y] == 1 and lives > 3: temp[x][y] = 0
            # Rule 4 - Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
            if live[x][y] == 0 and lives == 3: temp[x][y] = 1

    for y in range(0,a-1):
        for x in range(0,b-1):
            live[x][y] = temp[x][y]

# Count live neighbors
def live_neighbors(m,n):
    lives = 0
    if live[m-1][n+1] == 1: lives = lives + 1
    if live[m][n+1] == 1: lives = lives + 1
    if live[m+1][n+1] == 1: lives = lives + 1
    if live[m-1][n] == 1: lives = lives + 1
    if live[m+1][n] == 1: lives = lives + 1
    if live[m-1][n-1] == 1: lives = lives + 1
    if live[m][n-1] == 1: lives = lives + 1
    if live[m+1][n-1] == 1: lives = lives + 1
    return lives

# Draw all cells
def draw():
    for y in range(a-1):
        for x in range(b-1):
            if live[x][y]==0:
                canvas.itemconfig(cell[x][y], fill="white")
            if live[x][y]==1:
                canvas.itemconfig(cell[x][y], fill="red")

# main loop
root = Tk()
root.title("Opdracht Gospers Gun")
canvas = Canvas(root, width=800, height=600, highlightthickness=0, bd=0, bg='black')
canvas.pack()
load()
frame()
root.mainloop()

import show
show.show(live)


We get this traceback:

Traceback (most recent call last):
File "C:\Users\Gebruiker 1\Documents\Bio-ingenieur\Programmeervaardigheden\assignment1\gospers_gun.py", line 117, in <module>
load()
File "C:\Users\Gebruiker 1\Documents\Bio-ingenieur\Programmeervaardigheden\assignment1\gospers_gun.py", line 62, in load
live[x][y] = 0
IndexError: list index out of range
>>>

P.S. Sorry for the obvious rooky mistake, we tried to fix it with our best intentions but if you (the forum) have another suggestion, we will be glad to hear it.
Last edited by matsinator on Thu Mar 07, 2013 11:36 am, edited 2 times in total.
matsinator
 
Posts: 1
Joined: Wed Mar 06, 2013 12:59 pm

Re: Gonway's Game of Life

Postby micseydel » Wed Mar 06, 2013 8:23 pm

Yoriz, you should have replied or something, what you did is extremely confusing.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1258
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Gonway's Game of Life

Postby Yoriz » Wed Mar 06, 2013 8:53 pm

Its not confusing, they are going to read that link and precede to edit their post by putting their code into code tags with all the correct formatting.
Then they will post a traceback of the error they are getting and what they are wanting help with.
Then the forum can help them. simples
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Gonway's Game of Life

Postby micseydel » Wed Mar 06, 2013 8:58 pm

It's you speaking as them using "we", right? That's confusing. Without a mention of it, they might not even notice it, and others reading it will wonder why they made such a post instead of just doing it right in the first place.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1258
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Gonway's Game of Life

Postby Yoriz » Wed Mar 06, 2013 9:13 pm

They tended to use 'we' a lot i just joined in, it states in the edit that i added the p.s.
We the forum will not not know if they have errors due to their indentation and what exactly they would like us to help them with.
Will they notice it, who knows , they never notice the 'New Users, Read This' so maybe they won't and your right why didn't they do it right in the first place.
Others reading it and wondering why they made such a post instead of just doing it right in the first place is a good thing.
That means others read the 'New Users, Read This' first, which makes others better forum users, us the forum, can help others with their problems easier.
With a little effort they can join the others.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Gonway's Game of Life

Postby Mekire » Thu Mar 07, 2013 5:55 am

matsinator wrote: Before anyone helps us we will go read this post first http://python-forum.org/viewtopic.php?f=10&t=145


Aww... and I really wanted to talk about that extremely efficient way of seeding a Gosper glider gun. :D
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Gonway's Game of Life

Postby Mekire » Thu Mar 07, 2013 1:06 pm

Thanks for editing your post. Much better. The traceback should also be in code tags too (but not the same code tags as the code itself).
My first question to you however, before even attempting to get into your code and debug anything, would be (as I implied my previous post) are you required to supply your seed in the given format.

This method, aside from being a bit cumbersome, will be EXTREMELY slow.
Sets are the most appropriate object to use for this. You don't need to keep track of where there aren't living cells; just where there are living cells.
Code: Select all
#Seed for your 'Gosper Glider Gun' in set form.
SEED = {(22, 3),(17, 5),(16, 8),( 2, 6),(35, 3),(16, 4),(36, 4),(14, 3),(25, 7),
        (22, 4),(21, 4),(18, 6),( 1, 6),(25, 1),(36, 3),(13, 9),( 2, 5),(35, 4),
        (14, 9),(17, 7),(11, 7),(17, 6),(13, 3),(11, 5),(25, 6),(23, 2),(21, 3),
        ( 1, 5),(15, 6),(12, 4),(21, 5),(25, 2),(22, 5),(23, 6),(11, 6),(12, 8)}
#Convenient constant for looking at adjacent neighbors.
ADJACENTS = {(-1, 1),(0, 1),(1,1 ),(-1, 0),(1, 0),(-1,-1),(0,-1),(1,-1)}

Also you should really be using a class for this, as opposed to spewing variables into the global namespace.

I am not particularly familiar with doing things like this in Tkinter but I have written a Pygame Conway program if you want to take a look (you will find it a tad above your level but perhaps amusing nonetheless). Aside from the code needing a thorough update up to my current standards, it is still a fairly in depth Cellular Automata simulator.
http://python-forum.org/viewtopic.php?f=11&t=57

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests