Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Maze Program Debugging (Solved. Answer no longer needed, but can't delete question) Maze: 111111111111111 100000001000001 101110101010111 000010101010001 111110111011101 100000000010001 101010111010111 101010001010001 101011101011101 101000101010101 101110111010101 100010001000100
Maze Program Debugging (Solved. Answer no longer needed, but can't delete question)
Maze:
111111111111111 100000001000001 101110101010111 000010101010001 111110111011101 100000000010001 101010111010111 101010001010001 101011101011101 101000101010101 101110111010101 100010001000100 111111111111111
Desired output:
111111111111111 1 001 001 1 111 101 1 111 001 101 1 1 11111 111 111 1 10000 1 1 10101011101 111 10101000101 1 1010111010111 1 1010001010101 1 1011101110101 1 1000100010001 111111111111111
Here is my code:
// main.cpp
#include #include #include "Maze.hpp" // Most of the main file was copy/paste // from lab01. int main(int argc, char *argv[]) { if (argc != 2) { std::cout << "usage: " << argv[0] << " inputFileNameThatContainsDictionary inputFileNameThatContainsPairsOfWords "; exit(1); } // Create an ifstream and open up the path.txt file std::ifstream mazeStream; mazeStream.open(argv[1], std::ios_base::in); // Open up the file. This is a copy/paste from lab01. if (!(mazeStream.is_open())) { std::cout << "Unable to open input file ->" << argv[1] << "<- "; exit(2); } // Close the file when done. mazeStream.close(); Maze maze(argv[1]); return 0; }
// Maze.cpp
#include #include #include #include #include "Maze.hpp" // Parameterized Constructor. Some of this // is copy/paste from lab01, with minor tweaks. Maze::Maze(std::string inputFileName) { // Create necessary variables to open my // files and extract my string values std::ifstream fileOpener; std::string value; // Open file and extract all characters // into the ifstream fileOpener.open(inputFileName, std::ios_base::in); while (!(fileOpener.eof())) { std::getline(fileOpener, value); // Add values into my private variables. for (int i = 0; i < value.size(); i++) { input.push_back(value[i]); used.push_back(false); } // Make sure my data is collected with the input // and keeping track of what's being used. primaryVector.push_back(input); primaryUsed.push_back(used); // When finished, clear my input and used and // repeat the process within my while loop. input.clear(); used.clear(); } // Call my functions to begin the maze FindEndPoint(); pathFromTo(FindStartPoint()); // When done with the program, output my maze. printMaze(); } // Destructor. This is a copy/paste from lab01. // When I'm done with the ifstream file, make // sure to close it. Maze::~Maze() { if (mazeStream.is_open()) { mazeStream.close(); } } // FindStartPoint member function. std::tuple Maze::FindStartPoint() { // To move the icon, I will follow the Konami // cheat code sequence and start up, down, left, right. for (int i = 0; i < primaryVector.size(); i++) { // Move left if (primaryVector[i][0] == '0') { startPoint = std::make_tuple(i, 0); return startPoint; } // Move right if (primaryVector[i][primaryVector[0].size() - 1] == '0') { startPoint = std::make_tuple(i, primaryVector[0].size() - 1); return startPoint; } } for (int i = 0; i < primaryVector[0].size(); i++) { // Move up. if (primaryVector[0][i] == '0') { startPoint = std::make_tuple(0, i); return startPoint; } // Move down. if (primaryVector[primaryVector.size() - 1][i] == '0') { startPoint = std::make_tuple(primaryVector.size() - 1, i); return startPoint; } } // Return end result. return std::make_tuple(-1, -1); } // FindEndPoint() member function std::tuple Maze::FindEndPoint() { // Repeat the Konami sequence, but // with some tweaks. for (int i = 0; i < primaryVector.size(); i++) { // Move left if (primaryVector[i][0] == '0') { endPoint = std::make_tuple(i, 0); if (std::get<0>(startPoint) != std::get<0>(endPoint) && std::get<1>(startPoint) != std::get<1>(endPoint)) { return endPoint; } } // Move right if (primaryVector[i][primaryVector[0].size() - 1] == '0') { endPoint = std::make_tuple(i, primaryVector[0].size() - 1); if (std::get<0>(startPoint) != std::get<0>(endPoint) && std::get<1>(startPoint) != std::get<1>(endPoint)) { return endPoint; } } } for (int i = 0; i < primaryVector[0].size(); i++) { // Move up if (primaryVector[0][i] == '0') { endPoint = std::make_tuple(0, i); if (std::get<0>(startPoint) != std::get<0>(endPoint) && std::get<1>(startPoint) != std::get<1>(endPoint)) { return endPoint; } } // Move down if (primaryVector[primaryVector.size() - 1][i] == '0') { endPoint = std::make_tuple(primaryVector.size() - 1, i); if (std::get<0>(startPoint) != std::get<0>(endPoint) && std::get<1>(startPoint) != std::get<1>(endPoint)) { return endPoint; } } } // Return result return std::make_tuple(-1, -1); } // FindNextPoint() member function. std::tuple Maze::FindNextPoint(std::tuple currPoint) { // Create variables for axis points x & y. int xPoint = std::get<1>(currPoint); int yPoint = std::get<0>(currPoint); // Create a tuple to store the next move. std::tuple nextPoint; // Start right and go counter-clockwise. // Move right if (xPoint != primaryVector[0][primaryVector.size() - 1]) { if ((primaryVector[yPoint][xPoint + 1]) == '0' && !primaryUsed[yPoint][xPoint + 1]) { nextPoint = std::make_tuple(yPoint, xPoint + 1); return nextPoint; } } // Move up if (yPoint != 0) { if ((primaryVector[yPoint - 1][xPoint]) == '0' && !primaryUsed[yPoint - 1][xPoint]) { nextPoint = std::make_tuple(yPoint - 1, xPoint); return nextPoint; } } // Move left if (xPoint != 0) { if ((primaryVector[yPoint][xPoint - 1]) == '0' && !primaryUsed[yPoint][xPoint - 1]) { nextPoint = std::make_tuple(yPoint, xPoint - 1); return nextPoint; } } // Move down if (xPoint != primaryVector.at(0).size() - 1) { if ((primaryVector[yPoint + 1][xPoint]) == '0' && !primaryUsed[yPoint + 1][xPoint]) { nextPoint = std::make_tuple(yPoint + 1, xPoint); return nextPoint; } } // Have the next move be the result. nextPoint = std::make_tuple(-1, -1); return nextPoint; } // pathFromTo() function. Some of this is copy/paste // from lab01. std::vector> Maze::pathFromTo(std::tuple currPoint) { // Create a while loop that will keep the icon // moving until the currPoint variable reaches // the end of the maze. while (std::get<0>(currPoint) != -1) { // Create temporary variables to store my // tuples and use them to call my finder functions. std::tuple nextFinder; std::tuple endFinder; // Create more axis points int xPoint = 0; int yPoint = 0; endFinder = FindEndPoint(); stack.push_back(FindStartPoint()); // Once a path has been found, set Used to true and mark // an empty space. primaryUsed.at(std::get<0>(currPoint)).at(std::get<1>(currPoint)) = true; primaryVector[std::get<0>(currPoint)][std::get<1>(currPoint)] = ' '; // Keep moving until the stack size is reduced to 0. while (stack.size() != 0) { nextFinder = FindNextPoint(currPoint); // Move is successful if (std::get<1>(nextFinder) != -1) { stack.push_back(nextFinder); xPoint = std::get<1>(nextFinder); yPoint = std::get<0>(nextFinder); // Once movement is made, mark the location // as used. primaryUsed.at(yPoint).at(xPoint) = true; primaryVector[yPoint][xPoint] = ' '; currPoint = nextFinder; } // Backtrack if not successful. else { xPoint = std::get<1>(currPoint); yPoint = std::get<0>(currPoint); primaryVector[yPoint][xPoint] = '0'; stack.pop_back(); currPoint = stack.back(); // Return empty stack. if (stack.size() == 0) { return stack; } } if (std::get<1>(currPoint) == std::get<1>(endPoint) && std::get<0>(currPoint) == std::get<0>(endPoint)) { return stack; } } } return stack; } // printMaze() function. Most of this is // copy/paste from lab01. void Maze::printMaze() { for (int i = 0; i < primaryVector.size(); i++) { for (int j = 0; j < primaryVector[i].size(); j++) { std::cout << primaryVector[i][j]; } std::cout << std::endl; } }
// Maze.hpp
#ifndef PROJECT1_MAZE_HPP #define PROJECT1_MAZE_HPP #include #include #include #include #include // Class for my maze data class Maze { public: // Parameterized constructor & Destructor Maze(std::string inputFileName); ~Maze(); // Tuples for my starting point, // end point, and next point. std::tuple FindStartPoint(); std::tuple FindEndPoint(); std::tuple FindNextPoint(std::tuple currPoint); // Member function to find path through my maze std::vector> pathFromTo(std::tuple currPoint); void printMaze(); private: // Variables for my start and endpoint std::tuple startPoint; std::tuple endPoint; // String stream that will open my .txt file std::ifstream mazeStream; // Have a primary vector and used variable. // These will store my values and what was used. // Including the stack. std::vector> primaryVector; std::vector> primaryUsed; std::vector> stack; // These will posses the values of what was input, // used, then they will be stored in the primaryVector // and primaryUsed variables. std::vector input; std::vector used; }; #endif //PROJECT1_MAZE_HPP
// .txt file
111111111111111 100000001000001 101110101010111 000010101010001 111110111011101 100000000010001 101010111010111 101010001010001 101011101011101 101000101010101 101110111010101 100010001000100 111111111111111
Current output:
111111111111111 100000001000001 101110101010111 00010101010001 111110111011101 100000000010001 101010111010111 101010001010001 101011101011101 101000101010101 101110111010101 100010001000100 111111111111111
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started