示例#1
0
            public string GetPathString(out int steps)
            {
                // Find the starting line
                int x = 0;

                for (; ; x++)
                {
                    if (Values[x, 0].Type is DiagramCellType.Pathable)
                    {
                        break;
                    }
                }

                string result = "";

                steps = 1;

                Location2D location  = (x, 0);
                var        direction = new DirectionalLocation(Direction.Down, invertY: true);

                while (true)
                {
                    // Attempt going forward, then attempt turning left, then turning right from original
                    // If neither works; we've found the end
                    Location2D next;
                    if (!AttemptNextLocation())
                    {
                        direction.TurnLeft();
                        if (!AttemptNextLocation())
                        {
                            direction.TurnRight(2);
                            if (!AttemptNextLocation())
                            {
                                return(result);
                            }
                        }
                    }

                    steps++;
                    location = next;
                    char letter = this[location].Letter;
                    if (letter != default)
                    {
                        result += letter;
                    }

                    bool AttemptNextLocation()
                    {
                        next = location + direction.LocationOffset;
                        return(IsValidLocation(next) && this[next].Type is DiagramCellType.Pathable);
                    }
                }
            }
示例#2
0
            private void RunInstructions()
            {
                var current          = Location2D.Zero;
                var currentDirection = new DirectionalLocation(Direction.Up);
                var visitedLocations = new HashSet <Location2D> {
                    current
                };
                bool hasRevisited = false;

                foreach (var instruction in instructions)
                {
                    switch (instruction.Direction)
                    {
                    case Direction.Left:
                        currentDirection.TurnLeft();
                        break;

                    case Direction.Right:
                        currentDirection.TurnRight();
                        break;
                    }
                    for (int i = 1; i <= instruction.Forward; i++)
                    {
                        current += currentDirection.LocationOffset;

                        if (hasRevisited)
                        {
                            continue;
                        }

                        hasRevisited = !visitedLocations.Add(current);
                        if (hasRevisited)
                        {
                            FirstRevisitedLocation = current;
                        }
                    }
                }

                EndingLocation = current;
            }