diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..412eeda --- /dev/null +++ b/.gitattributes @@ -0,0 +1,22 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp +*.sln merge=union +*.csproj merge=union +*.vbproj merge=union +*.fsproj merge=union +*.dbproj merge=union + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b9d6bd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,215 @@ +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results + +[Dd]ebug/ +[Rr]elease/ +x64/ +build/ +[Bb]in/ +[Oo]bj/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.scc + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.Publish.xml +*.pubxml + +# NuGet Packages Directory +## TODO: If you have NuGet Package Restore enabled, uncomment the next line +#packages/ + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml +*.pfx +*.publishsettings + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + +############# +## Windows detritus +############# + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac crap +.DS_Store + + +############# +## Python +############# + +*.py[co] + +# Packages +*.egg +*.egg-info +dist/ +build/ +eggs/ +parts/ +var/ +sdist/ +develop-eggs/ +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg diff --git a/Arg_handles.h b/Arg_handles.h new file mode 100644 index 0000000..d3400c0 --- /dev/null +++ b/Arg_handles.h @@ -0,0 +1,21 @@ +#ifndef ARGH +#define ARGH + +#include // includes the getopt command for input flags + +class option long_options[] = { + {"version", no_argument, 0, 'v' }, + {"help", no_argument, 0, 'h' }, + {"size", required_argument, 0, 's' }, + {"columns", required_argument, 0, 'c' }, + {"rows", required_argument, 0, 'r'}, + {"input", required_argument, 0, 'i' }, + {"output", required_argument, 0, 'o' }, + {"check", no_argument, 0, 'b' }, + {"path", no_argument, 0, 'p' }, + {0, 0, 0, 0 } +}; + + +#endif +/* Arg_handles.h */ diff --git a/CPP_Sublime_Project.sublime-project b/CPP_Sublime_Project.sublime-project new file mode 100644 index 0000000..e2aa321 --- /dev/null +++ b/CPP_Sublime_Project.sublime-project @@ -0,0 +1,8 @@ +{ + "folders": + [ + { + "path": "/media/simple/Storage/Documents/Programming/CPP_Project" + } + ] +} diff --git a/CPP_Sublime_Project.sublime-workspace b/CPP_Sublime_Project.sublime-workspace new file mode 100644 index 0000000..9f04274 --- /dev/null +++ b/CPP_Sublime_Project.sublime-workspace @@ -0,0 +1,263 @@ +{ + "auto_complete": + { + "selected_items": + [ + [ + "I", + "IntArray" + ], + [ + "pr", + "print" + ], + [ + "linked", + "linked_list" + ], + [ + "is", + "is_empty" + ], + [ + "pop", + "pop_front" + ], + [ + "list", + "list1000" + ], + [ + "link", + "linked_list" + ] + ] + }, + "buffers": + [ + { + "file": "Maze.h", + "settings": + { + "buffer_size": 396, + "line_ending": "Windows" + } + }, + { + "file": "main.cpp", + "settings": + { + "buffer_size": 354, + "line_ending": "Windows" + } + } + ], + "build_system": "", + "command_palette": + { + "height": 0.0, + "selected_items": + [ + ], + "width": 0.0 + }, + "console": + { + "height": 0.0 + }, + "distraction_free": + { + "menu_visible": true, + "show_minimap": false, + "show_open_files": false, + "show_tabs": false, + "side_bar_visible": false, + "status_bar_visible": false + }, + "file_history": + [ + "/home/simple/Documents/Programming/CPP_Project/Int_Array.cpp", + "/home/simple/Documents/Programming/CPP_Project/Linked_list.cpp", + "/media/simple/Storage/Documents/Operativsystem/OS anteckningar", + "/home/simple/Documents/Programming/woop/test.cpp", + "/home/simple/Documents/Linux_commands", + "/home/simple/Documents/Programming/woop/bashscript.sh", + "/media/simple/Storage/Documents/Operativsystem/Latex/Linux labb.tex", + "/home/simple/Documents/Programming/Laboration_4/main.cpp", + "/media/simple/Storage/Documents/table", + "/home/simple/Documents/Programming/bashscript.sh", + "/home/simple/Documents/Programming/Laboration_3/main.cpp", + "/home/simple/Documents/Programming/woop/Makefile", + "/home/simple/Documents/Programming/Laboration_4/Timer.h", + "/home/simple/Documents/Programming/Laboration_4/Timer.cpp", + "/home/simple/Documents/Programming/Laboration_4/sort_functions.h", + "/home/simple/Documents/Programming/Laboration_4/linked_list.h", + "/home/simple/Documents/Programming/Laboration_4/sort_functions.cpp", + "/home/simple/Documents/Programming/Laboration_4/linked_list.cpp", + "/home/simple/Documents/Programming/Laboration_4/intArray.cpp", + "/home/simple/Documents/Programming/Laboration_4/intArray.h", + "/home/simple/Documents/Programming/Laboration_4/Makefile", + "/home/simple/Documents/Programming/Laboration_3/Timer.cpp", + "/home/simple/Documents/Programming/Laboration_3/Timer.h", + "/home/simple/Documents/Programming/Laboration_3/Makefile", + "/home/simple/Documents/Programming/Laboration_3/linked_list.h", + "/home/simple/Documents/Programming/Laboration_3/linked_list.cpp", + "/home/simple/Documents/Documents/Programming/Laboration_3/Makefile", + "/home/simple/Documents/Documents/Programming/Laboration_3/linked_list.h", + "/home/simple/Documents/Documents/Programming/Laboration_3/linked_list.cpp", + "/home/simple/Documents/Documents/Programming/Laboration_3/main.cpp", + "/home/simple/Documents/labb1_cpp/names.txt" + ], + "find": + { + "height": 35.0 + }, + "find_in_files": + { + "height": 0.0, + "where_history": + [ + ] + }, + "find_state": + { + "case_sensitive": false, + "find_history": + [ + ");", + "clock_gettime", + "front" + ], + "highlight": true, + "in_selection": false, + "preserve_case": false, + "regex": false, + "replace_history": + [ + ], + "reverse": false, + "show_context": true, + "use_buffer2": true, + "whole_word": false, + "wrap": true + }, + "groups": + [ + { + "selected": 1, + "sheets": + [ + { + "buffer": 0, + "file": "Maze.h", + "settings": + { + "buffer_size": 396, + "regions": + { + }, + "selection": + [ + [ + 0, + 0 + ] + ], + "settings": + { + "syntax": "Packages/C++/C++.tmLanguage" + }, + "translation.x": 0.0, + "translation.y": 0.0, + "zoom_level": 1.0 + }, + "type": "text" + }, + { + "buffer": 1, + "file": "main.cpp", + "settings": + { + "buffer_size": 354, + "regions": + { + }, + "selection": + [ + [ + 0, + 0 + ] + ], + "settings": + { + "syntax": "Packages/C++/C++.tmLanguage" + }, + "translation.x": 0.0, + "translation.y": 0.0, + "zoom_level": 1.0 + }, + "type": "text" + } + ] + } + ], + "incremental_find": + { + "height": 0.0 + }, + "input": + { + "height": 0.0 + }, + "layout": + { + "cells": + [ + [ + 0, + 0, + 1, + 1 + ] + ], + "cols": + [ + 0.0, + 1.0 + ], + "rows": + [ + 0.0, + 1.0 + ] + }, + "menu_visible": true, + "replace": + { + "height": 0.0 + }, + "save_all_on_build": true, + "select_file": + { + "height": 0.0, + "selected_items": + [ + ], + "width": 0.0 + }, + "select_project": + { + "height": 0.0, + "selected_items": + [ + ], + "width": 0.0 + }, + "show_minimap": true, + "show_open_files": false, + "show_tabs": true, + "side_bar_visible": true, + "side_bar_width": 150.0, + "status_bar_visible": true +} diff --git a/Getopt.h b/Getopt.h new file mode 100644 index 0000000..f886a5f --- /dev/null +++ b/Getopt.h @@ -0,0 +1,10 @@ +#ifndef GETOPTH +#define GETOPTH +struct option { + const char *name; + int has_arg; + int *flag; + int val; +}; + +#endif \ No newline at end of file diff --git a/Inputs.cpp b/Inputs.cpp new file mode 100644 index 0000000..35856fc --- /dev/null +++ b/Inputs.cpp @@ -0,0 +1,17 @@ +#include +#include "Inputs.h" +#include +int Check_valid_size(std::string value){ + int size; + // If it cannot be converted to an int, return false + if (!(size = std::stoi(value))) + return 0; + // If it's a negative number and less than 3, convert it to positive + if (size < -3) + size *= -1; + // If size is in the intervall [3, -3], return false + if (size < 3 && size > -3) + return 0; + return size; +} + diff --git a/Inputs.h b/Inputs.h new file mode 100644 index 0000000..10a53dc --- /dev/null +++ b/Inputs.h @@ -0,0 +1,7 @@ +#ifndef INPUTSH +#define INPUTSH + +int Check_valid_size(std::string value); + +#endif +/* MAZE.H */ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..35a8bb0 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +FILES = main.cpp Maze.cpp Inputs.cpp +OUT_EXE = out_Executable + +all: $(OUT_EXE) + +$(OUT_EXE): $(FILES) Maze.h Arg_handles.h Inputs.h + g++ -std=c++11 -o $(OUT_EXE) $(FILES) +clean: + rm -f *.o core + +rebuild: clean $(OUT_EXE) + ./$(OUT_EXE) diff --git a/Maze.cpp b/Maze.cpp new file mode 100644 index 0000000..9557a93 --- /dev/null +++ b/Maze.cpp @@ -0,0 +1,240 @@ +#include +#include "Maze.h" +#include +#include // EOF() +#include + +typedef unsigned int Uint; +const char Maze::WALL = '#'; +const char Maze::PATH = ' '; +const char Maze::SOLVEDPATH = '*'; +const char Maze::END = 'E'; +const char Maze::START = 'S'; +const int Maze::STARTposx = 1; +const int Maze::STARTposy = 1; + + +void Maze::Create_maze(Uint row, Uint col){ + if (row%2 == 0) + row++; + if (col%2 == 0) + col++; + + // Fill the vector with WALLs + Map = std::vector< std::vector >(row, std::vector(col, WALL)); + // Number of cells in the grid/map + TotalCells = row*col; + // Mark first CurrentCell as (STARTposx, STARTposy) + CurrentCell = std::make_pair(STARTposx,STARTposy); + // Set visited cells to 1 + VisitedCells = 1; + Generate_maze(CurrentCell); +} + +void Maze::Generate_maze(std::pair CurrentCell){ + srand (time(NULL)); + int RandElement; + while (VisitedCells < TotalCells){ + + // To check the direction of neighbour + x = CurrentCell.first, y = CurrentCell.second; + Map[x][y] = PATH; + + // NeighboursToVisit is an vector > + NeighboursToVisit.clear(); + NeighboursToVisit = Find_neighbours(CurrentCell); + + if (!NeighboursToVisit.empty()){ + // RandElement is the index to access a random neighbour + RandElement = rand()%NeighboursToVisit.size(); + + // Keep track of the direction of where to remove the wall + Direction = Check_direction(x, y, NeighboursToVisit); + + // Knock down the wall between this random cell and CurrentCell + Crush_wall(NeighboursToVisit[RandElement], Direction[RandElement]); + + // push CurrentCell location on the CellStack + CellStack.push(std::make_pair(CurrentCell.first, CurrentCell.second)); + + // make the new cell CurrentCell + CurrentCell = std::make_pair(NeighboursToVisit[RandElement].first, NeighboursToVisit[RandElement].second); + VisitedCells++; + } + else{ + if(!CellStack.empty()){ + // make the top of the cellstack to CurrentCell + CurrentCell = CellStack.top(); + // pop the most recent cell entry off the CellStack + CellStack.pop(); + } + else + VisitedCells = TotalCells; + } + } + // Set end position + Map[Map.size()-2][Map[Map.size()-2].size()-2] = END; + Map[1][1] = START; +} + +// x, y is the previous value of CurrentCell +std::vector Maze::Check_direction(Uint x, Uint y, std::vector > v){ + std::vector dir; + int dx, dy; + for (int i = 0; i < v.size(); i++) + { + dx = (v[i].first - x); // Computing the direction + dy = (v[i].second - y); // Computing the direction + //std::cout << "Delta:" << dx << " " << dy << std::endl; + + if (dx == 2) // If east + dir.push_back(1); // Push 1 (east by the enumeration) + else if (dx == -2) // if west + dir.push_back(3); // Push 3 (west by the enumeration) + else if (dy == -2) // if north + dir.push_back(0); // push 0 (north by the enumeration) + else if (dy == 2) // if south + dir.push_back(2); // push 2 (south by the enumeration) + //std::cout << dir.back() << std::endl; + } + return dir; +} + +std::vector< std::pair > Maze::Find_neighbours(std::pair CurrentCell){ + std::vector > v; + CurrentCell.first+=2; // East of original Cell + // If walls intact of the neighbour to the east of original cell + if (Walls_intact(CurrentCell)) + v.push_back(CurrentCell); + + // If walls intact of the neighbour to the west of original cell + CurrentCell.first-=4; // West of original cell + if (Walls_intact(CurrentCell)) + v.push_back(CurrentCell); + + CurrentCell.first+=2; // Back to original cell + // If walls intact of the neighbour to the south of original cell + CurrentCell.second+=2; // South of original cell + if (Walls_intact(CurrentCell)) + v.push_back(CurrentCell); + + // If walls intact of the neighbour to the north of original cell + CurrentCell.second-=4; // North of original cell + if (Walls_intact(CurrentCell)) + v.push_back(CurrentCell); + + //CurrentCell.second-=2; // Back to original cell + // Return a vector with all neighbours with all walls intact + return v; +} + +bool Maze::Walls_intact(std::pair CurrentCell){ // Returns wether all walls intact or not + int row = CurrentCell.first; + int col = CurrentCell.second; + if (col < 1 || row < 1) + return false; + // If it's outside the map + if(row < Map.size()-1 && col < Map[row].size()-1){ + // If all walls intact, return true + if( + (Map[row+1][col] == WALL) && // check east wall + (Map[row-1][col] == WALL) && // check west wall + (Map[row][col-1] == WALL) && // check north wall + (Map[row][col+1] == WALL)){ // check south wall + return true; + } + } + return false; +} + +void Maze::Crush_wall(std::pair CurrentCell, int dir){ + // dir is a variable obtained from Check_direction() + if (dir == north){ + Map[CurrentCell.first][CurrentCell.second+1] = PATH; // set north of CurrentCell to path + } + else if (dir == east){ + Map[CurrentCell.first-1][CurrentCell.second] = PATH; // set east of CurrentCell to path + } + else if (dir == south){ + Map[CurrentCell.first][CurrentCell.second-1] = PATH; // set south of CurrentCell to path + } + else if (dir == west){ + Map[CurrentCell.first+1][CurrentCell.second] = PATH; // set west of CurrentCell to path + } +} + +void Maze::Print_maze(std::string file) const{ + std::ofstream outfile(file.c_str()); + // if file is open, write to file otherwise write to console + std::ostream &out= (!outfile.is_open()? std::cout : outfile); + // Check if file failed to open + if(file != "" && (!outfile.is_open())) + std::cerr << "File failed to open." << std::endl; + for (int i = 0; i < Map.size(); i++) + { + for (int j = 0; j < Map[i].size(); j++) + { + out << Map[i][j]; + } + out << std::endl; + } + if(outfile.is_open()) + outfile.close(); +} + +bool Maze::Solve(int x, int y){ + // If x, y is outside maze, return false + if (x >= Map.size() || x < 0 || y < 0 || y >= Map[Map.size()-1].size()){ + return false; + } + // If x, y is the goal/end, return true + if (Map[x][y] == END){ + return true; + } + // If x, y is not open, return false + if (Map[x][y] != PATH && Map[x][y] != START) + return false; + // Mark x, y part of the solution + Map[x][y] = SOLVEDPATH; + + // If Solve North of x, y is true, return true + if (Solve(x, y-1) == true) return true; + // If Solve East of x, y is true, return true + if (Solve(x+1, y) == true) return true; + // If Solve South of x, y is true, return true + if (Solve(x, y+1) == true) return true; + // If Solve West of x, y is true, return true + if (Solve(x-1, y) == true) return true; + Map[x][y] = PATH; + return false; +} + +bool Maze::Read_file(std::string file){ + Map.clear(); // Make sure it's cleared + int j(0); + std::string line; + std::ifstream infile(file); // The input file + if (infile.is_open()) { // Make sure the file is open + while (!infile.eof()) { // until we're at the end of the file + std::getline(infile, line); // Take line by line + if(line.empty()) + continue; + // Push vector to make it a vector > + Map.push_back(std::vector()); + for (int i = 0; i < line.length(); i++){ + // If invalid character found, return false and give error message. + if ((line[i] != WALL) && (line[i] != PATH) && (line[i] != SOLVEDPATH) && (line[i] != START) && (line[i] != END)){ + std::cerr << "File consists of invalid characters." << std::endl; + return false; + } + // Add next element + Map[j].push_back(line[i]); + } + j++; + } + infile.close(); + return true; + } + std::cerr << "File failed to be open." << std::endl; + return false; +} \ No newline at end of file diff --git a/Maze.h b/Maze.h new file mode 100644 index 0000000..36614b8 --- /dev/null +++ b/Maze.h @@ -0,0 +1,63 @@ +#ifndef MAZEH +#define MAZEH +#include +#include // std::pair +#include +#include + +class Maze{ +public: + enum { north, east, south, west }; + static const char WALL, PATH, END, START, SOLVEDPATH; + static const int STARTposx, STARTposy; + unsigned int VisitedCells, TotalCells, x, y; + + + // sets total cells and fill the map with WALLS, calls Generate_maze + void Create_maze(unsigned int row, unsigned int col); + + // Self explantory, prints the maze + //void Print_maze(std::string file = "no!ne") const; + void Print_maze(std::string file = "") const; + + // Solves the maze with recurison + bool Solve(int x = 1, int y = 1); + + // Reads a file into a vector > + bool Read_file(std::string file); + + +private: + // Pair for the current cell + std::pair CurrentCell; + + // CellStack to hold list of all cell locations + std::stack< std::pair > CellStack; + + // Vector for which neighbours to visit + std::vector< std::pair > NeighboursToVisit; + + // Keep track of the direction of where to remove the wall + std::vector Direction; + + // Calls Walls_intact and returns neighbours with walls intact + void Generate_maze(std::pair CurrentCell); + + // Check whether the walls are still there or already crushed + bool Walls_intact(std::pair CurrentCell); + + // Crush a wall + void Crush_wall(std::pair CurrentCell, int dir); + + // Calls Find_neighbours and walks to next cell and crush the wall between + std::vector< std::pair > Find_neighbours(std::pair CurrentCell); + + // Find the direction between two cells, this will be applied with the Crush_wall function + std::vector Check_direction(unsigned int x, unsigned int y, std::vector > v); + + // vector with chars to indicate the maze map + std::vector< std::vector > Map; +}; + +#endif +/* MAZE.H */ diff --git a/hej.txt b/hej.txt new file mode 100644 index 0000000..fe64246 --- /dev/null +++ b/hej.txt @@ -0,0 +1,15 @@ +############### +#*****# # +#####*####### # +# #*******# # +### #######*# # +# # # #***# +# # # # # ###*# +# # # # #***# +# ##### # #*### +# # #***# +# ####### ###*# +# # # #*# +### # # ### #*# +# # # E# +############### diff --git a/hej2 b/hej2 new file mode 100644 index 0000000..05f8649 --- /dev/null +++ b/hej2 @@ -0,0 +1,18 @@ + +############### +#S # # +##### ####### E +# # # # + +### ####### # # +# # # # # +# # # # # ### # +# # # # # # +# ##### # # ### +# # # # +# ####### ### # +# # # # # + +### # # ### # # +# # # # +############### diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..3499c43 --- /dev/null +++ b/main.cpp @@ -0,0 +1,164 @@ +#include "Maze.h" +#include "Inputs.h" +#include "Arg_handles.h" +#include // includes the getopt command for input flags +#include /* for printf */ +#include // for EXIT_FAILURE and EXIT_SUCCESS +#include +#include +#include +#include + + + +int main(int argc, char **argv) +{ + // object for the class Maze + Maze labyrinth; + // FileName is the name to print the maze to + std::string FileName = ""; + // CheckFlag is to check if -b is called + bool Printing = false, CreateMaze = false, Solving = false, CheckFlag = false; + // Version number + std::string Version = "1.0.1"; + // Initializing values for row and col + int c, col(3), row(3); + int digit_optind = 0; + + while (1) { + int this_option_optind = optind ? optind : 1; + int option_index = 0; + // Flags for input, colon equals argument taken + c = getopt_long(argc, argv, "vhpbs:c:r:i:o:",long_options, &option_index); + if (c == -1) + break; + + switch (c) { + case 'v': // Version + std::cout << "Version: " << Version << std::endl; + //printf("Version: %s\n", Version.c_str()); + return 1; + break; + + case 'h': // Help + /*printf("\n\tProgram supports following commands:\n"); + printf("\033[1;32m--version \t| -v\033[0m \t- prints the current version\n"); + printf("\033[1;32m--help \t\t| -h\033[0m \t- Lists all possible commands\n"); + printf("\033[1;32m--size \t\t| -s\033[0m (NxN size) - Generates a labyrinth with size NxN\n"); + printf("\033[1;32m--columns \t| -c\033[0m (W-width) \t- defines width of the labyrint to be generated\n"); + printf("\033[1;32m--rows \t\t| -r\033[0m (H-height) - defines height of the labyrint to be generated\n"); + printf("\033[1;32m--input \t| -i\033[0m (File) \t- Use a input file instead of generating a maze\n"); + printf("\033[1;32m--output \t| -o\033[0m (File) \t- Print the labyrint to a file\n"); + printf("\033[1;32m--check \t| -b\033[0m \t- Check whether there's a solution or not\n"); + printf("\033[1;32m--path \t\t| -p\033[0m \t- Find the exit\n\n");*/ + std::cout << std::endl << "\tProgram supports following commands:" << std::endl; + std::cout << "\033[1;32m--version \t| -v\033[0m \t- prints the current version" << std::endl; + std::cout << "\033[1;32m--help \t\t| -h\033[0m \t- Lists all possible commands" << std::endl; + std::cout << "\033[1;32m--size \t\t| -s\033[0m (NxN size) - Generates a labyrinth with size NxN" << std::endl; + std::cout << "\033[1;32m--columns \t| -c\033[0m (W-width) \t- defines width of the labyrint to be generated" << std::endl; + std::cout << "\033[1;32m--rows \t\t| -r\033[0m (H-height) - defines height of the labyrint to be generated" << std::endl; + std::cout << "\033[1;32m--input \t| -i\033[0m (File) \t- Use a input file instead of generating a maze" << std::endl; + std::cout << "\033[1;32m--output \t| -o\033[0m (File) \t- Print the labyrint to a file" << std::endl; + std::cout << "\033[1;32m--check \t| -b\033[0m \t- Check whether there's a solution or not" << std::endl; + std::cout << "\033[1;32m--path \t\t| -p\033[0m \t- Find the exit" << std::endl << std::endl; + return 1; + break; + + case 's': // Size NxN + row = col = Check_valid_size(optarg); + if (col == 0 || row == 0){ + std::cout << "Enter a valid number (greater than 3) to represent the size of the maze" << std::endl; + break; + } + CreateMaze = true; + Printing = true; + break; + + case 'c': // Columns + col = Check_valid_size(optarg); + if (col == 0){ + std::cout << "Enter a valid number (greater than 3) to represent the width of the maze" << std::endl; + break; + } + CreateMaze = true; + Printing = true; + break; + + case 'r': // Rows + row = Check_valid_size(optarg); + if (row == 0){ + std::cout << "Enter a valid number (greater than 3) to represent the height of the maze" << std::endl; + break; + } + CreateMaze = true; + Printing = true; + break; + + case 'i': // Input file + labyrinth.Read_file(optarg); + Printing = true; + break; + + case 'o': // output file + FileName = optarg; + break; + + case 'b': // Check solution + CheckFlag = true; + break; + + case 'p': // print solution path + Solving = true; + break; + + case '?': + std::cout << "Type --h for info" << std::endl; + //printf("Type --h for info\n"); + break; + + default: + std::cout << "character " << c << " is not a valid choice" << std::endl << "Enter -h for help" << std::endl; + //printf("character %c is not a valid choice\nEnter -h for help\n", c); + } + } + if (optind < argc) { + printf("non-option ARGV-elements: "); + while (optind < argc) + printf("%s ", argv[optind++]); + printf("\n"); + } + + + //std::ofstream outfile(FileName.c_str()); + std::ofstream outfile(FileName); + // if file is open, write to file otherwise write to console + std::ostream &out= (!outfile.is_open()? std::cout : outfile); + // Check if file failed to open + if((FileName != "") && (!outfile.is_open())){ + std::cerr << "File failed to open." << std::endl; + return EXIT_FAILURE; + } + // CheckFlag sets to true if -b is called + if (CreateMaze) + labyrinth.Create_maze(col, row); + + if (CheckFlag){ + if(labyrinth.Solve()) + out << "Maze Solved.\n"; + else + out << "Maze could not be solved\n"; + if(outfile.is_open()) + outfile.close(); + return EXIT_SUCCESS; + } + if(outfile.is_open()) + outfile.close(); + + + if (Solving) + labyrinth.Solve(); + if (Printing) + labyrinth.Print_maze(FileName); + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/main3.cpp b/main3.cpp new file mode 100644 index 0000000..0effc8f --- /dev/null +++ b/main3.cpp @@ -0,0 +1,100 @@ +#include "Maze.h" +#include +#include +#include +#include // includes the getopt command for input flags +#include // for EXIT_FAILURE and EXIT_SUCCESS +#include "Inputs.h" +#include // File management + +int main(int argc, char const *argv[]) +{ + Maze labyrinth; + std::string choice, width, height, file; + int col, row; + std::cout << "\t -:: Welcome to Marcuz maze generator ::-" << std::endl; + while (choice != "3"){ // Simple user interface + std::cout + << "Choose option" << std::endl + << "[1] -\t Generate maze" << std::endl + << "[2] -\t Generate and solze maze" << std::endl + << "[3] -\t Use input-data" << std::endl + << "[4] -\t Use output-data" << std::endl + << "[5] -\t Exit program" << std::endl; + getline(std::cin, choice); + std::cout << std::endl; + if (choice.find('1') != std::string::npos){ // print maze + + + + std::cout << "Enter width of maze ...\n"; + std::getline(std::cin, width); + std::cout << "Enter height of maze ...\n"; + std::getline(std::cin, height); + row = Check_valid_size(height); + col = Check_valid_size(width); + if (row == 0 || col == 0){ + std::cout << "Enter a valid number (greater than 3) to represent the size of the maze" << std::endl; + } + if (row != 0 || col != 0){ + // Generate a maze + labyrinth.Create_maze(row, col); + labyrinth.Print_maze(); + } + + + + + + + }else if(choice.find('2') != std::string::npos){ // print and solve + std::cout << "Enter width of maze ...\n"; + std::getline(std::cin, width); + std::cout << "Enter height of maze ...\n"; + std::getline(std::cin, height); + row = Check_valid_size(height); + col = Check_valid_size(width); + // Generate a maze + labyrinth.Create_maze(row, col); + labyrinth.Solve(1, 1); + labyrinth.Print_maze(); + + + }else if(choice.find('3') != std::string::npos){ // print and solve + std::cout << "Enter name of file ...\n"; + std::getline(std::cin, file); + if (labyrinth.Read_file(file)){ + labyrinth.Solve(1, 1); + labyrinth.Print_maze(); + } + else{ + std::cout << "Error opening file" << std::endl; + } + + }else if(choice.find('4') != std::string::npos){ // print and solve + std::cout << "Enter name of file ...\n"; + std::getline(std::cin, file); + labyrinth.Create_maze(15, 15); + labyrinth.Solve(1, 1); + labyrinth.Print_maze(file); + + }else if(choice.find('5') != std::string::npos){ + return 0; + }else{ + std::cout << "Please choose between [1], [2] and [3]." << std::endl; + } + } + + + + + return 0; +} + + + + + + + + diff --git a/out_Executable b/out_Executable new file mode 100644 index 0000000..da575bb Binary files /dev/null and b/out_Executable differ diff --git a/test2 b/test2 new file mode 100644 index 0000000..ad69400 --- /dev/null +++ b/test2 @@ -0,0 +1 @@ +Maze Solved.