Overwhelmed by assignment based on Conway's Game of Life

885 views Asked by At

I am given files gameOfLife.cpp, life.cpp, and life.h. I am only allowed to edit life.cpp in order to make the program work. I don't know where to with editing life.cpp because there is so much going on that I am unfamiliar with. I am given a file checkoutLife.cpp to check my work.

I've spent the last two days looking at other people's Game of Life files trying to see how to proceed but am at a loss. I don't want someone to do my work for me but I need some direction.

gameOfLife.cpp

#include <iostream>
#include "life.cpp"
#include "life.h"

const int GENERATIONS=100;

using namespace std;

//make a random array of initial living cells
void gen(bool a[ROWS][COLS]){
    for(int i=0;i<ROWS;++i){
        for(int j=0;j<COLS;++j){
            if(rand()%100<10)a[i][j]=true;
            else a[i][j]=false;
        }
    }
    a[5][5]=true;
    a[5][6]=true;
    a[5][7]=true;
    return;
}

// check to see if two arrays are equal
bool equal(const bool a[ROWS][COLS], const bool b[ROWS][COLS]){
    int i,j;
    for(i=0;i<ROWS;++i)for(j=0;j<COLS;++j)if(a[i][j]!=b[i][j])return false;
    return true;
}

//copy the b array into the a array
void copy(bool a[ROWS][COLS], const bool b[ROWS][COLS]){
    for(int i=0;i<ROWS;++i){
        for(int j=0;j<COLS;++j){
            a[i][j]=b[i][j];
        }
    }
    return;
}

//print out the array
void print(const bool a[ROWS][COLS]){
    for(int i=0;i<ROWS;++i){
        for(int j=0;j<COLS;++j){
            if(a[i][j])cout << 'X';
            else       cout << ' ';
        }
        cout << endl;
    }
    return;
}


int main(){
    bool current[ROWS][COLS];
    bool next[ROWS][COLS];
    int i=0;

    //initialze the cell array and print it out
    gen(current);
    print(current);

    while(i<GENERATIONS){
        //get a carriage return before the next generation
        cin.get();

        //give the current generation to life()
        //it puts the next generation into next
        life(current,next);

        //copy the next generation into the current
        copy(current,next);

        //print it out
        print(current);
        i++;
    }

    return 0;
}

life.cpp

/*1. You need to write a file life.cpp that implements the function prototyped in life.h.  You can and should write other functions
     and tuck them into the same file; whatever you need to get your function working in an elegant manner.

  2. Compile your file with checkoutLife.cpp and run the resulting executable to see if it passes all the tests.

  3. Compile yourfile with gameOfLife.cpp and run the resulting executable to see if it makes pretty pictures.

  4. If you are convinced steps 2 and 3 are working, submit your life.cpp via canvas.
*/

#include <iostream>
#include <cstdlib>
#include "life.h"

using namespace std;

void life(const bool current[ROWS][COLS], bool next[ROWS][COLS]){

}

life.h

#ifndef LIFE_H
#define LIFE_H
const int ROWS=25;
const int COLS=25;

// Simulate one generation of Conways Game of Life
//
// Given:
//        a constant 2D bool array called "current" where each true element
//        indicates a live cell and each false element indicates a dead cell.
//
//        an empty  2D bool array called "next"

void life(const bool current[ROWS][COLS], bool next[ROWS][COLS]);
#endif
1

There are 1 answers

0
Malcolm Crum On

life() is called with two parameters: an array of your board's current state (which presumably you will not touch) and an array of the board's next state (which you will populate).

Here is an algorithm you could use:

  • For each row in ROW:
    • For each col in COL:
      • Add up all surrounding neighbours in current[][], store in "neighbours"
      • If current[row][col] is alive and neighbours < 2, next[row][col] = dead
      • Else if current[row][col] is alive and neighbours == 2 or 3, next[row][col] = alive
      • Else if current[row][col] is alive and neighbours > 4, next[row][col] = dead
      • Else if current[row][col] is dead and neighbours == 3, next[row][col] = alive

You can clean up the logic a bit, but I printed it out just like the rules on Wikipedia's Game of Life entry. The complicated part (IMO) is "Add up all surrounding neighbours" - there's a lot of bounds checking here. I suggest calling a different method that you write, for clarity's sake.