Like Tree1Likes

c++ Assignment: Game of Life

This is a discussion on c++ Assignment: Game of Life within the C++ Programming forums, part of the General Programming Boards category; thats what i am talking about, there will be a problem when you check the neighbours as it stands. You ...

  1. #16
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    thats what i am talking about, there will be a problem when you check the neighbours as it stands.

    You can step through the board in a nested loop of two yes. But then you need to check each cell to find out if it lives or dies on the next generation. It should be clear to you that each cell is always thought of as being surrounded by eight neighbours that 'touch it' so a further loop to examine the immediate neighbours is required. There are other fancy ways to optimally examine the board but in your case stick with the method discussed thus far

    Now given that some cells are at the edges of the board, what do you think the implications of this are?

    123
    4C5
    678

    C = current cell,

    For example imagine it is the top left cell of your board as you presently have it.
    Last edited by rogster001; 02-28-2012 at 02:43 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  2. #17
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    As rogster has pointed out, you don't want to access outside the bounds of the 2D array. But in your case that's easy since you can simply restrict the bounds of your loops. So the double loop will be like this.
    Code:
     for (int r=1; r<ROWS-1; r++)
     {
      for (int c=1; c<COLS-1; c++)
      {
        int cnt = 0;
        // if statements here that increment cnt
      }
     }
    Now think about how to check the square up one and left one. It will have the coordinates r-1 (up one row) and c-1 (left one column), so you can check it like this:
    Code:
    if (board[r-1][c-1] == LIVE)  cnt++;
    So try to make the other seven if statements. Look at rogster's post above to see the relative locations of the cells.

  3. #18
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    I would recommend calling function that returns a live count for the given current cell at x,y
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #19
    Registered User
    Join Date
    Feb 2012
    Posts
    26
    Okay this is what I have so-far:
    Code:
    
    
    #include <iostream>
    #include <string>
    #include <fstream>
    
    
    using namespace std;
    
    
    //Global Variables
    const int ROWS = 12;
    const int COLS = 30;
    const int BOARD_ROWS(10);
    const int BOARD_COLS(28); 
    const char LIVE = 'X'; //life cells
    const char DEAD = '.'; //dead cells
    //functions
    void MakeArray(string filename, char board[][COLS]);
    void GameBoard(char board[][COLS]);
    void NextState(char board[][COLS]); 
    
    
    int main()
    {
    	char board [ROWS][COLS];
    	string filename; //Name of the file
    	cout<<"Enter the filename: \n";
    	cin>>filename;
    
    
    	//call functions
    	MakeArray(filename, board);
    	NextState(board);
    	GameBoard(board);
    
    
    	//stop terminal window from quitting after programs ends
    	char q;
    	cin >> q; 
    
    
    	return 0;
    }
    
    
    void MakeArray(string filename, char board[][COLS])
    {
    	ifstream myfile; 
    	myfile.open (filename.c_str());
    	for (int r=0; r<ROWS; r++)
    	{
    		for (int c=0; c<COLS; c++)
    		{
    			myfile>>board[r][c];
    		}
    	}
    	myfile.close();  
    }
    void GameBoard (char board[][COLS])
    {
    	for (int r=0; r<ROWS; r++)
    	{
    		for (int c=0; c<COLS; c++)
    		{
    		
    			cout<<board[r][c];
    		}
    		cout<<endl;	
    	}
    }
    void NextState (char board[][COLS])
    {
    	int LiveCnt=0;
    	for (int r=0; r<ROWS; r++)
    	{
    		for (int c=0; c<COLS; c++)
    		{
    		
    			if (board[r-1][c-1]=='X')
    			{
    			LiveCnt++;	
    			}
    			if (board[r-1][c]=='X')
    			{
    			LiveCnt++;	
    			}
    			if (board[r-1][c+1]=='X')
    			{
    			LiveCnt++;	
    			}
    			if (board[r][c-1]=='X')
    			{
    			LiveCnt++;	
    			}
    			if (board[r][c]=='X')
    			{
    			LiveCnt++;	
    			}
    			if (board[r][c+1]=='X')
    			{
    			LiveCnt++;	
    			}
    			if (board[r+1][c-1]=='X')
    			{
    			LiveCnt++;	
    			}
    			if (board[r+1][c+1]=='X')
    			{
    			LiveCnt++;	
    			}
    		}
    		cout<<endl;	
    	}
    
    
    }
    Now I need to have another if statement for the rules? How do I do that?

  5. #20
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Code:
    void NextState (char board[][COLS])
    {
    	// You need a second board here. Call it board2.
    	int LiveCnt=0; // This needs to go at the top of the inner loop
    	               // since it needs to be reset to 0 every time.
    	for (int r=0; r<ROWS; r++) // Should be from 1 to ROWS-1
    	{
    		for (int c=0; c<COLS; c++) // Should be from 1 to COLS-1
    		{
    		
    			if (board[r-1][c-1]=='X')
    			{
    			LiveCnt++;	
    			}
    			if (board[r-1][c]=='X')
    			{
    			LiveCnt++;	
    			}
    			if (board[r-1][c+1]=='X')
    			{
    			LiveCnt++;	
    			}
    			if (board[r][c-1]=='X')
    			{
    			LiveCnt++;	
    			}
    
    			// You don't want to count the current cell,
    			// so get rid of this one.
    			if (board[r][c]=='X')
    			{
    			LiveCnt++;	
    			}
    
    			if (board[r][c+1]=='X')
    			{
    			LiveCnt++;	
    			}
    			if (board[r+1][c-1]=='X')
    			{
    			LiveCnt++;	
    			}
    
    			// You're missing the one right below the current cell here
    
    			if (board[r+1][c+1]=='X')
    			{
    			LiveCnt++;	
    			}
    		}
    
    		cout<<endl;  // Get rid of this.
    	}
    
    	// Here you put an if statement that says "if the cell is live and does not
    	// have either 2 or 3 neighbors then set it (in board2) to dead.
    	// else if it is dead and has exactly 3 neighbors then set it (in board2) to alive
    	// else set board2[r][c] to board[r][c].
    
    }
    Also, you're supposed to use your constant LIVE instead of 'X'.
    Last edited by oogabooga; 02-28-2012 at 05:20 PM.

  6. #21
    Registered User
    Join Date
    Feb 2012
    Posts
    26
    Quote Originally Posted by oogabooga View Post
    Code:
    void NextState (char board[][COLS])
    {
        // You need a second board here. Call it board2.
        int LiveCnt=0; // This needs to go at the top of the inner loop
                       // since it needs to be reset to 0 every time.
        for (int r=0; r<ROWS; r++) // Should be from 1 to ROWS-1
        {
            for (int c=0; c<COLS; c++) // Should be from 1 to COLS-1
            {
            
                if (board[r-1][c-1]=='X')
                {
                LiveCnt++;    
                }
                if (board[r-1][c]=='X')
                {
                LiveCnt++;    
                }
                if (board[r-1][c+1]=='X')
                {
                LiveCnt++;    
                }
                if (board[r][c-1]=='X')
                {
                LiveCnt++;    
                }
    
                // You don't want to count the current cell,
                // so get rid of this one.
                if (board[r][c]=='X')
                {
                LiveCnt++;    
                }
    
                if (board[r][c+1]=='X')
                {
                LiveCnt++;    
                }
                if (board[r+1][c-1]=='X')
                {
                LiveCnt++;    
                }
    
                // You're missing the one right below the current cell here
    
                if (board[r+1][c+1]=='X')
                {
                LiveCnt++;    
                }
            }
    
            cout<<endl;  // Get rid of this.
        }
    
        // Here you put an if statement that says "if the cell is live and does not
        // have either 2 or 3 neighbors then set it (in board2) to dead.
        // else if it is dead and has exactly 3 neighbors then set it (in board2) to alive
        // else set board2[r][c] to board[r][c].
    
    }
    Also, you're supposed to use your constant LIVE instead of 'X'.
    Okay here how's this:
    Code:
    void NextState (char board[][COLS]){
    	char board2[ROWS][COLS];
    	//int LiveCnt=0;
    	for (int r=1; r<ROWS-1; r++)
    	{
    		int LiveCnt=0;
    		for (int c=1; c<COLS-1; c++)
    		{
    		
    			if (board[r-1][c-1]==LIVE)
    			{
    				LiveCnt++;	
    			}
    			if (board[r-1][c]==LIVE)
    			{
    				LiveCnt++;	
    			}
    			if (board[r-1][c+1]==LIVE)
    			{
    				LiveCnt++;	
    			}
    			/*/if (board[r][c-1]==LIVE)
    			{
    				LiveCnt++;	
    			}/*/
    			if (board[r][c]==LIVE)
    			{
    				LiveCnt++;	
    			}
    			if (board[r][c+1]==LIVE)
    			{
    				LiveCnt++;	
    			}
    			if (board[r+1][c-1]==LIVE)
    			{
    				LiveCnt++;	
    			}
    			if (board[r+1][c]==LIVE)
    			{
    				LiveCnt++;
    			}
    			if (board[r+1][c+1]==LIVE)
    			{
    				LiveCnt++;	
    			}
    		}
    
    
    	}
    	if ()
    
    
    }
    Okay how do I do the IF statements that checks to see if the LIVE cell has 2 o3 neighbors? What do I put inside : if (?????)....

    Thanks for the help.

  7. #22
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    In the previous post, I put the blue text in the wrong place. It needs to be inside the inner loop, at the bottom. I've moved it there.

    I've moved the initialization of LiveCnt to the proper place.
    You removed (commented out) the wrong if. I fixed that.

    You need to do the rest yourself!
    Code:
    void NextState (char board[][COLS]){
    	char board2[ROWS][COLS];
    	for (int r=1; r<ROWS-1; r++)
    	{
    		for (int c=1; c<COLS-1; c++)
    		{
    			int LiveCnt=0;
    		
    			if (board[r-1][c-1]==LIVE)
    			{
    				LiveCnt++;	
    			}
    			if (board[r-1][c]==LIVE)
    			{
    				LiveCnt++;	
    			}
    			if (board[r-1][c+1]==LIVE)
    			{
    				LiveCnt++;	
    			}
    			if (board[r][c-1]==LIVE)
    			{
    				LiveCnt++;	
    			}
    			if (board[r][c+1]==LIVE)
    			{
    				LiveCnt++;	
    			}
    			if (board[r+1][c-1]==LIVE)
    			{
    				LiveCnt++;	
    			}
    			if (board[r+1][c]==LIVE)
    			{
    				LiveCnt++;
    			}
    			if (board[r+1][c+1]==LIVE)
    			{
    				LiveCnt++;	
    			}
    
    			// Here you put an if statement that says "if the cell is live and does not
    			// have either 2 or 3 neighbors then set it (in board2) to dead.
    			// else if it is dead and has exactly 3 neighbors then set it (in board2) to alive
    			// else set board2[r][c] to board[r][c].
    		}
    	}
    	// Here you simply copy board2 back to board
    	// (but only from 1 to ROWS-1 and 1 to COLS-1)
    }

  8. #23
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    Okay how do I do the IF statements that checks to see if the LIVE cell has 2 o3 neighbors? What do I put inside : if (?????)....
    This is getting to the stage where you are practically having it written for you,
    For example I wanted you to realise that out of bounds would occur by coding it yourself and getting all the program crashes, best way to learn.

    IMO there are still things that are being ignored that wil distort the evaluation at each generation

    You are doing the minimum each time you get some new advice, it seems you are just inserting the advice code and then saying 'ok how do i do the next bit?' and then adding that advice, etc.
    You honestly mean to say you cannot write a series of if else statements to apply the rules? Even given the almost working version you have now? Itt seems you have become divorced from whatis actually happeing in the program or something and cant see it any more.

    You use your live count in the if statements, obviously, that is why you go to the trouble of getting the live count in the first place, so you write conditions like,

    if live count is such and such, and cell at r,c is such and such then...

    Also where are you at with your compiling and testing? I am not trying to be critical just saying that you need to be incrementally building it at this stage, checking changes are compiling ok and outputting some data at least to show you that things are proceeding as expected.

    The beauty of coding game of life is that there are known outcomes that can be recognised, so when you finally build the rules in and it does not work and stuff is running all over the screen you will tweak it, until finally a eureka moment that makes it worthwhile, you will see an oscillator or similar pattern that says 'done coded the game of life'

    Well in your case this pattern that emerges depends on library input.. ie from your file, the tutor may have set up something nice to be shown when you crack it.

    Finally dont be discouragedby blank 'worlds'; extinction occurs...

    Finally Finally! I looked at the files you were supplied with, one is a bit of a red herring, whcih may confuse you but i can see why it was included
    Last edited by rogster001; 02-29-2012 at 03:22 AM.
    rags_to_riches likes this.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

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

Similar Threads

  1. the game of life
    By ben432 in forum C Programming
    Replies: 1
    Last Post: 06-13-2011, 10:23 AM
  2. Game of Life... Turned my life to HELL!
    By yigster in forum C Programming
    Replies: 1
    Last Post: 05-21-2009, 06:29 PM
  3. Game Of Life 3D
    By blackslither in forum C Programming
    Replies: 8
    Last Post: 11-02-2008, 02:30 PM
  4. Game of Life
    By CornedBee in forum Contests Board
    Replies: 74
    Last Post: 05-20-2008, 01:50 AM
  5. game of life
    By marleyman7 in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2002, 06:11 AM

Tags for this Thread


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