随机运动并不是那么随机

本文关键字:随机 并不是 运动 | 更新日期: 2023-09-27 18:17:16

我遇到了屏幕上对象随机移动的问题。它来回摆动,所以它的运动完全不是随机的。这只是一个小的c#控制台程序。

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;
                }
            }
        }
    }

基本上,我想要一些明显的和更随机的移动在边界内,而不是在控制台的底部来回移动。有人能给我指个正确的方向吗?

随机运动并不是那么随机

随机是好的,不知道为什么每个人都得到了。你的问题是你的初始行是40 -但你的控制台最初没有打开40行,当它是窗口。它在当前可见的最后一行绘制,可能是第20行。这就是控制台的行为方式。

如果您将控制台窗口最大化,或者将初始行设置为更低的值,例如10,您将看到它按照您的期望移动。

尝试在类上设置Random属性并在那里初始化它,而不是在move方法中反复初始化。像这样…

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;
                }
            }
        }
    }
}
相关文章: