Thread: Game of life. Help

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    You aren't initialising x on each loop.

    I might be wrong because it's been a while but don't diagonally adjacent squares also count?

    I have a feeling that using the gameR matrix is probably causing the wrong behaviour too but it's too difficult to tell without seeing the rest of your code. Either way you can replace those comparisons by only using your gameC matrix (I'm assuming from context that gameC is the previous generation).

    Also you need to make provisions for when you are at the edges of your matrix. For example on the very first iteration of your loop you will enounter if (gameR[0][0] == gameC[0][-1] ){ which is a bad array index.

    It seems unnecessary to initalise t and u in each loop. To be honest it seems unnecessary to initialise these at all. I would make '_' and '*' preprocessor macros (e.g #define EMPTY '_').

    Your code for assigning the character to the matrix square can be greatly simplified. You only need one case for when it is not emtpy ( when x is either 2 or 3 ) and another for when it is empty (all other cases).

  2. #2
    Registered User
    Join Date
    Feb 2014
    Location
    NY
    Posts
    56
    Quote Originally Posted by DeadPlanet View Post
    You aren't initialising x on each loop.
    Can you elaborate on this?

    Quote Originally Posted by DeadPlanet View Post
    You aren't initialising x on each loop.

    Also you need to make provisions for when you are at the edges of your matrix.
    How?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by 3DT View Post
    Can you elaborate on this?
    When you get back to the top of the loop (line 3 of the code in post #5) the second, third, fourth, time, etc, what is the value of x?
    Quote Originally Posted by 3DT View Post
    How?
    DeadPlanet already pointed out that you should beware of negative array indexes. Time for you to put your thinking cap on. What programming construct could you use to check a variable and do something based on it's value? Note that, just like you need to check if your array indexes would be less than 0, you also need to check if the are too big, lest they go off the other end of the arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the game of life
    By ben432 in forum C Programming
    Replies: 1
    Last Post: 06-13-2011, 10:23 AM
  2. Game of Life... Turned my life to HELL!
    By yigster in forum C Programming
    Replies: 1
    Last Post: 05-21-2009, 06:29 PM
  3. Game of Life
    By puk284 in forum C# Programming
    Replies: 2
    Last Post: 11-17-2008, 03:53 AM
  4. Game Of Life
    By din1983 in forum C Programming
    Replies: 20
    Last Post: 10-11-2005, 10:36 PM
  5. Game of life
    By JoshR in forum C++ Programming
    Replies: 30
    Last Post: 04-03-2005, 02:17 PM