Hi everybody,
I asked my professor for some practice problems to work on over the summer to prep me for Data Structures this fall and he gave me Conway's Game of Life to recreate. I haven't had any trouble with what I've completed so far, but I want to stop where I'm at and put everything in a class so get some more practice with OOP.
Problem is, I've never setup an array class before and I haven't really haven't found any examples online or in any books I have. I think I'm off to a good start, but there are some areas that I'm concerned about, namely how to destruct a 2d array or what should be included as private data members. I've created stack classes before, but this seems a little different.
In my Life.h file:
[CODE=C]
const int MAXROW = 10;
const int MAXCOL = 10;
class Life{
private:
LifeBoard[10][10]; // I'm really not sure what should be included here.
public:
Life(); // These are the only data members I've attempted to setup so far.
~Life(); // I know I need a copy constructor, but I don't think I'm
// going to get very far while I'm unsure about the private members.
}
[/CODE]
In my Life.cpp file:
[CODE=C]
Life(){
for(int i=0; i<MAXROW; i++){
for(int j=0; j<MAXCOL; j++){
LifeBoard[i][j] = 0;
}
}
}
~Life(){
for(int i=0; i<MAXROW; i++){
for(int j=0; j<MAXCOL; j++){
delete [] LifeBoard [j]; // I know this has got to be wrong, but I'm stuck.
}
}
}
[/CODE]
In main.cpp, I have a reset() function that basically does the same thing as the constructor and I have an init() function that randomly populates a 2d array with 1's.
Eventually, I believe I'm going to need a method that will traverse the original array and put values in a new 2d array's index locations that determine if a cell will live, die, or do nothing.
Once I've got the values for the second array, if I add it to the original 2d array, I should have the next state (or generation) of cells.
Just to entertain myself, I setup the original array in a do{}while() loop so that it will print the array to the monitor, delay for 2 seconds, clear the screen, and print the updated array. Right now, it's just randomly filling the original array with 1's since I haven't added it any other array. I got a kick out of watching the animation of the array as it filled up. Hopefully I can apply this to the next generation so that it will continually update until all of the cells die.
Oh, BTW, I don't want any clear cut answers to this problem. If I did, I would have downloaded the Java code from one of the many websites that have a Game of Life example.
Thanks for any guidance that can be spared...
** I added code=c tags to my code, but it's only outputting text. Just fyi so no one thinks I didn't at least try to make this post readable.
I asked my professor for some practice problems to work on over the summer to prep me for Data Structures this fall and he gave me Conway's Game of Life to recreate. I haven't had any trouble with what I've completed so far, but I want to stop where I'm at and put everything in a class so get some more practice with OOP.
Problem is, I've never setup an array class before and I haven't really haven't found any examples online or in any books I have. I think I'm off to a good start, but there are some areas that I'm concerned about, namely how to destruct a 2d array or what should be included as private data members. I've created stack classes before, but this seems a little different.
In my Life.h file:
[CODE=C]
const int MAXROW = 10;
const int MAXCOL = 10;
class Life{
private:
LifeBoard[10][10]; // I'm really not sure what should be included here.
public:
Life(); // These are the only data members I've attempted to setup so far.
~Life(); // I know I need a copy constructor, but I don't think I'm
// going to get very far while I'm unsure about the private members.
}
[/CODE]
In my Life.cpp file:
[CODE=C]
Life(){
for(int i=0; i<MAXROW; i++){
for(int j=0; j<MAXCOL; j++){
LifeBoard[i][j] = 0;
}
}
}
~Life(){
for(int i=0; i<MAXROW; i++){
for(int j=0; j<MAXCOL; j++){
delete [] LifeBoard [j]; // I know this has got to be wrong, but I'm stuck.
}
}
}
[/CODE]
In main.cpp, I have a reset() function that basically does the same thing as the constructor and I have an init() function that randomly populates a 2d array with 1's.
Eventually, I believe I'm going to need a method that will traverse the original array and put values in a new 2d array's index locations that determine if a cell will live, die, or do nothing.
Once I've got the values for the second array, if I add it to the original 2d array, I should have the next state (or generation) of cells.
Just to entertain myself, I setup the original array in a do{}while() loop so that it will print the array to the monitor, delay for 2 seconds, clear the screen, and print the updated array. Right now, it's just randomly filling the original array with 1's since I haven't added it any other array. I got a kick out of watching the animation of the array as it filled up. Hopefully I can apply this to the next generation so that it will continually update until all of the cells die.
Oh, BTW, I don't want any clear cut answers to this problem. If I did, I would have downloaded the Java code from one of the many websites that have a Game of Life example.
Thanks for any guidance that can be spared...
** I added code=c tags to my code, but it's only outputting text. Just fyi so no one thinks I didn't at least try to make this post readable.
Comment