-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
75 lines (67 loc) · 1.88 KB
/
Model.cpp
File metadata and controls
75 lines (67 loc) · 1.88 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
// Or Basker 316388743
// Nave Maymon 318887965
#include "Model.h"
MazeModel::MazeModel()
{
}
void MazeModel::generateMaze(const std::string &name, int rows, int cols)
{
SimpleMaze2dGenerator generator;
Maze2d generatedMaze = generator.generate(rows, cols);
addMaze(name, generatedMaze);
}
const Maze2d &MazeModel::getMaze(const std::string &name) const
{
return mazes_.at(name);
}
void MazeModel::solveMaze(const std::string &name, const std::string &algorithm)
{
if (mazes_.find(name) == mazes_.end())
{
throw std::invalid_argument("Maze not found");
}
if (algorithm == "BFS" || algorithm == "bfs")
{
BFS<std::pair<int, int>> bfs;
cout << "Solving the following maze using BFS" << endl;
mazes_.at(name).print();
Maze2dSearchable searchable(mazes_.at(name));
Solution<std::pair<int, int>> result = bfs.search(searchable);
// result.printSolution();
Solution<std::pair<int, int>> *newSolution = new Solution<std::pair<int, int>>(result);
solutions_[name] = newSolution;
cout << "Solution added to map" << endl;
}
else
{
throw std::invalid_argument("Invalid algorithm");
}
}
Solution<std::pair<int, int>> *MazeModel::getSolution(const std::string &name)
{
if (solutions_.find(name) == solutions_.end())
{
throw std::invalid_argument("Solution not found");
}
cout << "Solution found" << endl;
return solutions_.at(name);
}
std::unordered_map<std::string, Maze2d> &MazeModel::getMazes()
{
return mazes_;
}
const std::unordered_map<std::string, Solution<std::pair<int, int>> *> &MazeModel::getSolutions() const
{
return solutions_;
}
void MazeModel::addMaze(const std::string &name, const Maze2d &maze)
{
try
{
mazes_[name] = maze;
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n';
}
}