cpp assignment
Lab 6/.DS_Store
__MACOSX/Lab 6/._.DS_Store
Lab 6/Lab6.docx
This lab is intended to prepare you for PA #4. For the PA, you will implement both breadth-first and depth-first searches in a maze. In this lab, we cover how to convert the text file representation of our maze into computer code. In addition, will implement the basic "Maze Space" container that represents each space in our maze.
Maze Text File
Our maze will be read in via text file. The first line informs us of the number of spaces in the maze. Each line afterwards describes a connection between two points in the maze. For example:
|
2 1 S 2 2 N 1 |
The example above tells us that we have two spaces in our maze. Furthermore, we see that space 1 has a southern connection to space 2. Similarly, space 2 has a northward connection to space 1. The possible connections are N (north), E (east), S (south), and W (west).
MazeSpace
Each space in the maze will be represented by an object of type MazeSpace. Here's the UML class diagram:
__MACOSX/Lab 6/._Lab6.docx
Lab 6/main.tcc
#include <iostream> #include <fstream> #include <string> #include <cstdlib> #include "StringSplitter.h" #include "MazeSpace.h" using namespace std; enum Direction {NORTH = 'N', SOUTH = 'S', EAST = 'E', WEST = 'W'}; int main() { //prompt user for file to open string input_file_name; cout << "Enter maze file: "; getline(cin, input_file_name); //open file ifstream maze_file{input_file_name}; //figure out how many spaces in our maze MazeSpace *spaces = nullptr; int num_spaces = 0; //before we read from the file, make sure //that it has been opened properly if(maze_file.is_open() == true) { //first line tells us how many spaces are in our //file string line; getline(maze_file, line); num_spaces = atoi(line.c_str()); //make sure num_spaces is greater than 0 if(num_spaces > 0) { spaces = new MazeSpace[num_spaces + 1]; for(int i = 0; i < num_spaces + 1; i++) { spaces[i].setId(i); } } //build our maze while(maze_file.good() == true) { //grab next line getline(maze_file, line); //split line based on spaces vector<string> pieces = StringSplitter::split( line, " "); int source = atoi(pieces[0].c_str()); char direction = pieces[1][0]; int destination = atoi(pieces[2].c_str()); //goal: connect source's direction to destination switch(direction) { case NORTH: spaces[source].setNorth(&spaces[destination]); break; case SOUTH: spaces[source].setSouth(&spaces[destination]); break; case EAST: spaces[source].setEast(&spaces[destination]); break; case WEST: spaces[source].setWest(&spaces[destination]); break; } } } //end of file openness check //output space directions to screen for(int i = 0; i < num_spaces + 1; i++) { } //PA4 TODO: solve our maze //Always clean up dynamic memory delete[] spaces; return 0; }
Lab 6/maze1.txt
3 1 S 2 2 N 1 2 S 3 3 N 2
__MACOSX/Lab 6/._maze1.txt
Lab 6/maze2.txt
9 1 S 2 1 E 4 2 N 1 2 E 5 2 S 3 3 N 2 3 E 6 4 W 1 4 E 7 5 W 2 5 E 8 5 S 6 6 N 5 6 W 3 7 W 4 8 W 5 8 S 9
__MACOSX/Lab 6/._maze2.txt
Lab 6/MazeSpace.h
#ifndef MAZE_SPACE_H #define MAZE_SPACE_H #include <string> #include <sstream> using namespace std; class MazeSpace { private: int _id = 0; MazeSpace *_north_space = nullptr; MazeSpace *_south_space = nullptr; MazeSpace *_east_space = nullptr; MazeSpace *_west_space = nullptr; public: MazeSpace(int id = 0, MazeSpace *north = nullptr, MazeSpace *south = nullptr, MazeSpace *east = nullptr, MazeSpace *west = nullptr ) { _id = id; _north_space = north; _south_space = south; _east_space = east; _west_space = west; } int getId() { return _id; } void setId(int id) { _id = id; } MazeSpace *getNorth() { return _north_space; } void setNorth(MazeSpace *north) { _north_space = north; } MazeSpace *getEast() { return _east_space; } void setEast(MazeSpace *east) { _east_space = east; } MazeSpace *getSouth() { return _south_space; } void setSouth(MazeSpace *south) { _south_space = south; } MazeSpace *getWest() { return _west_space; } void setWest(MazeSpace *west) { _west_space = west; } string toString() { ostringstream output{}; string north_id = "None"; string south_id = "None"; string east_id = "None"; string west_id = "None"; if(_north_space != nullptr) { } return ""; /* output << _id << " N:" << _north_space->getId() << " " << " E: " << _ */ } }; #endif // MAZE_SPACE_H
__MACOSX/Lab 6/._MazeSpace.h
Lab 6/StringSplitter copy.h
#ifndef STRINGSPLITTER_H #define STRINGSPLITTER_H #include <string> #include <vector> using namespace std; class StringSplitter { public: //Breaks apart the supplied text based on the given delimiter static vector<string> split(string text, string delimiter) { //vectors are dynamically expanding arrays vector<string> pieces; //find the first delimiter int location = text.find(delimiter); //we are starting at the beginning of our string int start = 0; //go until we have no more delimiters while(location != string::npos) { //add the current piece to our list of pieces string piece = text.substr(start, location - start); pieces.push_back(piece); //update our index markers for the next round start = location + 1; location = text.find(delimiter, start); } //at the end of our loop, we're going to have one trailing piece to take care of. //handle that now. string piece = text.substr(start, location - start); pieces.push_back(piece); //now, return the completed vector return pieces; } }; #endif