• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Help me with my c++ project?

Status
Not open for further replies.
hey guys, i've got this project and i'm just not sure how to set it up... the assignment is as follows:

Game of Life:

Game of Life is a cellular automaton on an infinite rectangular grid. Each grid cell is either alive/on or dead/off. The new state of each cell is computed in discrete steps and is determined by its current state and the number of the live cells among it's surrounding 8 nearest neighboring cells. example:

1 2 3
4 * 5
6 7 8

where * is the cell in question. the rules are as follows:

death: if a live cell has 0 or 1 live neighbors, the cell dies of lonliness, or if a live cell has 4,5,6,7, or 8 live neighbors, the cell dies of overcrowding.

survival: if a live cell has 2 or 3 live neighbors, the cell survives to the next generation.

birth: if a dead cell has 3 live neighbors, the cell becomes alive; otherwise, a dead cell stays dead in the next generation.

For clarity, we display dead cells using '.' and live cells using '|'. for example, a game of life 10 by 10 grid with initial pattern of "cross" would look like:

. . . . | . . . .
. . . . | . . . .
. . . . | . . . .
. . . . | . . . .
| | | | | | | | | |
. . . . | . . . .
. . . . | . . . .
. . . . | . . . .
. . . . | . . . .

note that if a cell is on a boundary, we imagine it raps around...

here are the specifications:

syntax: game pattern
game pattern width height steps(# of generations)

for example: game cross
game cross 10 7 15

note that the "game" "cross" "width" "height" and "steps" are command line arguments.

okay, now my question is this. i must use two classes for this project. one "board" and one "cell". what is the best way to go about this? it seems to me that it would be much easier to use one class, but the specifications are the specifications. can anyone point me in the right direction? any help is appreciated!

ps: i'm not looking for an answer to the overall problem, just what the most efficient way to set up the two classes is.
 

Pochacco

asking dangerous questions
Blah.
That problem description is way overly complex.

I think your Board class will essentially be a matrix (2D Array), whose sizes are specified by the command line arguments.
The Cell class is what those matrices in the board class store.

Your Cell class should have a member variable isAlive that indicates whether this cell is dead or alive. It could also have getter/setter functions that return/set the status of this isAlive variable.

Your board class will probably contain all the logic to determine whether a cell is dead or alive. It's all mathematical and should be pretty simple. For instance, To test the status of cell[x][y], just determine the status of its surrounding cells, which should be:

Code:
cell[(x-1) % X][(y-1) % Y], cell[x % X][(y-1) % Y] , cell[(x+1) % X][(y-1) % Y]
cell[(x-1) % X][y % Y]    ,                        , cell[(x+1) % X][y % Y]
cell[(x-1) % X][y % Y]    , cell[x % X][(y+1) % Y] , cell[(x+1) % X][(y+1) % Y]
...where X = # columns (or the length of the 1st array in the matrix)
...and Y = # rows (or the length of the 2nd array in the matrix)

...um, yea. That's all I can think of for now.
Now that I think about it, the Cell class is kinda pointless. You could just represent it with a bool variable...so uh, I may be wrong. :p
 

Lathentar

Looking for Pants
So the cell class is pointless UNLESS...

You have different cells that mutate in different ways. The Life simulation I wrote required a handle to all cells to mutate into other kinds of cells without the board knowing.

Its nice if each Cell knew how to mutate.

Cell asks the Board class how many neighbors it has then decides whether to live or die. That way you can have different cells mutate differently.
 

Lazy

Member
Are you having trouble with the object-oriented design of the project?

I'm way too tired to get into specifics, but as with any OOP assignment you need to decide what objects you need, what properties they should have, what states they can be in, etc., and design the classes accordingly.

You need two classes, which means two objects -- a cell object and a board object.

The cell class should represent a single, individual cell on a board.
What states can a cell have? (It can be dead or alive, so in your cell class you'll need a variable that indicates the state of the cell and functions that change the state of the cell from dead to alive and vice versa.)
What properties of cells will the board need to know? (Will a cell require a unique identifier so the board object can distinguish between the different cells that are on it?)

Same thing with the board.
What is a board supposed to do? (It's supposed to store cell objects and determine which cell objects should be set as alive and dead at each generation. You board class should therefore have functions to do the required calculations and then set the individual cells to alive or dead using the functions you wrote in the cell class.)
What properties of a board should you be able to set? (Will you need to store the size of the current board and functions to set the size? If so, you'll need variables to represent the board size and functions to set and retrieve the board size.)

Then in your main function you'll need to simulate the game by creating a board object, populating the board with cells, changing the generations, etc.
 
Pochacco said:
Blah.
That problem description is way overly complex.

I think your Board class will essentially be a matrix (2D Array), whose sizes are specified by the command line arguments.
The Cell class is what those matrices in the board class store.

Your Cell class should have a member variable isAlive that indicates whether this cell is dead or alive. It could also have getter/setter functions that return/set the status of this isAlive variable.

Your board class will probably contain all the logic to determine whether a cell is dead or alive. It's all mathematical and should be pretty simple. For instance, To test the status of cell[x][y], just determine the status of its surrounding cells, which should be:

Code:
cell[(x-1) % X][(y-1) % Y], cell[x % X][(y-1) % Y] , cell[(x+1) % X][(y-1) % Y]
cell[(x-1) % X][y % Y]    ,                        , cell[(x+1) % X][y % Y]
cell[(x-1) % X][y % Y]    , cell[x % X][(y+1) % Y] , cell[(x+1) % X][(y+1) % Y]
...where X = # columns (or the length of the 1st array in the matrix)
...and Y = # rows (or the length of the 2nd array in the matrix)

...um, yea. That's all I can think of for now.
Now that I think about it, the Cell class is kinda pointless. You could just represent it with a bool variable...so uh, I may be wrong. :p

i agree with you 100%. i just can't see why you need to use two classes--it almost makes the program overly-difficult to write.

i'm just not sure what goes where. if the 2d array and logic to determine whether a cell lives or dies belong to class board, what's the point of class cell? it seems almost useless...

hopefully someone can further explain the point of using two classes?

edit: for instance, if class cell represents a single cell and class board holds the array, class cell will need to access class board's array to check whether it lives or dies, correct? then what the hell is the point of having the class cell when i could just do it in board to begin with? someone please explain the advantage of using two classes to one in this scenario. you help is appreciated!
 

Lathentar

Looking for Pants
={<SMOKE>}= said:
i agree with you 100%. i just can't see why you need to use two classes--it almost makes the program overly-difficult to write.

i'm just not sure what goes where. if the 2d array and logic to determine whether a cell lives or dies belong to class board, what's the point of class cell? it seems almost useless...

hopefully someone can further explain the point of using two classes?
Read my post.
 

Lazy

Member
={<SMOKE>}= said:
edit: for instance, if class cell represents a single cell and class board holds the array, class cell will need to access class board's array to check whether it lives or dies, correct? then what the hell is the point of having the class cell when i could just do it in board to begin with? someone please explain the advantage of using two classes to one in this scenario. you help is appreciated!

A cell doesn't decide whether it lives or not. A cell simply contains a variable which the board can lookup to determine if a cell is living or dead. The board object SETS the state (living or dead) of the cell. The cell object is an independent object. It just exists, that's it. It doesn't worry about anyone or anything.

The board, however, is responsible for all the cells. The elements of the board's 2-d array are independent cell objects (i.e. instead of storing numbers or values in the 2-d array, you'd store references to each of the cells you create). The board decides which cells should be living and which should be dead. It then sets the state of the cells accordingly.

I suspect that the purpose of this assignment is to teach you the concepts behind object-oriented programming. While a cell object isn't absolutely necessary, conceptually it makes solving this problem easier (although you probably don't see that right now). When you have more complicated assignments, you'll see the advantages of OOP. :)

Think of it this way (sorry if this confuses you!): you have to represent the cells somehow. You can use a boolean, an integer, a string, whatever. Think of each of these types (boolean, integer, string) as an object. So instead of using a predefined object (such as a boolean, an integer, or a string), why not create your own so that you can code the object with certain conveniences. For example, say for each cell, your board needs to keep track of an ID and a state. So cell 1 may have ID=1 and state=dead. Cell 2 may have ID=200 and state=alive.

How is your board class going to keep track of these two values for each cell if you don't create a cell object?
 

slayn

needs to show more effort.
there is no reason to have a cell class... that assignment is stupid.

just have 2 matrices of nxn bits. matrix A gets initialized by the starting condition. call algorithm on A initializing B such that it represents A after 1 round. Then call algorithm on B thus replacing A. Forever until whatever your stopping point is.
 

fart

Savant
oxoxooooxoxxxxoxxxxooooxxoxxxoxooooxooooxoxxxxoooxxo
oxoxoxxxxoxxxxoxxxxoxxoxxoxxxoxoxxoxoxxoxoxxxxoxxoxo
oooxoooxxoxxxxoxxxxoxxoxxoxoxoxoxxoxoooxxoxxxxoxxoxo
oxoxoxxxxoxxxxoxxxxoxxoxxoxoxoxoxxoxoxxoxoxxxxoxxoxx
oxoxooooxooooxooooxooooxxoooooxooooxoxxoxooooxoooxxo


hey when you get it working run that matrix. i want to see if it survives.
 

Lathentar

Looking for Pants
I'll give you the outline of my last assignment:

class AbstractCell {

~AbstractCell();
AbstractCell(state_type);
state_type alive();
AbstractCell* clone();
bool evolve();
void kill();
std::string morph();
void queryNeighbor(NeighborDir)
write(std::eek:stream&);

}

I'd build a cell off of this Abstract Cell

class Life { // This is Board for you

AbstractCell* at(size_type, size_type);
void clear();
size_type cols();
size_type generation();
bool next();
size_type population();
size_type rows();
write(std::eek:stream&);

}

Next would go to each cell and tell each cell how many neighbors it had. Then go through AGAIN and tell them to evolve.
 
so, if the board is 10x10, i have to instantiate 100 cell objects? this is what i'm unsure of. i was under the impression that i wouldn't have to do that...

hell, i'm not even sure how to do that considering the size of the board (and number of cells) can be changed (the user enters it as command line arguments).

i really appreciate the help guys.

edit: i also don't understand how the object board looks at the object cell... fuck.
 

Ferrio

Banned
Dynamic arrays, vectors whatever you want to use.

Then using that, each element will hold a pointer to a cell object.
 
okay, if the board is 10x10, then there's 100 cells. do i need to instantiate 100 objects of type cell? i think i do, but how do i do that? the problem is that i don't know how many objects i'll need to create. i don't see how dynamic arrays will allow me to do this... i can't store an object of type cell in an array space...

also, how does board run through n number of objects to check their status/next status?

edit: ferrio can you expand? i'd still have to create an unkown number of objects for the pointers to point to.
 

Ferrio

Banned
okay, if the board is 10x10, then there's 100 cells. do i need to instantiate 100 objects of type cell? i think i do, but how do i do that? the problem is that i don't know how many objects i'll need to create. i don't see how dynamic arrays will allow me to do this... i can't store an object of type cell in an array space...

You'll create a cellobject array. Then the array will store pointers to the objects you create. You create them on the fly (using "new cellobject" which will return a address which you will store int he array), and then store the address in the appropriate element.

also, how does board run through n number of objects to check their status/next status?

Just something you're going to have to come up with. You can just step through all the elements checking the appropriate spaces besides them, going to take awhile but it'll work. Your cellobjects should have appropriate class functions to return and set their status.
 

KonVex

Member
At Lathentar:
So the cell queries the board about its neighbours.
It has to be aware of the class Board, but at the same time
class Board needs to know about class Cell!
Won`t you run into dependency problems?

At ={<SMOKE>}=:
Remember that the state of a cell of generation n+1 depends on the state of its neighbours at generation n.
You need to store all states of both generations, read from n and write into n+1.
 
KonVex said:
At Lathentar:
So the cell queries the board about its neighbours.
It has to be aware of the class Board, but at the same time
class Board needs to know about class Cell!
Won`t you run into dependency problems?

At ={<SMOKE>}=:
Remember that the state of a cell of generation n+1 depends on the state of its neighbours at generation n.
You need to store all states of both generations, read from n and write into n+1.

thank you! that's my whole problem! i can't see how to write this thing without board having to access cell and cell having to access board. it just doesn't make any goddamn sense!

as far as storing present state and next state, i've already thought of that. i appreciate the heads up though!

this whole thing just doesn't make any sense to me... i can't wrap my brain around it.
 

Ferrio

Banned
={<SMOKE>}= said:
thank you! that's my whole problem! i can't see how to write this thing without board having to access cell and cell having to access board. it just doesn't make any goddamn sense!

as far as storing present state and next state, i've already thought of that. i appreciate the heads up though!

this whole thing just doesn't make any sense to me... i can't wrap my brain around it.


The cells only need to know about themselves, the board will then get information from the cells.
 
quick question. how do i populate my dynamic array full of pointers? i understand i want to use 'new' and create an array of the appropriate size, but how do i populate that array with pointers to object new class? i'm not sure how i can get the pointers to be distinguishable in name from one another...
 

Shompola

Banned
KonVex said:
At Lathentar:
So the cell queries the board about its neighbours.
It has to be aware of the class Board, but at the same time
class Board needs to know about class Cell!
Won`t you run into dependency problems?

At ={<SMOKE>}=:
Remember that the state of a cell of generation n+1 depends on the state of its neighbours at generation n.
You need to store all states of both generations, read from n and write into n+1.

Ok I have never done this project so please excuse me if I say something that may not make sense.

First off, there should be no dependency problems. A cell should also inherit what board it belongs to and its position other than what state it is in. When you create the cell you should also give an input of what board the cell belongs to. So a pointer to the board should be associated with the cell.

A board will not create the cells. And it will not decide what the cell should do. It's the cell itself who decides what to do according to the rules given. Implement a iAmThinking function for the cell class where it steps thru what the states it neighbours are and sets its own state accordingly.

Obviously all cells can't act simultaniously. Implement a simple que and traverse thru it continously. This que should be implemented in a simulation class.

When you make the board, size of widthxheight, number of cells should not be necessary widthxheight. Initially when the simulator is run, 1 )you should make the board, 2) randomly make 0 to widthxheight cells 3) randomly put them on the board 4) insert everything in the que 5) start the continuous loop that traverses thru the que

Last but not least, yes you should have three classes, i.e. three object types, cell, board and simulation. Simulation is the one who will create the board, the cells and run the que. Who knows you maybe want to run several boards and cells simultaniously and your design should not interferr with that.

edited some parts...
 
Shompola, i see what you are saying, but it leaves me in the same position. if object cell decides it's fate, it still needs to tell board the proper state to display. i'm unclear on two things:

1) how to populate a dynamic array with pointers to each cell element

2) how the two classes communicate ie: how cell tells board what state to display
 

Ferrio

Banned
Shompola said:
Ok I have never done this project so please excuse me if I say something that may not make sense.

First off, there should be no dependency problems. A cell should also inherit what board it belongs to and its position other than what state it is in. When you create the cell you should also give an input of what board the cell belongs to. So a pointer to the board should be associated with the cell.

A board will not create the cells. And it will not decide what the cell should do. It's the cell itself who decides what to do according to the rules given. Implement a iAmThinking function for the cell class where it steps thru what the states it neighbours are and sets its own state accordingly.

Obviously all cells can't act simultaniously. Implement a simple que and traverse thru it continously. This que should be implemented in a simulation class.

When you make the board, size of widthxheight, number of cells should not be necessary widthxheight. Initially when the simulator is run, 1 )you should make the board, 2) randomly make 0 to widthxheight cells 3) randomly put them on the board 4) insert everything in the que 5) start the continuous loop that traverses thru the que

Last but not least, yes you should have three classes, i.e. three object types, cell, board and simulation. Simulation is the one who will create the board, the cells and run the que. Who knows you maybe want to run several boards and cells simultaniously and your design should not interferr with that.

edited some parts...


Why should a cellobject need to know it's position? The board class could easily take care of this. That would make the cellobjects smaller, and eliminate the use of a simulation class.
 
ferrio, how do i populate a dynamic array with pointers to new cell objects where the pointers can be differentiated from one another? or, will *ptr = new cell work for each one?
 

Shompola

Banned
={<SMOKE>}= said:
Shompola, i see what you are saying, but it leaves me in the same position. if object cell decides it's fate, it still needs to tell board the proper state to display. i'm unclear on two things:

1) how to populate a dynamic array with pointers to each cell element

2) how the two classes communicate ie: how cell tells board what state to display

As said the board should not be the one doing the thinking. The board is just a board.
The simulator class will continuesly traverse thru the que with the cells in it. When doing so it can look up the state of each cell after the cell has been thinking. When it has looked up the state it will update the GUI or in your case to the text display.

And why would you need a dynamic array? Number of cells is allways fixed when the simulator is running, they are never removed when they are dead. You only make the array at the intial fase, the size of a random number between 0 to widthxheight of the board.
 
Shompola said:
As said the board should not be the one doing the thinking. The board is just a board.
The simulator class will continuesly traverse thru the que with the cells in it. When doing so it can look up the state of each cell after the cell has been thinking. When it has looked up the state it will update the GUI or in your case to the text display.

And why would you need a dynamic array? Number of cells is allways fixed when the simulator is running, they are never removed when they are dead. You only make the array at the intial fase, the size of a random number between 0 to widthxheight of the board.

i need a dynamic array because the user can change the length/width through command line arguments.
 

Ferrio

Banned
1) how to populate a dynamic array with pointers to each cell element

Ok you'll have a dynamic 2d array. Then step through each element of the array and creating a new cellobject using "new cellobject". This returns an address so all you have to do is do something like array[#][#] = new cellobject. Now to access it you'd do it the same way you'd access any element in array, by using the array name and it's indexes. Now i'm not going to write that out, cause that's 3 pointers involved there (2 for the array, 1 for the object) and I'd fuck up the syntax (always been hell with pointers). Once you've step to the whole array, it's populated with cells.



2) how the two classes communicate ie: how cell tells board what state to display

Stompola seems to have differing opinions on how to go about this. This is how I'd do it (simple but works).

1. Cellobjects. The only information they know is what their current status is, and what their new status is. Then you'd have functions to set and get each value.

2. A board object. Board object when created would use a constructor to make it's size to the correct amount, as well as populate itself with the right amount cells. Then it'd have a function to step through a generation. The board would get the information from the cell, the cell wouldn't have to get information from the board or other cells.


And why would you need a dynamic array? Number of cells is allways fixed when the simulator is running, they are never removed when they are dead. You only make the array at the intial fase, the size of a random number between 0 to widthxheight of the board.

I assume after the simulation is done running it asks the user if they want to try a different board size.
 
Ferrio said:
Ok you'll have a dynamic 2d array. Then step through each element of the array and creating a new cellobject using "new cellobject". This returns an address so all you have to do is do something like array[#][#] = new cellobject. Now to access it you'd do it the same way you'd access any element in array, by using the array name and it's indexes. Now i'm not going to write that out, cause that's 3 pointers involved there (2 for the array, 1 for the object) and I'd fuck up the syntax (always been hell with pointers). Once you've step to the whole array, it's populated with cells.





Stompola seems to have differing opinions on how to go about this. This is how I'd do it (simple but works).

1. Cellobjects. The only information they know is what their current status is, and what their new status is. Then you'd have functions to set and get each value.

2. A board object. Board object when created would use a constructor to make it's size to the correct amount, as well as populate itself with the right amount cells. Then it'd have a function to step through a generation. The board would get the information from the cell, the cell wouldn't have to get information from the board or other cells.




I assume after the simulation is done running it asks the user if they want to try a different board size.

excellent. but, how would the board access the information from the cell objects. how can an object from one class examine the properties of an object from another. for example, how can object board read the current state and next state settings of object cell? thanks for all your help dude.
 

Ferrio

Banned
={<SMOKE>}= said:
excellent. but, how would the board access the information from the cell objects. how can an object from one class examine the properties of an object from another. for example, how can object board read the current state and next state settings of object cell? thanks for all your help dude.


The board will have the array of pointers to cellobjects in it's class (it will be one of the variables you'll declare in private). If you have a pointer to an object you can access it.
 
Ferrio said:
The board will have the array of pointers to cellobjects in it's class (it will be one of the variables you'll declare in private). If you have a pointer to an object you can access it.

wow. i can't believe i didn't realize that... i think that pretty much wraps up all the questions i have. i really appreciate your help. i wish i had something cool to send you, but i've pretty much gotten rid of all my collectibles/pre-order bonus type of things. if i get something cool, i'll shoot you a private message. thanks!

edit: can i populate the 2d array with new cell in main then, or in a board member function?
 

Ferrio

Banned
={<SMOKE>}= said:
edit: can i populate the 2d array with new cell in main then, or in a board member function?


Like I said, that will be something done in the board class's constructor.

Also note none of the stuff said in this thread is wrong, there's nothingi stopping you from making a life program where the cells talk to the board instead, and that probably would be a lot cleaner looking too. THough judging from your questions in this thread, going the easy route might be your best bet.
 
ferrio, i hope you're still around... i understand creating the object(s) cell in the array and that i have a pointer to it, but how do i then view it's characteristics (current state and next state)? i can move to the pointer that points to an object, but i don't understand how to view it...
 

Shompola

Banned
I assume you have created functions in the cell class to access the private variables of the cell like state and such. It's not harder than that.
 

Ferrio

Banned
Here's the code that does the creation of a board, sets it up as a 2d array of pointers. Then has the array point to newley created objects. Of course there's no destruction, so you'll have to do that yourself

Code:
#include <iostream>
using namespace std;

class CellObject
{
public:
	CellObject() { cout << "I'm being created!" << endl;}
	void access() { cout << "I'm being accessed!" << endl;}
};



void main()
{


CellObject ***Board;  // Declare our Board.  
int x, y, i, j;

cout << "Enter the first dimension:  ";  // our x dimension
cin >> x;
cout << endl << "Enter the second dimension:  "; // our y dimension
cin >> y;
cout << endl;



Board = new CellObject**[y];  // creates 1d array of pointers to pointers to cellobjects
for(i = 0; i < y; i++)	{ // creates the 2d array of pointers to cellobjects.
     Board [i]= new CellObject*[x];
}

for( i = 0; i < y; i++){  // Create the cellobjects and have the pointers in the Board array point to each one
     for(j = 0; j < x; j++){
	Board[j][i] = new CellObject;
     }
}
  
for( i = 0; i < y; i++){  // Step through the array and access each object.   
     for(j = 0; j < x; j++){
	Board[j][i]->access();
     }
}


}
 
i understand how that works, but i don't see how a fucntion "access" will allow me to view the characteristics of the cell... i'm sorry, but this is my first project with classes and it's a lot to take in. thanks again for all the help guys.
 

Ferrio

Banned
={<SMOKE>}= said:
i understand that, but i don't understand how a pointer to an object allows you to view it's characteristics.


I don't see how you don't understand it.... isn't

Code:
Board[j][i]->access();

clear?


access() is a member function of the cellobject. If you have a pointer to an object you can access it, if you can access it you can call it's functions.
 
Ferrio said:
I don't see how you don't understand it.... isn't

Code:
Board[j][i]->access();

clear?


access() is a member function of the cellobject. If you have a pointer to an object you can access it, if you can access it you can call it's functions.

that is clear...

however, each cellobject has two characteristics, current state and next state. how do i check those? i don't want to access a member function, i just want to check it's variables...
 

Ferrio

Banned
={<SMOKE>}= said:
that is clear...

however, each cellobject has two characteristics, current state and next state. how do i check those? i don't want to access a member function, i just want to check it's variables...


I thought you said you understood that from Shompola two posts up.


You'll have functions that'll access those data members. The whole point of classes is that you don't directly access data, you access functions in the classes that access the data and return it to you.

So you'll have a function within the class like


GetState()
{
return state;
}


I'm curious how you got this assigment. It's not simple for someone starting off with classes, is this the first time your class has dealt with it?
 
so in the example you gave, how would access return a value? i know, i know... i suck at this...

edit: nevermind.

edit:

yeah, this is the first project dealing with classes. the thing is, i could write this program in 25 minutes using functions. the classes seem to make it more complicated. what do i know though?
 

Ferrio

Banned
liek this

Code:
#include <iostream>
using namespace std;

class CellObject
{
public:
	CellObject() { cout << "I'm being created!" << endl;  alive = true;}
	bool access() { cout << "I'm being accessed!" << endl; return alive;}
private:
     bool alive;
};



void main()
{


CellObject ***Board;  // Declare our Board.  
int x, y, i, j;
bool state;

cout << "Enter the first dimension:  ";  // our x dimension
cin >> x;
cout << endl << "Enter the second dimension:  "; // our y dimension
cin >> y;
cout << endl;



Board = new CellObject**[y];  // creates 1d array of pointers to pointers to cellobjects
for(i = 0; i < y; i++)	{ // creates the 2d array of pointers to cellobjects.
     Board [i]= new CellObject*[x];
}

for( i = 0; i < y; i++){  // Create the cellobjects and have the pointers in the Board array point to each one
     for(j = 0; j < x; j++){
	Board[j][i] = new CellObject;
     }
}
  
for( i = 0; i < y; i++){  // Step through the array and access each object.   
     for(j = 0; j < x; j++){
	state = Board[j][i]->access();  // Access the object and return if it's alive or not.
     }
}


}


Only thing that changed was the class (added a bool data member and modifyed access to return the bool data member), and the line that accesses it (returns the bool data member to the variable 'state').
 
ferrio, the catch is that i need to construct my board in my class board. therefore, i need to define the type of pointer it is in the header file. what type of array stores pointers, int?
 

KonVex

Member
Shompola said:
Ok I have never done this project so please excuse me if I say something that may not make sense.

First off, there should be no dependency problems. A cell should also inherit what board it belongs to and its position other than what state it is in. When you create the cell you should also give an input of what board the cell belongs to. So a pointer to the board should be associated with the cell.

There should be dependency problems! Here is how I would declare the classes:

class Cell
{
Board *OwnerBoard;
// attributes
public:
Cell(Board *);
// methods
}

class Board
{
Cell *BoardData;
// attributes
public:
// methods
}

Cell can`t know about Board because it wasn`t declared yet!
If I swap the declarations the problem will just be shifted.
Also C++ doesn`t have a generic Object class like JAVA.

I could change Cell`s constructor to
Cell(void *pBoard); (and change Board *OwnerBoard to void *)
and use a typecast (e.g. (Board *)pBoard) after Board is known, but that wouldn`t work in all instances!
 

Shompola

Banned
Well I was mostly thinking about my own design when saying there should not be any dependency problems as the board itself has no knowledge about the cells. The cells however have knowledge about what board they belong to thru a pointer to the board.
 

Azih

Member
Actually this is a good assignment, It'll get you used to thinking in object oriented terms, there's a number of ways you can split up responsibilites between the board and the cells so there's no one 'right' way. You could let the cell be smart enough to figure out what will happen to it in the next generation in which case the cells have to have some way of finding out the status of their neighbours, or you could make the board do that in which case the cell objects just hold info about themselves (an isAlive boolean really).

I prefer the first approach because it's more object oriented, the second approach really just makes the cell objects glorified booleans and there's no reason to not have a boolean of matrices do the same job more efficiently.
 
Status
Not open for further replies.
Top Bottom