use a 2D array, with each element of the array saying which directions are open to be moved into. Since there are only four, it will be easy.
- Code: Select all
// place in global code
#define NORTH 1
#define SOUTH 2
#define EAST 4
#define WEST 8
See how i doubled each time? that allows no conflict and will let us pic from east and south, for example.
So maybe you have a square where you can go north or south.
- Code: Select all
if(CopMap[cop.x,cop.y] == NORTH+SOUTH) // NORTH+SOUTH actually equals 3
{
do stuff here
}
So the possible moves are...
NORTH+SOUTH+EAST+WEST == 15
SOUTH+EAST+WEST == 14
NORTH+WEST+EAST == 13
WEST+EAST == 12
NORTH+SOUTH+WEST == 11
SOUTH+WEST = 10
NORTH+WEST == 9
WEST == 8
NORTH+SOUTH+EAST == 7
SOUTH+EAST == 6
NORTH+EAST == 5
EAST == 4
NORTH+SOUTH == 3
SOUTH == 2
NORTH == 1
and technically, no move(0), which should be impossible in your game. With 15 combinations, each is represented by a number in an array, and no combination of possible moves matches any others.
From there, pick a random number from 1 to 4, and use that to decide to head NSEW, if its possible from that square. Otherwise, just pick another number.