I have an array of class instances. The class constructor assigns each instance the following variables:
X & Y (for coordinates on a board) -- int, int
team name -- enum
direction (it is facing) -- enum
# of 'soldiers' it has (starts with 4) -- int
As the game progresses, i want to make sure that one piece doesn't impose on the coordinates of another piece. I'm using a 1D array in which I store all the instances.
Intent/question:
How can I effectively compare these instances while keeping all of the
"pieces" in an array?
I was thinking I could make another class called "Board" that would keep track of all the pieces. That way, I could easily compare the Xs & Ys of each instance so that they aren't the same. I could also use this class so that the pieces wouldn't move off of the board.
The problem here is that I don't have a clear idea of how to integrate the new Board class with the GamePiece class I already have.
So my next questions are these:
How do I assign the pieces I have created to a new Board class?
...
Well, I guess that's my only question really. I could ask how could I compare the piece coordinates using the new Board class, too, but I think i'd be able to figure that out easily enough if I knew how to assign the pieces to the Board.
It sounds to me that you don't need another class. Since you have an array of class instances, and you're using an enum for the team name, just make sure you compare the current team name to the elements in your array.
Hm! That's a usable idea. Let me show you some code real quick:
I want to call the moveForward function and make sure i'm not going to move over a friend or foe.
"piece" is the name of the 40 element array, each element holding an instance of the GamePiece class. 'm' is a random number. Here is a section of code showing how i call it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
m=rand()%40; //there are 40 elements in the array so it's going to randomly choose one
if(piece[m].onBoard())
{
randomAction=piece[m].action();
switch(randomAction){
case nothing: break;
case rght: piece[m].turnRight(); break;
case move: piece[m].moveForward(piece,40,m); break;
case attck: piece[m].attack(); break;
}
which, if randomAction equals 'move', the program goes here:
So, I don't know how to compare the current team name or any other variable specific to the piece I want to move to the variables of the rest of the elements in the array.
Should I pass the 'm' variable to the moveForward function so that I can easily get the current team name, coordinates, etc? Or is there a simpler way?
It looks like the moveForward() function is already part of your piece class, so the current random object knows its team name, just loop through the entire array and check to see if the coordinates of each class will stomp on each other.
1 2 3 4 5 6 7 8 9 10
for( int i = 0; i < 40; i++ )
{
if( piece[ i ].team == this->team )
continue;
/* Now check the coordinates of piece[ i ] against this object's coordinates: */
/* I'll leave that to you ;) */
}