Could someone help me write code for the game of life in python 2.6. I am a beginner to programming, and I know how to use lists and have functions from a function library that include: setupgame()- which creates a grid,state=isAlive(cell),
num=countNeighboursAlive(cell)in the neighbourhood, newState=decideState(state,num) stating whether the cell should be alive or dead, and setState(cell,state)- allowing the cell to change to a new state and displayBoard()- to display the grid. Thanks.

I mean the part after if __name__.... part in this program:

def plus(a,b):
    return a+b

def minus(a,b):
    return a-b

def getint(prompt):

    try:
        a = raw_input(prompt)
        if a:
            return int(a)
    except ValueError:
        print "Incorrect input, give again!"
        return getint(prompt)
    else:
        return a


if __name__=='__main__':
    a = b = True
    print 'Give empty number to finish'
    while a and b:
        a = getint('Give first integer number: ')
        if a:
            b = getint('Give second integer number: ')
            if b:
                print """
Sum of %i and %i is %i.
Difference of %i and %i is %i.
""" % (a, b, plus(a,b),
       a, b, minus(a,b))
                    
    print 'Bye, bye!'

I gave example of main program processing two integers like when inputting the dimensions of grid in game of life. To show main program in lines 21-34 and exception hanling for wrong input (you would only accept dimension inside some lower and upper bound, not all integers, so you would need to raise exception for wrong input values also). You can also make that code as one function and replace the lines by call to the function.

Could you post your plan for program and code so far. If you get errors or wrong results we can help you out little. Or if you have mistakes in your design.

I want to create a list which will list coordinates on a 50 by 50 game of life grid. I have been able to set the x-value from 0 to 49, while maintaining the y-value as 0. When I try to reset the x-value back to 0, and increment the y-value by 1- my code does not work. Could you help?

(x,y)=(0,0)
lst = []

answer = True
for x in range (50):
    lst=lst+[(x,y)]
    if (x>=49): 
        (x,y)=(0,y+1)
        answer = True
print lst

This makes number grid 0..49 50 times:

board = []
for x in range (50):
    board.append(range(50))

print board
import random
lifechance = .1 ## 10 % chance for life in square
board = []
for x in range (50):
    # '*' == life, '' == dead
    board.append(['*' if random.random()<lifechance else ''
                  for notcare in range(50)])
# make strings for lines replacing '' values (which are considered False) with ' '
print '\n'.join(''.join(life if life else ' '
                        for life in row)
                for row in board)

Edited 6 Years Ago by pyTony: n/a

Hi again. I'm trying to iterate over the list- and create two new lists with one list stating which cells have more than three neighbours, and which cells have less than three neighbours. My code is not working, and I really don't know why. I tried iterating over the original list- and I don't think that went well. Here is my code:

from conwayLibrary import *

lst = []

for y in range(50):
    for x in range(50):
        lst.append((x, y))
        num=countNeighboursAlive(x,y)

for num in lst[:]:
    if (num>3):
        lst.remove(lst)

    if (num<3):
        lst.remove(lst)
    print lst

Please- I need help iterating

from conwayLibrary import *

lst = []

for y in range(50):
    for x in range(50):
        lst.append((x, y))
        num=countNeighboursAlive(x,y)

Please: I need help iterating, 

for num in lst[:]:
    if (num>3):
        lst.remove(lst)

    if (num<3):
        lst.remove(lst)
    print lst

Looks like you want to do

from conwayLibrary import *

lst = []

for y in range(50):
    for x in range(50):
        if countNeighboursAlive(x,y)==3:
            lst.append((x, y))
print lst

I want to create two seperate new lists though, with one list stating which cells have more than three neighbours, and which cells have less than three neighbours. I attempted to run the code above, but it would not work either. I'm not sure why it won't work and how to create 2 lists.

You have to create the game board somehow first so that countNeighboursAlive(x,y) can have the information of living cells. Format depends on the functions used.

So now I've tried this to rectify the problem- but it won't work:

from conwayLibrary import *
setupGame()
displayBoard()
lst = []

for y in range(50):
    for x in range(50):
        if countNeighboursAlive(x,y)==3:
            lst.append((x, y))
print lst
for num in lst[:]:
    if (num>3):
        lst.remove(lst)

    if (num<3):
        lst.remove(lst)
    print lst

Where are you saving the board? The lines 10-17 do not make sense as they do opposite check as line 6-9, so they should do nothing. What it displays? How are the values stored?

Edited 6 Years Ago by pyTony: n/a

This article has been dead for over six months. Start a new discussion instead.