-7

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.

John
  • 3
  • 4
  • Are you have the labyrinth as a jaggen (or 2d) array, say `int[][]` where `1` is a wall, `0` is a free path, `2` goal, `3` starting point? If not what is your labyrinth representation? – Dmitry Bychenko Apr 21 '22 at 14:37
  • Observation: You have `if (_angle > 360) _angle = _angle - 360;` inside `NextPositionUnreachable()`. This means that when `_angle == 360` it will not be reset to `_angle = 0`, and if `_angle == 360` in `GetNextPosition()`, neither `nx` nor `ny` will be updated there. – Astrid E. Apr 21 '22 at 14:57
  • Sorry for not uncluding the maze is char[,] where '#' for wall, ' ' for space, 'S' for start, 'F' for finish. – John Apr 21 '22 at 20:49
  • Astrid this isn't the problem _angle is reseting as it should – John Apr 21 '22 at 20:50
  • Have you tried debugging your code, to see if `_angle`, `nx` and `ny` all change as you expect them to when you run it? – Astrid E. Apr 23 '22 at 05:54

1 Answers1

0

I think the problem is that the angle is not reduced when you hit a available space-tile. I.e. if you are going down a corridor and need to backtrack, you will start going up, and continue going up, when you should be trying to go right.

This also looks incorrect

if (_angle > 360) _angle = _angle - 360;

it should probably be _angle == 360, otherwise the angle can never reach zero.

I would highly recommend using appropriate types, like a Point or Vector2i type to describe the position, and using an enum to describe direction. Having a type to combine both would probably also be a good idea. Using separate fields all over the place is an invitation to mess something up.

JonasH
  • 28,608
  • 2
  • 10
  • 23