private List <RoomInfo> FindShortestPathBetweenRooms(ILabyrinthClient client, 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(); var connectionsToRoom = (client != null) ? client.QueryConnections(room) : connections[room]; foreach (var room2 in connectionsToRoom) { 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]); }
public void NavigateLabyrinth(ILabyrinthClient client) { client.Initialize(this); if (!client.IsRAMClient) { // This ensures that we are querying the database, not RAM, for the room connections. connections.Clear(); booksInRooms.Clear(); } // **** var roomsVisited = new HashSet <RoomInfo>(); var room = new RoomInfo(0, 0); //Console.WriteLine("Selecting a room for Jorge out of {0} rooms.", rooms.Count); var JorgesRoom = rooms[random.Next(rooms.Count)]; var JorgesPath = ConstructJorgesPath(client, JorgesRoom); var JorgesPathIndex = 0; 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(client, room, JorgesRoom); #if DEAD_CODE if (booksInRooms.ContainsKey(room)) { Console.WriteLine("You have found the book '{0}'.", booksInRooms[room]); } #else foreach (var book in client.QueryBooksInRoom(room)) { Console.WriteLine("You have found the book '{0}'.", book); } #endif if (room.Equals(roomGoal)) { Console.WriteLine("**** Congratulations! You have reached the goal! ****"); } //var neighbouringRooms = connections[room]; var neighbouringRooms = client.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(client, room, JorgesRoom); } } else if (input[0] == 'h') { var pathToGoal = FindShortestPathBetweenRooms(client, 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; while (JorgesPathIndex >= JorgesPath.Count) // ThAW 2013/09/23 : This "while" used to be an "if", but it crashed once. { JorgesPath = ConstructJorgesPath(client, JorgesRoom); JorgesPathIndex = 1; } JorgesRoom = JorgesPath[JorgesPathIndex]; } }