main.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Example of world building, display, and successor computation for the artificial
  2. // intelligence path-finding lab
  3. //
  4. // Author: Didier Lime
  5. // Date: 2018-10-03
  6. #include <iostream>
  7. #include <list>
  8. #include <cstdlib>
  9. #include <ctime>
  10. using namespace std;
  11. class World
  12. {
  13. private:
  14. // Number of columns
  15. unsigned int L;
  16. // Number of lines
  17. unsigned int H;
  18. // Unidimensional array for tiles
  19. int* w;
  20. public:
  21. // Constructor
  22. World(unsigned int L, unsigned int H, double P)
  23. {
  24. this->L = L;
  25. this->H = H;
  26. this->w = new int[L*H]();
  27. // Add walls to the first and last columns
  28. for (unsigned int i = 0; i < H; i++)
  29. {
  30. this->w[i * L] = 1;
  31. this->w[i * L + L - 1] = 1;
  32. }
  33. // Add walls to the first and last lines
  34. for (unsigned int j = 0; j < L; j++)
  35. {
  36. this->w[j] = 1;
  37. this->w[(H - 1) * L + j] = 1;
  38. }
  39. for (unsigned int i = 0; i < H; i++)
  40. {
  41. for (unsigned int j = 0; j < L; j++)
  42. {
  43. // add a wall in this tile with probability P and provided that it is neither
  44. // the starting tile nor the goal tile
  45. if ((double) rand() / RAND_MAX < P && !(i == 1 && j == 1) && !(i == H - 2 && j == L - 2))
  46. {
  47. this->w[i * L + j] = 1;
  48. }
  49. }
  50. }
  51. }
  52. // Display the world
  53. void display()
  54. {
  55. for (unsigned int i = 0; i < H; i++)
  56. {
  57. for (unsigned int j = 0; j < L; j++)
  58. {
  59. switch (this->w[i * L + j])
  60. {
  61. case 0:
  62. cout << ".";
  63. break;
  64. case 1:
  65. cout << "W";
  66. break;
  67. }
  68. }
  69. cout << endl;
  70. }
  71. }
  72. // compute the successors of tile number i in world w
  73. // we return the number n of valid successors
  74. // the actual list is in array r where only the first n
  75. // elements are significant
  76. unsigned int successors(unsigned int i, unsigned int r[4])
  77. {
  78. unsigned int n = 0;
  79. if (i >= 0 && i < this->L * this->H && this->w[i] != 1)
  80. {
  81. // if i is a correct tile number (inside the array and not on a wall)
  82. // look in the four adjacent tiles and keep only those with no wall
  83. const unsigned int moves[] = { i - 1, i + 1, i - L, i + L};
  84. for (unsigned int k = 0; k < 4; k++)
  85. {
  86. if (this->w[moves[k]] != 1)
  87. {
  88. r[n] = moves[k];
  89. n++;
  90. }
  91. }
  92. }
  93. return n;
  94. }
  95. // Depth-first search
  96. // starting from tile number s0, find a path to tile number t
  97. // return true if such a path exists, false otherwise
  98. // if it exists the path is given in variable path (hence the reference &)
  99. bool dfs(unsigned int s0, unsigned int t, list<unsigned int>& path)
  100. {
  101. bool r = false;
  102. // ... Complete here ...
  103. return r;
  104. }
  105. };
  106. int main()
  107. {
  108. // Initialise the random number generator
  109. srand(time(0));
  110. // Create a world
  111. World w(20, 10, 0.2);
  112. // Display it
  113. w.display();
  114. // Print the tile numbers of the successors of the starting tile (1, 1)
  115. unsigned int succs[4];
  116. unsigned int n = w.successors(21, succs);
  117. for (unsigned int k = 0; k < n; k++)
  118. {
  119. cout << succs[k] << " ";
  120. }
  121. cout << endl;
  122. return 0;
  123. }