void Mouse::Explore() { std::unique_ptr<Path_new> * path_chosen; // Turn to face a direction where there's actually an opening // Start off going forward once, set previous to start MoveForward(); maze->printClean(); maze->printMaze(true); maze->printClean(); while (true) { // Delay ******************************************************* std::this_thread::sleep_until( std::chrono::system_clock::now() + std::chrono::seconds(1)); // ************************************************************* // Get sensor data this->sensor->sense(maze.get()); // Check to see if there's more than one new path in the current physical room switch (CheckForOptions()) { // dead end case 0: TurnAround(); // Reverse backtracking variable backtracking ? backtracking = false : backtracking = true; MoveForward(); break; // only one option case 1: // Get the next cell to turn to as there's only one option Turn(currentRoom->get_children(previousRoom)->front()); MoveForward(); break; // 2 or 3 options in direction case 2: case 3: if (!backtracking) { // Guess all the possible paths from the current "cross-roads" path_chosen = Evaluate(); // Get the next room from the new completePathGuess, aka path_chosen delete(completePathGuess); completePathGuess = path_chosen->get(); Room * nextr = completePathGuess->Rooms()->at(actualPath->Rooms()->size()); // Start down the new path Turn(nextr); MoveForward(); } else { // backtracking // Choose the room with the least visits, avoiding going back down the previous path Room * nextr = Filter::LeastVisited(currentRoom->get_children(previousRoom)); if (!actualPath->Contains(nextr->Location().x, nextr->Location().y)) { // change back to regular advancements backtracking = false; } // Start down path Turn(nextr); MoveForward(); } break; } maze->printClean(); maze->printMaze(true); maze->printClean(); } }
//------------------------------------------------------------------------------ bool Set::InterpretAction() { #ifdef DEBUG_PARSING MessageInterface::ShowMessage("%s::InterpretAction for \"%s\" entered, " "type = %s\n", typeName.c_str(), generatingString.c_str(), typeName.c_str()); #endif std::string mainString = generatingString; // Remove trailing semicolons // Note: Cannot use UnsignedInt here because return is really a size_t size_t loc = mainString.find_last_of(';'); while (loc != std::string::npos) { #ifdef DEBUG_PARSING MessageInterface::ShowMessage("Went from '%s' to ", mainString.c_str()); #endif mainString = mainString.substr(0, loc); #ifdef DEBUG_PARSING MessageInterface::ShowMessage("'%s'\n", mainString.c_str()); #endif loc = mainString.find_last_of(';'); } StringArray blocks = parser.DecomposeBlock(mainString); #ifdef DEBUG_PARSING MessageInterface::ShowMessage("Top level blocks from \"%s\":\n", generatingString.c_str()); for (StringArray::iterator i = blocks.begin(); i != blocks.end(); ++i) MessageInterface::ShowMessage(" \"%s\"\n", i->c_str()); #endif // Check for paren matching loc = blocks[0].find('('); if (loc != std::string::npos) { size_t loc2 = blocks[0].find('(', loc+1); if (loc2 != std::string::npos) { throw CommandException("Too many opening parens in the Set command"); } loc2 = blocks[0].find(')'); if (loc2 == std::string::npos) { throw CommandException("Missing closing parens in the Set command"); } if (loc > loc2) { throw CommandException("Closing paren found before opening paren in the Set command"); } size_t loc3 = blocks[0].find(')', loc2+1); if (loc3 != std::string::npos) { throw CommandException("Too many closing parens in the Set command"); } } StringArray chunks = parser.SeparateBy(blocks[0], "()"); #ifdef DEBUG_PARSING MessageInterface::ShowMessage("Chunks from block \"%s\":\n", blocks[0].c_str()); for (StringArray::iterator i = chunks.begin(); i != chunks.end(); ++i) MessageInterface::ShowMessage(" \"%s\"\n", i->c_str()); #endif StringArray subchunks = parser.SeparateBy(chunks[0], " "); if (subchunks.size() < 3) throw CommandException(typeName + "::InterpretAction() cannot identify " "either the target or the data source -- is one missing? -- in " "line\n" + generatingString); if (subchunks.size() > 3) throw CommandException(typeName + "::InterpretAction() has too many " "component strings in the line\n" + generatingString); // Parse the main part of the command if (subchunks[0] != typeName) throw CommandException(typeName + "::InterpretAction() does not identify " "the correct command type in line\n" + generatingString); targetName = subchunks[1]; interfaceName = subchunks[2]; #ifdef DEBUG_PARSING MessageInterface::ShowMessage("Identified the target \"%s\" and the " "data source \"%s\"\n", targetName.c_str(), interfaceName.c_str()); #endif if (chunks.size() > 2) throw CommandException(typeName + "::InterpretAction() found too many components to parse in the " "line\n" + generatingString); // Parse the command options if (chunks.size() == 2) CheckForOptions(chunks[1]); #ifdef DEBUG_PARSING MessageInterface::ShowMessage("%s::InterpretAction succeeded\n", typeName.c_str()); #endif return true; }