Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <random>
- using namespace std;
- // Function to display the labyrinth
- void display(string** labyrinth, int rowsCount, int columnsCount)
- {
- for (int i = 0; i < rowsCount; i++)
- {
- for (int j = 0; j < columnsCount; j++)
- {
- cout << labyrinth[i][j] << " ";
- }
- cout << "\n";
- }
- }
- // Function to generate random start and end position
- int randomStartOrEnd(int rowsCount)
- {
- mt19937 engine(time(nullptr));
- uniform_int_distribution<int> dist(1, rowsCount - 2);
- return dist(engine);
- }
- int main()
- {
- string** labyrinth;
- int rowsCount = 10;
- int columnsCount = 10;
- int startAt = randomStartOrEnd(rowsCount);
- int endAt = randomStartOrEnd(rowsCount);
- // If the counter is equal to 10 the endAt is equal to the startAt variable
- int counter = 0;
- while (true)
- {
- counter++;
- if (endAt == startAt)
- {
- endAt = randomStartOrEnd(rowsCount);
- }
- else if (counter == 10)
- {
- break;
- }
- else
- {
- break;
- }
- }
- // Dynamically allocating row space in the heap
- labyrinth = new string * [rowsCount];
- // Dynamically allocating column space in the heap
- for (int i = 0; i < rowsCount; i++)
- {
- labyrinth[i] = new string[columnsCount];
- }
- for (int i = 0; i < rowsCount; i++)
- {
- for (int j = 0; j < columnsCount; j++)
- {
- if ( (i == startAt) && (j == 0) )
- {
- labyrinth[i][j] = "S";
- }
- else if ( (i == endAt) && (j == columnsCount - 1) )
- {
- labyrinth[i][j] = "E";
- }
- else if ( (i > 0) && (i < rowsCount - 1) && (j > 0) && (j < columnsCount - 1) )
- {
- labyrinth[i][j] = " ";
- /*if ( (i == startAt) || (i == endAt) )
- {
- labyrinth[i][j] = "#";
- }
- else
- {
- labyrinth[i][j] = " ";
- }*/
- }
- else
- {
- labyrinth[i][j] = "#";
- }
- }
- }
- // Displaying the labyrinth
- display(labyrinth, rowsCount, columnsCount);
- // Free space after the use of the array
- for (int i = 0; i < rowsCount; i++)
- {
- delete[] labyrinth[i];
- }
- delete[] labyrinth;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement