Reputation: 159
I have a 2Dimensional array, that is supposed to hold objects that are constantly moving.
class ZombieLand : public Singleton<ZombieLand>
{
DECLARE_SINGLETON(ZombieLand);
public:
MachineState world [19][19];
MachineState getWorld()
{
std::cout<<"World";
return world[19][19];
}
void setWorld(MachineState & state)
{
world [state.x][state.y] = state;
}
}
I try to check if a certain location is Null, but the "NULL" word does not work, nor does 0
switch (state.m_Facing)
{
case (MachineState::UP):
if(ZombieLand::get().world[state.x][state.y-1] != NULL )
{
state.m_occupied = true;
break;
}
My question is, how can I check to see if a location of my world array already holds an object? Thank you in Advance.
My MachineState class
struct MachineState
{
template <typename MachineTraits>
friend class Machine;
enum Facing { UP, RIGHT, DOWN, LEFT};
MachineState()
: m_ProgramCounter(1)
, m_ActionsTaken(0)
, m_Facing(UP)
, m_Test(false)
, m_Memory(nullptr)
,x(0)
,y(0)
,point1(25, 10)
,point2(10, 40)
,point3(40, 40)
{ }
int m_ProgramCounter;
int m_ActionsTaken;
Facing m_Facing;
bool m_Test;
bool m_occupied;
int x;
int y;
Point point1;
Point point2;
Point point3;
int GetActionsPerTurn() const throw() { return m_ActionsPerTurn; }
int GetMaxMemory() const throw() {return m_MaxMemory; }
bool GetInfect() const throw() { return m_InfectOnAttack; }
void setPoint(Point p1, Point p2, Point p3)
{
point1=p1;
point2=p2;
point3=p3;
}
};
Upvotes: 0
Views: 2170
Reputation: 4291
There are two ways for you to go here.
First: In your constructor of MachineState
you'd have to set a variable, which determines what is on that field and to add a function, which tells you that. If the return value of that is '0' you'll know that the field is empty.
Second: Declare a two dimensional array of pointers.
MachineState *world[19][19];
memset(world, 0, sizeof(world));
This little code will set all the pointers to 0 (NULL), which you can then check. Of course you'd need to allocate objects to this two dimensional array manually then.
As a little plus, your getWorld()
function looks rather buggy.
Please consider using this function instead:
MachineState *getField(MachineState state) {
return world[state.x][state.y];
}
Alternatively, you can of course use:
MachineState *getField(int X, int Y) {
return world[X][Y];
}
The latter function could be called as:
MachineState *Field = ZombieLand::getField(state.x, state.y - 1);
if(Field != NULL) state.m_occupied = true;
Of course you'd need to add boundary checking, but that's beyond the scope of this answer.
Upvotes: 2