I've made this Supaplex-like game, but I have a problem.
I want to have more levels, so I've made that when moving to the next level, it clears the vectors with all the objects, and putting new objects into.

this is my code for moving to level 2:

void start_level(HWND m_hwnd){
    grasses.clear();
    persons.clear();
    bombs.clear();
    cinfos.clear();
    level++;
    switch(level){
        case 1:
            persons.push_back(man(1,2,m_hwnd,"person",true));
            grasses.push_back(grass(0,1,false));
            grasses.push_back(grass(1,0,false));
            grasses.push_back(grass(1,1,false));
            grasses.push_back(grass(2,0,false));
            grasses.push_back(grass(4,1,false));
            grasses.push_back(grass(4,2,false));
            grasses.push_back(grass(2,3,false));
            grasses.push_back(grass(1,4,false));
            grasses.push_back(grass(2,4,false));
            for(int i = 0; i <= 4; i++){
                grasses.push_back(grass(3,i,false));
            }
            bombs.push_back(bomb(0,2));
            bombs.push_back(bomb(0,4));
            bombs.push_back(bomb(1,3));
            bombs.push_back(bomb(2,2));
            bombs.push_back(bomb(2,1));
            bombs.push_back(bomb(4,3));
            bombs.push_back(bomb(4,0));
            cinfos.push_back(cinfo(0,0));
            cinfos.push_back(cinfo(0,3));
            cinfos.push_back(cinfo(4,4));
            break;
    }
    InvalidateRect(m_hwnd,NULL,TRUE);
}

It's working fine, except that it creates objects at places i haven't told to. For example:

cinfos.push_back(cinfo(0,0));
cinfos.push_back(cinfo(0,3));
cinfos.push_back(cinfo(4,4));

this part creates 3 cinfos (an object in the game :P), it's working fine when i'm only creating 2, but when creating 3, it's also creaing one at (4,3) and i really don't know why :/

And sometimes it's creating a bomb object at (0,0)

I hope that anybody can help me!

Dani AI

Generated

Symptoms described by — extra objects appearing at coordinates not explicitly pushed (an extra cinfo at (4,3) when three were added, occasional bomb at (0,0)) — most often come from either incorrect object state/copying or memory corruption elsewhere (map/paint indexing, out‑of‑bounds writes, dangling pointers to vector elements). The following checklist isolates the cause and fixes the usual culprits.

Add lightweight diagnostics inside the classes and right after level setup to verify what actually gets created and copied. Example tracing additions (adapt member names to the real classes):

cinfo::cinfo(int r, int c) : row(r), col(c) {
    std::cerr << "cinfo ctor (" << row << ',' << col << ")\n";
}
cinfo::cinfo(const cinfo& o) : row(o.row), col(o.col) {
    std::cerr << "cinfo copy from (" << o.row << ',' << o.col << ")\n";
}

and after filling the vector:

for (size_t i = 0; i < cinfos.size(); ++i)
    std::cout << "cinfos[" << i << "] = (" << cinfos[i].row << ',' << cinfos[i].col << ")\n";

If logs show only the three expected entries but rendering still paints an extra object, focus on the rendering/mapping code: confirm the draw loop uses the same coordinates system and that any grid arrays are bounded (check indices and width/height math).

Check for these concrete causes and remedies:

  • Uninitialized members: give all members default values in constructors.
  • Classes owning raw pointers or resources: implement correct copy/move semantics (rule of three/five) or switch to std::string/smart pointers.
  • Dangling pointers to vector elements: avoid storing addresses of vector elements across reallocation (use indices or stable containers, or call reserve()).
  • Buffer overruns in map arrays: run with AddressSanitizer/Valgrind or MSVC debug heap to catch writes outside arrays.

As hinted, encoding levels as data (arrays of strings or a resource file) makes setup simpler and less error prone for more levels.

Recommended Answers

All 2 Replies

I don't know the answer to your question but there's got to be a much simplier way to code that switch statement. What are you going to do if someone wants to be a level 255? Lets see -- 20 lines of code for level 2 I guess will mean over 5,000 lines of code for level 255. Not a very efficient program.

that's no problem because there's only 3 levels :P
it's my first C++ game ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.