[Tutor] Game of python, help please.

Dave Angel d at davea.name
Mon Apr 9 14:19:09 CEST 2012


On 04/09/2012 02:26 AM, leo degon wrote:
> Hello all, Im trying to learn python and programming in my free time

Welcome to the list;  I think you'll enjoy Python programming, and think
it makes an excellent first language.

> , 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.

Thanks for giving that information.  Many people skip the step of
describing the environment.

Some general comments first.  You've managed in the code below to avoid
many of the beginner mistakes in Python, like building a two dimensional
array by replicating one row.  But you do have a few quirks, by
inspection.  I've not tried to run any of the code.

The biggest one is that you have only one function.  Much of this code
is very repetitive, and thus likely to have errors in some portion that
doesn't run too often.  And when you fix one place, you have to check
many others to see whether it has the same problem.  So factor it out
into functions according to feature.  A good start is to write a
function for each line in your docstring, except for the the do-while
statement.  Then when you realize the biggest function is the "nextmove"
one, factor that as well.  Most of it is spent calculating 'surronding'.

I also found it strange that each element of the grid is itself a list,
with exactly one element (an integer 0 or 1) in it.  Why not just make
it an integer ?  There are a lot of places which would then change, so
probably you shouldn't do that till you've refactored things to simplify.



>  I'm trying to give it a number of options. it dimensions, the max
> number of turns, initial set up, and 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.
>
>
> <SNIP>
> #while not(initial=='r' or initial=='c' or initial=='g' or initial=='e'):
> #    initial=input('
> SPACE=[]
> for i in range(X):
>     SPACE.append([])
>     for j in range(Y):
>         SPACE[i].append([0])
>
> print(SPACE)
>
> #initial set up-random, checker board, glider, eternal.
> initial=0
> while not(initial =='r' or initial =='c' or initial=='g' or initial=='e'):
>     initial=input('Chose inital setup: press "r" for random,"c" for
> checker, "g" for glider,"e"=eternal')
>
>
> if initial=='c':
>     idex=0
>     for i in range(X):
>         jdex=0
>         for j in range(Y):
>             if (idex+jdex)%2==0:
>                 SPACE[idex][jdex]=[0,]
>                 jdex+=1
>             else:
>                 SPACE[idex][jdex]=[1,]
>                 jdex+=1
>             idex+=1

Here you have set each element of the grid to be a list containing a
tuple containing an int.  To be consistent with the rest of the code as
it currently is, you should use
            SPACE[idex][jdex] = [0]

or if/when you take my earlier suggestion to make it just an int,
            SPACE[idex][jdex] = 0

Note, though, that you don't want to make too many changes to the logic
at the same time.


> <SNIP>

> turn=0
>
>
> while turn<T:
Any reason not to replace those two lines and the one at the end with:
     for turn in range(T):

HTH

-- 

DaveA



More information about the Tutor mailing list