I actually just finished it, without the extra cells. I used a combiniation of two functions and a some exception handling to do so. Here is the script.
#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 basesurrounding(space,i,j): if i==0: if j==0: s=[space[i][j+1],space[i+1][j],space[i+1][j+1]] elif j==(y-1): s=[space[i][j-1],space[i+1][j-1],space[i+1][j]] else: s=[space[i][j-1],space[i][j+1],space[i+1][j-1],space[i+1][j],space[i+1][j+1]] elif i==(x-1): if j==0: s=[space[i-1][j],space[i-1][j+1],space[i][j+1]] elif j==(y-1): s=[space[i-1][j-1],space[i-1][j],space[i][j-1]] else: s=[space[i-1][j-1],space[i-1][j],space[i-1][j+1],space[i][j-1],space[i][j+1]] else: if j==0: s=[space[i-1][j],space[i-1][j+1],space[i][j+1],space[i+1][j],space[i+1][j+1]] elif j==(y-1): s=[space[i-1][j-1],space[i-1][j],space[i][j-1],space[i+1][j-1],space[i+1][j]] else: 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(s) def findsurrounding(space,i,j,boundry,x): if boundry=='d': surrounding=0 for i in x: surrounding+=i return(surrounding) if boundry=='l': surrounding=8-len(x) for i in x: surrounding+=i return(surrounding) else: try: return(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]) except IndexError: try:#if x goes over. return(space[i-1][j-1]+space[i-1][j]+space[i-1][j+1]+space[i][j-1]+space[i][j+1]+space[0][j-1]+space[0][j]+space[0][j+1]) except IndexError: try:#if y goes over return(space[i-1][j-1]+space[i-1][j]+space[i-1][0]+space[i][j-1]+space[i][0]+space[i+1][j-1]+space[i+1][j]+space[i+1][0]) except IndexError:#both go over return(space[i-1][j-1]+space[i-1][j]+space[i-1][0]+space[i][j-1]+space[i][0]+space[0][j-1]+space[0][j]+space[0][0]) 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][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][j]=1 else: space[i][j]=0 elif initial=='g': space[1][2]=1 space[2][3]=1 space[3][1]=1 space[3][2]=1 space[3][3]=1 elif initial=='e': space[2][2]=1 space[2][3]=1 space[2][4]=1 space[3][1]=1 space[3][2]=1 space[3][3]=1 #show initial conditions of board on turn 0 print("---------Turn 0---------\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): surrounding=findsurrounding(space,i,j,boundry,basesurrounding(space,i,j)) mortality=determinelife(surrounding,space,i,j) new[i][j]=mortality space=new[:] print('-------Turn %s--------' %(str(turn+1))) printset(space) print("This is the end ") On Wed, Apr 18, 2012 at 12:53 PM, bob gailer <bgai...@gmail.com> wrote: > On 4/17/2012 2:23 PM, leo degon wrote: > > > 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. > > I'm glad to see you using some of my ideas! > > > > 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 bounadry 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. > > [snip] > > I again recommend you add extra rows and columns around the space so you > always use one formula for surround. > > For dead initialize the extra cells to 0 > For live initialize the extra cells to 1 > For bound - every time you change a boundary cell make the same change to > the corresponding extra cell. > > I hope that makes sense. Does it? > > Also recall my condensed way of summing surrounding cells. I realize I > made a mistake in my original proposal - in that it counted the cell itself > as well as the neighbors. Easy to fix. > > Originally I suggested: > > for r in range(1,x+1): > for c in range1,(y+1): > surrounding = sum([sum(space[r+z][c-1:c+2]) for z in(-1,0,1)]) > > New suggestion: > def determinelife(surronding,**space,i,j): > return sum(space[i-1][j-1:j+2]) + sum(space[i][j-1:j+2:2]) + > sum(space[i+1][j-1:j+2]) > > HTH > > > > -- > Bob Gailer > 919-636-4239 > Chapel Hill NC > >
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor