[Tutor] Game of python, help please.

bob gailer bgailer at gmail.com
Tue Apr 10 04:33:17 CEST 2012


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.
In addition to Dave's comments I add:
> I'm trying to give it a number of options. it dimensions, the max 
> number of turns, initial set up, and boundary conditions. 
Please explain "boundary conditions"
> The dimensions and turns were pretty easy to include. 
> The boundary conditions more difficult, and now I'm getting stuck on 
> the initial set up options. I can set it up randomly but Im having 
> problems implementing a checker board pattern.
It really helps when you explain the problem. What does not work as 
expected?
> '''displays: the text of the game of life for a set number of X x Y 
> for a set of R turns.
>
> [-][-][-][-][-]
> [-][-][-][-][-]
> [-][-][-][-][-]
> [-][-][-][-][-]
> [-][-][-][-][-]
I suggest you simplify the display by using - or x e.g.:

--------
--------
---xx---
---xxx--
----xx--
--------

> Get X,Y,T, Initial value
>
> 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
>     create new data space according to rules and old data space.
>     replace dataspace
>     print data space.
>
> print end. '''
>
> import random
>
>
> X,Y,T=0,0,0
> while not 4<X<102:
>     X=int(input('How many rows? enter a integer between 5 and 101: '))
> while not 4<Y<102:
>     Y=int(input('How many columns? enter a integer between 5 and 101: '))
> while not 4<T<102:
>     T=int(input('How many turns? enter a integer between 5 and 101: '))
>
Good place for a function: (untested). Also note it's common practice to 
use names starting with lower case for variables. Note how we validate 
input to ensure it is integer.

def getNum(prompt, lower, upper):
   while True:
     n = input(prompt + " enter a integer between %s and %s" %  (lower, 
upper))
     if n.isdigit():
       n = int(n)
       if lower <= n <= upper:
         return n
x = getNum("How many rows?", 5, 101)
y = getNum("How many rows?", 5, 101)
t = getNum("How many rows?", 5, 101)

entry=0
> while not (entry=='b' or entry=='l' or entry=='d'):
can be simplified to while entry not in ('b', 'l', 'd')
>     entry=input('Press "b" for bound dimensions, press "l" for live 
> boundry, or press "d" for dead boundry: ')
>
>
> #while not(initial=='r' or initial=='c' or initial=='g' or initial=='e'):
> #    initial=input('
> SPACE=[]
Note (again) it's common practice to use names starting with lower case 
for variables.

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

Assuming you decide to have each cell contain either 1 or 0.

You might also consider giving space an extra row at the top and one at 
the bottom, ditto for extra columns.
That will make calculating the neighbor count a lot easier, at  the cost 
of maintaining the extra rows/columns.

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)])
     ....

OK enough for now - there's more but I'm out of time/energy right now.

-- 
Bob Gailer
919-636-4239
Chapel Hill NC



More information about the Tutor mailing list