Skip to content

Commit

Permalink
code update
Browse files Browse the repository at this point in the history
  • Loading branch information
mucylulu committed Jul 24, 2024
1 parent 4ebe24a commit ef02124
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 137 deletions.
23 changes: 23 additions & 0 deletions algorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef _algorithm_h
#define _algorithm_h
#include "map.h"
#include "mnode.h"
#include <map>
class Algorithm
{
private:
Map aMap;
MNode startNode;
MNode endNode;

public:
Algorithm() = default;
Algorithm(Map& _map):aMap(_map){}
Algorithm(Map& _map, MNode& _start, MNode& _end):aMap(_map),startNode(_start), endNode(_end){}
virtual void Resolve() = 0;
virtual void FindPath() = 0;
virtual ~Algorithm() = default;


};
#endif
119 changes: 0 additions & 119 deletions astar_algorithm.cpp
Original file line number Diff line number Diff line change
@@ -1,119 +0,0 @@
#include <algorithm>
#include <chrono>
#include <curses.h>
#include <iostream>
#include <list>
#include <sstream>
#include <thread>
#include <vector>
#include "mnode.h"
#include "debug_tool.h"
#include "astar_algorithm.h"
using std::cout;
using std::endl;
using std::stringstream;
using std::vector;
using std::find_if;
using std::make_pair;

static map<ENodeType, list<pair<int,int>>> ElementsMap =
{
{ENodeType::BARRIER,
{
{13,4},{14,4},{15,4}, {15,4},{16,4}, {17,4},{18,4},{19,4},
{13,5},{14,5},{15,5}, {15,5},{16,5}, {17,5},{18,5},{19,5},
{0,4},{1,4},{2,4}, {3,4},{4,4}, {5,4},{6,4},{7,4},{8,4},{9,4},{10,4},{11,4},{11,5},{11,6},{11,7},{11,8},{11,9},{11,10},{11,11},
{0,5},{1,5},{2,5}, {3,5},{4,5}, {5,5},{6,5},{7,5},
}
},
{ENodeType::WATER,
{
{2,12},{3,12},{4,12},{5,12},{6,12},{7, 12}, {8,12}, {9,12},{10,12},{11,12},{12,12},
{2,13},{3,13},{4,13},{5,13},{6,13},{7, 13}, {8,13}, {9,13},{10,13},{11,13},{12,13}
}
}
};

void ConstructMap(Map &aMap)
{
if(aMap.Size() > 0)
{
//cout<<endl;
//cout<<"constsruct--------------------------------------------------start"<<endl;
for (pair<ENodeType, list<pair<int, int>>> ePair : ElementsMap)
{
for (pair<int, int> miPair : ePair.second)
{
MNode& node = aMap.GetNode(miPair.first, miPair.second);
node.NTypeSetter(ePair.first);
//cout<<node.NType<<"$->"<<node.str<<",";
aMap.Draw(node, EDrawType::TYPE);
}
}
//cout<<endl;
//cout<<"constsruct--------------------------------------------------end"<<endl;
refresh();
}
}

void BFS(Map &aMap, const MNode &startNode, const MNode &endNode, map<int, int>& originMap)
{
if(!aMap.NodeCheck(startNode) || !aMap.NodeCheck(endNode) || aMap.Size() <= 0)
{
stringstream ss;
ss<<"invalid start node:"<<startNode.ToString()<<" or end node:"<<endNode.ToString()<<endl;
cout<<ss.str();
endwin();
return;
}
list<MNode> waveList;
waveList.push_back(startNode);
vector<MNode> reachedVector;
reachedVector.push_back(startNode);
list<MNode> nbList;
while(!waveList.empty())
{
MNode ckNode = waveList.front();
if(!ckNode.IsSamePos(startNode) && !ckNode.IsSamePos(endNode))
{
ckNode.NStateSetter(ENodeState::FINDDING);
aMap.Draw(ckNode, EDrawType::STATE);
}
nbList = aMap.GetNeighbors(ckNode, nbList);

//map.Draw(nbList);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
//refresh();
for ( MNode nextNode: nbList)
{
//如果还没有检查过,插入到下一个要找的步骤
auto checkedFun = [nextNode](MNode node){return node.IsSamePos(nextNode); };
if(originMap.find(aMap.GetNodeNum(nextNode)) == originMap.cend())
{
originMap.insert(make_pair<int, int>(aMap.GetNodeNum(nextNode), aMap.GetNodeNum(ckNode)));
}
if(find_if(reachedVector.cbegin(), reachedVector.cend(), checkedFun) == reachedVector.cend())
{
waveList.push_back(nextNode);
if(!nextNode.IsSamePos(startNode) && !nextNode.IsSamePos(nextNode))
{
nextNode.NStateSetter(ENodeState::NEXT);
}
reachedVector.push_back(nextNode);
}
}
waveList.pop_front();
//map.Draw(nbList);
refresh();
}
}

void Dijkstra(const Map &map, const MNode &startNode, const MNode &endNode)
{

}

void AStar(const Map &map, const MNode &startNode, const MNode &endNode)
{

}
23 changes: 12 additions & 11 deletions astar_algorithm.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#ifndef ASTAR_ALGORITHM_H
#define ASTAR_ALGORITHM_H
#include<queue>
#ifndef _ASTAR_ALGORITHM_H
#define _ASTAR_ALGORITHM_H
#include "algorithm.h"
#include "map.h"
#include "mnode.h"

void ConstructMap(Map& aMap);

void BFS(Map& aMap, const MNode& startNode, const MNode& endNode, map<int, int>&);
void Dijkstra(const Map& aMap, const MNode& startNode, const MNode& endNode);
void AStar(const Map& aMap, const MNode& startNode, const MNode& endNode);

class AStarAlgorithm : public Algorithm
{
public:
AStarAlgorithm() = default;
AStarAlgorithm(Map& _map):Algorithm(_map){}
AStarAlgorithm(Map& _map, MNode& _start, MNode& _end):Algorithm(_map, _start, _end){}
void Resolve() override;
void FindPath() override;
};
#endif
122 changes: 122 additions & 0 deletions astar_algorithm1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include <algorithm>
#include <chrono>
#include <curses.h>
#include <iostream>
#include <list>
#include <sstream>
#include <thread>
#include <vector>
#include "mnode.h"
#include "debug_tool.h"
#include "astar_algorithm.h"
using std::cout;
using std::endl;
using std::stringstream;
using std::vector;
using std::find_if;
using std::make_pair;

static map<ENodeType, list<pair<int,int>>> ElementsMap =
{
{ENodeType::BARRIER,
{
{13,4},{14,4},{15,4}, {15,4},{16,4}, {17,4},{18,4},{19,4},
{13,5},{14,5},{15,5}, {15,5},{16,5}, {17,5},{18,5},{19,5},
{0,4},{1,4},{2,4}, {3,4},{4,4}, {5,4},{6,4},{7,4},{8,4},{9,4},{10,4},{11,4},{11,5},{11,6},{11,7},{11,8},{11,9},{11,10},{11,11},
{0,5},{1,5},{2,5}, {3,5},{4,5}, {5,5},{6,5},{7,5},
}
},
{ENodeType::WATER,
{
{2,12},{3,12},{4,12},{5,12},{6,12},{7, 12}, {8,12}, {9,12},{10,12},{11,12},{12,12},
{2,13},{3,13},{4,13},{5,13},{6,13},{7, 13}, {8,13}, {9,13},{10,13},{11,13},{12,13}
}
}
};

void ConstructMap(Map &aMap)
{
if(aMap.Size() > 0)
{
//cout<<endl;
//cout<<"constsruct--------------------------------------------------start"<<endl;
for (pair<ENodeType, list<pair<int, int>>> ePair : ElementsMap)
{
for (pair<int, int> miPair : ePair.second)
{
MNode& node = aMap.GetNode(miPair.first, miPair.second);
node.NTypeSetter(ePair.first);
//cout<<node.NType<<"$->"<<node.str<<",";
aMap.Draw(node, EDrawType::TYPE);
}
}
//cout<<endl;
//cout<<"constsruct--------------------------------------------------end"<<endl;
refresh();
}
}

void BFS(Map &aMap, const MNode &startNode, const MNode &endNode, map<int, int>& originMap)
{
if(!aMap.NodeCheck(startNode) || !aMap.NodeCheck(endNode) || aMap.Size() <= 0)
{
stringstream ss;
ss<<"invalid start node:"<<startNode.ToString()<<" or end node:"<<endNode.ToString()<<endl;
cout<<ss.str();
endwin();
return;
}
list<MNode> waveList;
waveList.push_back(startNode);
vector<MNode> reachedVector;
reachedVector.push_back(startNode);
list<MNode> nbList;
while(!waveList.empty())
{
MNode ckNode = waveList.front();
if(!ckNode.IsSamePos(startNode) && !ckNode.IsSamePos(endNode))
{
ckNode.NStateSetter(ENodeState::FINDDING);
aMap.Draw(ckNode, EDrawType::STATE);
}
nbList = aMap.GetNeighbors(ckNode, nbList);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
for ( MNode nextNode: nbList)
{
//如果还没有检查过,插入到下一个要找的步骤
auto checkedFun = [nextNode](MNode node){return node.IsSamePos(nextNode); };
if(originMap.find(aMap.GetNodeNum(nextNode)) == originMap.cend())
{
originMap.insert(make_pair<int, int>(aMap.GetNodeNum(nextNode), aMap.GetNodeNum(ckNode)));
}
if(find_if(reachedVector.cbegin(), reachedVector.cend(), checkedFun) == reachedVector.cend())
{
waveList.push_back(nextNode);
if(!nextNode.IsSamePos(startNode) && !nextNode.IsSamePos(nextNode))
{
nextNode.NStateSetter(ENodeState::NEXT);
}
reachedVector.push_back(nextNode);
}
}
waveList.pop_front();
refresh();
}
}

void FindBFSPath(const map<int, int> & originMap, const MNode& startNode, const MNode& endNode, vector<int>& pathVec)
{
if(originMap.size()>0)
{
}
}

void Dijkstra(const Map &map, const MNode &startNode, const MNode &endNode)
{

}

void AStar(const Map &map, const MNode &startNode, const MNode &endNode)
{

}
16 changes: 16 additions & 0 deletions astar_algorithm1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef ASTAR_ALGORITHM_H
#define ASTAR_ALGORITHM_H
#include<queue>
#include <vector>
#include "map.h"
#include "mnode.h"
using std::vector;

void ConstructMap(Map& aMap);

void BFS(Map& aMap, const MNode& startNode, const MNode& endNode, map<int, int>&);
void FindBFSPath(const map<int, int>&, const MNode& startNode, const MNode& endNode, vector<int>&);
void Dijkstra(const Map& aMap, const MNode& startNode, const MNode& endNode);
void AStar(const Map& aMap, const MNode& startNode, const MNode& endNode);

#endif
Empty file added bfs_algorithm.cpp
Empty file.
14 changes: 14 additions & 0 deletions bfs_algorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef _BFS_ALGORITHM_H
#define _BFS_ALGORITHM_H
#include "algorithm.h"
#include "map.h"
class BFSAlgorithm : public Algorithm
{
public:
BFSAlgorithm() = default;
BFSAlgorithm(Map& _map):Algorithm(_map){}
BFSAlgorithm(Map& _map, MNode& _start, MNode& _end):Algorithm(_map, _start, _end){}
void Resolve() override;
void FindPath() override;
};
#endif
Empty file added dijstra_algorithm.cpp
Empty file.
14 changes: 14 additions & 0 deletions dijstra_algorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef _DIJ_ALGORITHM_H
#define _DIJ_ALGORITHM_H
#include "algorithm.h"
#include "map.h"
class DIJAlgorithm : public Algorithm
{
public:
DIJAlgorithm() = default;
DIJAlgorithm(Map& _map):Algorithm(_map){}
DIJAlgorithm(Map& _map, MNode& _start, MNode& _end):Algorithm(_map, _start, _end){}
void Resolve() override;
void FindPath() override;
};
#endif
7 changes: 0 additions & 7 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,11 @@ int main(int argc, char* argv[])
endNode.NStateSetter(ENodeState::NONE);
tMap.Draw(startNode, EDrawType::TYPE);
tMap.Draw(endNode, EDrawType::TYPE);
//cout<<"path----------------------------------1"<<endl;
//print_map(tMap);
ConstructMap(tMap);
//cout<<"path----------------------------------2"<<endl;
//print_map(tMap);
refresh();
map<int, int> originMap;
BFS(tMap, startNode, endNode, originMap);
print_map_path(originMap, tMap, startNode, endNode);
refresh();
//cout<<"path----------------------------------3"<<endl;
//print_map(tMap);
getch();
tMap.ClearMap();
return EXIT_SUCCESS;
Expand Down
Empty file added map_config.cpp
Empty file.
4 changes: 4 additions & 0 deletions map_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef _MAP_CONFIG_H
#define _MAP_CONFIG_H

#endif

0 comments on commit ef02124

Please sign in to comment.