[Tutor] Game of python, help please.
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
> '''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.:
> 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
> 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):
n = input(prompt + " enter a integer between %s and %s" % (lower,
n = int(n)
if lower <= n <= upper:
x = getNum("How many rows?", 5, 101)
y = getNum("How many rows?", 5, 101)
t = getNum("How many rows?", 5, 101)
> 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('
Note (again) it's common practice to use names starting with lower case
These nested loops
> for i in range(X):
> for j in range(Y):
can be replaced with:
space = *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.
Chapel Hill NC
More information about the Tutor