Ghosts have to move in one compass direction (N, S, E or W) until they reach an intersection, at which point they can turn left or right; and they will also change direction if they hit obstructions / walls.
CellType == 'x' is my wall
int x = rnd.Next(0, 2);
int y = rnd.Next(0, 2);
Here's my attempt so far but it keeps putting the ghost into walls
if (x == 0) // this is the random positon 0 which faces the ghost down
{
if (cells[yposg + 1, xposg].CellType == 'o' || cells[yposg + 1, xposg].CellType == '.' || cells[yposg + 1, xposg].CellType == '!')
{
gb.Ghost.yposghost += 20;
if (cells[yposg, xposg - 1].CellType == 'x')
{
y = 1|0;
}
}
if (y == 0) // this is the random positon 3 which faces the ghost right
{
if (cells[yposg, xposg ].CellType == 'x' )
{
y = 1;
}
if (cells[yposg, xposg - 1].CellType == 'o' || cells[yposg, xposg - 1].CellType == '.' || cells[yposg, xposg - 1].CellType == '!')
{
gb.Ghost.xposghost -= 20;
}
}
else if (y == 1) // this is the random positon 2 which faces the ghost left
{
if (cells[yposg, xposg ].CellType == 'x')
{
y = 0;
}
if (cells[yposg, xposg + 1].CellType == 'o' || cells[yposg, xposg + 1].CellType == '.' || cells[yposg, xposg + 1].CellType == '!')
{
gb.Ghost.xposghost += 20;
}
}
}
else if (x ==1) // this is the random positon 1 which faces the ghost up
{
if (cells[yposg , xposg].CellType == 'o' || cells[yposg - 1, xposg].CellType == '.' || cells[yposg - 1, xposg].CellType == '!')
{
gb.Ghost.yposghost -= 20;
if (cells[yposg, xposg - 1].CellType == 'x')
{
y = 1 | 0;
}
}
if (y == 0) // this is the random positon 3 which faces the ghost right
{
if (cells[yposg, xposg ].CellType == 'x')
{
y = 1;
}
if (cells[yposg, xposg - 1].CellType == 'o' || cells[yposg, xposg - 1].CellType == '.' || cells[yposg, xposg - 1].CellType == '!')
{
gb.Ghost.xposghost -= 20;
}
}
else if (y == 1) // this is the random positon 2 which faces the ghost left
{
if (cells[yposg, xposg ].CellType == 'x')
{
y = 0;
}
if (cells[yposg, xposg + 1].CellType == 'o' || cells[yposg, xposg + 1].CellType == '.' || cells[yposg, xposg + 1].CellType == '!')
{
gb.Ghost.xposghost += 20;
}
}
}