From 150cf527e2b4038553f309d9c9c1df9d43df9da2 Mon Sep 17 00:00:00 2001 From: leguims Date: Mon, 25 Jun 2018 20:24:33 +0200 Subject: [PATCH 1/5] Improve code with warning of Visual code analysis --- source/AStar.cpp | 14 +++++++++++++- source/AStar.hpp | 3 ++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/source/AStar.cpp b/source/AStar.cpp index 7bf471a..83e5f5c 100644 --- a/source/AStar.cpp +++ b/source/AStar.cpp @@ -25,7 +25,7 @@ AStar::uint AStar::Node::getScore() return G + H; } -AStar::Generator::Generator() +AStar::Generator::Generator() noexcept : heuristic{}, direction {}, walls{}, worldSize{ 0, 0 }, directions{ 0 } { setDiagonalMovement(false); setHeuristic(&Heuristic::manhattan); @@ -52,11 +52,15 @@ void AStar::Generator::setHeuristic(HeuristicFunction heuristic_) void AStar::Generator::addCollision(Vec2i coordinates_) { + verify(coordinates_); + walls.push_back(coordinates_); } void AStar::Generator::removeCollision(Vec2i coordinates_) { + verify(coordinates_); + auto it = std::find(walls.begin(), walls.end(), coordinates_); if (it != walls.end()) { walls.erase(it); @@ -68,8 +72,16 @@ void AStar::Generator::clearCollisions() walls.clear(); } +void AStar::Generator::verify(const Vec2i& v) const +{ + if ((v.x < 0)||(v.x >= worldSize.x) || (v.y < 0) || (v.y >= worldSize.y)) + throw std::out_of_range("Vec2i is out of range !"); +} + AStar::CoordinateList AStar::Generator::findPath(Vec2i source_, Vec2i target_) { + verify(source_); verify(target_); + Node *current = nullptr; NodeSet openSet, closedSet; openSet.insert(new Node(source_)); diff --git a/source/AStar.hpp b/source/AStar.hpp index d006820..3d86b2c 100644 --- a/source/AStar.hpp +++ b/source/AStar.hpp @@ -40,9 +40,10 @@ namespace AStar bool detectCollision(Vec2i coordinates_); Node* findNodeOnList(NodeSet& nodes_, Vec2i coordinates_); void releaseNodes(NodeSet& nodes_); + void verify(const Vec2i&) const; public: - Generator(); + Generator() noexcept; void setWorldSize(Vec2i worldSize_); void setDiagonalMovement(bool enable_); void setHeuristic(HeuristicFunction heuristic_); From 8ab2d2ef53efdf6044385940e7cf834622f390e6 Mon Sep 17 00:00:00 2001 From: leguims Date: Mon, 25 Jun 2018 20:47:13 +0200 Subject: [PATCH 2/5] Add documentation for setWorldSize() method. --- ReadMe.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ReadMe.md b/ReadMe.md index 0647f42..02b02c0 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -11,7 +11,7 @@ int main() { AStar::Generator generator; // Set 2d map size. - generator.setWorldSize({25, 25}); + generator.setWorldSize({25, 25}); // World is between {0, 0} and {24, 24} // You can use a few heuristics : manhattan, euclidean or octagonal. generator.setHeuristic(AStar::Heuristic::euclidean); generator.setDiagonalMovement(true); From e7aaf02cd4b6d9f57ee7efd19e9a7a26319c2ebf Mon Sep 17 00:00:00 2001 From: leguims Date: Thu, 28 Jun 2018 23:16:02 +0200 Subject: [PATCH 3/5] Improve code with warning of Visual code analysis --- source/AStar.cpp | 21 +++++++++++++++++---- source/AStar.hpp | 13 ++++++++++++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/source/AStar.cpp b/source/AStar.cpp index 83e5f5c..8eaf4bf 100644 --- a/source/AStar.cpp +++ b/source/AStar.cpp @@ -25,7 +25,7 @@ AStar::uint AStar::Node::getScore() return G + H; } -AStar::Generator::Generator() noexcept : heuristic{}, direction {}, walls{}, worldSize{ 0, 0 }, directions{ 0 } +AStar::Generator::Generator() noexcept : heuristic{}, direction{}, walls{}, worldSize{ 0, 0 }, directions{ 0 }, state_{State::Ready} { setDiagonalMovement(false); setHeuristic(&Heuristic::manhattan); @@ -35,6 +35,11 @@ AStar::Generator::Generator() noexcept : heuristic{}, direction {}, walls{}, wor }; } +AStar::State AStar::Generator::getState() const +{ + return state_; +} + void AStar::Generator::setWorldSize(Vec2i worldSize_) { worldSize = worldSize_; @@ -72,10 +77,18 @@ void AStar::Generator::clearCollisions() walls.clear(); } -void AStar::Generator::verify(const Vec2i& v) const +void AStar::Generator::verify(const Vec2i& v) { - if ((v.x < 0)||(v.x >= worldSize.x) || (v.y < 0) || (v.y >= worldSize.y)) - throw std::out_of_range("Vec2i is out of range !"); + if ((v.x < 0) || (v.x >= worldSize.x) || (v.y < 0) || (v.y >= worldSize.y)) + { + setState(AStar::State::InvalidArguments); + //throw std::out_of_range("Vec2i is out of range !"); + } +} + +void AStar::Generator::setState(State state) +{ + state_ = state; } AStar::CoordinateList AStar::Generator::findPath(Vec2i source_, Vec2i target_) diff --git a/source/AStar.hpp b/source/AStar.hpp index 3d86b2c..4636ee9 100644 --- a/source/AStar.hpp +++ b/source/AStar.hpp @@ -19,6 +19,14 @@ namespace AStar bool operator == (const Vec2i& coordinates_); }; + enum class State + { + Ready, + InvalidArguments, + InternalError, + Success + }; + using uint = unsigned int; using HeuristicFunction = std::function; using CoordinateList = std::vector; @@ -40,10 +48,12 @@ namespace AStar bool detectCollision(Vec2i coordinates_); Node* findNodeOnList(NodeSet& nodes_, Vec2i coordinates_); void releaseNodes(NodeSet& nodes_); - void verify(const Vec2i&) const; + void verify(const Vec2i&); + void setState(State); public: Generator() noexcept; + State getState() const; void setWorldSize(Vec2i worldSize_); void setDiagonalMovement(bool enable_); void setHeuristic(HeuristicFunction heuristic_); @@ -57,6 +67,7 @@ namespace AStar CoordinateList direction, walls; Vec2i worldSize; uint directions; + State state_; }; class Heuristic From d977c96667bba95c6aec21dcc30f53902a3a8a4a Mon Sep 17 00:00:00 2001 From: leguims Date: Thu, 28 Jun 2018 23:18:54 +0200 Subject: [PATCH 4/5] Replace getState() error management by exception management. --- source/AStar.cpp | 15 ++------------- source/AStar.hpp | 11 ----------- 2 files changed, 2 insertions(+), 24 deletions(-) diff --git a/source/AStar.cpp b/source/AStar.cpp index 8eaf4bf..aec6bbb 100644 --- a/source/AStar.cpp +++ b/source/AStar.cpp @@ -25,7 +25,7 @@ AStar::uint AStar::Node::getScore() return G + H; } -AStar::Generator::Generator() noexcept : heuristic{}, direction{}, walls{}, worldSize{ 0, 0 }, directions{ 0 }, state_{State::Ready} +AStar::Generator::Generator() noexcept : heuristic{}, direction{}, walls{}, worldSize{ 0, 0 }, directions{ 0 } { setDiagonalMovement(false); setHeuristic(&Heuristic::manhattan); @@ -35,11 +35,6 @@ AStar::Generator::Generator() noexcept : heuristic{}, direction{}, walls{}, worl }; } -AStar::State AStar::Generator::getState() const -{ - return state_; -} - void AStar::Generator::setWorldSize(Vec2i worldSize_) { worldSize = worldSize_; @@ -81,16 +76,10 @@ void AStar::Generator::verify(const Vec2i& v) { if ((v.x < 0) || (v.x >= worldSize.x) || (v.y < 0) || (v.y >= worldSize.y)) { - setState(AStar::State::InvalidArguments); - //throw std::out_of_range("Vec2i is out of range !"); + throw std::out_of_range("Vec2i is out of range !"); } } -void AStar::Generator::setState(State state) -{ - state_ = state; -} - AStar::CoordinateList AStar::Generator::findPath(Vec2i source_, Vec2i target_) { verify(source_); verify(target_); diff --git a/source/AStar.hpp b/source/AStar.hpp index 4636ee9..d31ddbf 100644 --- a/source/AStar.hpp +++ b/source/AStar.hpp @@ -19,14 +19,6 @@ namespace AStar bool operator == (const Vec2i& coordinates_); }; - enum class State - { - Ready, - InvalidArguments, - InternalError, - Success - }; - using uint = unsigned int; using HeuristicFunction = std::function; using CoordinateList = std::vector; @@ -49,11 +41,9 @@ namespace AStar Node* findNodeOnList(NodeSet& nodes_, Vec2i coordinates_); void releaseNodes(NodeSet& nodes_); void verify(const Vec2i&); - void setState(State); public: Generator() noexcept; - State getState() const; void setWorldSize(Vec2i worldSize_); void setDiagonalMovement(bool enable_); void setHeuristic(HeuristicFunction heuristic_); @@ -67,7 +57,6 @@ namespace AStar CoordinateList direction, walls; Vec2i worldSize; uint directions; - State state_; }; class Heuristic From a4024fe58b3c947f2e6ec16521fa65dd7fb32934 Mon Sep 17 00:00:00 2001 From: leguims Date: Thu, 28 Jun 2018 23:31:10 +0200 Subject: [PATCH 5/5] verify becomes const method. --- source/AStar.cpp | 2 +- source/AStar.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/AStar.cpp b/source/AStar.cpp index aec6bbb..4205570 100644 --- a/source/AStar.cpp +++ b/source/AStar.cpp @@ -72,7 +72,7 @@ void AStar::Generator::clearCollisions() walls.clear(); } -void AStar::Generator::verify(const Vec2i& v) +void AStar::Generator::verify(const Vec2i& v) const { if ((v.x < 0) || (v.x >= worldSize.x) || (v.y < 0) || (v.y >= worldSize.y)) { diff --git a/source/AStar.hpp b/source/AStar.hpp index d31ddbf..3d86b2c 100644 --- a/source/AStar.hpp +++ b/source/AStar.hpp @@ -40,7 +40,7 @@ namespace AStar bool detectCollision(Vec2i coordinates_); Node* findNodeOnList(NodeSet& nodes_, Vec2i coordinates_); void releaseNodes(NodeSet& nodes_); - void verify(const Vec2i&); + void verify(const Vec2i&) const; public: Generator() noexcept;