Thread: Game of Life help needed

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    19

    Game of Life help needed

    Code:
    #include <iostream.h>
       const int ROW = 7;
       const int COL = 7;
    
       void main()
       {
    
          char M[ROW][COL]={
                                {' ',' ',' ',' ',' ',' ',' '},
                                {' ',' ',' ','*','*',' ',' '},
                                {' ',' ','*',' ','*',' ',' '},
                                {' ',' ',' ',' ','*',' ',' '},
                                {' ',' ','*',' ',' ',' ',' '},
                                {' ',' ','*',' ','*',' ',' '},
                                {' ',' ',' ',' ',' ',' ',' '}
                           };
    
          int num = 0;
          cout << "\nPlease enter the number of generations to print: ";
          cin >> num;
    
          cout << "\n\n\tGeneration: " << 0 << endl;
          for (int i=1; i<ROW-1; i++){
             cout << endl << "\t" << '|';
             for (int j=1; j<COL-1; j++){
                cout << M[i][j] << '|';
             }//for
          }//for
    
          cout << endl <<endl;
    
          for (int k=0; k<num; k++){
             for (int i=1; i<ROW-1; i++){
                for (int j=1; j<COL-1; j++){
                   if (M[i-1][j]   == '*') num++;
                   if (M[i-1][j-1] == '*') num++;
                   if (M[i-1][j=1] == '*') num++;
                   if (M[i][j-1]   == '*') num++;
                   if (M[i][j+1]   == '*') num++;
                   if (M[i+1][j]   == '*') num++;
                   if (M[i+1][j-1] == '*') num++;
                   if (M[i+1][j+1] == '*') num++;
                }//for
             }//for
             cout << "\tGeneration: " << k+1 << endl;
                for (int i=1; i<ROW-1; i++){
                   cout << endl << "\t" << '|';
                   for (int j=1; j<COL-1; j++){
                      cout << M[i][j] << '|';
                   }//for
                }//for
    
             cout << endl <<endl;
          }//for
       }//main


    Here is my code. When I try to execute this, it will only print the first generation no matter how many generation I enter. What am I doing wrong?

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    It's an endless loop problem:
    Code:
          for (int k=0; k<num; k++){
             for (int i=1; i<ROW-1; i++){
                for (int j=1; j<COL-1; j++){
                   if (M[i-1][j]   == '*') num++;
                   if (M[i-1][j-1] == '*') num++;
                   if (M[i-1][j=1] == '*') num++;
                   if (M[i][j-1]   == '*') num++;
                   if (M[i][j+1]   == '*') num++;
                   if (M[i+1][j]   == '*') num++;
                   if (M[i+1][j-1] == '*') num++;
                   if (M[i+1][j+1] == '*') num++;
                }//for
             }//for
    You are always evaluating to true on at least one of the if statements and so the value of num is always increased. Thus k will always be less than num and you're stuck in the loop.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    First problem:
    >if (M[i-1][j=1] == '*') num++;
    This is why it only prints the first generation, infinite loop #1, j is always less than COL-1, program no workie.

    Second problem (with first problem corrected):
    Code:
    for (int k=0; k<num; k++){
             for (int i=1; i<ROW-1; i++){
                for (int j=1; j<COL-1; j++){
                   if (M[i-1][j]   == '*') num++;
                   if (M[i-1][j-1] == '*') num++;
                   if (M[i-1][j-1] == '*') num++;
                   if (M[i][j-1]   == '*') num++;
                   if (M[i][j+1]   == '*') num++;
                   if (M[i+1][j]   == '*') num++;
                   if (M[i+1][j-1] == '*') num++;
                   if (M[i+1][j+1] == '*') num++;
                }//for
             }//for
    See it? k gets incremented by one with every iteration of the outer loop, but num gets incremented quite a bit more than that so k will always be less than num. Infinite loop #2*, program no workie.

    *Well, not quite infinite. It'll break on some really high value, but it's still not what you want.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    19

    I think I found the problem

    Was it the j = 1 that you were refering to?

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    19

    Fixed

    I forgot a big part of my code. Sorry guys here is the working code.

    Code:
    #include <iostream.h>
       const int ROW = 7;
       const int COL = 7;
    
      int main()
       {
          char ch = 'X';
          char M[ROW][COL]={
                                {' ',' ',' ',' ',' ',' ',' '},
                                {' ',' ',' ','*','*',' ',' '},
                                {' ',' ','*',' ','*',' ',' '},
                                {' ',' ',' ',' ','*',' ',' '},
                                {' ',' ','*',' ',' ',' ',' '},
                                {' ',' ','*',' ','*',' ',' '},
                                {' ',' ',' ',' ',' ',' ',' '}
                           };
    
          int num = 0;
          cout << "\nPlease enter the number of generations to print: ";
          cin >> num;
    
          cout << "\n\n\tGeneration: " << 0 << endl;
          for (int i=1; i<ROW-1; i++){
             cout << endl << "\t" << '|';
             for (int j=1; j<COL-1; j++){
                cout << M[i][j] << '|';
             }//for
          }//for
    
          cout << endl <<endl;
    
         for (int k=0; k<num; k++){
             for (int i=1; i<ROW-1; i++){
                for (int j=1; j<COL-1; j++){
                   int num = 0;
                   if (M[i-1][j]   == '*') num++;
                   if (M[i-1][j-1] == '*') num++;
                   if (M[i-1][j+1] == '*') num++;
                   if (M[i][j-1]   == '*') num++;
                   if (M[i][j+1]   == '*') num++;
                   if (M[i+1][j]   == '*') num++;
                   if (M[i+1][j-1] == '*') num++;
                   if (M[i+1][j+1] == '*') num++;
                      if(M[i][j]=='*'){
                         if((num < 2) || (num >=4))
                            M[i][j] =' ';
                      } else if (M[i][j] == ' '){
    
                        if (num == 3)
                           M[i][j] = '*';
    
                      }
                   }//for
             }//for
             cout << "\tGeneration: " << k+1 << endl;
                for (int i=1; i<ROW-1; i++){
                   cout << endl << "\t" << '|';
                   for (int j=1; j<COL-1; j++){
                      cout << M[i][j] << '|';
                   }//for
                }//for
    
             cout << endl <<endl;
    
          }//for
             cout << "That's the Game of Life!! Press any key to exit: ";
             cin >> ch;
             while (ch == '_');
       }//main

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    theres one major problem.............post this in the game programming forums
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  7. #7
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    No it's fine here. It has more to do with C++ than it has to do with game programming. Moron.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game of Life... Turned my life to HELL!
    By yigster in forum C Programming
    Replies: 1
    Last Post: 05-21-2009, 06:29 PM
  2. Game of Life
    By CornedBee in forum Contests Board
    Replies: 74
    Last Post: 05-20-2008, 01:50 AM
  3. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 01:32 PM
  4. Game of life
    By JoshR in forum C++ Programming
    Replies: 30
    Last Post: 04-03-2005, 02:17 PM
  5. game of life
    By marleyman7 in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2002, 06:11 AM