Example #1
0
        static void Main(string[] args)
        {
            LoadMap();
            Console.WriteLine("If you want to use default values for the agents, press <enter> to skip each value:");
            _agentA = LoadAgent("A", 2, 2, 1);
            _agentB = LoadAgent("B", 5, 7, 1);

            // Draw
            _map.Draw(_agentA, _agentB);
            Console.ReadLine();

            // Run until A catches B.
            while (true)
            {
                // Make B avoid A
                _agentB.Avoid(_agentA, _map);

                // Make A chase B
                _agentA.Chase(_agentB, _map);

                // Draw the map
                _map.Draw(_agentA, _agentB);

                // Stop the loop if A has caught B.
                if (_agentA.X == _agentB.X && _agentA.Y == _agentB.Y) break;

                Console.ReadLine();
            }

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Agent A has caught agent B at X:" + _agentA.X + " Y:" + _agentA.Y + "!");
            Console.ReadLine();
        }
Example #2
0
        /// <summary>
        /// Simple way to avoid the target.
        /// </summary>
        /// <param name="target">The target to avoid.</param>
        /// <param name="map">The map that the agent is traveling on.</param>
        public void Avoid(Agent target, Map map)
        {
            var iterations = 0;
            int newX = X, newY = Y;

            var targetNode = map[target.X, target.Y];
            var currentDist = map[X, Y].DistanceTo(targetNode);

            // Only 10 steps are needed.
            while (iterations < 20)
            {
                var neighbours = map.NeighbourNodes(map[newX, newY]);

                // Check which neighbour node that leads furthest away from the target.
                foreach (Node neighbour in neighbours)
                {
                    if (neighbour.Closed) continue;

                    var tempDist = neighbour.DistanceTo(targetNode);

                    if (tempDist > currentDist)
                    {
                        currentDist = tempDist;
                        var optimalNeighbour = neighbours.IndexOf(neighbour);

                        newX = neighbours[optimalNeighbour].X;
                        newY = neighbours[optimalNeighbour].Y;
                    }
                }

                iterations++;
            }

            // Find the path to the target location.
            Path = AStar.FindPathTo(map[X, Y], map[newX, newY], map);
            Move();
        }
Example #3
0
 /// <summary>
 /// Chase the target using the aStar algorithm.
 /// </summary>
 /// <param name="target">The target to chase.</param>
 /// <param name="map">The map the agent is traveling on.</param>
 public void Chase(Agent target, Map map)
 {
     Path = AStar.FindPathTo(map[X, Y], map[target.X, target.Y], map);
     Move();
 }
Example #4
0
        /// <summary>
        /// Draws the map to the console.
        /// </summary>
        /// <param name="agentA">The location of agent A.</param>
        /// <param name="agentB">The location of agent B.</param>
        public void Draw(Agent agentA, Agent agentB)
        {
            Console.Clear();

            for (int x = 0; x < Height; x++)
            {
                Console.Write("+");
                for (int y = 0; y < Width; y++)
                {
                    Console.Write("-+");
                }
                Console.WriteLine();

                Console.Write("|");
                for (int y = 0; y < Width; y++)
                {
                    var tempNode = new Node(x, y);

                    if (_map[x, y].Closed) Console.Write("X");
                    else if (x == agentA.X && y == agentA.Y) Console.Write("A");
                    else if (x == agentB.X && y == agentB.Y) Console.Write("B");
                    else if (agentA.Path.Contains(tempNode)) Console.Write("a");
                    else if (agentB.Path.Contains(tempNode)) Console.Write("b");
                    else Console.Write(" ");
                    Console.Write("|");
                }
                Console.WriteLine();
            }

            Console.Write("+");
            for (int y = 0; y < Width; y++)
            {
                Console.Write("-+");
            }

            Console.WriteLine();
            Console.Write("Press <enter> to continue to next step.");
        }