- #include <iostream>
- #include <cstring>
- #include <fstream>
- #include <string.h>
- using namespace std;
- #define MAX_FILENAME_LEN 30
- struct Maze
- {
- char* cells;
- int width;
- int length;
- };
- void write_maze(Maze &maze);
- void read_maze(bool &format_check, int &width, int &length, ifstream &maze_in);
- void solve();
- void open_input_file(ifstream &in, const char prompt[]);
- void determine_size(ifstream &in, int &width, int &length, bool &format_check);
- int main()
- {
- ifstream maze_in;
- bool format_check = true;
- int width = 0; int length = 0; int index = 0;
- char ch;
- Maze maze;
- read_maze(format_check, width, length, maze_in);
- if(!format_check)
- {
- cout << "There is an error in your input file, please check the file and \
- re-start program" << endl;
- }
- else
- {
- maze.width = width;
- maze.length = length;
- cout << "creating an array of size " << width * length << endl;
- maze.cells = new char[width * length];
- while(!maze_in.eof())
- {
- maze_in.get(ch);
- if(maze_in.eof())
- break;
- maze.cells[index] = ch;
- index++;
- }
- write_maze(maze);
- }
- }
- void write_maze(Maze &maze)
- {
- int index = 0;
- cout << endl;
- cout << maze.length << " " << maze.width << endl;
- cout << maze.length * maze.width << endl;
- for(int row = 0; row < maze.length; row++)
- {
- for(int col = 0; col <= maze.width; col++)
- {
- cout << maze.cells[index];
- index++;
- }
- }
- cout << "\n";
- cout.flush();
- }
- void read_maze(bool &format_check, int &width, int &length, ifstream &maze_in)
- {
- open_input_file(maze_in, "enter the name of the maze file :");
- determine_size(maze_in, width, length, format_check);
- //to do: find start location
- }
- void open_input_file(ifstream &in, const char prompt[])
- {
- bool success = false;
- char filename[MAX_FILENAME_LEN + 1];
- cout << prompt;
- cin.getline(filename, MAX_FILENAME_LEN + 1, '\n');
- in.open(filename);
- while(success == false)
- {
- if (in.fail())
- {
- cout << "couldn't open file" << endl;
- cout << prompt;
- cin.getline(filename, MAX_FILENAME_LEN + 1, '\n');
- in.open(filename);
- if(!in.fail())
- success = true;
- }
- else
- {
- success = true;
- }
- }
- }
- void determine_size(ifstream &in, int &width, int &length, bool &format_check)
- {
- char line_in[100];
- int first_width;
- in.getline(line_in, 100, '\n');
- length++;
- first_width = strlen(line_in);
- while(!in.eof() && format_check)
- {
- in.getline(line_in, 100, '\n');
- length++;
- width = strlen(line_in);
- if(width != first_width)
- {
- format_check = false;
- }
- }
- width = first_width;
- in.clear();
- in.seekg(0, ios::beg);
- }