0

I'm having an issue with some random movement of an object on the screen. It kind of ticks back and forth so it's movement isn't random at all. This is just a small C# console program.

namespace MorgSimulator
{
    class Program
    {
        static void Main(string[] args)
        {
            Morg A = new MorgA();
            A.MovingTime();

            Console.ReadKey();
        }
    }
   class Morg
    {
        public Morg()
        {}

        protected MoveBehavior moveBehavior;

        public void MovingTime()
        {
            moveBehavior.move();
        }
 class MorgA : Morg
    {
        public MorgA()
        {
            moveBehavior = new Ooze();
        }
interface MoveBehavior
    {
        void move();
    }
class Ooze : MoveBehavior
    {
        public void move()
        {
            int row = 40, col = 25;
            Console.CursorVisible = false;
            Console.SetCursorPosition(col, row);

            int direction = 0;
            Random r = new Random();

            for (int i = 0; i < 25; i++)   // count of movement
            {
                Console.Write("<(._.)>");
                System.Threading.Thread.Sleep(100);
                Console.Clear();

                direction = r.Next(5);

                while (direction == 0)
                    direction = r.Next(5);

                switch (direction)
                {
                    case 1:
                        if (row + 1 >= 80)
                            row = 0;
                        Console.SetCursorPosition(col, row++);
                        break;
                    case 2:
                        if (row - 1 <= 0)
                            row = 79;
                        Console.SetCursorPosition(col, row--);
                        break;
                    case 3:
                        if (col + 1 >= 50)
                            col = 0;
                        Console.SetCursorPosition(col++, row);
                        break;
                    case 4:
                        if (col - 1 <= 0)
                            col = 49;
                        Console.SetCursorPosition(col--, row);
                        break;
                }
            }
        }
    }

Basically, I want some obvious and more random movement within the bounds instead of shifting back and forth at the bottom of the console. Can someone point me in the right direction?

  • Have you debugged your code to make sure that your `r.Next(5)` is not returning the exact same number? – John Odom Oct 12 '15 at 21:24
  • 4
    You must move the Random object creation out of the method so you don't create it over and over again, feeding it with the same seed. Make it a field of your class so you only use one. – Hans Passant Oct 12 '15 at 21:24
  • Try also getting a random distance factor in addition to the direction. – Robert McKee Oct 12 '15 at 21:26

2 Answers2

1

The Random is fine, not sure why everyone is getting at that. Your problem is your initial row is 40 - but your console doesn't initially open with 40 rows when it is windowed. So it draws it on the last row that is currently visible, probably about row 20. That's how the console behaves.

If you maximize your console window, or set your initial row to something lower, like 10, you'll see it moving as you expect.

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
  • Wow, thank you very much that actually helped it a lot! Thank you very much! – user3269626 Oct 12 '15 at 21:31
  • Cool, glad you found a problem! The use of random in the original source is a second problem. That's probably why myself and a few others instantly assumed it was the (only) problem. See http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number for example. – Trevor Ash Oct 13 '15 at 01:01
0

Try making Random a property on the class and initializing it once there, instead of over and over again in the move method. Something like this...

namespace MorgSimulator
{
    class Program
    {
        static void Main(string[] args)
        {
            Morg A = new MorgA();
            A.MovingTime();

            Console.ReadKey();
        }
    }

    class Morg
    {
        public Morg()
        {}

        protected MoveBehavior moveBehavior;

        public void MovingTime()
        {
            moveBehavior.move();
        }
    }

    class MorgA : Morg
    {
        public MorgA()
        {
            moveBehavior = new Ooze();
        }
    }

    interface MoveBehavior
    {
        void move();
    }

    class Ooze : MoveBehavior
    {
        private readonly Random randomizer;

        public Ooze()
        {
            this.randomizer = new Random();
        }

        public void move()
        {
            int row = 40, col = 25;
            Console.CursorVisible = false;
            Console.SetCursorPosition(col, row);

            int direction = 0;

            for (int i = 0; i < 25; i++)   // count of movement
            {
                Console.Write("<(._.)>");
                System.Threading.Thread.Sleep(100);
                Console.Clear();

                direction = this.randomizer.Next(5);

                while (direction == 0)
                    direction = this.randomizer.Next(5);

                switch (direction)
                {
                    case 1:
                        if (row + 1 >= 80)
                            row = 0;
                        Console.SetCursorPosition(col, row++);
                        break;
                    case 2:
                        if (row - 1 <= 0)
                            row = 79;
                        Console.SetCursorPosition(col, row--);
                        break;
                    case 3:
                        if (col + 1 >= 50)
                            col = 0;
                        Console.SetCursorPosition(col++, row);
                        break;
                    case 4:
                        if (col - 1 <= 0)
                            col = 49;
                        Console.SetCursorPosition(col--, row);
                        break;
                }
            }
        }
    }
}
Trevor Ash
  • 623
  • 4
  • 8