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: -
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.
-
}
-
In my Life.cpp file: -
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.
-
}
-
}
-
}
-
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.
6 4066
Language-specific code tags are broken right now, the important thing is that you used them at all.
If you're going to #define MAXROWS and MAXCOLS, you should use them as the bounds of your array, rather than hardcoding the size in. Also, you never initialize your array with new. If you're using delete, you should be using new and vice versa. Deleting a 2d array is done as follows: -
for (int a = 0; a < NUMROWS; a++)
-
delete[] matrix[a];
-
delete matrix;
-
Whew, I thought that maybe I was doing something wrong in regards to the code tags. Thanks for clearing that up.
When I use the new operator to build my array, should I use pointers or should I use the open/close brackets ([ ])? This, apparently, is another array that I'm lacking knowledge in. I know arrays and pointers are essentially the same, so I'm interested to know both ways to initialize a 2d array, if there's a difference, when creating an array class.
Just read this Howto. It'll explain all this fun stuff.
Thanks Laharl for that link to the array article. It's a good article, however, I read it before I posted.
I'm not sure how to apply it in a class. I'm lost on what should be included as a private data member (whether they should be pointers or array[][]) and also what some typical methods are that should be included in an array class.
I'll make note of a few things, because I think what you really need to do is reread certain topics. They are confusing in any case.
As a note, putting everything in a class doesn't mean your code uses OOP. OOP is a design paradigm, and if your code is modeled around that paradigm, using OOP syntax will just bloat your code. I realize you're trying to practice OOP, but I want you to realize what you're actually doing is just working with the syntax, not the actual design concept.
Problem is, I've never setup an array class
I'm not entirely sure what you mean by an array class. The two things that come to mind are C++ vectors and Boost.Array. But as I see your code, it seems your real question is, "how do I deal with arrays in my class?".
namely how to destruct a 2d array
You can only destruct two things. You can destruct, given a pointer to a dynamically allocated block of memory. Or you can do it for an array of something.
I know arrays and pointers are essentially the same
Wrong. Arrays and pointers are fundamentally different things. Confusingly though, certain syntax and semantics are mixed with arrays and pointers. Google for something called "C FAQ". Read that. There's areas where they explain the relationship between arrays and pointers. FYI, there is also a C++ FAQ online, which is an equally valuable resource.
In any case, the syntax can become problematic, because: -
int someVar; //easy to understand, this is an integer variable
-
int someVar2[10]; //also straightforward, 10 integers in an array
-
int *someVar3; //it points to an integer...like
-
someVar3 = &someVar; //straightforward
-
someVar3 = someVar2; //huh?
-
someVar2 isn't a pointer. It's an array. But C (and therefore C++) have a difficult way of handling arrays. When you refer to someVar2, it's like getting a pointer (which points to an array). It's not a pointer, but syntactically it's treated as one. Note: -
//correct:
-
someVar3 = new int[10];
-
delete someVar3;
-
//But this is not a smart idea:
-
delete [] someVar2;
-
Once you've read the C FAQ, post here, if you understand what's going on with arrays and pointers.
I'm still confused about the proper way to setup a 2d array using classes. Most notably, creating the constructor, a copy constructor, and a destructor.
One of the replies I received helped out with the destructor, but if I'm not setting it up properly from the beginning, the destructor really doesn't do me any good.
I have no problem setting up a 2d, 3d, 50d array in main, however, I've never done it before in a class, so I'm a little lost.
Just to reiterate my initial post, I'm trying to recreate Conway's Game of Life. In order to set up the game, I need two 2d arrays...one of the arrays (current state) will be initialized randomly with 1s and the second (test) will test the first array to see if a 0, 1, or -1 needs to go in the index locations. Then, the arrays can be added together to achieve the next state. This will continue as long as there are next generations or all of the index locations become -1.
I don't want to use vectors or boost.array (or whatever it's called) because I feel it's important to have a basic understanding. I want to set everything up in a class because I need the practice.
Hopefully this helps explain what I'm trying to achieve...
Sign in to post your reply or Sign up for a free account.
Similar topics
by: its me |
last post by:
Let's say I have a class of people...
Public Class People
Public Sex as String
Public Age as int
Public Name as string
end class
And I declare an array of this class...
|
by: Buddy Ackerman |
last post by:
I created a simple class:
Public Class MyTestClass
Public Test() As String
End Class
I tried to assign some values to the array Test() and display them like this:
|
by: Gianni Mariani |
last post by:
Does anyone know if this is supposed to work ?
template <unsigned N>
int strn( const char str )
{
return N;
}
#include <iostream>
|
by: michi |
last post by:
Hello,
I need to initialize a 2 dimensional square arrays of structures. The size of array I get from the user. I can
do one-dimensional array, but I don't know how to specify the size of array...
|
by: justanotherguy63 |
last post by:
Hi,
I am designing an application where to preserve the hierachy and for
code substitability, I need to pass an array of derived class object in
place of an array of base class object. Since I...
|
by: Rade |
last post by:
Please have a look at the following program:
#include <iostream>
template <const int array, size_t index>
class ArrayIndex
{
public:
static const int value = array;
};
|
by: Armand |
last post by:
Hi Guys,
I have a set of array that I would like to clear and empty out.
Since I am using "Array" not "ArrayList", I have been struggling in
finding the solution which is a simple prob for those...
|
by: sandy |
last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory',
within my class Directory (Can you already smell disaster?)
Each Directory can have subdirectories so I thought to put these...
|
by: =?Utf-8?B?U2hhcm9u?= |
last post by:
Hi Gurus,
I need to transfer a jagged array of byte by reference to unmanaged
function, The unmanaged code should changed the values of the array, and when
the unmanaged function returns I need...
|
by: npankey |
last post by:
I've started experimenting with template metaprogramming in a small
project of mine. What I'm trying to accomplish is to generate a static
array of templated objects that get specialized based on...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |