示例#1
0
        static void Main()
        {
            Snake_Q_RL.QRLModel Model = new Snake_Q_RL.QRLModel(0.8, 0.5, 0.8, 4, 4, 4, 4, 4, 4);
            int i = 0;

            while (i < 5000)
            {
                WriteLine(i);
                Game(Model, false);
                i++;
            }

            while (true)
            {
                Game(Model, true);
            }
        }
示例#2
0
        static void Game(Snake_Q_RL.QRLModel qRL, bool draw)
        {
            WindowHeight = 16;
            WindowWidth  = 32;

            var rand = new Random();

            var score = 0;

            var head  = new Pixel(WindowWidth / 2, WindowHeight / 2, ConsoleColor.Red);
            var berry = new Pixel(rand.Next(1, WindowWidth - 2), rand.Next(1, WindowHeight - 2), ConsoleColor.Cyan);

            var body = new List <Pixel>();

            var currentMovement = Direction.Right;

            var gameover = false;

            int up;
            int down;
            int left;
            int right;
            int dir;
            int rdir;

            while (true)
            {
                up    = Scale(head.YPos);
                down  = Scale(WindowHeight - head.YPos);
                left  = Scale(head.XPos);
                right = Scale(WindowWidth - head.XPos);
                dir   = DirToBerry(head.XPos, head.YPos, berry.XPos, berry.YPos);
                rdir  = DirToNum(currentMovement);
                int reward = -1;

                int chosenDir = qRL.GetBestaction(up, down, left, right, dir, rdir);
                currentMovement = ReadMovement(currentMovement, chosenDir);

                body.Add(new Pixel(head.XPos, head.YPos, ConsoleColor.Green));

                switch (currentMovement)
                {
                case Direction.Up:
                    head.YPos--;
                    break;

                case Direction.Down:
                    head.YPos++;
                    break;

                case Direction.Left:
                    head.XPos--;
                    break;

                case Direction.Right:
                    head.XPos++;
                    break;
                }



                gameover |= (head.XPos == WindowWidth - 1 || head.XPos == 0 || head.YPos == WindowHeight - 1 || head.YPos == 0);

                //DrawBorder();

                if (berry.XPos == head.XPos && berry.YPos == head.YPos)
                {
                    //score++;
                    reward = 50;
                    berry  = new Pixel(rand.Next(1, WindowWidth - 2), rand.Next(1, WindowHeight - 2), ConsoleColor.Cyan);
                }

                if (draw)
                {
                    Clear();
                }
                for (int i = 0; i < body.Count; i++)
                {
                    if (draw)
                    {
                        DrawPixel(body[i]);
                    }

                    gameover |= (body[i].XPos == head.XPos && body[i].YPos == head.YPos);
                }

                int nup    = Scale(head.YPos);
                int ndown  = Scale(WindowHeight - head.YPos);
                int nleft  = Scale(head.XPos);
                int nright = Scale(WindowWidth - head.XPos);
                int ndir   = DirToBerry(head.XPos, head.YPos, berry.XPos, berry.YPos);
                int nrdir  = DirToNum(currentMovement);
                if (gameover)
                {
                    reward = -50;
                    qRL.UpdateQTable(up, down, left, right, dir, rdir, chosenDir, nup, ndown, nleft, nright, ndir, nrdir, reward);
                    break;
                }
                qRL.UpdateQTable(up, down, left, right, dir, rdir, chosenDir, nup, ndown, nleft, nright, ndir, nrdir, reward);

                if (draw)
                {
                    DrawPixel(head);
                    DrawPixel(berry);
                }


                if (body.Count > score)
                {
                    body.RemoveAt(0);
                }

                if (draw)
                {
                    System.Threading.Thread.Sleep(10);
                }
            }

            if (draw)
            {
                SetCursorPosition(WindowWidth / 5, WindowHeight / 2);
                WriteLine($"Game over, Score: {score}");
                SetCursorPosition(WindowWidth / 5, WindowHeight / 2 + 1);
            }
        }