Thread: Game of Life... Turned my life to HELL!

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    33

    Game of Life... Turned my life to HELL!

    As you can read from the title I am having trouble writing Conway's Game of Life... actually this is not entirely true. I can write it as so;

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
       void header(void);
       void survivalRule(char [][20], int, int);
       void birthRule(char [][20], int, int);
       void deathRule(char [][20], int, int);
           
        int main(void)
       {
          char Life[20][20];
          int orgs, gens;
          int i, a, b, row, col;
          int count = 0;
          int x = 19;
          int y = 19;
          char ans;
           
          header();
      
          do{
             printf("\nPlease enter the number of organisms you would like to live in the first generation: ");
             scanf("%i", &orgs);
          
             srand( (unsigned)time( NULL ) );
                  
             for(i = 0; i<orgs; i++)
             {
                row = rand();
                row %= 20;
                col = rand();
                col %= 20;
                Life[row][col] == '*';
             }
            
             for(row = 0; row<20; row++)
             {
                for(col = 0; col<20; col++)
                {
                   if(Life[row][col] != '*')
                       Life[row][col] == ' ';
                }
             }
                  
             printf("\nNow enter the number of generations you would like to simulate: ");
             scanf("%i", &gens);
                  
             for(row = 0; row<20; row++)
             {
                  for(col = 0; col<20; col++)
                  {
                      printf("%s", Life[row][col]);
                  }
                  puts(" ");
              }
                
             do{                
                birthRule(Life, x, y);
                survivalRule(Life, x, y);
                deathRule(Life, x, y);
                    for(row = 0; row<20; row++)
                 {
                      for(col = 0; col<20; col++)
                      {
                          printf("%s", Life[row][col]);
                      }
                      puts(" ");
                 }
                count++;
             }while(count<gens);
              
             printf("Would you like to simulate another generation? y or n:\n");
              
             scanf("%c", &ans);
                  
          }while( ans != 'n' && ans != 'N');
           
          return 0;
       }
                
                    
        void header(void) /*function for program header*/
       {
          printf("\n\t..Welcome to the Game of Life..\n");
          printf("Follow the instructions to start your own simulation of life!\n\n");
       }
        
        void survivalRule(char Life[][20], int x, int y)
       {
          int row, col;
          int neighbors = 0;
          for(row = 1; row<19; row++)
          {
             for(col = 1; col<19; col++)
             {
                if(Life[row][col] == '*')
                {
                   if(Life[row - 1][col - 1] == '*')
                      ++neighbors;
                   if(Life[row - 1][col] == '*')
                      ++neighbors;
                   if(Life[row - 1][col + 1] == '*')
                      ++neighbors;
                   if(Life[row][col - 1] == '*')
                      ++neighbors;
                   if(Life[row][col + 1] == '*')
                      ++neighbors;
                   if(Life[row + 1][col - 1] == '*')
                      ++neighbors;
                   if(Life[row + 1][col] == '*')
                      ++neighbors;
                   if(Life[row + 1][col + 1] == '*')
                      ++neighbors;
                   if(neighbors == 2 || neighbors == 3)
                   {
                      Life[row][col] == '*';
                   }
                }
             }
          }
          return;
       }
        
        void birthRule(char Life[][20], int x, int y)
       {
          int row, col;
          int neighbors = 0;
          for(row = 1; row<19; row++)
          {
             for(col = 1; col<19; col++)
             {
                if(&Life[row][col]== " ")
                {
                   if(Life[row - 1][col - 1] == '*')
                      neighbors++;
                   if(Life[row - 1][col] == '*')
                      neighbors++;
                   if(Life[row - 1][col + 1] == '*')
                      neighbors++;
                   if(Life[row][col - 1] == '*')
                      neighbors++;
                   if(Life[row][col + 1] == '*')
                      neighbors++;
                   if(Life[row + 1][col - 1] == '*')
                      neighbors++;
                   if(Life[row + 1][col] == '*')
                      neighbors++;
                   if(Life[row + 1][col + 1] == '*')
                      neighbors++;
                   if(neighbors == 3)
                   {
                       Life[row][col] == '*';
                   }
                }
             }
          }
      
          return;
       }
        
        void deathRule(char Life[][20], int x, int y)
       {
          int row, col;
          int neighbors = 0;
          for(row = 1; row<19; row++)
          {
             for(col = 1; col<19; col++)
             {
                if(Life[row][col] == '*')
                {
                   if(Life[row - 1][col - 1] == '*')
                      neighbors++;
                   if(Life[row - 1][col] == '*')
                      neighbors++;
                   if(Life[row - 1][col + 1] == '*')
                      neighbors++;
                   if(Life[row][col - 1] == '*')
                      neighbors++;
                   if(Life[row][col + 1] == '*')
                      neighbors++;
                   if(Life[row + 1][col - 1] == '*')
                      neighbors++;
                   if(Life[row + 1][col] == '*')
                      neighbors++;
                   if(Life[row + 1][col + 1] == '*')
                      neighbors++;
                   if(neighbors < 2 || neighbors > 4)
                   {
                      Life[row][col] == ' ';
                   }
                }
             }
          }
          return;
       }
    but I have to submit it following a specific skeleton;

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_ROWS 21
    #define MAX_COLUMNS 44  
    
    #define FALSE 0
    #define TRUE 1
    
    #define DELAY 100000000
    
    
    /* main functions */
    void PrintWorld(char [][], int, int);
    void ClearWorld(char [][], int, int);
    void CopyWorld(char [][], char [][], int, int);
    void NextGeneration(char [][], char [][], int, int); 
    void Animate(int,int);
    int LiveCell(char [][], int, int);
    
    /* neighboring cells in major wind directions */
    int North(char [][], int, int);
    int East(char [][], int, int);
    int South(char [][], int, int);
    int West(char [][], int, int);
    
    /* neighboring cells in minor wind directions */
    int NorthEast(char [][], int, int);
    int SouthEast(char [][], int, int);
    int SouthWest(char [][], int, int);
    int NorthWest(char [][], int, int);
    
    
    main()
    {
            char emptyWorld[MAX_ROWS][MAX_COLUMNS] = {"!------------------------------------------!",
                                                      "!                                          !",
                                                      "!                                          !",   
                                                      "!                                          !",
                                                      "!                                          !",
                                                      "!                                          !",
                                                      "!                                          !",
                                                      "!                                          !",
                                                      "!                                          !",
                                                      "!                                          !",
                                                      "!                                          !",
                                                      "!                                          !",
                                                      "!                                          !",
                                                      "!                                          !",
                                                      "!                                          !",
                                                      "!                                          !",
                                                      "!                                          !",
                                                      "!                                          !",
                                                      "!                                          !",
                                                      "!                                          !",
                                                      "!------------------------------------------!"};
                                               
                                               
            char stableWorld[MAX_ROWS][MAX_COLUMNS] = {"!------------------------------------------!",
                                                       "!                                          !",
                                                       "!       xx xx                              !",   
                                                       "!        x x                               !",
                                                       "!        x x            xx                 !",
                                                       "!       xx xx           xx                 !",
                                                       "!                                          !",
                                                       "!                                          !",
                                                       "!                                          !",
                                                       "!       xxxx             x                 !",
                                                       "!                        xx                !",
                                                       "!                         x                !",
                                                       "!                                          !",
                                                       "!                                          !",
                                                       "!                                          !",
                                                       "!                                          !",
                                                       "!            x                             !",
                                                       "!            xxx                           !",
                                                       "!                                          !",
                                                       "!                                          !",
                                                       "!------------------------------------------!"};
             
            
            
            char oscillatingWorld[MAX_ROWS][MAX_COLUMNS] = {"!------------------------------------------!",
                                                            "!                                          !",
                                                            "!                                          !",   
                                                            "!                                          !",
                                                            "!                                          !",
                                                            "!       xx     xx                          !",
                                                            "!        xx   xx               xxx         !",
                                                            "!     x  x x x x  x                        !",
                                                            "!     xxx xx xx xxx                        !",
                                                            "!      x x x x x x                         !",
                                                            "!       xxx   xxx                          !",
                                                            "!                                          !",
                                                            "!       xxx   xxx                          !",
                                                            "!      x x x x x x               x         !",
                                                            "!     xxx xx xx xxx              x         !",
                                                            "!     x  x x x x  x              x         !",
                                                            "!        xx   xx                           !",
                                                            "!       xx     xx                          !",                                 
                                                            "!                                          !",
                                                            "!                                          !",
                                                            "!------------------------------------------!"};
                                                            
          
          
             char spaceShipWorld[MAX_ROWS][MAX_COLUMNS] = {"!------------------------------------------!",
                                                          "!                                          !",
                                                          "!     x                                    !",   
                                                          "!      x                                   !",
                                                          "!    xxx                                   !",
                                                          "!                                          !",
                                                          "!                                          !",
                                                          "!                                          !",
                                                          "!                                          !",
                                                          "!                                          !",
                                                          "!                                          !",
                                                          "!                                          !",
                                                          "!                                          !",
                                                          "!                                          !",
                                                          "!                                          !",
                                                          "!                                          !",
                                                          "!           x                              !",
                                                          "!          x                               !",
                                                          "!          xxx                             !",
                                                          "!                                          !",
                                                          "!------------------------------------------!"};
                                                          
                                                          
                                                          
            char world [MAX_ROWS][MAX_COLUMNS], newWorld [MAX_ROWS][MAX_COLUMNS];       
            int tick;
            
          
    }
    
    /* print the grid of the world including the boundaries */
    void PrintWorld(char world[][MAX_COLUMNS], int m, int n)
    {
            
    }
    
    
    /* clear all cells of the world. CAUTION: do not overwrite the array elements along the margins */ 
    void ClearWorld(char world[][MAX_COLUMNS], int m, int n)
    {
           
    }
    
    
    /* make a copy of the world */
    void CopyWorld(char world[][MAX_COLUMNS], char copyWorld[][MAX_COLUMNS], int m, int n)
    {
            
    }
    
    
    /* compute the next generation of the world */
    void NextGeneration(char oldWorld[][MAX_COLUMNS], char newWorld[][MAX_COLUMNS], int m, int n)
    {
            
    }
    
    
    /* insert sufficient printf("\n") statements to display the world in the same place as the one
       before and delay printing the next world sufficiently so that it looks like a movie */
    void Animate(int tick, int delay)
    {
                 
    }
    
    
    /* return 1 if a cell at (x, y) is alive and 0 otherwise */
    int LiveCell(char world[][MAX_COLUMNS], int i, int j)
    {
           
    }
    
    
    /* return 1 if a cell North of (x, y) is alive and 0 otherwise 
       special care must be taken to find the neighbors of cells 
       along the upper boundary of the world                        */                             
    int North(char world[][MAX_COLUMNS], int row, int column)
    {
           
    }
    
    
    /* return 1 if a cell Northeast of (x, y) is alive and 0 otherwise  
       special care must be taken to find the neighbors of cells 
       along the upper and right boundaries of the world              */  
    int NorthEast(char world[][MAX_COLUMNS], int row, int column)
    {
            
    }
    
    
    /* return 1 if a cell East of (x, y) is alive and 0 otherwise  
       special care must be taken to find the neighbors of cells 
       along the right boundary of the world                        */  
    int East(char world[][MAX_COLUMNS], int row, int column)
    {
            
    }
    
    
    /* return 1 if a cell Southeast of (x, y) is alive and 0 otherwise  
       special care must be taken to find the neighbors of cells 
       along the left and lower boundaries of the world             */  
    int SouthEast(char world[][MAX_COLUMNS], int row, int column)
    {
           
    }
    
    
    
    /* return 1 if a cell South of (x, y) is alive and 0 otherwise  
       special care must be taken to find the neighbors of cells 
       along the lower boundary of the world                        */  
    int South(char world[][MAX_COLUMNS], int row, int column)
    {
            
    }
    
    
    /* return 1 if a cell Southwest of (x, y) is alive and 0 otherwise  
       special care must be taken to find the neighbors of cells 
       along the lower and left boundaries of the world             */  
    int SouthWest(char world[][MAX_COLUMNS], int row, int column)
    {
              
    }
    
    
    /* return 1 if a cell West of (x, y) is alive and 0 otherwise  
       special care must be taken to find the neighbors of cells 
       along the left boundary of the world                        */  
    int West(char world [][MAX_COLUMNS], int row, int column)
    {
            
    }
    
    
    /* return 1 if a cell Northwest of (x, y) is alive and 0 otherwise  
       special care must be taken to find the neighbors of cells 
       along the left and upper boundaries of the world             */  
    int NorthWest(char world [][MAX_COLUMNS], int row, int column)
    {
            
    }
    What the **** am I going to do with this north northwest crap! Sorry for the cursing but I have been up fo 24 hours now and I am going to.. no already lost it!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not sure what you're really having a problem with here. NW is -1,-1. SE is +1,+1. If you really need "north northwest", then NNW is -1,-2, assuming you count columns before rows.


    Quzah.
    Hope is the first step on the road to disappointment.

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. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  3. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM
  4. Game of Life help needed
    By Nick Dillon in forum C++ Programming
    Replies: 6
    Last Post: 04-09-2002, 07:42 PM
  5. Game of Life Program Help
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 01-23-2002, 10:32 AM