|
@@ -22,8 +22,8 @@ class World
|
|
|
// Number of lines
|
|
|
unsigned int h;
|
|
|
|
|
|
- // Size of the array
|
|
|
- const unsigned int size;
|
|
|
+ // Size of the array
|
|
|
+ const unsigned int size;
|
|
|
|
|
|
// Unidimensional array for tiles
|
|
|
int* board;
|
|
@@ -31,7 +31,7 @@ class World
|
|
|
public:
|
|
|
// Constructor
|
|
|
World(unsigned int l_, unsigned int h_, double p)
|
|
|
- :l(l_), h(h_), size(l_ * h_)
|
|
|
+ :l(l_), h(h_), size(l_ * h_)
|
|
|
{
|
|
|
board = new int[size]();
|
|
|
|
|
@@ -75,10 +75,12 @@ class World
|
|
|
case 0:
|
|
|
cout << " ";
|
|
|
break;
|
|
|
-
|
|
|
case 1:
|
|
|
cout << "#";
|
|
|
break;
|
|
|
+ case 2:
|
|
|
+ cout << "+";
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
cout << endl;
|
|
@@ -112,6 +114,15 @@ class World
|
|
|
return n;
|
|
|
}
|
|
|
|
|
|
+ // Mark a list of points in the world
|
|
|
+ void mark(const list<unsigned int>& path, int value = 2) {
|
|
|
+ for (auto tile : path) {
|
|
|
+ board[tile] = value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
// Depth-first search
|
|
|
// starting from tile number s0, find a path to tile number t
|
|
|
// return true if such a path exists, false otherwise
|
|
@@ -120,22 +131,22 @@ class World
|
|
|
{
|
|
|
bool r = false;
|
|
|
|
|
|
- bool explored[size];
|
|
|
+ bool explored[size];
|
|
|
|
|
|
- stack<unsigned int> open;
|
|
|
+ stack<unsigned int> open;
|
|
|
open.push(s0);
|
|
|
|
|
|
- for (unsigned int k(0); k < size; k ++)
|
|
|
- explored[k] = false;
|
|
|
+ for (unsigned int k(0); k < size; k ++)
|
|
|
+ explored[k] = false;
|
|
|
|
|
|
- explored[s0] = true;
|
|
|
+ explored[s0] = true;
|
|
|
|
|
|
- int current;
|
|
|
+ int current;
|
|
|
int neighbour;
|
|
|
unsigned int succs[4];
|
|
|
unsigned int nbSuccs;
|
|
|
|
|
|
- do {
|
|
|
+ do {
|
|
|
current = open.top();
|
|
|
open.pop();
|
|
|
|
|
@@ -152,7 +163,7 @@ class World
|
|
|
path.push_back(current);
|
|
|
|
|
|
|
|
|
- } while (!r && !open.empty());
|
|
|
+ } while (!r && !open.empty());
|
|
|
|
|
|
return r;
|
|
|
}
|
|
@@ -164,9 +175,25 @@ int main()
|
|
|
srand(time(0));
|
|
|
|
|
|
// Create a world
|
|
|
- World w(20, 10, 0.2);
|
|
|
+ const unsigned int l(20), h(10);
|
|
|
+ const double wallProbability(0.2);
|
|
|
+
|
|
|
+ World w(l, h, wallProbability);
|
|
|
+
|
|
|
+ unsigned int start(1), end(5);
|
|
|
|
|
|
// Display it
|
|
|
+ cout << endl << "Generated world" << endl;
|
|
|
+ w.display();
|
|
|
+
|
|
|
+ // Find a path with Depth-First Search
|
|
|
+ list<unsigned int> dfsPath;
|
|
|
+ bool exitFound = w.dfs(start, end, dfsPath);
|
|
|
+
|
|
|
+ // Display DFS
|
|
|
+ cout << endl << "Depth-First Search" << endl;
|
|
|
+
|
|
|
+ w.mark(dfsPath);
|
|
|
w.display();
|
|
|
|
|
|
return 0;
|