A small error about a collision in my program

I try to find out my problem within an hour.
I am going to do what is happening.

char trap = 'Q'; 
char character = 'L';

.... 
.Q..
..L.
....

when L moves up and Q moves to the right, they collide and the program ends. but:

....
.QL.
....
....

when L moves to the left and Q moves to the right, they do not collide in the same way as in the example above, which happens:

....
..Q.
....
....



here is my code. sorry for my bad english :(

check if there is "w", "a", "s" or "d":

void cave::move(int& x, int& y, char m, char unit)
{
                if ( m == 'W' || m == 'w' ) // if moves up
                {
                    floor[x][y] = tile;
                    x -= 1;
                    for ( unsigned short int x = 0; x < 3; x++ )
                    {
                            if ( floor[x][y] == wall && floor[x][y] == trap[x] )
                            {
                                x += 1;
                                trapsMove();
                        }
                        }
                        floor[x][y] = unit;
                }

                else if ( m == 'A' || m == 'a' ) // if moves to left
                {
                            floor[x][y] = tile;
                            y -= 1;
                        for ( unsigned short int x = 0; x < 3; x++ )
                        {
                            if ( floor[x][y] == wall && floor[x][y] == trap [x] )        
                            {
                                   y += 1;
                                   trapsMove();
                                }
                        }
                            floor[x][y] = unit;
                }

                else if ( m == 'S' || m == 's' ) // if moves down
                {
                        floor[x][y] = tile;
                            x += 1;
                            for ( unsigned short int x = 0; x < 3; x++ )
                        {
                                if ( floor[x][y] == wall && floor[x][y] == trap[x] )
                                {
                                    x -= 1;
                                    trapsMove();
                                }
                            }
                            floor[x][y] = unit;
                }
               else if ( m == 'D' || m == 'd' )  // if moves to right
               {
                        floor[x][y] = tile;
                            y += 1;
                            for ( unsigned short int x = 0; x < 3; x++ )
                            {
                                if ( floor[x][y] == wall && floor[x][y] == trapx] )
                                {
                                    y -= 1;
                                    trapsMove();
                                }
                            }
                            floor[x][y] = unit;
               }
               else 
                        control(); 
               return;
 }

ai moves the function

void cave::trapsMove() 
{
                    int r[3]; // each index will hold the movement of traps 
                    for ( unsigned short int x = 0; x < 3; x++ )
                    {
                            r[x] = rand() % 4 + 1;
                            if ( r[x] == 1 ) // moves up
                                move(traps_positionX[x],traps_positionY[x],'w',trap[x]);
                            else if ( r[x] == 2 ) // moves to left
                                move(traps_positionX[x],traps_positionY[x],'a',trap[x]);
                            else if ( r[x] == 3 ) // moves down
                                move(traps_positionX[x],traps_positionY[x],'s',trap[x]);
                            else if ( r[x] == 4 ) // moves to right
                                move(traps_positionX[x],traps_positionY[x],'d',trap[x]);
                    }
                    return;
}

check if collide

bool cave::collision()
{
                    for ( unsigned short int x = 0; x < 3; x++ )
                    {
                            if ( floor[character_positionX][character_positionY] == trap[x] )
                                return true;
                    }
                    return false;
}
+4
source share
1 answer

, , , Trap Player . (Q) (L) .

:

(1).QL.

(2).L..//L Q

(3).Q.//Q L .


, , . , , .

. , :

(1)

(2)

(2a) , ( , || & &)

< - (2a) - , .

(3)

(4)

(5) (1)

, :

(1)

(2)

(2a)

(3)

(3a)

(6) (1)


, . , move() char 'w' (...) 'x' 'y'. ,

move(...) {
    if((m=='w')||(m=='W')) { y = y+1 }
    else if((m=='a')||(m=='A')) { x = x-1 } // same for SD

    if(floor[x][y] != wall) {
        // set new position to object, if it can't move, just don't set it
    }
}

, for-loop .

+1

All Articles