Game of Life Program

Hey everybody, I'm a freshman in college and definitely a beginner at this. My professor assigned a game of life program for us to write and implement. I don't like to complain, but this professor really hasn't given us much of any help, and we've more or less had to teach ourselves. I'm really lost with this assignment, and I barely know where to start. The instructions read:

" We ask you to implement a working version of the game of life program that do the following:
The program should display a welcome message “welcome to the game of life” and offer the user a chance to seed the board randomly (each location with a 40% chance of being alive) or read in a board configuration file (an input pbm file as we discussed in class). The size of the random board or the name of the input file should be entered by the user. After the initialization, the program should compute how the board will evolve over 1000 iterations. At each iteration, write the current board as an output pbm file using the function writePBM() (under Files Homework_Data) as we discussed in class. You will need to modify the main() function in the lecture slides and implement three functions: one that initializes the board from an input file, one that initializes a random board and one that computes the next generation from the current.
To test your code, use the input file “glider.pbm” (under Files Homework_Data), compute how the board will evolve over 1000 iteration and submit the “output0500.pbm”. We encourage you to test your code first on simple cases as we show in class during your implementation"

If anyone could help, it would be greatly appreciated. Thank you!

and I barely know where to start


The program should display a welcome message “welcome to the game of life”
seems like a good place to start.
Well, beyond that.
offer the user a chance to seed the board randomly... or read in a board configuration file
int world[rows][cols];
That's illegal C++. Your compiler shouldn't let you do that.

Why are you using a 2D array of int values? What int values are you going to store in there? What information do you actually need to store?
Right, in the case where the user decides to seed randomly I'm not sure where to go. My code so far reads

#include <iostream>

using namespace std;

int main(){

int rows;
int cols;
int i,j;
vector<int> board;

cout<<"welcome to the game of life"<<endl;
cout<<"Input the word seed if you would like to randomly seed the board, or input a pbm file if you would like to read in a configuration file."<<endl;
cin>>n;
if(n="seed"){
cout<<"Please enter the number of rows and columns, respectively. "
cin>>rows>>cols;
int world[rows][cols];
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
I honestly couldn't tell you. I'm so beginner its ridiculous, and this class has not helped me at all. I'm looking through past posts about the game of life program to try to figure it out, that's where I got that code.
Well, programming is about thinking. Building programs by pasting together old code you found on the internet is an awful way to program.

You need a 2D array in which every element is in one of two states. On or off. True or false. Alive or dead. Do you know a type that has two states? Something that obviously, logically can be used to represent on or off, true or false, alive or dead?
well, bool right?
As @Repeader said, int world[rows][cols] is illegal syntax. You cannot declare an array of unknown (at compile time) size on the stack. You need to create it dynamically (think new and delete[]).

However, you have vector<int> board; declared previously. How are board and world different?

I would work with std::vector in this case. It manages the dynamic allocation for you, so you don't have to worry about correctly calling new and delete[].

Resize board to be rows * cols big. Then, access to element (r, c) will be at index (r * cols) + c. (Each complete row takes up cols elements, and you are looking for the c'th element in the next partial row. Make sure that r and c are 0-based so that this works.)
Last edited on
well, bool right?


Sure, that'd do it. To be honest, the exact type isn't so important so long as you decide what counts as alive and what counts as dead and you stick to it.

As Doug4 suggests, vectors are better here.

What you need to think about at this point is how to create your board, and how you're going to access each element by row and column value. It doesn't need to be a 2D array, it doesn't need to be a 2D vector. It just has to have the right number of elements, and you need to be able to identify the element representing each location fo the board. A function that takes your input row and column value and gives you the corresponding element.

Don't think about anything else until you've got that. Nothing else matters if you can't create and access the elements correctly.

This is programming. Thinking about the problem. Splitting it into small pieces. Solving small pieces individually. All that syntax you've memorised is just syntax. That's not programming. Programming is thinking about your problem in such a way that the solution lends itself to programmatic expression.
closed account (E0p9LyTq)
You can create a 2D int vector as long as you supply both dimensions when creating it. The elements are default initialized to zero, you don't have to push back values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <vector>

int main()
{
   std::cout << "Creating a 2-dimensional vector, enter row size: ";
   int row_size;
   std::cin >> row_size;

   std::cout << "Enter column size: ";
   int col_size;
   std::cin >> col_size;
   std::cout << "\n";

   // create a 2 dimensional int vector with known dimensions
   // this initializes each element to zero 
   std::vector<std::vector<int>> aVector(row_size, std::vector<int>(col_size));

   // initialize the vector with some values
   for (int row_loop = 0; row_loop < row_size; row_loop++)
   {
      for (int col_loop = 0; col_loop < col_size; col_loop++)
      {
         aVector[row_loop][col_loop] = ((row_loop + 1) * 100 + col_loop + 1);
      }
   }

   // let's display the filled 2D vector
   for (int row_loop = 0; row_loop < row_size; row_loop++)
   {
      for (int col_loop = 0; col_loop < col_size; col_loop++)
      {
         // let's display the filled 2D vector
         std::cout << aVector[row_loop][col_loop] << ' ';
      }
      std::cout << '\n';
   }
   std::cout << '\n';
}

Creating a 2-dimensional vector, enter row size: 5
Enter column size: 3

101 102 103
201 202 203
301 302 303
401 402 403
501 502 503
Topic archived. No new replies allowed.