Like Tree1Likes

Conway's Life - C Program - Errors have me stumped - Please help

This is a discussion on Conway's Life - C Program - Errors have me stumped - Please help within the C Programming forums, part of the General Programming Boards category; Excellent! That did the trick, thank you! Now the program actually runs, asks me to play, after I enter 'y' ...

  1. #16
    Registered User
    Join Date
    May 2007
    Posts
    12
    Excellent! That did the trick, thank you! Now the program actually runs, asks me to play, after I enter 'y' - it creates another board, then stops =/ But progress at last. I'll have a go at fixing this when I get home. Thank you to everyone that helped me get this far.

    ~Sarah87

  2. #17
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Note that this line
    if (neighbours == 3)
    does not do what it should do...
    I suppose it should be
    if (p== 3)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #18
    Registered User
    Join Date
    May 2007
    Posts
    12
    Ok, I'm a little lost as to what to do next. Basically, I can now run my program - It comes up with the original board, asking me to play. I hit 'y' and it makes a new board with the wrong pattern and won't let me create any more from there (will not ask me to play again, even though I included another getchar in the main function! :S)

    I'm fairly sure the line:
    Code:
    count = p + q;
    Is wrong, but I wasn't sure what else to put there, when I first made it.





    I've uploaded a screen shot of how the program looks when run:
    http://i19.tinypic.com/6f96yid.jpg





    And my latest code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void display_fctn (char b1[][21], char b2[][21]);
    void new_board (char b1[][21], char b2[][21]);
    void copy_back (char b1[][21], char b2[][21]);
    int neighbours (int p, int q);
    
    char b1[20][21] = {"**..................",
    						 "**..................",
    						 "....................",
    						 "....................",
    						 "....................",
    						 "....................",
    						 "....................",
    					    "........***.........",
    					    "........*...........",
    					    ".........*..........",
    					    "....................",
    					    "....................",
    					    "....................",
    					    "....................",
    					    "....................",
    					    "....................",
    					    "....................",
    					    "....................",
    					    "....................",
    					    "...................."};
    
    char b2[20][21];
    
    
    int main() {
    	char c;
    	display_fctn(b1, b2);
    	printf("Would you like to play? (y/n)");
    	c = getchar();
    	while (c == 'y') {
    		new_board(b1, b2);
    		copy_back(b1, b2);
    		display_fctn(b1, b2);
    		c = getchar();
    	}
    }
    
    
    void display_fctn(char b1[][21], char b2[][21]) {
    	int i;
    	for (i=0; i<20; i++) {
    		printf("%s \n", b1[i]);
    	}
    }
    
    void new_board(char b1[][21], char b2[][21]) 
    {
    	int i, j, p;
    	for (i=0; i<20; i++) 
    	{
    		for (j=0; j<20; j++) 
    		{
    			p=neighbours(i,j);
    			if(b1[i][j]=='*')
    			{
    				if(p==2||p==3)
    				{
    					b2[i][j] = '*';
    				} 
    				else 
    				{
    					b2[i][j] = '.';
    				}
    			}
    			else 
    			{
    				if (p == 3) 
    				{
    					b2[i][j] = '*';                        /*	b2 corresponding position gets a '*' */
    				} 
    				else 
    				{
    					b2[i][j] = '.';			                                     /*b2 gets a '.' */
    				}
    			}
    		}
    	}
    	b2[i][j] = '\0'; 						/*	b2[i][j] = '\0'; 	 ----assign  ????? \0 */
    }
    
    void copy_back(char b1[][21], char b2[][21]) {
    	int i;
    	for (i=0; i<20; i++) {
    		strcpy(b1[i], b2[i]);
    	}
    }
    
    int neighbours(int p, int q) {
    	int count;
    	/* ((8 ifs?????))*/
    	if (p!=0 && q!=19 && b1[p-1][q+1] == '*') {
    		count = p + q; /* ??? */
    		}
    	return count; /* return count */
    }
     
    /* initialise variables eg int i = 0 ?? */



    Any tips on where to go from here would be great. Thank you. =)
    Preton likes this.

  4. #19
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Maybe complete your neighbours function?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Let's look at these two functions:
    Code:
    void copy_back(char b1[][21], char b2[][21]) {
    	int i;
    	for (i=0; i<20; i++) {
    		strcpy(b1[i], b2[i]);
    	}
    }
    You shouldn't have to deal with strings in this program. The arrays are char, and char is just a small positive integer, a char with no end of string marker "'\0', is NOT a string.

    I don't like messing with string marker's for a single char - imo, it's more work than help. One mistake and you have a harder bug to find and correct.

    Try:
    Code:
    int row, col;
    /* This uses 20 rows from 0 to 19? Please adjust as needed. */
    
    for (row = 0; row < 20; row++)  { 
       for (col = 0; col < 20; col++) 
           b1[row][col] = b2[row][col];
    } /* curly braces aren't needed here, but they may be helpful to you */
    Code:
    int neighbours(int p, int q) {
    	int count;
    	/* ((8 ifs?????))*/
    	if (p!=0 && q!=19 && b1[p-1][q+1] == '*') {
    		count = p + q; /* ??? */
    		}
    	return count; /* return count */
    }
    In neighbors(), you need to test for living neighbors, in every direction. That includes several special cases, where the cell is:

    From 12 Noon to 12 Midnight, clockwise:

    1) On the top row. You don't want to try and check a cell that is out of bounds, so you can't decrement the row counter, when checking these cells.
    So check towards 3, 4:30, 6, 7:30, & 9 o'clock directions, only.
    * * * * * * * * * *
    * * * * * * * * * *

    2) On the top row, in the far right hand corner. You can't decrement the row or increment the column counter.
    Check towards 6, 7:30, and 9 o'clock only.
    * * * * * * * * * *
    * * * * * * * * * *

    3) On the bottom row, in the far right hand corner. You can't increment the row counter, or increment the column counter.
    Check towards 12, 9, and 10:30 o'clock only.
    * * * * * * * * * *
    * * * * * * * * * *

    4) On the bottom row, anywhere except the corners. You can't increment the row counter.
    What directions would you need to check from here?

    5) On the bottom row, in the far left hand corner. You can't increment the row counter, or decrement the column counter.
    What about from here?


    6) On the far top left hand corner. Here, you can't decrement the row or the column counter.
    How about from here?

    A normal interior cell needs to be checked in directions of 12, 1:30, 3, 4:30, 6, 7:30, 9, and 10:30.

    Good luck!
    Last edited by Adak; 05-23-2007 at 02:35 PM.

  6. #21
    Registered User
    Join Date
    May 2007
    Posts
    12
    Thanks for the help with those Adak, I was quite lost there. At last my program is finished and working perfectly! =D A BIG BIG thanks to all those who helped (you know who you are) I really do appreciate it. Now I can move on and start studying for the final exam (hooray :P)

    Thanks again guys =)

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program giving errors
    By andy bee in forum C Programming
    Replies: 5
    Last Post: 08-11-2010, 11:38 PM
  2. Errors with program to replace string with another
    By Dan17 in forum C Programming
    Replies: 3
    Last Post: 09-14-2006, 11:15 AM
  3. Need help to solve program errors
    By pattop in forum C++ Programming
    Replies: 6
    Last Post: 05-28-2006, 02:57 AM
  4. C++ Game of Life Program
    By rayrayj52 in forum C++ Programming
    Replies: 16
    Last Post: 09-26-2004, 04:58 PM
  5. Simple Program wont execute after compiles w. no errors
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 02-03-2002, 04:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21