[Tutor] Game of python, help please.
leo degon
existentialleo at gmail.com
Tue Apr 17 20:23:58 CEST 2012
Ok so I've done a bit of work on the program and rewrote it. I tried to
take everyones advice. I've used more functions, I've made it so that it is
a list of lists each containing an integer instead of another list with a
single entry.
Im am having problems thinking of how to simply and elegantly calculate the
surrounding cells.I could brute force it as i did originally, but I'd like
some pointers of how to avoid that. As of now, the script is finished for
that 'small' detail. I have it so that instead of actually calculating the
surrounding cells, that functions says that all the cells on the board are
surrounding by three cells. Therefore setting the entire board alive after
the turn zero. I am still trying to include three different versions of the
boundry conditions.
Boundary conditions, what happens when a cell on the edge of the board
tries to calculate the number of surrounding live cells.
Dead- Those nonexistent cells add nothing.
Live-Those nonexistent cells add one per cell. So add 5 to the corners, and
one to edges.
Bound- The dimensions are bound. So you get to the end and loop around.
the script follows
#leoslife.py
# John Conways Game of Life.
plan='''displays: the text of the game of life for a set number of X x Y
for a set of R turns.
[-][-][-][-][-]
[-][-][-][-][-]
[-][-][-][-][-]
[-][-][-][-][-]
[-][-][-][-][-]
Get X,Y,T, Initial Values, Boundry conditions
Create a data space X x Y
Assign initial value
print initial value and the text' initial value'
do while turns<T:
check data space according to boundry conditions
create new data space according to rules and old data space.
replace dataspace
print data space.
print end. '''
#import modules needed to run
import random
# define functions to be used
#function to get an integer between lower and upper bound
def getnum(prompt,lower=3,upper=10):
while True:
n=input(prompt+" Enter an integer between %s and %s. : "
%(lower,upper))
if n.isdigit():
n=int(n)
if lower<=n<=upper:
return n
def printset(x):
for i in x:
print(i)
def findsurronding(space,i,j,boundry):
#s=space[i-1][j-1]+space[i-1][j]+space[i-1][j+1]+space[i][j-1]+space[i][j+1]+space[i+1][j-1]+space[i+1][j]+space[i+1][j+1]
return(3)
def determinelife(surronding,space,i,j):
if surronding==3:
return(1)
elif (surronding<2 or surronding>3):
return(0)
else:
if space[i][j]==1:
return(1)
else:
return(0)
#get x,y,t initial value, boundry condition
x=getnum('How many rows?')
y=getnum('How many columns?')
t=getnum('How many turns?')
#get boundry conditions
boundry=0
while boundry not in ("b","d","l"):
boundry=input("Press 'b' for bound dimenions, 'd' for dead boundry, or
'l' for live boundry. : ")
#get initial set up of space
initial=0
while initial not in ('r','c','g','e'):
initial=input("Press 'r' for random intitial set up, 'c' for a checker
board pattern, 'g' for a single glider, or 'e' for a pattern that repeats
infinitely. : ")
if initial=='g':
if x<5:
x=5
if y<5:
y=5
if initial=='e':
if x<6:
x=6
if y<6:
y=6
#create space
space = []
for i in range(x):
space.append([])
for j in range(y):
space[i].append(0)
#set intital distribution
if initial=='r':
for i in range(x):
for j in range(y):
space[i].__setitem__(j,random.randint(0,1))
elif initial=='c':
for i in range(x):
for j in range(y):
if (i+j)%2==0:
space[i].__setitem__(j,1)
else:
space[i].__setitem__(j,0)
elif initial=='g':
space[1].__setitem__(2,1)
space[2].__setitem__(3,1)
space[3].__setitem__(1,1)
space[3].__setitem__(2,1)
space[3].__setitem__(3,1)
elif initial=='e':
space[2].__setitem__(2,1)
space[2].__setitem__(3,1)
space[2].__setitem__(4,1)
space[3].__setitem__(1,1)
space[3].__setitem__(2,1)
space[3].__setitem__(3,1)
#show initial conditions of board on turn 0
print("-------Initial Board-------\n")
printset(space)
for turn in range(t):
#Create new empty space
new=[]
for i in range(x):
new.append([])
for j in range(y):
new[i].append(0)
#rewrite each space
for i in range(x):
for j in range(y):
surronding=findsurronding(space,i,j,boundry)
mortality=determinelife(surronding,space,i,j)
new[i][j]=mortality
space=new[:]
print('-------Turn %s--------' %(str(turn+1)))
printset(space)
print("This is the end
")
On Tue, Apr 10, 2012 at 10:44 AM, bob gailer <bgailer at gmail.com> wrote:
> On 4/9/2012 10:56 PM, Dave Angel wrote:
>
>> On 04/09/2012 10:33 PM, bob gailer wrote:
>>
>>> On 4/9/2012 2:26 AM, leo degon wrote:
>>>
>>>> Hello all, Im trying to learn python and programming in my free time,
>>>> and I'm trying to do a little personal project to trying and gain
>>>> some skills. Im trying to do version of John conways game of life. I
>>>> have a working version of the game. Written for 3.2.2 on a mac to be
>>>> accessed through terminal.
>>>>
>>> <SNIP>
>>> These nested loops
>>>
>>>> for i in range(X):
>>>> SPACE.append([])
>>>> for j in range(Y):
>>>> SPACE[i].append([0])
>>>>
>>>> can be replaced with:
>>>
>>> space = [0]*y]*x
>>>
>>> not counting the typo, that's the trap I was warning about. If he does
>> NOT replace the individual cells with zeroes and ones, this is wrong.
>> You pointed that out. But it's also wrong for the rows, as you only
>> have one row here, duplicated x times.
>>
> OOPS - what comes of trying to be creative while tired. Sorry and thanks
> for pointing out my error.
>
>
>>
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120417/502aec50/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: leoslife.py
Type: application/octet-stream
Size: 3678 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20120417/502aec50/attachment.obj>
More information about the Tutor
mailing list