"LIFE" Program

This is a discussion on "LIFE" Program within the C++ Programming forums, part of the General Programming Boards category; Hey I'm working on a game of life program. This is the code I've got so far... I'm pretty close ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    "LIFE" Program

    Hey I'm working on a game of life program. This is the code I've got so far... I'm pretty close to finishing up but it's not displaying right... I don't see where my logic is wrong.

    Code:
    # include <iostream>
    
    using namespace std;
    
    const int N_ROW = 20;
    const int N_COL = 20;
    
    typedef char MapType[N_ROW][N_COL];
    
    void Readint(int&, int&, MapType);
    void Display(MapType);
    void NextGeneration(MapType);
    int CheckNeighbor(int, int, MapType);
    void CopyMatrix(MapType, MapType);
    void ClearMatrix(MapType);
    
    int CheckNeighbor(int Row, int Col, MapType org)
    {
    	int numN=0;
    	for(int i = Row-1; i <= Row+1; i++)
    	for(int j = Col-1; j <= Col+1; j++)
    
    {
    	if (org[i][j]==1)
    		numN++;
    }
    
    	return numN;
    
    }//END OF CHECK NEIGHBOR FUNCTION 
    
    void NextGeneration(MapType orggen)
    {
    	int Row, Col, numN;
    
    	MapType New;
    
    	for (Row = 1; Row < N_ROW-1; Row++)
    	for (Col = 1; Col < N_COL-1; Col++)
    		numN = CheckNeighbor(Row,Col,orggen);
    
    	if (numN == 3)
    		New[Row][Col] = 1;
    	else if((numN <2) || (numN >3))
    		New[Row][Col] = 0;
    	else
    		New[Row][Col] = orggen[Row][Col];
    }//END OF NEXT GENERATION
    
    void Display(MapType org)
    {
    
    	for(int i = 0; i < N_ROW; i++)
    	for(int j = 0; j < N_COL; j++)
    	{
    		if(org[i][j]==1)
    		cout << '@';
    		else
    		cout<<' ';
    	}
    	cout << "\n";
    }//END OF DISPLAY
    
    void CopyMatrix(MapType org, MapType next)
    {
    	for (int i=0;i<N_ROW;i++)
    	for (int j=0; j<N_COL;j++)
    		org[i][j]=next[i][j];
    }//END OF COPY MATRIX
    
    void ClearMatrix(MapType org)
    {
    	for (int i = 0; i < N_ROW; i++)
    	for (int j = 0; j < N_COL; j++)
    		org[i][j] = 0;
    
    }//END OF CLEAR MATRIX
    
    void Readint(int& noc, int& nog, MapType org)
    {
    	int rowno, colno;
    
    	cout<<"Please enter the number of initial cells: ";
    	cin>>noc;
    
    	for (int i = 0; i < noc; i++)
    	{
    		cout<<"The position of cell is (row, col): ";
    		cin>>rowno>>colno;
    		if((rowno < 1) || (rowno > 19))
    		{
    			cout<< "Invalid position, enter new position. ";
    			org[rowno][colno] = 0;
    			cin >> rowno>>colno;
    		}
    		if((colno < 1) || (colno > 19))
    		{
    			cout<< "Invalid position, enter new position. ";
    			org[rowno][colno] = 0;
    			cin >> rowno>>colno;
    		}
    		org[rowno][colno] = 1;
    	}
    
    	cout<<"How many generations do you want to generate? ";
    	cin >> nog;
    }//END OF READ
    
    int main()
    {
    
    	int no_cells;
    	int no_gens;
    	int i=0;
    
    	MapType currmap, nextmap;
    	char yesno='n';
    
    	do 
    	{
    		ClearMatrix(currmap);
    		Readint(no_cells,no_gens, currmap);
    		Display(currmap);
    		cin.get( );
    
    		for(i = 2; i <= no_gens; i++)
    			{
    				ClearMatrix(nextmap);
    				NextGeneration(currmap);
    				CopyMatrix(currmap, nextmap);
    				Display(nextmap);
    				cin.get( );
    			}
    		cout<<"Do you want to do it again? (Y/N)";
    		cin>> yesno;
    	}
    	while(yesno=='Y' || yesno =='y');
    		cout<<"Good Bye"<<endl;
    	return 0;
    }//END OF MAIN
    Actual Assignment.

    The game of Life is intended to model life in a society of organisms. consider a rectangular array of cells, each of which may contain an organism. If the array is considered to extend indefinitely in both directions, then each cell has eight neighbors, the eight cells surrounding it. Births and deaths occur according to the following rules:
    1. An organism is born in any empty cell having exactly three neighbors.
    2. An organism dies from isolation if it has fewer than two neighbors.
    3. An organism dies from overcrowding if it has more than three neighbors.
    4. All other organisms survive to the next generation.

    Write a program to play the game of Life and investigate the patterns produced by various initial configurations. Some configurations die off rather rapidly; others repeat after a certain number of generations; others change shape and size and may move across the array.

    NOTE:
    1. For representing each organism, use "@" characters.
    2. For an array of cells, use 30x30 two-dimensional array.
    3. Each generation should be displayed on the screen and should be paused.
    (You can use cin.get( ) library function to pause running your program.)
    4. For all programs for this class, you will be expected to hand in a diskette (3.5") with the appropriate files (a source file and an executable file).
    5. Use a folder to hand in your assignment.
    6. The source file should be called life.cpp
    7. All assignments are expected to be INDIVIDUAL work. All work handed in must be original. Duplicate or very similar programs receive zero points.


    Input

    A set of initial cells will be given using the interactive way using a keyboard.

    The followings are the prompt for the input:

    Please Enter the number of initial cells: 4
    The position of cell is 10 10
    The position of cell is 10 11
    The position of cell is 10 12
    The position of cell is 11 11

    How many generations do you want to display? 3

    Output
    The program then display each generation of organisms at a time.

    The 1-generation
    @
    @@@

    Press any key to continue!!

    (This picture should be displayed on the new screen.)

    The 2-generation
    @
    @@@
    @@@

    Press any key to continue!!

    The 3-generation
    @@@

    @ @
    @

    Do you want to do it again?(Yes/No) N

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And what does "not working" mean this time?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Just looking it over, I'm suspicious of how you've handled the limits here:

    Code:
    Seems like the Row and Col should either be Row = 1; Row <= N_ROW; or 
    Row = 0; Row < N_ROW, or i and j in display() should be changed. There you start with 
    i = 0; i < N_Row;, and same with j. 
    
    
    	for (Row = 1; Row < N_ROW-1; Row++)  
    	for (Col = 1; Col < N_COL-1; Col++)
    		numN = CheckNeighbor(Row,Col,orggen);
    
    	if (numN == 3)
    		New[Row][Col] = 1;
    	else if((numN <2) || (numN >3))
    		New[Row][Col] = 0;
    	else
    		New[Row][Col] = orggen[Row][Col];
    }//END OF NEXT GENERATION
    
    void Display(MapType org)
    {
    
    	
            for(int i = 0; i < N_ROW; i++)
            for(int j = 0; j < N_COL; j++)
    	{
    Being consistent is a big plus. Indenting your loops within loops would help everyone spot an error, easier.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    In your subroutine, don't you want to check each cell in your "if statement"? What you have now will only check the last value of numN at the end of the the two "for" loops.

    Code:
    void NextGeneration(MapType orggen)
    {
    	int Row, Col, numN;
    
    	MapType New;
    
    	for (Row = 1; Row < N_ROW-1; Row++)
                    {
    	       for (Col = 1; Col < N_COL-1; Col++)
                           {
    		numN = CheckNeighbor(Row,Col,orggen);
    
    	                if (numN == 3)
    		     New[Row][Col] = 1;
    	                else if((numN <2) || (numN >3))
    		     New[Row][Col] = 0;
    	                else
    		     New[Row][Col] = orggen[Row][Col];
                           }
                    }
    }//END OF NEXT GENERATION

  5. #5
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Sorry, the braces were lined up before I posted that last message.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by kcpilot View Post
    Sorry, the braces were lined up before I posted that last message.
    It is because you are mixing spaces with tabs - do not do it
    Use or only tabs or only spaces for indentation
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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