Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <iostream>
- using namespace std;
- //We need a struct that has the coordinates of the node, whether the node has been visited, and whether the node is passable.
- //THIS IS NOT A FUNCTION
- struct coord
- {
- int x;
- int y;
- };
- struct node
- {
- coord pos;
- bool hasBeenVisited;
- bool isObstructed;
- bool isGoal;
- };
- static const int gridWidth = 10;
- static const int gridHeight = 10;
- node grid[gridWidth][gridHeight];
- coord getRandomCoordinate()
- {
- coord randCoord;
- randCoord.x = rand() % gridWidth;
- randCoord.y = rand() % gridHeight;
- return randCoord;
- }
- //Initializes our node grid.
- void initGrid()
- {
- for(int i = 0; i < gridHeight; i++)
- {
- for(int j = 0; j < gridWidth; j++)
- {
- grid[j][i].pos.x = j;
- grid[j][i].pos.y = i;
- grid[j][i].hasBeenVisited = false;
- grid[j][i].isObstructed = false;
- grid[j][i].isGoal = false;
- } //close inner for loop
- } //close outer for loop
- } //close initGrid function
- //Sets a randomly determined starting position for the algorithm.
- void startPos()
- {
- coord startPosition = getRandomCoordinate();
- grid[startPosition.x][startPosition.y].hasBeenVisited = true;
- }
- void addSolidBlocks(int numBlocks)
- {
- coord currentBlock = getRandomCoordinate();
- while(numBlocks > 0)
- {
- while(grid[currentBlock.x][currentBlock.y].isObstructed || grid[currentBlock.x][currentBlock.y].hasBeenVisited || grid[currentBlock.x][currentBlock.y].isGoal)
- {
- currentBlock = getRandomCoordinate();
- }
- grid[currentBlock.x][currentBlock.y].isObstructed = true;
- numBlocks--;
- }
- }
- //Sets a randomly determined ending position for the algorithm.
- void endPos()
- {
- coord endPosition = getRandomCoordinate();
- while(grid[endPosition.x][endPosition.y].hasBeenVisited)
- {
- endPosition = getRandomCoordinate();
- }
- grid[endPosition.x][endPosition.y].isGoal = true;
- }
- void drawNode(node nodeToDraw)
- {
- if(nodeToDraw.isObstructed)
- {
- cout << " X ";
- }
- else if(nodeToDraw.isGoal)
- {
- cout << " ! ";
- }
- else if(nodeToDraw.hasBeenVisited)
- {
- cout << " . ";
- }
- else
- {
- cout << " ";
- }
- }
- void drawRow(int row)
- {
- for(int i = 0; i < gridWidth; i++)
- {
- drawNode(grid[i][row]);
- }
- cout << "\n";
- }
- void drawGrid()
- {
- for(int i = 0; i < gridHeight; i++)
- {
- drawRow(i);
- }
- }
- //THIS IS A FUNCTION
- int main()
- {
- initGrid();
- startPos();
- endPos();
- addSolidBlocks(20);
- drawGrid();
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement