2

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;
        }
    }
}
Richard Slater
  • 6,313
  • 4
  • 53
  • 81
jim
  • 39
  • 3
  • At a guess, your ghost changes direction, and then checks if it is next to a wall again (which it definitely still is) and then changes direction again before it's even moved – Alfie Goodacre Apr 14 '16 at 13:57
  • 3
    You are using OOP language, so use it! You should define your enum (or classes) for 4 different directions, and just update them in appropriate way. With such code you will encounter multiple problems just because of copy-paste and unclear logic. – Alex Zhukovskiy Apr 14 '16 at 13:59
  • strategy: fill an array with possibile(valid) movements. then generate a random index and extract corrispondent move from array. – Exceptyon Apr 14 '16 at 14:14

0 Answers0