public void Start(ISnake snake)
        {
            this.Snake = snake;

            this.SpawnApple();

            this.ResetObstructions();

            while (true)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKey key = Console.ReadKey(true).Key;

                    switch (key)
                    {
                    case ConsoleKey.LeftArrow:
                        Snake.CurrentDirection = Direction.Left;
                        break;

                    case ConsoleKey.RightArrow:
                        Snake.CurrentDirection = Direction.Right;
                        break;

                    case ConsoleKey.UpArrow:
                        Snake.CurrentDirection = Direction.Up;
                        break;

                    case ConsoleKey.DownArrow:
                        Snake.CurrentDirection = Direction.Down;
                        break;

                    case ConsoleKey.Spacebar:
                        Pause();
                        break;
                    }
                }

                Snake.Move();

                if (Snake.Head.Equals(AppleLocation))
                {
                    SpawnApple();
                    AppleCounter++;
                    Snake.Extend++;

                    ResetObstructions();
                }

                OutputWriter.Draw(this);

                if (Snake.Dead)
                {
                    OutputWriter.DisplayDeathMessage(this);
                    break;
                }

                Thread.Sleep(300);
            }
        }
Esempio n. 2
0
        public void Start(ISnake snake)
        {
            this.Snake = snake;

            this.SpawnApple();

            while (true)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKey key = Console.ReadKey(true).Key;

                    switch (key)
                    {
                    case ConsoleKey.LeftArrow:
                        appleDirection = Direction.Left;
                        break;

                    case ConsoleKey.RightArrow:
                        appleDirection = Direction.Right;
                        break;

                    case ConsoleKey.UpArrow:
                        appleDirection = Direction.Up;
                        break;

                    case ConsoleKey.DownArrow:
                        appleDirection = Direction.Down;
                        break;

                    case ConsoleKey.Spacebar:
                        Pause();
                        break;
                    }
                }

                if (random.Next(5) == 0)
                {
                    int nextDirectionId = random.Next(2);

                    if (nextDirectionId == 0)
                    {
                        if (Snake.CurrentDirection == Direction.Up)
                        {
                            Snake.CurrentDirection = Direction.Right;
                        }
                        else if (Snake.CurrentDirection == Direction.Right)
                        {
                            Snake.CurrentDirection = Direction.Down;
                        }
                        else if (Snake.CurrentDirection == Direction.Down)
                        {
                            Snake.CurrentDirection = Direction.Left;
                        }
                        else if (Snake.CurrentDirection == Direction.Left)
                        {
                            Snake.CurrentDirection = Direction.Up;
                        }
                    }
                    else
                    {
                        if (Snake.CurrentDirection == Direction.Up)
                        {
                            Snake.CurrentDirection = Direction.Left;
                        }
                        else if (Snake.CurrentDirection == Direction.Right)
                        {
                            Snake.CurrentDirection = Direction.Up;
                        }
                        else if (Snake.CurrentDirection == Direction.Down)
                        {
                            Snake.CurrentDirection = Direction.Right;
                        }
                        else if (Snake.CurrentDirection == Direction.Left)
                        {
                            Snake.CurrentDirection = Direction.Down;
                        }
                    }
                }

                int currX = AppleLocation.X;
                int currY = AppleLocation.Y;

                int targetX;
                int targetY;

                switch (appleDirection)
                {
                case Direction.Up:
                    targetX = currX;
                    targetY = currY - 1;
                    break;

                case Direction.Down:
                    targetX = currX;
                    targetY = currY + 1;
                    break;

                case Direction.Left:
                    targetX = currX - 1;
                    targetY = currY;
                    break;

                case Direction.Right:
                    targetX = currX + 1;
                    targetY = currY;
                    break;

                default:
                    throw new InvalidOperationException("Invalid direction");
                }

                if (targetX < 0)
                {
                    targetX = Width - 1;
                }
                else if (targetX == Width)
                {
                    targetX = 0;
                }
                else if (targetY < 0)
                {
                    targetY = Height - 1;
                }
                else if (targetY == Height)
                {
                    targetY = 0;
                }

                AppleLocation = new Position(targetX, targetY);

                if (Snake.Head.Equals(AppleLocation))
                {
                    SpawnApple();
                    AppleCounter++;
                    Snake.Extend++;
                }

                OutputWriter.Draw(this);

                if (!Snake.Head.Equals(AppleLocation) && Snake.Positions.Contains(AppleLocation))
                {
                    OutputWriter.DisplayDeathMessage(this);
                    break;
                }

                if (random.Next(2) == 0)
                {
                    Snake.Move();
                }

                Thread.Sleep(300);
            }
        }