Thread: requesting help for writing the game of life program's client

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    requesting help for writing the game of life program's client

    Requirements:

    The grid of cells is represented by a two-dimensional array. The starting grid is generation 0 and is read from a supplied input file. Most positions in the grid have 8 neighbors like the center square in a tic-tac-toe game. The four corner positions have only 3 neighbors each. The remaining positions around the edge of the grid have 5 neighbors each. The rules for the state of each position in the next generation of the grid are as follows:

    If the cell is currently empty:
    If the cell has exactly three living neighbors, it will come to life in the next generation.
    If the cell has any other number of living neighbors, it will remain empty.
    If the cell is currently living:
    If the cell has one or zero living neighbors, it will die of loneliness in the next generation.
    If the cell has four or more living neighbors, it will die of overcrowding in the next generation.
    If the cell has two or three neighbors, it will remain living.
    All births and deaths occur simultaneously. This point is critical to the correct result.


    Create a class named boolMatrix for storing and processing a two-dimensional array of bool values. The class should be saved in a specification file and an implementation file. Your client program must use this class rather than declaring its own arrays. You must not use any arrays in your client program.

    The size of each dimension of the matrix must be specified near the top of the specification file by two global named constants. You must use a 20 by 20 array to start. These named constants will also be used to limit the loops that process the array in both the member functions and the client code. When processing the array, do not access any memory outside of the array bounds.

    Keep in mind as you write the class that it is to be completely unrelated to the game of Life. The class should not mention anything about life, cells, neighbors, etc. The class will have exactly one private data member that is an array of bool. The class will have seven member functions:

    default constructor: initialize all of the array elements to false
    get: return the current contents of a single array element. Use arguments to indicate the row and column of the array element that should be returned.
    set: set the contents of a single array element. Use arguments to indicate the row and column of the array element that should be set, and the value to which it should be set.
    rowcount: return the number of true values that exist in any given row
    colcount: return the number of true values that exist in any given column
    totalcount: return the number of true values that exist in the entire array
    print: display the contents of the array, including the row and column indices (see the posted correct output). For each element of the array, a true value must be displayed as an asterisk ("*") and a false value must be displayed as a space. This member function is the only one that displays output.

    Use const to indicate const functions and unchangeable reference parameters whenever possible.

    In the client code, a minimum of three functions is required in addition to the main function. Remember our discussions about functional cohesion, value-returning vs. void functions, and value parameters vs. reference parameters.


    Correct output after 5 generations:
    01234567890123456789
    0
    1 * *
    2 * * *** *
    3 * **** *
    4 * * * **
    5 * * ***** *
    6 ** * * *** *
    7 * *** * *
    8 ****
    9 * ******
    10* * *
    11 * * ** *
    12** **** ***
    13 * *** ** *
    14 * * * ***
    15 ** *** ** **
    16 * * **
    17
    18
    19
    Total alive in row 10 = 3
    Total alive in col 10 = 1
    Total alive = 95






    I've written up the code for the boolmatrix class and now I'm on to the client.

    Code:
    #include <iostream>
    #include <fstream>
    #include "boolmatrix.h"
    
    using namespace std;
    
    void createfirstgen(boolMatrix &generation);
    
    
    
    //BOOLMATRIX_H 
        /////////Class Member Functions////////////
    #include <iostream>
    //#include "boolmatrix.h" // class's header file
    #include <iomanip>
     
     
    using namespace std;
     
     
    // class constructor
    boolMatrix::boolMatrix()
    {
      for (int row = 0; row < rows; row++)
        {
          for (int col = 0; col < cols; col++)
            {container[row][col] = false;}
        }
     
    }
     
     
    bool boolMatrix::get(int row, int col)
    {
    return container[row][col];
    }
     
     
    void boolMatrix::set(int row, int col, bool answer)
    {
    container[row][col] = answer;
    }
     
     
    int boolMatrix::rowcount(int row)
    {
    int rowtotal = 0;
     
    for (int count = 0; count < rows; count++){
    if ( container[row][count] == true ){
    rowtotal++;
    }
    }
    return rowtotal;
    }
     
     
    int boolMatrix::colcount(int col)
    {
    int coltotal = 0;
     
    for (int count = 0; count < cols; count++){
    if ( container[count][col] == true ){
    coltotal++;
    }
    }
    return coltotal;
    }
     
     
    int boolMatrix::totalcount()
    {
    int total = 0;
    for (int row = 0; row < rows; row++){
    for (int col = 0; col < cols; col++){
    if ( container[row][col] == true ){
    total++;
    }
    }
    }
    return total;
     
    }
     
     
    void boolMatrix::print()
    {
    int toptable[9];
    int sidetable[rows];
    int test = 0;
     
    cout << " ";
    for ( int count = 0; count < 10; count++){
    toptable[count] = count;
    }
     
    for ( int row = 0; row < rows; row++){
    sidetable[row] = row;
    }
     
    for (int col = 0; col < cols; col++){
    cout << toptable[test];
    test++;
    if ( test == 10 )
    test = 0;
    }
    cout << endl;
     
    for (int row = 0; row < rows; row++){
    cout << setw(2) << sidetable[row];
    for (int col = 0; col < cols; col++){
    if ( container[row][col] == true ){
    cout << "*";
    } else if ( container[row][col] == false ){
    cout << " ";
    }
    }
    cout << endl;
    }
    }
    
    
    
    int main()
    {
        boolMatrix generation;
        boolMatrix newgeneration;
        createfirstgen(generation);
    
    
    system("pause");
    }
    
    
    
    
    void createfirstgen(boolMatrix &generation)
    {
         ifstream infile("lifeinput.dat");
         int row, col;
        
         infile >> row >> col;
         while (infile) {
               generation.set(row, col, true);
               infile >> row >> col;
         }
         infile.close();
    }


    the specification file for the class:
    Code:
    //**********************************************************************
    // SPECIFICATION FILE (boolMatrix)
    // This file givest the specification
    // of the boolMatrix abstract data type
    //**********************************************************************
    
    const int rows = 20;
    const int cols = 20;
    
     
    class boolMatrix
    {
    	public: // Available for clients use.
    
    	        boolMatrix();
                bool get(int row, int col);
                void set(int row, int col, bool answer);
                int rowcount(int row);
                int colcount(int col);
                int totalcount();
                void print();
    		
    
    	private: // Can only be used within the class, Client cannot call it.
                bool container[rows][cols];
    };

    As you can see, the client is still incomplete. I'm currently having writer's block as to what to do next and how I should incorporate the class's functions into the client to print out the next generations. Should I make a function that can generate the next generation based on the the first's cells' positioning and loop it until generation five is reached? Then follow it with a function printing out the number of remaining cells? Argh, it kind of seems I answered myself but I can't get my head wrapped around how I'll write it out.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I guess you begin with
    Code:
    class gameoflife {
      public:
        // some methods here
      private:
        boolMatrix oldGen;
        boolMatrix newGen;
    };
    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.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    2
    Must I create another class to generate everything? Can't I just use void functions to put into my main with what I have now?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Or you could do that.

    Begin by trying to write something like
    bool itsAlive( map, x, y );
    which implements the rules you listed, and returns true if x,y should be alive in the next generation (or false if it's to die)
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game of life program
    By paranoidgnu in forum C Programming
    Replies: 31
    Last Post: 05-05-2011, 05:10 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. C++ Game of Life Program
    By rayrayj52 in forum C++ Programming
    Replies: 16
    Last Post: 09-26-2004, 03:58 PM
  4. Requesting Java Chat Client Maintenence
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-02-2003, 01:57 PM
  5. Game of Life Program Help
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 01-23-2002, 10:32 AM