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!
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?
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.)
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.
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.