Help with code modification

This is a discussion on Help with code modification within the C Programming forums, part of the General Programming Boards category; Hi everyone, I need some help with some code modification for a game. It's based off of John Conway's Game ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    31

    Question Help with code modification

    Hi everyone, I need some help with some code modification for a game. It's based off of John Conway's Game of Life, and some parts of the code are proving tricky. The code I have now compiles and runs, but I'm a bit confused as to how I'm supposed to allow the user to enter 1. how many generations they want to see, and 2. how to let the user enter which cells they want to be alive. The code I have is provided below. Thanks in advance everyone.

    Code:
    #include <stdafx.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    /* This class creates an application to simulate John Conway's Life game.
     * Output is sent to the console.
     * The rules for the Life game are as follows...
     *
     * Life is played on a grid of square cells--like a chess board but 
     * extending infinitely in every direction. A cell can be alive or dead.  
     * A live cell is shown by putting a marker on its square. A dead cell is 
     * shown by leaving the square empty. Each cell in the grid has a 
     * neighborhood consisting of the eight cells in every direction 
     * including diagonals. 
     *
     * To apply one step of the rules, we count the number of live neighbors 
     * for each cell. What happens next depends on this number.
     *
     * -A dead cell with exactly three live neighbors becomes a live cell (birth).   
     *
     * -A live cell with two or three live neighbors stays alive (survival).    
     *
     * -In all other cases, a cell dies or remains dead (overcrowding or 
     *  loneliness).     
     *
     * Note: The number of live neighbors is always based on the cells 
     * before the rule was applied. 
     *
     * Your final version of the program should explain the game and its use
     * to the user.
     */
    	//These two defines establish the dimentions of the board
    
    #define COLS 10
    #define ROWS 10
    
    	//method prototypes
    void fillBoard(bool board[ROWS][COLS]);
    void showBoard(bool board[ROWS][COLS]);
    void getNewBoard(bool board[ROWS][COLS]);
    void makeDead(bool board[][COLS]);
    void copyBoard(bool destin[][COLS], bool source[][COLS]);
    int countNeighbors(int thisRow, int thisCol, bool board[][COLS]);
    
    
    	int main()
    	{
    		bool board[ROWS][COLS];
            
    		makeDead(board);
    		fillBoard(board);
    		showBoard(board);
    
    		// ***Ask the user how many generations to show.
    
    		getNewBoard(board);
    		showBoard(board);
    	}
    
    	//This method randomly populates rows 4-5 of the board
    	// *** Rewrite this method to allow the user to populate the board by entering the
    	// *** coordinates of the live cells.  If the user requests that cell 1, 1 be alive,
    	// *** your program should make cell 0,0 alive.
    
    	void fillBoard(bool board[][COLS])
    	{
    		int row, col;
    		int rndNum;
    		srand(time(NULL));
    		for(row = 3; row < 6; row++)
    			for(col = 3; col < 6; col++)
    			{
    			    rndNum = rand() % 2;
    			    printf("%d", rndNum);
    				if (rndNum == 0)
    				  board[row][col] = false;
    				else
    				  board[row][col] = true;
    			}
    			printf("\n");
    	}
    
    	//This method displays the board
    
    	void showBoard(bool board[][ROWS])
    	{
    		int row, col;
    		printf("\n");
    		for(row = 0; row < ROWS; row++)
    		{
    			for(col = 0; col < COLS; col++)
    				if (board[row][col])
    					printf("X");
    				else
    					printf(".");
    			printf("\n");
    		}
    		printf("\n");
    	}
    
    	//This method creates the next generation and updates the main board
    
    	void getNewBoard(bool board[][ROWS])
    	{
    		int row;
    		int col;
    		int neighbors;
    		bool newBoard[ROWS][COLS];
    
    
    		makeDead(newBoard);
    
    		for(row = 1; row < ROWS-1; row++)
    			for(col = 1; col < COLS-1; col++)
    			{
    				neighbors = countNeighbors(row, col, board);
    				//make this work with one less if
    				if (neighbors < 2)
    					newBoard[row][col]=false;
    				else if (neighbors > 3)
    					newBoard[row][col] = false;
    				else if (neighbors == 2)
    					newBoard[row][col]= board[row][col];
    				else
    					newBoard[row][col] = true;
    			}
            	copyBoard(board, newBoard);
    	}
    
    	//This method counts the number of neighbors surrounding a cell.
    	//It is given the current cell coordinates and the board
    
    	int countNeighbors(int thisRow, int thisCol, bool board[][ROWS])
    	{
    		int count = 0;
    		int row, col;
    
    		for (row = thisRow - 1; row < thisRow + 2; row++)
    			for(col = thisCol - 1; col < thisCol + 2; col++)
    			  if (board[row][col])
    			  	count++;
    		if (board[thisRow][thisCol])
    			count--;
    
    		return count;
    	}
    
    	//This method makes each cell in a board "dead."
    
    	void makeDead(bool board[][COLS])
    	{
    		int row, col;
    
    		for(row = 0; row < ROWS; row++)
    			for(col = 0; col < COLS; col++)
    				board[row][col] = false;
    	}
    	
    	//This method copies the second array parameter's contents
    	//to the first array parameter
    
    	void copyBoard(bool dest[][COLS], bool source[][COLS])
    	{
    	    int row, col;
    
    	    for(row = 0; row < ROWS; row++)
                for(col = 0; col < COLS; col++)
                  dest[row][col] = source[row][col];
    	}

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Make a smallest possible compilable sample representing the problem and ask your question again... Do not spam the board with long lines of even not indented code...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    Quote Originally Posted by vart View Post
    Make a smallest possible compilable sample representing the problem and ask your question again... Do not spam the board with long lines of even not indented code...
    This is the part of the code that I'm having the most trouble with. It doesn't compile in its current form, but if someone could give me some pointers in the right direction, I can try to get it working the way I want:

    Code:
    // *** Rewrite this method to allow the user to populate the board by entering the
    	// *** coordinates of the live cells.  If the user requests that cell 1, 1 be alive,
    	// *** your program should make cell 0,0 alive.
    
    	void fillBoard(bool board[][COLS])
    	{
    		int row, col;
    		int rndNum;
    		srand(time(NULL));
    		for(row = 3; row < 6; row++)
    			for(col = 3; col < 6; col++)
    			{
    			    rndNum = rand() % 2;
    			    printf("%d", rndNum);
    				if (rndNum == 0)
    				  board[row][col] = false;
    				else
    				  board[row][col] = true;
    			}
    			printf("\n");
    	}

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    rndNum = rand() &#37; 2;
    replace this line with code that will ask user to enter number 0 or 1
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    Quote Originally Posted by vart View Post
    rndNum = rand() % 2;
    replace this line with code that will ask user to enter number 0 or 1
    That would make that cell alive or dead, right? The problem I'm running into now is how I can allow the user to specify which cell he or she wants alive or dead. For example, if I want cell 1,1 to be alive, how would I code that with the arrays?

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    Alright, I did some work to my code, but the section where the user is supposed to enter the coordinates is still giving me problems. Here's what I have so far:

    Code:
    void fillBoard(bool board[][COLS])
    	{
    		int row, col;
    		int row1, col1;
    		char quit;
    		for(row = 3; row <= 9; row++)
    			for(col = 3; col <= 9; col++)
    			{
    			    printf("Please enter the cells you want to be alive using row,col format\n");
    			    printf("and enter 'quit' to quit: ");
    			    scanf("&#37;d,%d,%c", &row1,&col1,&quit);
    			    printf("\n");
    				if (quit == quit)
    				  board[row][col] = false;
    				else
    				  board[row][col] = true;
    			}
    			printf("\n");
    	}
    Can anyone tell me why it isn't working the way I want? Thanks.

    EDIT: The output it's giving me is weird. I'll provide a quick sample:

    Please enter the cells you want to be alive using row,col format
    and enter 'quit' to quit: 4,4

    Please enter the cells you want to be alive using row,col format
    and enter 'quit' to quit: 3,3

    Please enter the cells you want to be alive using row,col format
    and enter 'quit' to quit: quit

    Please enter the cells you want to be alive using row,col format
    and enter 'quit' to quit:This part is repeated 45 times before it shows the blank grids. Does anyone know what's wrong with my code?

    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........


    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........

    Press any key to continue . . .
    Last edited by DCMann2; 05-06-2008 at 09:41 PM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Right now, you have the user's input of row and col, inside two for loops.

    That's not what you want, and that's why you're getting all the extra crap.

    You might want the user's input inside just one while loop:

    Code:
    num = 0; row= 3; col = 3;
    while(row < 10)   {
       gotoxy(10, 10);
       printf("Enter the row of the cell you want to change, or 10 to stop entering");
       scanf("%d",  &row);
       gar = getchar();  //remove the newline char to garbage
       if(row > 9)
          continue;
       printf(Enter a column of the cell you want to change")   
       scanf("%d", &col);
       gar = getchar();
       printf("Enter a number value (0 or 1) for this cell");
       scanf("%d", &num);
       gar = getchar();
    
    
       if(row > 0 && row < 10 && col > 0 && col < 10)
          //assignment
          board[row-1][col -1] = num;  //your assignment wants you to change from 1 base to zero base array elements for this.
    
    }
    For gotoxy() to work, you must include conio.h or other header which includes it. On a Windows system, you can use the API Gotoxy() by including it's header file.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    Quote Originally Posted by Adak View Post
    Right now, you have the user's input of row and col, inside two for loops.

    That's not what you want, and that's why you're getting all the extra crap.

    You might want the user's input inside just one while loop:

    Code:
    num = 0; row= 3; col = 3;
    while(row < 10)   {
       gotoxy(10, 10);
       printf("Enter the row of the cell you want to change, or 10 to stop entering");
       scanf("%d",  &row);
       gar = getchar();  //remove the newline char to garbage
       if(row > 9)
          continue;
       printf(Enter a column of the cell you want to change")   
       scanf("%d", &col);
       gar = getchar();
       printf("Enter a number value (0 or 1) for this cell");
       scanf("%d", &num);
       gar = getchar();
    
    
       if(row > 0 && row < 10 && col > 0 && col < 10)
          //assignment
          board[row-1][col -1] = num;  //your assignment wants you to change from 1 base to zero base array elements for this.
    
    }
    For gotoxy() to work, you must include conio.h or other header which includes it. On a Windows system, you can use the API Gotoxy() by including it's header file.
    I ended up getting it to work on my own, but I'll definitely save this code here for future reference. Thanks a lot for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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