Search code examples
c#oopdebugginginfinite-looprandom-walk

Why is my C# class code in an infinite loop? What can I do to fix it?


namespace Random_walk_2d
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Point C = new Point(2, 5);

            C.Move('n');
            Point.Print(C);
        }
    }

    public class Point
    {
        public int x;
        public int y;

        readonly public Dictionary<char, Point> directions = new Dictionary<char, Point>()
        {
            { 'e', new Point(1, 0) },
            { 'n', new Point(0, 1) },
            { 'w', new Point(-1, 0) },
            { 's', new Point(0, -1) },
        };

        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        public static Point operator +(Point A, Point B)
        {
            return new Point(A.x + B.x, A.y + B.y);
        }

        public static void Print(Point A)
        {
            Console.WriteLine($"({A.x}, {A.y})");
        }

        public void Move(char dir)
        {
            Point displacement = directions[dir];
            this.x += displacement.x;
            this.y += displacement.y;
        }

    }
}

I can see that the problem lies somewhere within the dictionary or the Move() method, but what I don't understand is what's actually going on under the hood? What's causing the infinite loop to occur?

And how can I solve the issue?

My friend suggested me to use arrays of 2 ints instead of Points in the dictionary, but... that kinda defeats the purpose of the whole class. Why use Points if I can just use 2 int arrays? I want a solution that doesn't do that.

If you have any other kind of advice, do let me know.

EDIT: I should've mentioned this right away, but when I run it, I get an infinite amount of at Random_walk_2d.Point..ctor(Int32, Int32) messages in the console.

EDIT2: I'm just experimenting with classes. For this project I wanted to create a random walk on a 2d grid. A 2d grid can be nicely modeled by a class with 2d point objects. I need the Move() method to move 1 unit to the right(east = 'e'), up(north = 'n') and so on.


Solution

  • the problem is stack overflow, this done here

    public class Point {
        public int x;
        public int y;
    
        readonly public Dictionary<char, Point> directions = new Dictionary<char, Point>()
        {
            { 'e', new Point(1, 0) },
            { 'n', new Point(0, 1) },
            { 'w', new Point(-1, 0) },
            { 's', new Point(0, -1) },
        };
    

    when you create a Point, that creates a 'directions' member, this in turn creates 4 new Points, each of those new Points creates its own 'directions' each one creates 4 Points .....

    Its not clear what you are trying to do but this fixed it

    public class Point {
        public int x;
        public int y;
    
        static readonly public Dictionary<char, Point> directions = new Dictionary<char, Point>()
        {
            { 'e', new Point(1, 0) },
            { 'n', new Point(0, 1) },
            { 'w', new Point(-1, 0) },
            { 's', new Point(0, -1) },
        };
    

    By declaring 'directions' static there is only one for the whole Point class, maybe thats what you want

    EDIT - yes this is what you want the 'directions' map is effectively a constant, so static works fine