Advertisement
PIBogdanov

C++

Feb 7th, 2023
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <random>
  4.  
  5. using namespace std;
  6.  
  7. // Function to display the labyrinth
  8. void display(string** labyrinth, int rowsCount, int columnsCount)
  9. {
  10. for (int i = 0; i < rowsCount; i++)
  11. {
  12. for (int j = 0; j < columnsCount; j++)
  13. {
  14. cout << labyrinth[i][j] << " ";
  15. }
  16.  
  17. cout << "\n";
  18. }
  19. }
  20.  
  21. // Function to generate random start and end position
  22. int randomStartOrEnd(int rowsCount)
  23. {
  24. mt19937 engine(time(nullptr));
  25.  
  26. uniform_int_distribution<int> dist(1, rowsCount - 2);
  27.  
  28. return dist(engine);
  29. }
  30.  
  31. int main()
  32. {
  33. string** labyrinth;
  34.  
  35. int rowsCount = 10;
  36.  
  37. int columnsCount = 10;
  38.  
  39. int startAt = randomStartOrEnd(rowsCount);
  40.  
  41. int endAt = randomStartOrEnd(rowsCount);
  42.  
  43. // If the counter is equal to 10 the endAt is equal to the startAt variable
  44. int counter = 0;
  45.  
  46. while (true)
  47. {
  48. counter++;
  49.  
  50. if (endAt == startAt)
  51. {
  52. endAt = randomStartOrEnd(rowsCount);
  53. }
  54.  
  55. else if (counter == 10)
  56. {
  57. endAt == startAt;
  58.  
  59. break;
  60. }
  61.  
  62. else
  63. {
  64. break;
  65. }
  66. }
  67.  
  68. // Dynamically allocating row space in the heap
  69. labyrinth = new string * [rowsCount];
  70.  
  71. // Dynamically allocating column space in the heap
  72. for (int i = 0; i < rowsCount; i++)
  73. {
  74. labyrinth[i] = new string[columnsCount];
  75. }
  76.  
  77. for (int i = 0; i < rowsCount; i++)
  78. {
  79. for (int j = 0; j < columnsCount; j++)
  80. {
  81. if ( (i == startAt) && (j == 0) )
  82. {
  83. labyrinth[i][j] = "S";
  84. }
  85.  
  86. else if ( (i == endAt) && (j == columnsCount - 1) )
  87. {
  88. labyrinth[i][j] = "E";
  89. }
  90.  
  91. else if ( (i > 0) && (i < rowsCount - 1) && (j > 0) && (j < columnsCount - 1) )
  92. {
  93. labyrinth[i][j] = " ";
  94.  
  95. /*if ( (i == startAt) || (i == endAt) )
  96. {
  97. labyrinth[i][j] = "#";
  98. }
  99.  
  100. else
  101. {
  102. labyrinth[i][j] = " ";
  103. }*/
  104. }
  105.  
  106. else
  107. {
  108. labyrinth[i][j] = "#";
  109. }
  110. }
  111. }
  112.  
  113. // Displaying the labyrinth
  114. display(labyrinth, rowsCount, columnsCount);
  115.  
  116. // Free space after the use of the array
  117. for (int i = 0; i < rowsCount; i++)
  118. {
  119. delete[] labyrinth[i];
  120. }
  121.  
  122. delete[] labyrinth;
  123.  
  124. return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement