I need help finishing this assignment. I am really confused!

//Game of Life
    //Dylan Metz
    
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <cstring>
    
    using namespace std;
    
    //function(s)
    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';
    char dead = '.';
    char name [ROW1][COL1];
    //end of Global variables
    int main()
    {
    int q; //stops terminal window from quitting	
    //call functions
    GetFile();
    MakeArray();
    ChgArray();
    
    //Stop Program from quitting	
    cin >> q;	
    	
    return 0;
    }
    
    //Other Functions
    void GetFile()
    {
    cout<<"Enter the filename: \n";
    cin>>filename;
    return;
    }
    
    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;
    }
    }
    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;	
    }
    
    }

Here is the 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: http://en.wikipedia.org/wiki/Conway's_Game_of_Life.
>
> 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 beplayed 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)
> Blockquote

This is what I've done sofar:
I've asked the user to input the file to open (ie life01.txt)
The file is opened in a 12by30 2D array and then moved to another 2d array.

Here is what I really need help on: I need help with the last part (#3). I really don't understand what to do. All I know is the rest of the code needs to look like this when its finish:http://pastebin.com/94Q5aCiY and that it needs to follow the rules.
I think it may need if statements?

Here is what life01.txt looks like:
http://pastebin.com/ZqgrEbKi

What do I do???

Thanks!

Recommended Answers

All 2 Replies

  1. I would change the MakeArray() function to populate the board as 10 x 28 not a 12 x 30. In the sample output link you gave the 'O' are not on the boarder of the board. I would also rename the name[][] array to board, it just makes more sense when reading the code.
  2. Make a function that given a position, it determines what its new value should be base on the rules of the game. Call it getNextState() .
  3. Test some of the simple input configurations.
  4. Test the boundaries of both row and column.
  5. After you think that is working, do more error checking.
  6. Remove the globals.

I tried to not make too many changes to your code, so take a look. I don't know if you are to print before or after the first iteration, so be aware that you might have to change were the prints are happening.

//Game of Life
//Dylan Metz
    
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>

using namespace std;
    
//function(s)
void GetFile(); //Get filename
bool MakeArray(); //Make 2d array 
char ChgArray(); //change the array
char GameBoard(); //Game Board

//Global Variables
const int ROW1 =12;
const int COL1 =30;
const int BOARD_ROWS(10);
const int BOARD_COLS(28);
ifstream myfile;
string filename;
char live = 'X';
char dead = '.';
// From what the output is supposed to 
// look like the 'O' are not there so
// the row should be 10 and the columns
// should be 28
char board [BOARD_ROWS][BOARD_COLS];

//end of Global variables
int main()
{
    int q; //stops terminal window from quitting	
    //call functions
    GetFile();
    if ( MakeArray() ) {
      // I put this loop in because the output link you gave
      // had a print of 10 boards
      for ( int i(0); i <10; i++)
         ChgArray();
    } 
    else {
      cout << "Error parsing input file" << endl;
    }
    //Stop Program from quitting	
    cin >> q;	
    return 0;
}
    
//Other Functions
void GetFile()
{
    cout<<"Enter the filename: \n";
    cin>>filename;
    return;
}
    
bool MakeArray()
{
    bool ret(false);
    char val;
    int  totCnt(BOARD_ROWS*BOARD_COLS);
    myfile.open (filename.c_str());
    if ( myfile ) {
       for (int r=0; r<ROW1; r++)
       {
    	  for (int c=0; c<COL1; c++)
          {
             myfile>>val;
             if ( val == dead || val == live ) {
                board[r-1][c-1] = val;
                totCnt--;
             }
          }
       }
       if ( !totCnt ) {
         ret = true;
       }
       myfile.close();
    }
    return ret;
}
char getNextState(char b[BOARD_ROWS][BOARD_COLS], int r, int c)
{
   char ret;
   // In this function you want to count the number of 'X' around
   // the point b[r][c] to see if it will live or die
   
   // Code the rules:
   // 1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
   // 2. Any live cell with two or three live neighbours lives on to the next generation.
   // 3. Any live cell with more than three live neighbours dies, as if by overcrowding.
   // 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

   // The thing that get tricky is the boundaries, i.e. when the row is 0 or 9 and column is 0 or 27.
   // Start with the easy one in the middle and then start to test the boundaries.
 
   // The return will be 'X' or '.'
   return ret; 
}
char ChgArray()
{
    char boardTmp[BOARD_ROWS][BOARD_COLS];
    for (int r=0; r<BOARD_ROWS; r++)
    {
    	for (int c=0; c<BOARD_COLS; c++)
    	{
            // Compute what the new value should be and save it
            boardTmp[r][c] = getNextState(board,r,c);
            // -----> Not having all of the input files to test
            //              with I don't know if you are to print
            //              before the first iteration or after!
            cout << boardTmp[r][c];
    	}
    	cout<<endl;	
    }
    // Save off the new board value
    memcpy(board,boardTmp,sizeof(board));
    cout << endl;
}

@histrungalot

Thanks for the code. I will take a more detailed look at it tomorrow when I'm at my windows computer with my compiler (dev-c++).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.