-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.cpp
More file actions
89 lines (79 loc) · 2.29 KB
/
Maze.cpp
File metadata and controls
89 lines (79 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Maze.cpp: source file for Maze class
// Created by Michael Gosling on 1/28/19.
// Updated by Michael Gosling on 1/30/19.
#include "Maze.h"
#include <iostream>
#include <fstream>
#include <cstring>
Maze::Maze(std::string fileName) {
std::ifstream file;
std::string line;
rows = 0;
columns = 0;
// try to read the maze file
try {
file.open(fileName);
if (file.is_open()){
while(getline(file,line)){
if (columns == 0) {
bool lastColumnFound = false;
int i = (int) line.size();
while (!lastColumnFound) {
i--;
if (line[i] == '+')
lastColumnFound = true;
}
columns = i + 1;
}
rows++;
}
file.clear();
file.seekg(0, std::ios::beg);
line = "";
mazeArray = new char*[rows];
int row = 0;
while(getline(file, line)) {
char *lineArr = new char[columns];
for (unsigned long i = 0; i < columns; i++)
lineArr[i] = line[i];
mazeArray[row] = lineArr;
row++;
}
file.close();
} else
std::cout << "Couldn't open file" << std::endl;
} catch (std::exception e) {
std::cout << e.what() << std::endl;
}
}
Maze::~Maze() {
for (auto i = columns; i >= 0; i--)
mazeArray[i] = nullptr;
mazeArray = nullptr;
}
int Maze::getRows() { return rows; }
int Maze::getCols() { return columns; }
char Maze::getPosition(int row, int col) {
return mazeArray[row][col];
}
void Maze::setPosition(char symbol, int row, int col) {
mazeArray[row][col] = symbol;
}
void Maze::printMaze() {
for (auto i = 0; i < rows; i++)
std::cout << mazeArray[i] << std::endl;
}
void Maze::writeToFile(std::string fileName) {
std::ofstream file;
std::string line;
try {
file.open(fileName);
if (file.is_open()) {
for (auto i = 0; i < rows; i++)
file << mazeArray[i] << std::endl;
file.close();
}
} catch (std::exception e) {
std::cout << e.what() << std::endl;
}
}