-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixSolver.cpp
58 lines (51 loc) · 1.77 KB
/
MatrixSolver.cpp
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
//
// Created by yuvallevy on 16/01/2020.
//
#include "MatrixSolver.h"
using namespace std;
MatrixSolver::MatrixSolver(Searcher<Point> *myAlgorithmSearcher) : myAlgorithmSearcher(myAlgorithmSearcher) {
}
// this is our Object Adopter.
string MatrixSolver::solve(Searchable<Point> *problem) {
vector<State<Point>* > myPath;
// calling the algorithm to solve the problem.
myPath = this->myAlgorithmSearcher->search(problem);
return returnPath(myPath);
}
// return a string which will show the path from the initial state to the goal as a string.
// can be: RIGHT,LET,UP,DOWN and the cost each time.
string MatrixSolver::returnPath(vector<State<Point> *> myPath) {
if(myPath.empty()){
return "No Path Found.";
}
string myStringSolution;
int sizeOfPath = myPath.size();
// going over the states.
int count = (myPath.at(0)->getCost());
for(int i=0; i < sizeOfPath -1; i++){
count+=int(myPath.at(i+1)->getCost());
myStringSolution.append(returnNextMove(myPath.at(i)->getState(), myPath.at(i+1)->getState()));
myStringSolution.append("(");
myStringSolution.append(to_string(count));
myStringSolution.append(")");
myStringSolution.append(",");
}
myStringSolution.erase(myStringSolution.size()-1);
return myStringSolution;
}
// return the next move as string.
string MatrixSolver::returnNextMove(Point prevPoint, Point thisPoint) {
int prevLeft = prevPoint.getX();
int prevRight = prevPoint.getY();
int currLeft = thisPoint.getX();
int currRight = thisPoint.getY();
if(prevLeft < currLeft){
return "DOWN";
} else if (prevLeft > currLeft){
return "UP";
} else if (prevRight < currRight){
return "RIGHT";
} else{
return "LEFT";
}
}