0

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;
 }

};

3
  • How is MachineState defined? Have you considered creating a two dimensional array of pointers to objects of type MachineState? Commented Apr 24, 2013 at 19:05
  • FYI singletons are bad, macros are bad Commented Apr 24, 2013 at 19:10
  • @Dave Thanks i'm just following the book. It said that it was better than declaring global variables so I try to implement it Commented Apr 24, 2013 at 19:19

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey @Refugnic Eternium. 1st of all thanks for your help. second i've added my machine class in case you need to look at it...but it looks like your explanation does the trick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.