Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. //We need a struct that has the coordinates of the node, whether the node has been visited, and whether the node is passable.
  7.  
  8. //THIS IS NOT A FUNCTION
  9. struct coord
  10. {
  11. int x;
  12. int y;
  13. };
  14.  
  15. struct node
  16. {
  17. coord pos;
  18. bool hasBeenVisited;
  19. bool isObstructed;
  20. bool isGoal;
  21. };
  22.  
  23. static const int gridWidth = 10;
  24. static const int gridHeight = 10;
  25.  
  26. node grid[gridWidth][gridHeight];
  27.  
  28. coord getRandomCoordinate()
  29. {
  30. coord randCoord;
  31. randCoord.x = rand() % gridWidth;
  32. randCoord.y = rand() % gridHeight;
  33. return randCoord;
  34. }
  35.  
  36. //Initializes our node grid.
  37. void initGrid()
  38. {
  39. for(int i = 0; i < gridHeight; i++)
  40. {
  41. for(int j = 0; j < gridWidth; j++)
  42. {
  43. grid[j][i].pos.x = j;
  44. grid[j][i].pos.y = i;
  45. grid[j][i].hasBeenVisited = false;
  46. grid[j][i].isObstructed = false;
  47. grid[j][i].isGoal = false;
  48. } //close inner for loop
  49. } //close outer for loop
  50. } //close initGrid function
  51.  
  52. //Sets a randomly determined starting position for the algorithm.
  53. void startPos()
  54. {
  55. coord startPosition = getRandomCoordinate();
  56. grid[startPosition.x][startPosition.y].hasBeenVisited = true;
  57. }
  58.  
  59. void addSolidBlocks(int numBlocks)
  60. {
  61. coord currentBlock = getRandomCoordinate();
  62. while(numBlocks > 0)
  63. {
  64. while(grid[currentBlock.x][currentBlock.y].isObstructed || grid[currentBlock.x][currentBlock.y].hasBeenVisited || grid[currentBlock.x][currentBlock.y].isGoal)
  65. {
  66. currentBlock = getRandomCoordinate();
  67. }
  68. grid[currentBlock.x][currentBlock.y].isObstructed = true;
  69. numBlocks--;
  70. }
  71. }
  72.  
  73. //Sets a randomly determined ending position for the algorithm.
  74. void endPos()
  75. {
  76. coord endPosition = getRandomCoordinate();
  77. while(grid[endPosition.x][endPosition.y].hasBeenVisited)
  78. {
  79. endPosition = getRandomCoordinate();
  80. }
  81. grid[endPosition.x][endPosition.y].isGoal = true;
  82. }
  83.  
  84. void drawNode(node nodeToDraw)
  85. {
  86. if(nodeToDraw.isObstructed)
  87. {
  88. cout << " X ";
  89. }
  90. else if(nodeToDraw.isGoal)
  91. {
  92. cout << " ! ";
  93. }
  94. else if(nodeToDraw.hasBeenVisited)
  95. {
  96. cout << " . ";
  97. }
  98. else
  99. {
  100. cout << " ";
  101. }
  102. }
  103.  
  104. void drawRow(int row)
  105. {
  106. for(int i = 0; i < gridWidth; i++)
  107. {
  108. drawNode(grid[i][row]);
  109. }
  110. cout << "\n";
  111. }
  112.  
  113. void drawGrid()
  114. {
  115. for(int i = 0; i < gridHeight; i++)
  116. {
  117. drawRow(i);
  118. }
  119. }
  120.  
  121. //THIS IS A FUNCTION
  122. int main()
  123. {
  124.  
  125. initGrid();
  126.  
  127. startPos();
  128.  
  129. endPos();
  130.  
  131. addSolidBlocks(20);
  132. drawGrid();
  133.  
  134.  
  135. system("pause");
  136.  
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement