-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
82 lines (70 loc) · 2.44 KB
/
main.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
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
#include <iostream>
#include <pthread.h>
#include <mutex>
#include "Solver.h"
#include "StringReverser.h"
#include "FileCacheManager.h"
#include "MyTestClientHandler.h"
#include "MySerialServer.h"
#include "MyParallelServer.h"
#include "MyClientHandler.h"
#include "SearchableMatrix.h"
#include "BestFirstSearch.h"
#include "AStar.h"
#include "DepthFirstSearch.h"
#include "SolverToSearcherAdapter.h"
std::string GetPath(State<std::pair<int,int>>* go) {
State<std::pair<int,int>>* current = go;
State<std::pair<int,int>>* previous = current->getCameFrom();
std::string result;
while (previous != nullptr) {
std::pair<int,int> current_pos = current->getState();
std::pair<int,int> previous_pos = previous->getState();
if (current_pos.first == previous_pos.first + 1 ) {
result.insert(0, ",down");
} else if (current_pos.first == previous_pos.first - 1) {
result.insert(0, ",up");
} else if (current_pos.second == previous_pos.second - 1) {
result.insert(0, ",left");
} else if (current_pos.second == previous_pos.second + 1) {
result.insert(0, ",right");
}
current = previous;
previous = current->getCameFrom();
}
result.erase(0,1);
return result;
}
int main(int argc, char** argv) {
/* Trying to get port from client. */
int port;
if (argc == 1) {
//perror("No port provided.\n");
exit(0);
} else if (argc == 2) {
try {
port = std::stoi(argv[1]);
} catch (std::exception &e) {
//perror("Bad port provided.\n");
exit(0);
}
} else {
//perror("To many arguments provided.\n");
exit(0);
}
/* Creates searcher as a solver. */
ISearcher<std::pair<int, int> , State<std::pair<int,int>>*>* searcher;
searcher = new BestFirstSearch<std::pair<int, int> , State<std::pair<int,int>>*>;
Solver<ISearchable<std::pair<int, int>> *,State<std::pair<int, int>> *> *solver
= new SolverToSearcherAdapter(searcher);
/* Creates CacheManager and ClientHandler. */
CacheManager* cacheManager = new FileCacheManager("matrix.txt");
MyClientHandler myClientHandler(solver, cacheManager);
/* Create and call server. */
MyParallelServer myParallelServer;
myParallelServer.open(port, &myClientHandler);
/* Delete used space. */
delete(searcher);
delete(solver);
delete(cacheManager);
};