Thread: Game of Life. Please Help!

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    4

    Game of Life. Please Help!

    [ Completed Project, Thanks for your help ]
    Last edited by KBakerSR; 05-18-2008 at 08:23 PM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Seems to me like you have out-of-bounds memory accesses and you check them after the fact in livingcell and deadcell. Also, the upper bound check in those functions is incorrect.

    What would be a lot easier would be to modify boolMatrix.get():
    Code:
    bool boolMatrix::get(int row, int col)
    {
      if(row < 0 || row >= rows || col < 0 || col >= cols) {
        return false;
      }
      return matrix[row][col];
    }
    The other thing you have wrong is that you're counting the current cell.

    The distinction between deadcell and livingcell is not useful. The functions are too similar.

    Also, the loops in those functions are misplaced. Way too complicated for something so simple, since you know they'll never change. Unroll them. Also, you can take advantage of the implicit conversion of bool to int.
    Code:
    int truecount =
      generation.get(row-1, col-1) + generation.get(row-1, col  ) + generation.get(row-1, col+1)
      generation.get(row-1, col  ) +              0               + generation.get(row  , col+1)
      generation.get(row-1, col+1) + generation.get(row+1, col  ) + generation.get(row+1, col+1)
    Then you do:
    Code:
    switch(truecount)
    {
    case 2: newGeneration.set(row, col, generation.get(row, col));
    case 3: newGeneration.set(row, col, true);
    default: newGeneration.set(row, col, false);
    }
    This is possible because of a shorter reformulation of the rules:
    1) If a cell has two living neighbors, it stays the way it is.
    2) If a cell has three living neighbors, it will be alive, whether it was now or not.
    3) Otherwise, it will be dead, whether it was now or not.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    4
    THANK YOU!! Your way was much more simple to follow and check for errors. The boundries were a little messed up because I just coppied and pasted the contents of one for statement 3 times in each function and forgot to change what all the boundries were reading to fit each for statement. Also, I wasn't even thinking about the current cell being counted which is probably why I wasn't able to get the correct answer no matter how many different ways i wrote this section of code. I've re-written it four times now and yours is by far the best way.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    for your reference, there was a very similar problem on the CEMC contest (a Canadian CS contest for high school students, which I participated in)
    http://www.cemc.uwaterloo.ca/ccc/2006/senior.pdf

    scroll down to S5. The name is Origin of Life, your teacher probably changed it to avoid googling. The problem is much harder, though, because it wants the students to work backwards, from a current generation, generating a tree, and also it's a lot more parameterized. That was the super hard question of the year =).

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    4
    Actually that game was changed from the original, the original thing is called Conway’s Game of Life which is pretty much the exact program my teacher is having us write. They are almost exactly the same with an exception of the Origin of Life game having to find the garden of eden generation and what you said about having to work backwards to figure out the Origin of Life. Thank you for that link though, it will be an interesting thing to check out.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > [ Completed Project, Thanks for your help ]
    Please don't do this - you've basically trashed the thread so no-one else can possibly learn from (or even contribute to) the discussion.

    Talk about being selfish - you've got your cookie, and it's "adios suckers".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Salem, I agree. That should totally be disallowed. Maybe users with fewer than x posts should not be allowed to edit them.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    4
    alright, sorry for that. I will post my entire code with comments and everything later tonight or tomarrow. and Salem what is the problem with being urgent about getting an assignment done that was due in one day. I had worked on trying to get the correct answer to come out for hours and couldnt do it so my last resort was to post on two forums that I thought looked like I would get the best answers from, not all over the internet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  2. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  3. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  4. Game Of Life
    By din1983 in forum C Programming
    Replies: 20
    Last Post: 10-11-2005, 10:36 PM
  5. Please help with some coding..
    By stehigs321 in forum C Programming
    Replies: 2
    Last Post: 10-27-2003, 06:44 PM