[Tutor] Life Grid Implementation

Alan Gauld alan.gauld at btinternet.com
Mon Apr 20 18:16:27 CEST 2015


On 20/04/15 13:59, niyana morgan wrote:
> I don't understand how to do this. I attempted with my code and hoped a
> tutor could help me with this

Always write code that runs. Then fix the mistakes as they
happen. That way you only have a few lines of code to look
at. Your code is far too big and intertwined to be able
to debug easily.

Look at your specification:
• SparseLifeGrid() Creates a new infinite-sized game grid
    with all cells initially dead.
...
  • clearCell(row, col) Clears the individual cell (row, col)
    and sets it to dead. If the cell is already dead, no action
    is taken.
  • setCell(row, col) Sets the indicated cell (row, col)
    to be alive. If the cell is already alive, no action is taken.
....

I suggest you start over with a class definition and an init().
See if you can create an instance and if it has the
fields you expect. Thats your first API bullet done.

Once you can do that create the methods needed to populate
it with data (setCell()) and clear a cell(clearCell()).
Nothing else, just those two methods. Get them working.
Now you have three parts of your assignment done.

Only when you can do those things correctly attempt one of the remaining 
API methods - maybe the isLiveCell() method.

Then try minRange and maxRange after that.

Finally you can try numLiveNeighbours confident that the
rest of the class works.

Always run the code. Every time you complete a method
your code should work. If you write your tests as code
in a function, that function should always work even
after you complete a new stage. If anything that used
to work stops working, go back and fix it before trying
anything new. The code you changed will still be fresh
in your mind.

If you really get stuck come back here and ask.
Always copy any error messages in full.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list