Moving objects on a grid in C++ I have a code that works but is very badly written. The main is very verbose, it is just to test the class.
I want to make objects move on a chessboard (a coordinate grid). I've defined these object as Vehicles. They have a position (x,y) on the plane and a direction. Being forced to move only North-South-East-West possible directions could be


North: (0, 1)
South: (0, -1)
East: (1, 0)
West: (-1, 0)


Vehicles can proceed only step by step (x+1, y), (x, y+1), where x, y are integers. Two vehicles cannot stand in the same position. A vehicle maintains its direction while it is possible, that is while it encounters an occupied position.

So I have to create a void move function which has as arguments the positions of the nearby vehicles (North South East West). Such a function should:


Check if the vehicle can maintain its direction and, if it can, refresh the vehicle position with the new position;
If the cell is occupied, go for another direction, check if the cell in that direction is free...
If every cell surrounding the vehicle is occupied, stay still.




I wrote a code that accomplish point 1 but I have difficulties with point 2. In particular, I can't think about a smart way to change the Vehicle's direction. Here's my code so far, it works but surely this is not the best way to do what I want to do:   

 #include &lt;iostream&gt;
#include &lt;cmath&gt;

using namespace std;

class Vehicle {
private:
    int x, y;
    int vx, vy;
public:
    int getx() { return x;}
    int gety() { return y;}
    int getvx() { return vx;}
    int getvy() { return vy;}
void sety(int a) { y = a;}
void setx(int a) { x = a;}
void setvx(int a) { 
    if (a != 0) {   
        vx = a / abs(a);
    }else { vx = a;}
} 
void setvy(int a) { 
    if (a != 0) {   
        vy = a / abs(a);
    }else { vy = a;}
}

void move(Vehicle * v) {
    int big = 0;
    int per[8];
    per[0]=0;   // North
    per[1]=1;
    per[2]=0;   // South
    per[3]=-1;
    per[4]=1;   // West
    per[5]=0;
    per[6]=-1;  // East
    per[7]=0;   


    while(big&lt;8) {
        int cont=0;
        for(int i=0; i&lt;4; i++) {
            if (x+vx != v[i].getx() or y+vy != v[i].gety()) {
                cont ++;
            }
        }
        if(cont==4) {
            x = x + vx;
            y = y + vy;
            break;
        } else { 
            vx = per[big];
            vy = per[big+1];
            big = big + 2;
        }
    }
}

void print() {
    cout &lt;&lt; "x: " &lt;&lt; x &lt;&lt; endl;
    cout &lt;&lt; "y: " &lt;&lt; y &lt;&lt; endl;
    cout &lt;&lt; "vx: " &lt;&lt; vx &lt;&lt; endl;
    cout &lt;&lt; "vy: " &lt;&lt; vy &lt;&lt; endl;   
    cout &lt;&lt; endl;
}

};

int main() {
Vehicle x;  
x.setx(0);
x.sety(0);
x.setvx(0);
x.setvy(1);
x.print();

Vehicle v[4];

v[0].setx(0);
v[0].sety(1);
v[1].setx(-1);
v[1].sety(0);
v[2].setx(0);
v[2].sety(-1);
v[3].setx(1);
v[3].sety(0);

for(int i = 0; i&lt;4; i++) {
    v[i].setvx(0);
    v[i].setvy(1);
    v[i].print();
}

x.move(v);
x.print();

v[2].setx(0);
v[2].sety(-2);
v[2].print();

x.move(v);
x.print();


}
The first thing to do to clean up the code would be to make a

struct pos {
   int x;
   int y;
};


This will simplify the remaining of the code, making it easier to reason about the logic.

For example, right now it is possible to set the vehicle' velocity (direction) to {1,1}, which your problem statement doesn't allow.

You should then also define an equality comparison for your location. 

Your move function seems to have hard-coded the number of vehicles in play. Instead you should pass it a std::vector&lt;Vehicle&gt; by reference, and use size().

Once you fix these things, the logic of the move function is quite OK. It is quadratic in complexity, but that is OK for few cars. If you ever intend to increase the number of cars by orders of magnitude, there are two ways of improving the code:


Limit the world, and keep a grid where you mark if it is occupied or not.
Store the cars in a std::unordered_map, with their position as key. Then you can look up a position in constant time.




Edit: as suggested in the comment below, using namespace std should not be used. There are several really good reasons for not using it, and the only reason to use it is "I'm lazy". Look around other answers on Code Review for reasoning. This point comes up in about half of the C++ questions.