private int _yCoordinate; // Also used by Master Graph

        #endregion Fields

        #region Constructors

        /*************************
         *  CONSTRUCTOR(S)
         *************************/
        /*
         * GraphBuilder constructs an instance of the GraphBuilder class.
         */
        public GraphBuilder()
        {
            //System.Console.WriteLine("\nIn GraphBuilder()");

            _currentGraph = new Graph();

            _xCoordinate = 0;

            _yCoordinate = 0;

            _alreadyIssuedCoordinates = new List<string>();

            _currentNode = new Node();

            _previousNode = new Node();

            _directionTraveled = "none";

            _nodeNamesList = new List<string> {"A", "B", "C", "D", "E",
                                               "F", "G", "H", "I", "J",
                                               "K", "L", "M", "N", "O",
                                               "P", "Q", "R", "S", "T",
                                               "U", "V", "W", "X", "Y",};

            _nodeNamesListCounter = 0;

            _descriptionsOfNodesUsedInCurrentRun = new List<string>();

            _directionsTraveledInCurrentRun = new List<string>();

            _masterGraph = new Graph();

            _masterGraphAlreadyIssuedCoordinates = new List<string>();

            _masterGraphNodeNamesListCounter = 0;

            _masterGraphTargetNode = new Node();

            _masterGraphTargetNodeXCoordinate = 0;

            _masterGraphTargetNodeYCoordinate = 0;

            //System.Console.WriteLine("Leaving GraphBuilder()\n");
        }
        /*************************
         *  CONSTRUCTOR(S)
         *************************/
        /*
         * GraphNavigatorIntermediate constructor takes a graph, starting node, and orientation ("north", "east", "south", or "west")
         * with that node, setting up the frame work in which an agent class will recieve information about it navigation options
         * within the graph and traverse the graph.
         */
        public GraphNavigatorIntermediate(Graph graph, Node startingNode, string startingOrientation)
        {
            //System.Console.WriteLine("\nIn GraphNavigatorIntermediate()");

            if (!graph.ContainsNode(startingNode.Name))
            {
                System.Console.WriteLine("Error; Graph does not contain specified starting node");

                return;
            }

            _graphBeingNavigated = graph;

            _currentNode = startingNode;

            _orientation = startingOrientation;

            //System.Console.WriteLine("Leaving GraphNavigatorIntermediate()\n");
        }
        /*
         * StartNewRun prepares the GraphBuilder to begin an new run by updating the Master Graph and reseting all Current Run Graph
         * variables to their start state.
         */
        public void StartNewRun()
        {
            //System.Console.WriteLine("\nIn StartNewRun()");

            UpdateMasterGraph();

            _currentGraph = new Graph();

            _xCoordinate = 0;

            _yCoordinate = 0;

            _alreadyIssuedCoordinates = new List<string>();

            _currentNode = new Node();

            _previousNode = new Node();

            _directionTraveled = "none";

            _nodeNamesListCounter = 0;

            _descriptionsOfNodesUsedInCurrentRun = new List<string>();

            _directionsTraveledInCurrentRun = new List<string>();

            //System.Console.WriteLine("Leaving StartNewRun()\n");
        }
        /*
         * UpdateMasterGraph is used to add what was learned in the last run to the Master Graph.
         */
        private void UpdateMasterGraph()
        {
            //System.Console.WriteLine("\nIn UpdateMasterGraph()");

            List<string> reversedPathOfDirectionsTraveledInCurrentRun = new List<string>();

            System.Console.WriteLine("The path traveled in the last run was...");

            foreach (string direction in _directionsTraveledInCurrentRun)
            {
                System.Console.Write(direction + " -- ");

                reversedPathOfDirectionsTraveledInCurrentRun.Insert(0, Helper.GiveOppositeDirection(direction));
            }

            System.Console.WriteLine("AT TARGET NODE");

            System.Console.WriteLine("Reversing that path from the target node, the path is now...");

            foreach (string reversedDirection in reversedPathOfDirectionsTraveledInCurrentRun)
            {
                System.Console.Write(reversedDirection + " -- ");
            }

            System.Console.WriteLine("AT LAST RUN'S STARTING NODE");

            List<string> reversedListOfDescriptionsOfNodesUsedInCurrentRun = new List<string>();

            System.Console.WriteLine("The nodes used to get to the target node, in order, look as follows...");

            foreach (String nodeDescription in _descriptionsOfNodesUsedInCurrentRun)
            {
                System.Console.Write(nodeDescription + " -- ");

                reversedListOfDescriptionsOfNodesUsedInCurrentRun.Insert(0, nodeDescription);
            }

            System.Console.WriteLine("AT TARGET NODE");

            System.Console.WriteLine("That list of node descriptions reversed look as follows...");

            foreach (String nodeDescriptionFromReversedList in reversedListOfDescriptionsOfNodesUsedInCurrentRun)
            {
                System.Console.Write(nodeDescriptionFromReversedList + " -- ");
            }

            System.Console.WriteLine("AT LAST RUN'S STARTING NODE");

            if (_masterGraph.NumNodes() == 0)
            {
                _masterGraph = _currentGraph;

                _masterGraphTargetNode = _currentNode;

                _masterGraphTargetNode.IsTarget = true;

                _masterGraphTargetNodeXCoordinate = _xCoordinate;

                _masterGraphTargetNodeYCoordinate = _yCoordinate;

                _masterGraphAlreadyIssuedCoordinates = _alreadyIssuedCoordinates;

                _masterGraphNodeNamesListCounter = _nodeNamesListCounter;
            }
            else
            {
                _currentNode = _masterGraphTargetNode; // Not necessary; Simply explicative

                _xCoordinate = _masterGraphTargetNodeXCoordinate;

                _yCoordinate = _masterGraphTargetNodeYCoordinate;

                int backtrackingCounter = 0;

                while (backtrackingCounter < reversedPathOfDirectionsTraveledInCurrentRun.Count)
                {
                    string backtrackingDirectionTraveled = reversedPathOfDirectionsTraveledInCurrentRun.ElementAt(backtrackingCounter);

                    if (backtrackingDirectionTraveled.Equals("north"))
                    {
                        GoNorth();
                    }

                    if (backtrackingDirectionTraveled.Equals("east"))
                    {
                        GoEast();
                    }

                    if (backtrackingDirectionTraveled.Equals("south"))
                    {
                        GoSouth();
                    }

                    if (backtrackingDirectionTraveled.Equals("west"))
                    {
                        GoWest();
                    }

                    bool northArcOptionIsPresent = false;

                    bool eastArcOptionIsPresent = false;

                    bool southArcOptionIsPresent = false;

                    bool westArcOptionIsPresent = false;

                    if (reversedListOfDescriptionsOfNodesUsedInCurrentRun.ElementAt(backtrackingCounter + 1).Contains("N"))
                    {
                        northArcOptionIsPresent = true;
                    }

                    if (reversedListOfDescriptionsOfNodesUsedInCurrentRun.ElementAt(backtrackingCounter + 1).Contains("E"))
                    {
                        eastArcOptionIsPresent = true;
                    }

                    if (reversedListOfDescriptionsOfNodesUsedInCurrentRun.ElementAt(backtrackingCounter + 1).Contains("S"))
                    {
                        southArcOptionIsPresent = true;
                    }

                    if (reversedListOfDescriptionsOfNodesUsedInCurrentRun.ElementAt(backtrackingCounter + 1).Contains("W"))
                    {
                        westArcOptionIsPresent = true;
                    }

                    UpdateMasterGraphHelper(northArcOptionIsPresent, eastArcOptionIsPresent, southArcOptionIsPresent, westArcOptionIsPresent);

                    backtrackingCounter += 1;
                }
            }

            System.Console.WriteLine("The GraphBuilder's Master Graph is now setup as follows...");

            _masterGraph.ListNodes();

            _masterGraph.ListArcs();

            System.Console.WriteLine("The GraphBuilder's Master Graph target node is: " +
                                        _masterGraphTargetNode.Name + " [" + _masterGraphTargetNodeXCoordinate +
                                        "," + _masterGraphTargetNodeYCoordinate + "]");

            //System.Console.WriteLine("Leaving UpdateMasterGraph()\n");
        }
Exemple #5
0
        static void Main(string[] args)
        {
            System.Console.WriteLine("Would you like to traverse a graph or a maze? ");

            string userOption1 = System.Console.ReadLine();

            while (!(userOption1.Equals("graph") || userOption1.Equals("maze")))
            {
                System.Console.WriteLine("Input not recognized. Please enter \"graph\" or \"maze\".");

                userOption1 = System.Console.ReadLine();
            }

            Intermediate intermediate;

            if (userOption1.Equals("graph"))
            {
                System.Console.WriteLine("Graph option selected");

                Node node1 = new Node("A", false, false, true, false);
                Node node2 = new Node("B", false, true, false, false);
                Node node3 = new Node("C", false, true, true, true);
                Node node4 = new Node("D", false, false, false, true);
                Node node5 = new Node("E", false, false, true, false);
                Node node6 = new Node("F", true, false, true, false);
                Node node7 = new Node("G", false, true, true, false);
                Node node8 = new Node("H", true, true, true, true);
                Node node9 = new Node("I", false, false, true, true);
                Node node10 = new Node("J", true, false, true, false);
                Node node11 = new Node("K", true, true, true, false);
                Node node12 = new Node("L", true, false, true, true);
                Node node13 = new Node("M", true, false, false, false);
                Node node14 = new Node("N", true, true, true, false);
                Node node15 = new Node("O", true, false, true, true);
                Node node16 = new Node("P", true, false, true, false);
                Node node17 = new Node("Q", true, true, false, false);
                Node node18 = new Node("R", false, true, true, true);
                Node node19 = new Node("S", true, false, false, true);
                Node node20 = new Node("T", true, false, true, false);
                Node node21 = new Node("U", true, false, false, false);
                Node node22 = new Node("V", false, true, false, false);
                Node node23 = new Node("W", true, true, false, true);
                Node node24 = new Node("X", false, false, false, true);
                Node node25 = new Node("Y", true, false, false, false);

                node13.IsTarget = true;

                Graph graph1 = new Graph();

                graph1.AddNode(node1);
                graph1.AddNode(node2);
                graph1.AddNode(node3);
                graph1.AddNode(node4);
                graph1.AddNode(node5);
                graph1.AddNode(node6);
                graph1.AddNode(node7);
                graph1.AddNode(node8);
                graph1.AddNode(node9);
                graph1.AddNode(node10);
                graph1.AddNode(node11);
                graph1.AddNode(node12);
                graph1.AddNode(node13);
                graph1.AddNode(node14);
                graph1.AddNode(node15);
                graph1.AddNode(node16);
                graph1.AddNode(node17);
                graph1.AddNode(node18);
                graph1.AddNode(node19);
                graph1.AddNode(node20);
                graph1.AddNode(node21);
                graph1.AddNode(node22);
                graph1.AddNode(node23);
                graph1.AddNode(node24);
                graph1.AddNode(node25);

                graph1.AddArc(node1, "south", node6);

                graph1.AddArc(node2, "east", node3);

                graph1.AddArc(node3, "east", node4);
                graph1.AddArc(node3, "south", node8);

                graph1.AddArc(node5, "south", node10);

                graph1.AddArc(node6, "south", node11);

                graph1.AddArc(node7, "east", node8);
                graph1.AddArc(node7, "south", node12);

                graph1.AddArc(node8, "east", node9);
                graph1.AddArc(node8, "south", node13);

                graph1.AddArc(node9, "south", node14);

                graph1.AddArc(node10, "south", node15);

                graph1.AddArc(node11, "east", node12);
                graph1.AddArc(node11, "south", node16);

                graph1.AddArc(node12, "south", node17);

                graph1.AddArc(node14, "east", node15);
                graph1.AddArc(node14, "south", node19);

                graph1.AddArc(node15, "south", node20);

                graph1.AddArc(node16, "south", node21);

                graph1.AddArc(node17, "east", node18);

                graph1.AddArc(node18, "east", node19);
                graph1.AddArc(node18, "south", node23);

                graph1.AddArc(node20, "south", node25);

                graph1.AddArc(node22, "east", node23);

                graph1.AddArc(node23, "east", node24);

                /* List of nodes in graph should be:
                 *
                 *  A through Y (25 total)
                 *
                 * List of arcs in graph should be:
                 *
                 *  A<-->F
                 *  B<-->C
                 *  C<-->D
                 *  C<-->H
                 *  E<-->J
                 *  F<-->K
                 *  G<-->H
                 *  G<-->L
                 *  H<-->I
                 *  H<-->M
                 *  I<-->N
                 *  J<-->O
                 *  K<-->L
                 *  K<-->P
                 *  L<-->Q
                 *  N<-->O
                 *  N<-->S
                 *  O<-->T
                 *  P<-->U
                 *  Q<-->R
                 *  R<-->S
                 *  R<-->W
                 *  T<-->Y
                 *  V<-->W
                 *  W<-->X
                 *
                 * Graph structure looks like:
                 *
                 *  A   B---C---D   E
                 *  |       |       |
                 *  F   G---H---I   J
                 *  |   |   |   |   |
                 *  K---L   M   N---O
                 *  |   |       |   |
                 *  P   Q---R---S   T
                 *  |       |       |
                 *  U   V---W---X   Y
                 *
                 * Graph target node is:
                 *
                 *  M
                 *
                 */

                intermediate = new GraphNavigatorIntermediate(graph1, node1, "north");
            }
            else
            {
                System.Console.WriteLine("Maze option selected");

                // When maze option is ready, it will go here.

                System.Console.WriteLine("Enter the arc's lighter color reading:");

                double lighterColorReading = Convert.ToDouble(System.Console.ReadLine());

                System.Console.WriteLine("Enter the arc's darker color reading:");

                double darkerColorReading = Convert.ToDouble(System.Console.ReadLine());

                System.Console.WriteLine("Enter the arc's reading for white:");

                double whiteReading = Convert.ToDouble(System.Console.ReadLine());

                intermediate = new MazeNavigatorIntermediate(darkerColorReading, lighterColorReading, whiteReading, "north");
            }

            System.Console.WriteLine("Would you like to traverse the graph manually? [n/y]");

            string userOption2 = System.Console.ReadLine();

            while (!(userOption2.Equals("y") || userOption2.Equals("n")))
            {

                System.Console.WriteLine("Input not recognized. Please enter \"n\" or \"y\".");

                userOption2 = System.Console.ReadLine();
            }

            if (userOption2.Equals("y"))
            {

                System.Console.WriteLine("Manual option seleted");

                ManualAgent agent1 = new ManualAgent(intermediate);

                agent1.Traverse();
            }
            else {

                System.Console.WriteLine("Automatic option seleted");

                AutonomousAgent agent2 = new AutonomousAgent(intermediate);

                agent2.Traverse();

            }

            System.Console.WriteLine("------------------------");
            System.Console.WriteLine("- Press any key to end program");
            System.Console.WriteLine("------------------------\n\n");

            System.Console.ReadLine();
        }