I'm trying to implement simple algorithm in which the "player" goes up/down and left/right, until he finally reaches exit. But I have problem, that it goes to few directions a then it stucks.
public override bool GetNextPosition(out int nx, out int ny)
{
// "return" new position based on _angle and X,Y
// wall-following
ny = Y;
nx = X;
switch(_angle)
{
case 0: ny++; break;
case 90: nx--; break;
case 180: ny--; break;
case 270: nx++; break;
}
return true;
}
public override void NextPositionUnreachable()
{
// Didn't moved
OldX = X;
OldY = Y;
_angle += 90;
if (_angle > 360) _angle = _angle - 360;
}
code inside main loop:
int nx, ny;
// check if dwarf is not already on desired pos.
if (!dwarfs[i].GetNextPosition(out nx, out ny)) continue;
// check for BlockType
var bc = maze.GetBlockType(nx, ny);
switch (bc)
{
// if pos. == finish || "space" - go there
case eBlockType.Space:
case eBlockType.Finish: dwarfs[i].MoveToPosition(nx, ny); continue;
default:
// if pos. unreachable - rotate x axis and repeat procedure
dwarfs[i].NextPositionUnreachable();
continue;
}
Thanks in advance for any help.