0

I have this Singleton class called ZombieLand that is supposed to represent a 2D array. This 2D array is getting constantly updated and populated by different instances of my MachineState struct, which contain x and y coordinates to help me navigate through the 2D array.

#pragma once
#include "singleton.h"
#include "machine.h"
#include "traits.h"
#include <list>


class ZombieLand : public Singleton<ZombieLand>
{
    DECLARE_SINGLETON(ZombieLand);
public:
    MachineState * world [19][19];
    bool map[19][19];
    ZombieLand()
    {

        for(int i = 0; i<19; i++)
        {
            for(int m = 0; m<19; m++)
            {
                map[i][m] = false;
            }
        }
        memset(world,0,sizeof(world));
    }
    MachineState *getField(int x, int y)
    {
        return world[x][y];
    }

    void setWorld(MachineState state)
    {
        world[state.x][state.y] = &state;
        map[state.x][state.y] = true;
    }
    void setFlag(int x, int y)
    {
        delete world[x][y];
        world[x][y] = NULL;
        map [x][y] = false;
    }

};

I'm having problems checking to see if there is a MachineState at location in the 2D array world[Machinestate.x][Machinestate.y]. I used a bool map to tell me if a certain location is occupied or not, but that doesn't give me a pointer to the actual object that is at coordinate x and y, in case i try to delete it. I tried doing MachineState * machine = Zombieland::get().getField(state.x,state.y-1) and then check to see if (machine != NUlL) but i keep getting errors when i compile.

void Op::checkNeighboor(MachineState & state)
{
    switch (state.m_Facing)
    {
    case (MachineState::UP):
            if((ZombieLand::get().map[state.x][state.y-1] != false) || (state.y-1) == -1 )
            {

                state.m_occupied = true;
            }
            else
            {
                state.m_occupied = false;
            }
        break;
    case (MachineState::DOWN):
            if((ZombieLand::get().map[state.x][state.y+1] == true) || (state.y+1 == 20))
            {

                state.m_occupied = true;
                break;
            }
            else
            {
                state.m_occupied = false;
                break;
            }
        case (MachineState::RIGHT):
            {
                MachineState *Field = ZombieLand::get().getField(state.x+1, state.y );
                if(Field != NULL)
                {
                    std::cout<<"TEST FIELD PASSED";
                    state.m_occupied = true;
                    break;
                }
                else
                {
                    state.m_occupied = false;
                    break;
                }
            }

        default:
        case (MachineState::LEFT):
            if((ZombieLand::get().map[state.x-1][state.y] != false) || (state.x-1) == -1 )
            {
                state.m_occupied = true;
                break;
            }
            else
            {
                state.m_occupied = false;
                break;
            }
    }
}



void OpForward :: Execute(MachineState& state) throw()
{
    DebugOutput(state);
    ZombieLand::get().setWorld(state);
    checkNeighboor(state);
    switch (state.m_Facing)
    {
        case (MachineState::UP):
            if(state.y == 0 || state.m_occupied == true)
            {
                state.m_ActionsTaken++;
                break;
            }
            else
            {

                state.y--;
                ZombieLand::get().setFlag(state.x, state.y+1);
                state.point1.Y = state.point1.Y - 30;
                state.point2.Y = state.point2.Y - 30;
                state.point3.Y = state.point3.Y - 30;
                state.m_ActionsTaken++;
                break;
            }

        case (MachineState::RIGHT):
            if(state.x == 19 || state.m_occupied == true)
            {
                state.m_ActionsTaken++;
                break;
            }
            else
            {
                state.x++;
                ZombieLand::get().setFlag(state.x-1, state.y);
                state.point1.X = state.point1.X + 30;
                state.point2.X = state.point2.X + 30;
                state.point3.X = state.point3.X + 30;
                state.m_ActionsTaken++;
                break;
            }
        case (MachineState::LEFT):
            if(state.x == 0 || state.m_occupied == true)
            {
                state.m_ActionsTaken++;
                break;
            }
            else
            {
                state.x--;
                ZombieLand::get().setFlag(state.x+1,state.y);
                state.point1.X = state.point1.X - 30;
                state.point2.X = state.point2.X - 30;
                state.point3.X = state.point3.X - 30;
                state.m_ActionsTaken++;
                break;
            }

        default:
        case (MachineState::DOWN):
            if(state.y == 19 || state.m_occupied == true)
            {
                std::cout<<"OCcupation "<< state.m_occupied;
                state.m_ActionsTaken++;
                break;
            }
            else
            {

                state.y++;
                ZombieLand::get().setFlag(state.x, state.y-1);
                state.point1.Y = state.point1.Y + 30;
                state.point2.Y = state.point2.Y + 30;
                state.point3.Y = state.point3.Y + 30;
                state.m_ActionsTaken++;
                break;
            }
    }
    ZombieLand::get().setWorld(state);
    state.m_ProgramCounter++;

}

void OpTestRandom::Execute(MachineState& state) throw()
{
    DebugOutput(state);
    //ZombieLand::get().setWorld(state);
    if(state.m_Test == false)
    {
        state.m_Test = true;
    }
    else
    {
        state.m_Test = false;
    }
    state.m_ProgramCounter++;
}

void OpAttack::Execute(MachineState& state) throw()
{
    DebugOutput(state);
    checkNeighboor(state);
}
2
Contributors
1
Reply
12
Views
4 Years
Discussion Span
Last Post by Moschops
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.