Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ do while loop

I have a vector holding 10 items (all of the same class for simplicity call it 'a'). What I want to do is to check that 'A' isn't either a) hiding the walls or b) hiding another 'A'. I have a collisions function that does this.

The idea is simply to have this looping class go though and move 'A' to the next position, if that potion is causing a collision then it needs to give itself a new random position on the screen. Because the screen is small, there is a good chance that the element will be put onto of another one (or on top of the wall etc). The logic of the code works well in my head - but debugging the code the object just gets stuck in the loop, and stay in the same position. 'A' is supposed to move about the screen, but it stays still!

When I comment out the Do while loop, and move the 'MoveObject()' Function up the code works perfectly the 'A's are moving about the screen. It is just when I try and add the extra functionality to it is when it doesn't work.

    void Board::Loop(void){


        //Display the postion of that Element. 
        for (unsigned int i = 0; i <= 10; ++i){


            do {

                if (checkCollisions(i)==true){
                moveObject(i); 
                }
                else{
                    objects[i]->ResetPostion();

                }

            }
            while (checkCollisions(i) == false);
            objects[i]->SetPosition(objects[i]->getXDir(),objects[i]->getYDir());
        }

}

The class below is the collision detection. This I will expand later.

    bool Board::checkCollisions(int index){

    char boundry = map[objects[index]->getXDir()][objects[index]->getYDir()];

    //There has been no collisions - therefore don't change anything 
    if(boundry == SYMBOL_EMPTY){
        return false;
    }
    else{
        return true;

    }

}

Any help would be much appreciated. I will buy you a virtual beer :-)

Thanks

Edit:

ResetPostion -> this will give the element A a random position on the screen moveObject -> this will look at the direction of the object and adjust the x and Y cord's appropriately.

like image 852
KingJohnno Avatar asked Nov 04 '22 00:11

KingJohnno


1 Answers

I guess you need:

do { ...
... } while (checkCollisions(i)); 

Also, if you have 10 elements, then i = 0; i < 10; i++

And btw. don't write if (something == true), simply if (something) or if (!something)

like image 154
gcvt Avatar answered Nov 08 '22 05:11

gcvt