C ++ do while loop

I have a vector containing 10 elements (all the same classes are called "a" for simplicity). What I want to do is to verify that "A" is not a) a concealment of walls, or b) a concealment of another "A". I have a collision function that does this.

The idea is to simply start this cycle, and move "A" to the next position, if this potion causes a collision, then it needs to be given a new random position on the screen. Since the screen is small, there is a good chance that the element will be placed in another (or over the wall, etc.). The logic of the code works well in my head - but debugging the code, which the object just gets stuck in the loop, and remains in the same position. "A" should move around the screen, but it remains stationary!

When I comment on the Do while loop and move the MoveObject () function, the code works fine, "A moves around the screen. It is simple when I try to add additional functions when it does not 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 next class is collision detection. This I will tell 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 greatly appreciated. I will buy you a virtual beer :-)

thank

Edit:

ResetPostion → this will give element A a random position on the screen moveObject → it will look at the direction of the object and correctly adjust the cords x and Y.

+5
source share
3 answers

I think you need:

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

In addition, if you have 10 items, then i = 0; i < 10; i++

And by the way. don't write if (something == true)just if (something)orif (!something)

+2
source
for (unsigned int i = 0; i <= 10; ++i){

wrong because loop for eleven elements use

for (unsigned int i = 0; i < 10; ++i){

instead.

, " ", , .

0

, . , , , . ( , )

do/while() - , , , .: do... while vs while

while if/else, for. do , while for , , . ( do , while(true)/break, )

, , :

void Board::Loop(void) {
    //Display the postion of that Element. 
    for (unsigned int i = 0; i < 10; ++i) {
        while(IsGoingToCollide(i))  //check is first, do while doesn't make sense
            objects[i]->ResetPosition();
        moveObject(i);   //same as ->SetPosition(XDir, YDir)?
                         //either explain difference or remove one or the other
    }
}

:

bool Board::checkCollisions(int index) {

:

// returns true if moving to next position (based on inertia) will 
// cause overlap with any other object or structure current location
bool Board::IsGoingToCollide(int index) {

checkCollisions() :

// returns true if there is no overlap between this object's
// current location and any other object or structure current location
bool Board::DidntCollide(int index) {

Final note: double-check what ->ResetPosition()places objects inside .

0
source

All Articles