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; I need help finishing this assignment : Conway's Game of Life For this assignment your are to write a program, ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    26

    Exclamation c++ Assignment: Game of Life

    I need help finishing this assignment:
    Conway's Game of Life

    For this assignment your are to write a program, that plays Conway's game of Life. See the Wikipedia definition, if
    you have never played the game: Conway's Game of Life - Wikipedia, the free encyclopedia.

    Here is how our implementation will work:
    (1) The program will ask the user to enter a filename from the console. (Just like the last assignment).
    (2) The initial configuration will be read in from a file, which will be a 12 by 30 two-dimensional array of characters. The game board will be surrounded by all O's.
    (3) The game will be played on 10 by 28 two-dimensional array. (Can you guess why?). A period ('.') will represent a dead cell and an 'X' will represent a live cell.

    You will be severely penalized if your program does not have at least three functions.

    View solution (interface must match)(the program will be uploaded to hypergrade)

    Here is my current code:
    Code:
    //Game of Life
    //Dylan
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <cstring>
    using namespace std;
    //functions
    void GetFile(); //Get filename
    char MakeArray(); //Make 2d array 
    char ChgArray(); //change the array
    char GameBoard(); //Game Board
    
    //Global Variables
    const int ROW1 =12;
    const int COL1 =30;
    ifstream myfile; 
    string filename;
    char live = 'x'; //life cells
    char dead = '.'; //dead cells
    char name [ROW1][COL1]; //12by30 2d array
    char name2 [12][30]; //another 12by30 2d array
    //end of Global variables
    //Main Function
    int main()
    {
    int q; //stops terminal window from quitting after programs ends 
    //call the functions
    GetFile();
    MakeArray();
    ChgArray();
    //GameBoard();
    //Stop the Program from quitting 
    cin >> q; //enter any letter to end the program 
     
    return 0;
    }
    //GetFile Function
    void GetFile()
    {
    cout<<"Enter the filename: \n";
    cin>>filename;
    return;
    }
    //MakeArray Function
    char MakeArray()
    {
    myfile.open (filename.c_str());
    for (int r=0; r<12; r++)
    {
     for (int c=0; c<30; c++)
         {
           myfile>>name[r][c];
            //cout << name[r][c];
          }
    //cout << endl;
    }
    }
    //ChgArray Function
    char ChgArray()
    {
    //char name2 [12][30];
    for (int r=0; r<12; r++)
    {
     for (int c=0; c<30; c++)
     {
        name2[r][c]=name[r][c];
    //cout<<name2[r][c];
         }
         //cout<<endl; 
    }
    }
    /*/char GameBaord()
    {
    //do something
    }
    /*/
    Here is one of the files that the user can open:
    Game Of Life.cpp|life01.txt|life05.txt

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to ask a more specific question.

    You shouldn't be using so many globals.

    Your indentation/spacing could be better. Don't mix tabs and spaces.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I almost added to my post that when someone just posts the assignment description and some minor piece of code they've probably cross-posted, since they're basically asking for someone to do the assignment for them.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    26
    I am not asking anyone to do it for me. but help me get started on the last part. I do not know how to start and because of budget cuts to my college their is limited resources from the tutors at the computer lab (i.e there is only one programming expert there). Also my c++ professor doesn't really teach you that much and we only class once a week.

    I know we need to do something with IF statements, but don't know how to set it up and get going. I posted the same post on many different forums because I could not take the chance of the forum not being reply to before the deadline which is midnight 3/1.

    Thank-you in advance for any help or advice given to me.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your code is such a mess that it's hard to see where to start.

    • Rename ROW1 and COL1 to ROWS and COLS (since the 1 makes no sense) and use them consistently (replace 12 in your code with ROWS and 30 with COLS).
    • Make live and dead const and capitalize them.
    • Make myfile local to MakeArray and close the file at the end of that function.
    • Make filename local to main and put the code of GetFile inside main.
    • Rename name to board and make it local to main. Pass it into the functions that use it. The prototype for such a function will be something like void MakeArray(string filename, char board[][COLS])
    • Make your program read in the file and print out the board (I'm assuming GameBoard is supposed to print out the board). Post your code when you've gotten that far.

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    26

    Exclamation

    Quote Originally Posted by oogabooga View Post
    Your code is such a mess that it's hard to see where to start.
    • Rename ROW1 and COL1 to ROWS and COLS (since the 1 makes no sense) and use them consistently (replace 12 in your code with ROWS and 30 with COLS).
    • Make live and dead const and capitalize them.
    • Make myfile local to MakeArray and close the file at the end of that function.
    • Make filename local to main and put the code of GetFile inside main.
    • Rename name to board and make it local to main. Pass it into the functions that use it. The prototype for such a function will be something like void MakeArray(string filename, char board[][COLS])
    • Make your program read in the file and print out the board (I'm assuming GameBoard is supposed to print out the board). Post your code when you've gotten that far.
    Okay here is my new code. Is it better? I am also now getting[link error]

    code:
    Code:
    //Game of Life//Dylan
    
    
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <cstring>
    
    
    using namespace std;
    
    
    //functions
    char MakeArray(); //Make 2d array 
    char GameBoard(); //Game Board
    
    
    //Global Variables
    const int ROWS=12;
    const int COLS=30; 
    const char LIVE = 'x'; //life cells
    const char DEAD = '.'; //dead cells
    //end of Global variables
    
    
    //Main Function
    int main()
    {
    string filename; //Name of the file
    char board [ROWS][COLS]; //12by30 2d array
    int q; //stops terminal window from quitting after programs ends
    
    
    
    
    cout<<"Enter the filename: \n";
    cin>>filename;
    
    
    
    
    //call the functions
    
    
    MakeArray();
    
    
    
    
    //Stop the Program from quitting	
    cin >> q; 
    	
    return 0;
    }
    
    
    
    
    
    
    //MakeArray Function
    char MakeArray(string filename, char board[][COLS])
    {
    ifstream myfile; 
    myfile.open (filename.c_str());
    for (int r=0; r<12; r++)
    {
    	for (int c=0; c<30; c++)
         {
    		myfile>>board[r][c]; 
            cout<<board[r][c];
    	  }
    cout<<endl;
    }
    myfile.close();
    }
    Error:
    Code:
       [Linker error] C:\Users\SHYWOL~1\AppData\Local\Temp\cckIvcKe.o:Game Of Life 2.cpp:(.text+0x4d): undefined reference to `MakeArray()'    collect2: ld returned 1 exit status
    Did I do something wrong?

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    That's pretty close. You just have to be more consistent, like calling MakeArray with the right parameters and changing its prototype to match. I've done that for you below. Add the GameBoard (or maybe call it DisplayBoard) function to display the board; I've modified its prototype to what it should be. Notice that I've also fixed your spacing. Remember not to mix spaces and tabs (I've used all tabs since that's mostly what you had).

    Closely compare the code below to what you've posted to see all the changes.
    Code:
    //Game of Life//Dylan
    
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    //Global Variables
    const int ROWS = 12;
    const int COLS = 30; 
    const char LIVE = 'x'; //life cells
    const char DEAD = '.'; //dead cells
    
    //functions
    void MakeArray(string filename, char board[][COLS]);
    void GameBoard(char board[][COLS]);
    
    
    int main()
    {
    	string filename; //Name of the file
    	cout<<"Enter the filename: \n";
    	cin>>filename;
    
    	char board [ROWS][COLS];
    	MakeArray(filename, 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();
    }

  9. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    26
    Quote Originally Posted by oogabooga View Post
    That's pretty close. You just have to be more consistent, like calling MakeArray with the right parameters and changing its prototype to match. I've done that for you below. Add the GameBoard (or maybe call it DisplayBoard) function to display the board; I've modified its prototype to what it should be. Notice that I've also fixed your spacing. Remember not to mix spaces and tabs (I've used all tabs since that's mostly what you had).

    Closely compare the code below to what you've posted to see all the changes.
    I will look over the changes you made in more detail tomorrow when I am in front of my windows desktop that has dev-c++ on it.

    Thank-you!

  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    26
    Quote Originally Posted by Dylan Metz View Post
    I will look over the changes you made in more detail tomorrow when I am in front of my windows desktop that has dev-c++ on it.

    Thank-you!
    okay I looked it over and compared what you did. Now I can I print the board to see if it is printing right?

    Here is what i tried with your code:
    Code:
    //Game of Life
    //Dylan
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    //Global Variables
    const int ROWS = 12;
    const int COLS = 30; 
    const char LIVE = 'x'; //life cells
    const char DEAD = '.'; //dead cells
    //functions
    void MakeArray(string filename, char board[][COLS]);
    void GameBoard(char board[][COLS]);
    
    int main()
    {
     string filename; //Name of the file
     cout<<"Enter the filename: \n";
     cin>>filename;
     char board [ROWS][COLS];
     MakeArray(filename, 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];
       cout << board[r][c];
      }
     }
     cout<<endl;
     myfile.close();
    }

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I meant for you to write a SEPARATE function to print the board.
    So make your main like this and write the GameBoard function:
    Code:
    int main()
    {
     string filename; //Name of the file
     cout<<"Enter the filename: \n";
     cin>>filename;
     char board [ROWS][COLS];
     MakeArray(filename, board);
     GameBoard(board);
     //stop terminal window from quitting after programs ends
     char q;
     cin >> q; 
     return 0;
    }
    And take the couts out of MakeArray.

  12. #12
    Registered User
    Join Date
    Feb 2012
    Posts
    26
    Code:
    //Game of Life
    //Dylan
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    //Global Variables
    const int ROWS = 12;
    const int COLS = 30; 
    const char LIVE = 'x'; //life cells
    const char DEAD = '.'; //dead cells
    //functions
    void MakeArray(string filename, char board[][COLS]);
    void GameBoard(char board[][COLS]);
    
    int main()
    {
     char board [ROWS][COLS];
     string filename; //Name of the file
     cout<<"Enter the filename: \n";
     cin>>filename;
     MakeArray(filename, 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; 
     }
    }
    Is this what you mean?

  13. #13
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    That looks right (I assume it works).

    Now you need to write the function that actually calculates the next state of the board. Let's call it NextState and give it the prototype:
    Code:
    void NextState(board[][COLS]);
    Try writing it. It will need another board, say board2, locally defined in NextState to be the same type as board in main.You'll need to write a double loop to go through board and count how many LIVE neighbors each cell has. If the cell itself is LIVE, then it will only remain LIVE if it has 2 or 3 neighbors, otherwise it will be DEAD. If the cell is DEAD then it will remain DEAD if it has anything but 3 neighbors; if it has exactly 3 neighbors it will become LIVE.

    You'll set board2 to these new values. After the end of the double loop going through board, you'll have another double loop to simply copy board2 back into board to give it its new state.

    Give that a try. Your main for now should be something like:
    Code:
    int main()
    {
     char board [ROWS][COLS];
     string filename; //Name of the file
     cout<<"Enter the filename: \n";
     cin>>filename;
    
     MakeArray(filename, board);
     GameBoard(board);
     NextState(board);
     GameBoard(board);
    
     //stop terminal window from quitting after programs ends
     char q;
     cin >> q; 
     return 0;
    }

  14. #14
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    Point (3) in the assignment is not being addressed, nor is a part of point 2, which gives a clue to: your tutor wants you to to play the game in a space smaller than the total allocation, and asks the question why?

    There is a reason for asking the 'World' to fit in a smaller space than total memory allocation. So you will lose marks if you apply the rules to the whole space, as you have not shown understanding of why extra area is required, in this implementation.

    When you start applying the rules the reason should become apparent to you.
    Last edited by rogster001; 02-28-2012 at 02:37 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'"

  15. #15
    Registered User
    Join Date
    Feb 2012
    Posts
    26
    Quote Originally Posted by oogabooga View Post
    That looks right (I assume it works).

    Now you need to write the function that actually calculates the next state of the board. Let's call it NextState and give it the prototype:
    Code:
    void NextState(board[][COLS]);
    Try writing it. It will need another board, say board2, locally defined in NextState to be the same type as board in main.You'll need to write a double loop to go through board and count how many LIVE neighbors each cell has. If the cell itself is LIVE, then it will only remain LIVE if it has 2 or 3 neighbors, otherwise it will be DEAD. If the cell is DEAD then it will remain DEAD if it has anything but 3 neighbors; if it has exactly 3 neighbors it will become LIVE.

    You'll set board2 to these new values. After the end of the double loop going through board, you'll have another double loop to simply copy board2 back into board to give it its new state.

    Give that a try. Your main for now should be something like:
    Code:
    int main()
    {
     char board [ROWS][COLS];
     string filename; //Name of the file
     cout<<"Enter the filename: \n";
     cin>>filename;
    
     MakeArray(filename, board);
     GameBoard(board);
     NextState(board);
     GameBoard(board);
    
     //stop terminal window from quitting after programs ends
     char q;
     cin >> q; 
     return 0;
    }
    I know what I need to do, but this is where I'm stuck. How do you check the neighbors of the LIVE cells. I know you need to use a lot of IF statements, but I really do not know how to get started on this.

Page 1 of 2 12 LastLast
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, 11:23 AM
  2. Game of Life... Turned my life to HELL!
    By yigster in forum C Programming
    Replies: 1
    Last Post: 05-21-2009, 07:29 PM
  3. Game Of Life 3D
    By blackslither in forum C Programming
    Replies: 8
    Last Post: 11-02-2008, 03:30 PM
  4. Game of Life
    By CornedBee in forum Contests Board
    Replies: 74
    Last Post: 05-20-2008, 02:50 AM
  5. game of life
    By marleyman7 in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2002, 07: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