|
@@ -11,6 +11,7 @@
|
|
|
#include <ctime>
|
|
|
#include <stack>
|
|
|
#include <queue>
|
|
|
+#include <unistd.h>
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
@@ -18,6 +19,8 @@ using namespace std;
|
|
|
#define DEFAULT_LENGTH 50
|
|
|
#define DEFAULT_HEIGHT 15
|
|
|
#define DEFAULT_PROBABILITY 0.2
|
|
|
+#define DEFAULT_ANIMATION true
|
|
|
+#define DEFAULT_ANIMATION_DELAY 50000 // us
|
|
|
|
|
|
// Tile codes
|
|
|
#define FREE 0
|
|
@@ -311,6 +314,21 @@ class World
|
|
|
return targetIsReached;
|
|
|
}
|
|
|
|
|
|
+ void animate(bool exitFound, const list<unsigned int>& discovered, const list<unsigned int>& path) {
|
|
|
+ for (unsigned int tile : discovered) {
|
|
|
+ markOne(tile, DISCOVERED);
|
|
|
+ display();
|
|
|
+ usleep(DEFAULT_ANIMATION_DELAY);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (exitFound)
|
|
|
+ for (unsigned int tile : path) {
|
|
|
+ markOne(tile, TRACE);
|
|
|
+ display();
|
|
|
+ usleep(DEFAULT_ANIMATION_DELAY);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
void showResults(bool exitFound, const list<unsigned int>& discovered, const list<unsigned int>& path) {
|
|
|
|
|
|
markAll(discovered, DISCOVERED);
|
|
@@ -352,6 +370,8 @@ int main()
|
|
|
w.markOne(end, TARGET);
|
|
|
w.display();
|
|
|
|
|
|
+ const bool animation(DEFAULT_ANIMATION);
|
|
|
+
|
|
|
// 1
|
|
|
cout << endl << "Depth-First Search" << endl;
|
|
|
|
|
@@ -360,6 +380,9 @@ int main()
|
|
|
list<unsigned int> dfsDiscovered;
|
|
|
bool dfsExitFound = dfsWorld.dfs(start, end, dfsPath, dfsDiscovered);
|
|
|
|
|
|
+ if (animation)
|
|
|
+ dfsWorld.animate(dfsExitFound, dfsDiscovered, dfsPath);
|
|
|
+
|
|
|
dfsWorld.showResults(dfsExitFound, dfsDiscovered, dfsPath);
|
|
|
|
|
|
// 2
|
|
@@ -370,6 +393,9 @@ int main()
|
|
|
list<unsigned int> bfsDiscovered;
|
|
|
bool bfsExitFound = bfsWorld.bfs(start, end, bfsPath, bfsDiscovered);
|
|
|
|
|
|
+ if (animation)
|
|
|
+ bfsWorld.animate(bfsExitFound, bfsDiscovered, bfsPath);
|
|
|
+
|
|
|
bfsWorld.showResults(bfsExitFound, bfsDiscovered, bfsPath);
|
|
|
|
|
|
// End
|