Skip to content

Commit

Permalink
make the search algorithm into class
Browse files Browse the repository at this point in the history
  • Loading branch information
linkliu committed Aug 6, 2024
1 parent 5808e5b commit 5b58adf
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 174 deletions.
9 changes: 7 additions & 2 deletions astar_algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
#define _ASTAR_ALGORITHM_H
#include "algorithm.h"
#include "map.h"
#include <map>
#include <vector>
using std::vector;
using std::map;

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;
map<int, int> Resolve() override;
vector<int> FindPath() override;
};
#endif
122 changes: 0 additions & 122 deletions astar_algorithm1.cpp

This file was deleted.

16 changes: 0 additions & 16 deletions astar_algorithm1.h

This file was deleted.

47 changes: 37 additions & 10 deletions bfs_algorithm.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,51 @@
#include "bfs_algorithm.h"
#include "mnode.h"
#include <map>
#include <vector>
#include <chrono>
#include <ncurses.h>
#include <thread>
#include <utility>
using std::make_pair;

map<int, int> BFSAlgorithm::Resolve()
{
Map& aMap = GetMap();
MNode& sNode = GetStartNode();
MNode& eNode = GetEndNode();
map<int, int> ori_map;
if(!aMap.NodeCheck(sNode) || !aMap.NodeCheck(eNode) || aMap.Size() < 0)
MNode& startNode = GetStartNode();
MNode& endNode = GetEndNode();
map<int, int> solveMap;
if(!aMap.NodeCheck(startNode) || !aMap.NodeCheck(endNode) || aMap.Size() < 0)
{
stringstream ss;
ss<<"invalid start node:"<<sNode.ToString()<<" or end node:"<<eNode.ToString()<<endl;
ss<<"invalid start node:"<<startNode.ToString()<<" or end node:"<<endNode.ToString()<<endl;
cout<<ss.str();
endwin();
return ori_map;
return solveMap;
}

return ori_map;
list<MNode> waveList;
waveList.push_back(startNode);
while(!waveList.empty())
{
MNode & checkNode = waveList.front();
list<MNode> neighborList = aMap.GetNeighbors(checkNode);
for (MNode nextNode : neighborList)
{
//还没有查询过
if(solveMap.find(aMap.GetNodeNum(nextNode)) == solveMap.cend())
{
//test,log out
std::this_thread::sleep_for(std::chrono::milliseconds(50));
if(nextNode!=startNode && nextNode!=endNode)
{
nextNode.NStateSetter(ENodeState::FINDDING);
aMap.Draw(nextNode, EDrawType::STATE);
refresh();
}
solveMap.insert(make_pair(aMap.GetNodeNum(nextNode), aMap.GetNodeNum(checkNode)));
waveList.push_back(nextNode);
}
}
waveList.pop_front();
}
return solveMap;
}


Expand Down
2 changes: 1 addition & 1 deletion compile.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
g++ -g -std=c++11 main.cpp astar_algorithm.cpp map.cpp debug_tool.cpp -lncursesw -o main
g++ -g -std=c++11 *.cpp -lncursesw -o main
8 changes: 6 additions & 2 deletions dijstra_algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
#define _DIJ_ALGORITHM_H
#include "algorithm.h"
#include "map.h"
#include <map>
#include <vector>
using std::map;
using std::vector;
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;
map<int, map> Resolve() override;
vector<int> FindPath() override;
};
#endif
20 changes: 6 additions & 14 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
#include <asm-generic/errno.h>
#include <bits/types/struct_tm.h>
#include <chrono>
#include <clocale>
#include <cstdlib>
#include<iostream>
#include<string>
#include<queue>
#include<thread>
#include "debug_tool.h"
#include <iostream>
#include <string>
#include "bfs_algorithm.h"
#include "mnode.h"
#include"map.h"
#include "astar_algorithm.h"
#include "map.h"
using std::cout;
using std::cin;
using std::endl;
using std::string;

int main(int argc, char* argv[])
Expand All @@ -30,10 +24,8 @@ int main(int argc, char* argv[])
endNode.NStateSetter(ENodeState::NONE);
tMap.Draw(startNode, EDrawType::TYPE);
tMap.Draw(endNode, EDrawType::TYPE);
ConstructMap(tMap);
refresh();
map<int, int> originMap;
BFS(tMap, startNode, endNode, originMap);
BFSAlgorithm bfs(tMap, startNode, endNode);
map<int, int> bfsSolveMap = bfs.Resolve();
refresh();
getch();
tMap.ClearMap();
Expand Down
9 changes: 5 additions & 4 deletions map.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include"map.h"
#include "map.h"
#include "mnode.h"
#include <algorithm>
#include <cmath>
#include <curses.h>
#include<iostream>
#include <iostream>
#include <list>
#include <ncurses.h>
#include<sstream>
#include <sstream>
#include <stdexcept>
#include <utility>
using std::cout;
Expand Down Expand Up @@ -372,8 +372,9 @@ void Map::filterNeightbor(pair<int, int> &npair, list<MNode> & nlist)
}
}

list<MNode>& Map::GetNeighbors(const MNode& node, list<MNode>& neighborsList)
list<MNode> Map::GetNeighbors(const MNode& node)
{
list<MNode> neighborsList;
if(!neighborsList.empty())
{
neighborsList.clear();
Expand Down
2 changes: 1 addition & 1 deletion map.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Map
pair<int, int> ExchNumToMapIndex(int num) const;
pair<int, int> ExchMapIndexToPOS(int _mapIndex_Y, int _mapIndex_X) const;
int ExchMapIndexToNum(int _mapIndex_Y, int _mapIndex_X) const;
list<MNode>& GetNeighbors(const MNode& node, list<MNode>& neighborsList);
list<MNode> GetNeighbors(const MNode& node);
bool NodeCheck(const MNode& node);
};
#endif
4 changes: 2 additions & 2 deletions map_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ map<ENodeType, list<pair<int, int>>> MapConfig::TerrainMap =
{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}
{2,13},{3,13},{4,13},{5,13},{6,13},{7, 13}, {8,13}, {9,13},{10,13},{11,13},{12,13},
}
}
}
};
11 changes: 11 additions & 0 deletions mnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ struct MNode
}
return *this;
}

string ToString() const
{
stringstream ss;
Expand All @@ -164,6 +165,16 @@ struct MNode
return mapIndex_Y == node.mapIndex_Y && mapIndex_X == node.mapIndex_X;
}

bool operator==(const MNode& node) const
{
return mapIndex_Y==node.mapIndex_Y && mapIndex_X==node.mapIndex_X;
}

bool operator!=(const MNode& node) const
{
return !(*this == node);
}

void NTypeSetter(ENodeType ntype)
{
NType = ntype;
Expand Down

0 comments on commit 5b58adf

Please sign in to comment.