Exemplo n.º 1
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];
                }
            }
        }
Exemplo n.º 2
0
        public void Generate()
        {
            var label = 0;

            numberOfDifferentLabels = numberOfLevels * numberOfRoomsPerLevel;

            for (int l = 0; l < numberOfLevels; ++l)
            {
                for (int r = 0; r < numberOfRoomsPerLevel; ++r)
                {
                    var room = new RoomInfo(l, r);

                    rooms.Add(room);
                    roomLabels[room]  = label++;
                    connections[room] = new List <RoomInfo>();
                    openList.Add(room);
                }
            }

#if USE_ULTIMATE_ROOMS_LIST
            for (int r = 0; r < numberOfRoomsPerLevel; ++r)
            {
                ultimateRoomsList.Add(new RoomInfo(0, r));
                ultimateRoomsList.Add(new RoomInfo(numberOfLevels - 1, r));
            }
#endif

            while (numberOfDifferentLabels > 1)
            {
                if (openList.Count == 0)
                {
#if TRY_TO_REFACTOR_LABYRINTH
                    if (numberOfAttemptsToRefactor >= maximumNumberOfAttemptsToRefactor)
                    {
                        throw new Exception(string.Format("Attempted to refactor {0} times; all failed.", numberOfAttemptsToRefactor));
                    }

                    ++numberOfAttemptsToRefactor;
                    Refactor();
#else
                    throw new Exception("Open list is unexpectedly empty.");
#endif
                }

#if USE_ULTIMATE_ROOMS_LIST
                RoomInfo room1;

                if (ultimateRoomsList.Count > 0)
                {
                    room1 = ultimateRoomsList[random.Next(ultimateRoomsList.Count)];
                    ultimateRoomsList.Remove(room1);
                }
                else
                {
                    room1 = openList[random.Next(openList.Count)];
                }
#else
                var room1 = openList[random.Next(openList.Count)];
#endif

                var      possibleNeighbours = room1.GeneratePossibleNeighbours(this);
                RoomInfo room2 = null;

                while (room2 == null && possibleNeighbours.Count > 0)
                {
                    room2 = possibleNeighbours[random.Next(possibleNeighbours.Count)];

                    if (roomLabels[room1] != roomLabels[room2] && !FindConflictingConnections(room1, room2))
                    {
                        break;
                    }

                    possibleNeighbours.Remove(room2);
                    room2 = null;
                }

                if (room2 == null)
                {
                    openList.Remove(room1);
                    continue;
                }

                // We have now chosen room1 and room2.
                connections[room1].Add(room2);
                connections[room2].Add(room1);

                // Join the two "blobs" to which the two rooms belong, by modifying room labels.
                var label1   = roomLabels[room1];
                var label2   = roomLabels[room2];
                var minLabel = Math.Min(label1, label2);
                var maxLabel = Math.Max(label1, label2);

                foreach (var room in rooms)
                {
                    if (roomLabels[room] == maxLabel)
                    {
                        roomLabels[room] = minLabel;
                    }
                }

                --numberOfDifferentLabels;
            }

            if (numberOfExtraConnections > 0)
            {
                AddExtraConnections();
            }
        }
Exemplo n.º 3
0
 private List <RoomInfo> FindLongestPathFromRoom(RoomInfo room)
 {
     return(FindShortestPathBetweenRooms(room, null));
 }
Exemplo n.º 4
0
        private void Refactor()
        {
            Console.WriteLine("Refactoring...");

            RoomInfo room1;
            RoomInfo room2;

            FindPossibleNeighboursWithDifferentLabels(out room1, out room2);

            // Resolve the conflicts that are preventing a connection between room1 and room2.

            // Test 1: Room 3 must not be connected to room 4.

            // 4  2
            //  \/
            //  /\
            // 1  3

            var room3 = new RoomInfo(room2.levelNumber, room1.roomNumber);
            var room4 = new RoomInfo(room1.levelNumber, room2.roomNumber);

            if (connections[room3].Contains(room4))
            {
                Console.WriteLine("Found a Type 1 conflict.");
                connections[room3].Remove(room4);
                connections[room4].Remove(room3);
                PropagateNewLabel(room3, FindUnusedLabel(), true);
                PropagateNewLabel(room4, FindUnusedLabel(), true);
            }

            // Test 2: Room 3 must not be connected to room 1.

            // 3
            //  \
            //   1
            //  /
            // 2

            room3 = new RoomInfo(2 * room1.levelNumber - room2.levelNumber, room2.roomNumber);

            if (connections.ContainsKey(room3) && connections[room3].Contains(room1))
            {
                Console.WriteLine("Found a Type 2 conflict.");
                connections[room1].Remove(room3);
                connections[room3].Remove(room1);
                PropagateNewLabel(room3, FindUnusedLabel(), true);
            }

            // Test 3: Room 3 must not be connected to room 2.

            // 3
            //  \
            //   2
            //  /
            // 1

            room3 = new RoomInfo(2 * room2.levelNumber - room1.levelNumber, room1.roomNumber);

            if (connections.ContainsKey(room3) && connections[room3].Contains(room2))
            {
                Console.WriteLine("Found a Type 3 conflict.");
                connections[room2].Remove(room3);
                connections[room3].Remove(room2);
                PropagateNewLabel(room3, FindUnusedLabel(), true);
            }

            // Connect room1 and room2.
            PropagateNewLabel(room2, roomLabels[room1], false);
            connections[room1].Add(room2);
            connections[room2].Add(room1);

            var setOfLabels = new HashSet <int>(roomLabels.Values);

            numberOfDifferentLabels = setOfLabels.Count;
        }