コード例 #1
0
        private List <RoomInfo> FindShortestPathBetweenRooms(RoomInfo room, RoomInfo roomGoalLocal)
        {
            var openListLocal = new Queue <RoomInfo>();
            var paths         = new Dictionary <RoomInfo, List <RoomInfo> >();

            openListLocal.Enqueue(room);
            paths[room] = new List <RoomInfo>()
            {
                room
            };

            if (room.Equals(roomGoalLocal))
            {
                return(paths[room]);
            }

            while (openListLocal.Count > 0)
            {
                room = openListLocal.Dequeue();

                foreach (var room2 in connections[room])
                {
                    if (!paths.Keys.Contains(room2))    // paths.Keys is essentially the union of openListLocal and closedList.
                    {
                        openListLocal.Enqueue(room2);
                        paths[room2] = new List <RoomInfo>(paths[room]);
                        paths[room2].Add(room2);

                        if (room2.Equals(roomGoalLocal))
                        {
                            return(paths[room2]);
                        }
                    }
                }
            }

            // Here, room is the last room to be dequeued (and thus the last room to be enqueued).
            return(paths[room]);
        }
コード例 #2
0
        public void NavigateLabyrinth()
        {
            var roomsVisited    = new HashSet <RoomInfo>();
            var room            = new RoomInfo(0, 0);
            var JorgesRoom      = rooms[random.Next(rooms.Count)];
            var JorgesPath      = ConstructJorgesPath(JorgesRoom);
            var JorgesPathIndex = 0;

            PlaceBooksInRooms();

            using (var cassandraClient = new CassandraClient())
            {
                cassandraClient.Connect("192.168.56.10");
                cassandraClient.CreateSchema();
                cassandraClient.InsertData(connections);

                for (; ;)
                {
                    roomsVisited.Add(room);

                    Console.WriteLine();
                    Console.WriteLine("You are now in room {0}.", room);
                    //Console.WriteLine("The Venerable Jorge is now in room {0}.", JorgesRoom);
                    //Console.WriteLine("Jorge's destination is room {0}", JorgesPath[JorgesPath.Count - 1]);

                    ReportProximityToJorge(room, JorgesRoom);

                    if (booksInRooms.ContainsKey(room))
                    {
                        Console.WriteLine("You have found the book '{0}'.", booksInRooms[room]);
                    }

                    if (room.Equals(roomGoal))
                    {
                        Console.WriteLine("**** Congratulations!  You have reached the goal! ****");
                    }

                    //var neighbouringRooms = connections[room];
                    var neighbouringRooms = cassandraClient.QueryConnections(room);

                    Console.WriteLine("Possible moves:");

                    for (var i = 0; i < neighbouringRooms.Count; ++i)
                    {
                        var neighbouringRoom = neighbouringRooms[i];
                        var str = string.Format("  {0}. {1}", i, neighbouringRoom);

                        if (roomsVisited.Contains(neighbouringRoom))
                        {
                            str = str + " Visited";
                        }

                        Console.WriteLine(str);
                    }

                    Console.Write("Your move (or (h)elp or (q)uit): ");

                    var input = Console.ReadLine();
                    int inputInt;
                    var inputIsInt = int.TryParse(input, out inputInt);

                    if (string.IsNullOrEmpty(input))
                    {
                        Console.WriteLine("The input is empty.");
                    }
                    else if (inputIsInt)
                    {
                        if (inputInt < 0 || inputInt >= neighbouringRooms.Count)
                        {
                            Console.WriteLine("The input is out of range.");
                        }
                        else
                        {
                            room = neighbouringRooms[inputInt];
                            ReportProximityToJorge(room, JorgesRoom);
                        }
                    }
                    else if (input[0] == 'h')
                    {
                        var pathToGoal = FindShortestPathBetweenRooms(room, roomGoal);

                        Console.WriteLine("Path to goal: {0}", string.Join(" to ", pathToGoal));
                    }
                    else if (input[0] == 'q')
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine("The input was not recognized.");
                    }

                    // Jorge's move.
                    ++JorgesPathIndex;

                    if (JorgesPathIndex >= JorgesPath.Count)
                    {
                        JorgesPath      = ConstructJorgesPath(JorgesRoom);
                        JorgesPathIndex = 1;
                    }

                    JorgesRoom = JorgesPath[JorgesPathIndex];
                }
            }
        }