Beispiel #1
0
        private List <Point> ValidPoints(Point p, Models.Snake snake)
        {
            List <Point> output = new List <Point>();
            Point        currentPoint;

            currentPoint = new Point(p.X + 1, p.Y);
            if (IsValidPoint(currentPoint, snake))
            {
                output.Add(currentPoint);
            }
            currentPoint = new Point(p.X - 1, p.Y);
            if (IsValidPoint(currentPoint, snake))
            {
                output.Add(currentPoint);
            }
            currentPoint = new Point(p.X, p.Y + 1);
            if (IsValidPoint(currentPoint, snake))
            {
                output.Add(currentPoint);
            }
            currentPoint = new Point(p.X, p.Y - 1);
            if (IsValidPoint(currentPoint, snake))
            {
                output.Add(currentPoint);
            }
            return(output);
        }
Beispiel #2
0
        public Queue <ConsoleKey> FindAPath(Models.Snake snake, Food food)
        {
            Points.Push(snake.Head);
            Visted.Add(snake.Head);
            while (true)
            {
                Point currentPoint = Points.Peek();
                Point nextPoint    = GetNextPoint(currentPoint, food, snake);
                if (nextPoint != null)
                {
                    Visted.Add(new Point(nextPoint.X, nextPoint.Y));
                    // For Debugging
                    //Console.SetCursorPosition(nextPoint.X, nextPoint.Y);
                    //Console.Write('.');
                    Points.Push(nextPoint);
                    if (nextPoint.X - currentPoint.X == -1)
                    {
                        ConsoleKeys.Push(ConsoleKey.LeftArrow);
                    }
                    else if (nextPoint.X - currentPoint.X == 1)
                    {
                        ConsoleKeys.Push(ConsoleKey.RightArrow);
                    }
                    if (nextPoint.Y - currentPoint.Y == -1)
                    {
                        ConsoleKeys.Push(ConsoleKey.UpArrow);
                    }
                    else if (nextPoint.Y - currentPoint.Y == 1)
                    {
                        ConsoleKeys.Push(ConsoleKey.DownArrow);
                    }
                    double distance = CalculateDistance(food, nextPoint);
                    if (distance == 0)
                    {
                        break;
                    }
                }
                else
                {
                    Points.Pop();
                    if (Points.Count == 0)
                    {
                        break;
                    }
                    ConsoleKeys.Pop();
                }
            }
            Stack <ConsoleKey> temp   = new Stack <ConsoleKey>();
            Queue <ConsoleKey> output = new Queue <ConsoleKey>();

            while (ConsoleKeys.Count != 0)
            {
                temp.Push(ConsoleKeys.Pop());
            }
            while (temp.Count != 0)
            {
                output.Enqueue(temp.Pop());
            }
            return(output);
        }
Beispiel #3
0
 public Game(int speed, bool deadlywalls, int food, bool isBotEnabled)
 {
     Console.Clear();
     Wall         = new Wall();
     Foods        = new List <Food>();
     Snake        = new Models.Snake(Console.WindowWidth - 1, Console.WindowHeight - 1, Foods, deadlywalls);
     Speed        = speed;
     FoodCount    = food;
     IsBotEnabled = isBotEnabled;
 }
Beispiel #4
0
 private bool IsValidPoint(Point p, Models.Snake snake)
 {
     if (snake.Body.Any(a => a.X == p.X && a.Y == p.Y))
     {
         return(false);
     }
     if (Visted.Any(a => a.X == p.X && a.Y == p.Y))
     {
         return(false);
     }
     if (Points.Any(a => a.X == p.X && a.Y == p.Y))
     {
         return(false);
     }
     if (p.X < 1 || p.Y < 1 || p.X > snake.MaxX - 2 || p.Y > snake.MaxY - 2)
     {
         return(false);
     }
     return(true);
 }
Beispiel #5
0
 private Point GetNextPoint(Point p, Food food, Models.Snake snake)
 {
     return(GetTheBestPoint(ValidPoints(p, snake), food));
 }