Exemple #1
0
 public bool Touches(Pixel pixel)
 {
     return((this.XPos == pixel.XPos) && (this.YPos == pixel.YPos));
 }
Exemple #2
0
 public bool WasEaten(Pixel pixel)
 {
     return((this.XPos == pixel.XPos) && (this.YPos == pixel.YPos));
 }
Exemple #3
0
        static void Main(string[] args)
        {
            WindowWidth  = 32;
            WindowHeight = 16;
            SetBufferSize(WindowWidth, WindowHeight);

            int startingLength = 5;

            var head = new Pixel(WindowWidth / 2, WindowHeight / 2, ConsoleColor.Green);
            var body = new List <Pixel> ();

            var berry = new Berry();

            Direction currentMovement = Direction.Right;

            Border.Draw();
            Intro.Draw();

            while (!Console.KeyAvailable)
            {
            }

            Intro.Clear();
            Border.Draw();
            berry.Spawn();

            while (true)
            {
                var headOld = new Pixel(head.XPos, head.YPos);

                var sw = Stopwatch.StartNew();
                while (sw.ElapsedMilliseconds <= 100)
                {
                    currentMovement = ReadMovement(currentMovement);
                }

                body.Add(headOld);
                body.ForEach(x => x.Clear());

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

                case Direction.Down:
                    head.MoveDown();
                    break;

                case Direction.Left:
                    head.MoveLeft();
                    break;

                case Direction.Right:
                    head.MoveRight();
                    break;
                }

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

                headOld.Clear();
                body.ForEach(x => x.Draw());
                head.Draw();

                if (berry.WasEaten(head))
                {
                    Score.Increase();
                    berry.Spawn();
                }

                Score.Draw();

                if (body.Any(x => x.Touches(head) == true))
                {
                    break;
                }

                if (head.XPos == 0 || head.XPos == WindowWidth - 1 || head.YPos == 0 || head.YPos == WindowHeight - 1)
                {
                    break;
                }
            }

            Score.Gameover();

            head.Explode();

            SetCursorPosition(1, 1);
            ReadKey();
        }
Exemple #4
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);
            }
        }
Exemple #5
0
 public void DrawBerry()
 {
     Pixel.DrawPixel(m_berry.Position);
 }
Exemple #6
0
 public Pixel(Pixel pix)
 {
     x          = pix.x;
     y          = pix.y;
     PixelColor = pix.PixelColor;
 }
Exemple #7
0
 public static void DrawPixel(Pixel pixel)
 {
     SetCursorPosition(pixel.XPos, pixel.YPos);
     ForegroundColor = pixel.ScreenColor;
     Write("■");
 }