Quote:
ii have tried modulus ,but its not working on edges
You mention only "..., but ist not working in edges", but you don't describe what is not working i.e. what you expect and what you get.
My guess is, you don't get the right value for 0->3, because you make something like this:
(0 - 1) % 4
and you expect 3, but get -1.
Why -1 % 4 == -1 you can find in the literature, i.e.
Modulo operation - Wikipedia, the free encyclopedia[
^]
For you the trick to correct this is:
const int cRange= 4;
int position;
int direction;
position= (Position + direction + cRange) % cRange;
Finally for Position == 0 and direction is -1:
(0 - 1 + 4) % 4 => 3
or Position = 3 and direction is + 1:
(3 + 1 + 4) % 4 => 0
I hope it helps.