Assuming the numbers are as follows:
0 - North | 1 - East | 2 - South | 3 - West
Then run it through the algorithm
int reverse = (direction + 2) % 4;
should give the opposite direction.
Test it:
North: 0 + 2 = 2.2% 4 = 2: South
South: 2 + 2 = 4. 4% 4 = 0: North
East: 1 + 2 = 3. 3% 4 = 3: West
West: 3 + 2 = 5.5% 4 = 1: East
Success!
Turning left and right is as easy as adding or subtracting, and then with module 4, to make sure it goes back.
int left = (direction +3) % 4; int right = (direction + 1) % 4;
source share