我有一個2D維數組,它應該保存持續移動的對象。檢查一個二維數組中的對象是否爲
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;
}
}
我嘗試檢查,如果某個位置是空的,但「空」字不工作,也沒有0
switch (state.m_Facing)
{
case (MachineState::UP):
if(ZombieLand::get().world[state.x][state.y-1] != NULL)
{
state.m_occupied = true;
break;
}
我的問題是,我怎麼能檢查,看是否有我的世界數組的位置已經擁有一個對象?先謝謝你。
我MachineState類
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;
}
};
'MachineState'是如何定義的?你有沒有考慮過創建'指針'的二維數組到'MachineState'類型的對象? – 2013-04-24 19:05:43
僅供參考單身人士是壞的,宏是壞 – David 2013-04-24 19:10:06
@Dave謝謝,我只是跟着這本書。它說這比聲明全局變量要好,所以我嘗試去實現它。 – 2013-04-24 19:19:18