Skip to content

Commit

Permalink
Repo fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
DevBoiAgru committed Sep 9, 2024
1 parent 25993d8 commit 7da0046
Show file tree
Hide file tree
Showing 36 changed files with 118 additions and 372 deletions.
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@
vcpkg_installed/

# visual studio
.vs/
.vs/
PathFinding/

# Debug builds
x64/
*.sln
*.vcxproj
*.vcxproj.filters
*.vcxproj.user
Binary file not shown.
Binary file modified .vs/PathFinding/v17/.suo
Binary file not shown.
Binary file modified .vs/PathFinding/v17/Browse.VC.db
Binary file not shown.
68 changes: 36 additions & 32 deletions .vs/PathFinding/v17/DocumentLayout.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"Version": 1,
"WorkspaceRootPath": "C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\",
"WorkspaceRootPath": "C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\",
"Documents": [
{
"AbsoluteMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\PathFinding.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"AbsoluteMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\PathFinding.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|solutionrelative:PathFinding.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
},
{
"AbsoluteMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Pathfinding.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"AbsoluteMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Pathfinding.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|solutionrelative:Pathfinding.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
},
{
"AbsoluteMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Maze.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"AbsoluteMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Maze.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|solutionrelative:Maze.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
},
{
"AbsoluteMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Maze.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|solutionrelative:Maze.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
"AbsoluteMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Node.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|solutionrelative:Node.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
},
{
"AbsoluteMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Node.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|solutionrelative:Node.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
"AbsoluteMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Maze.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|solutionrelative:Maze.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
},
{
"AbsoluteMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Node.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|solutionrelative:Node.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
"AbsoluteMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Node.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{2E29F4AA-3298-430E-8DAE-7FDF9DCECC38}|PathFinding.vcxproj|solutionrelative:Node.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
}
],
"DocumentGroupContainers": [
Expand All @@ -34,17 +34,21 @@
"DocumentGroups": [
{
"DockedWidth": 200,
"SelectedChildIndex": 0,
"SelectedChildIndex": 1,
"Children": [
{
"$type": "Bookmark",
"Name": "ST:0:0:{3ae79031-e1bc-11d0-8f78-00a0c9110057}"
},
{
"$type": "Document",
"DocumentIndex": 0,
"Title": "PathFinding.cpp",
"DocumentMoniker": "C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\PathFinding.cpp",
"DocumentMoniker": "C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\PathFinding.cpp",
"RelativeDocumentMoniker": "PathFinding.cpp",
"ToolTip": "C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\PathFinding.cpp",
"ToolTip": "C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\PathFinding.cpp",
"RelativeToolTip": "PathFinding.cpp",
"ViewState": "AQIAAAAAAAAAAAAAAAAAAIAAAAAeAAAA",
"ViewState": "AQIAAAYAAAAAAAAAAAAAAAcAAAAoAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
"WhenOpened": "2024-04-13T05:55:35.151Z",
"EditorCaption": ""
Expand All @@ -53,11 +57,11 @@
"$type": "Document",
"DocumentIndex": 1,
"Title": "Pathfinding.h",
"DocumentMoniker": "C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Pathfinding.h",
"DocumentMoniker": "C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Pathfinding.h",
"RelativeDocumentMoniker": "Pathfinding.h",
"ToolTip": "C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Pathfinding.h",
"ToolTip": "C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Pathfinding.h",
"RelativeToolTip": "Pathfinding.h",
"ViewState": "AQIAAAAAAAAAAAAAAAAAAAIAAAATAAAA",
"ViewState": "AQIAAAAAAAAAAAAAAAAAAAEAAAAcAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|",
"WhenOpened": "2024-04-14T06:30:42.321Z",
"EditorCaption": ""
Expand All @@ -66,50 +70,50 @@
"$type": "Document",
"DocumentIndex": 2,
"Title": "Maze.h",
"DocumentMoniker": "C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Maze.h",
"DocumentMoniker": "C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Maze.h",
"RelativeDocumentMoniker": "Maze.h",
"ToolTip": "C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Maze.h",
"ToolTip": "C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Maze.h",
"RelativeToolTip": "Maze.h",
"ViewState": "AQIAAAAAAAAAAAAAAAAAABAAAAAyAAAA",
"ViewState": "AQIAAAAAAAAAAAAAAAAAAAoAAAAfAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|",
"WhenOpened": "2024-04-13T06:19:48.736Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 3,
"DocumentIndex": 4,
"Title": "Maze.cpp",
"DocumentMoniker": "C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Maze.cpp",
"DocumentMoniker": "C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Maze.cpp",
"RelativeDocumentMoniker": "Maze.cpp",
"ToolTip": "C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Maze.cpp",
"ToolTip": "C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Maze.cpp",
"RelativeToolTip": "Maze.cpp",
"ViewState": "AQIAACwAAAAAAAAAAAAAADoAAAAAAAAA",
"ViewState": "AQIAAAAAAAAAAAAAAAAAAA4AAAACAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
"WhenOpened": "2024-04-13T06:20:09.473Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 5,
"DocumentIndex": 3,
"Title": "Node.h",
"DocumentMoniker": "C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Node.h",
"DocumentMoniker": "C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Node.h",
"RelativeDocumentMoniker": "Node.h",
"ToolTip": "C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Node.h",
"ToolTip": "C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Node.h",
"RelativeToolTip": "Node.h",
"ViewState": "AQIAAAAAAAAAAAAAAAAAABMAAAAVAAAA",
"ViewState": "AQIAAAAAAAAAAAAAAAAAABIAAAAYAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|",
"WhenOpened": "2024-04-13T06:02:40.99Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 4,
"DocumentIndex": 5,
"Title": "Node.cpp",
"DocumentMoniker": "C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Node.cpp",
"DocumentMoniker": "C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Node.cpp",
"RelativeDocumentMoniker": "Node.cpp",
"ToolTip": "C:\\Users\\Agrim Budakoti\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Node.cpp",
"ToolTip": "C:\\Users\\Agrim\\Desktop\\Playground\\Cpp\\Projects\\PathFinding\\Node.cpp",
"RelativeToolTip": "Node.cpp",
"ViewState": "AQIAAAcAAAAAAAAAAAAAAAoAAAAAAAAA",
"ViewState": "AQIAAAAAAAAAAAAAAAAAAAUAAAABAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
"WhenOpened": "2024-04-13T06:18:44.989Z",
"EditorCaption": ""
Expand Down
File renamed without changes.
11 changes: 1 addition & 10 deletions Maze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,7 @@ Maze::Maze(int width, int height) {
grid.push_back(row);
}
}
void Maze::display() {
for (auto i : grid) {
for (auto j : i) {
std::cout << "(" << j->coords[0] << ", " << j->coords[1] << ") ";
//std::cout << j->coords[0] << j->coords[1] << " ";
}
std::cout << "\n";
}
std::cout << "\nGrid height: " << gridHeight << "\nGrid width: " << gridWidth;
}

std::array<Node*, 4> Maze::getNeighbours(Node* target) {
int x = target->coords[0];
int y = target->coords[1];
Expand Down
3 changes: 1 addition & 2 deletions Maze.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ class Maze {
int gridHeight, gridWidth;
public:
Maze(int width, int height);

std::vector<std::vector<Node*>> grid;

// 0: left 1: top 2: right 3: down
std::array<Node*, 4> getNeighbours(Node* target);
void display();
};
int ManhattanDistance(Node* point1, Node* point2);
22 changes: 0 additions & 22 deletions Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,4 @@
Node::Node(int x, int y) {
coords[0] = x;
coords[1] = y;
}
void Node::display() {
std::cout << "(" << coords[0] << ", " << coords[1] << ") ";
}
void Node::displayState() {
switch (state) {
case Discovered:
std::cout << "Discovered";
break;
case Undiscovered:
std::cout << "Undiscovered";
break;
case Goal:
std::cout << "Goal";
break;
case Start:
std::cout << "Start";
break;
case Blocking:
std::cout << "Blocking";
break;
}
}
3 changes: 1 addition & 2 deletions Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum NodeState {
Undiscovered,
Goal,
Start,
Path,
Blocking
};

Expand All @@ -16,6 +17,4 @@ class Node {
std::array<int, 2> coords;
NodeState state{Undiscovered};
Node* parent = nullptr;
void display();
void displayState();
};
88 changes: 69 additions & 19 deletions PathFinding.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
#include "Pathfinding.h"

bool DISPLAY_COORDS{ true };

int main(){
// Only need one size because everything is square
constexpr int MAZE_SIZE = 25;
constexpr int WINDOW_SIZE = 1000;

Maze maze(25, 25);
sf::RenderWindow window(sf::VideoMode(1000, 1000), "Path finding");
constexpr int START_X = 15, START_Y = 17, GOAL_X = 2, GOAL_Y = 2;

bool DISPLAY_COORDS{ false };

int main(int argc, char** argv){
if (argc > 1) {
if (strcmp("coords", argv[1]))
DISPLAY_COORDS = true;
}

Maze maze(MAZE_SIZE, MAZE_SIZE);
sf::RenderWindow window(sf::VideoMode(WINDOW_SIZE, WINDOW_SIZE), "Path finding");
sf::Font font;
if (!font.loadFromFile("Roboto-Medium.ttf"))
std::cerr << "Failed to load font";
if (DISPLAY_COORDS) {
if (!font.loadFromFile("Assets/Roboto-Medium.ttf"))
std::cerr << "Failed to load font!";
}

Node* startNode = nullptr;
Node* goalNode = nullptr;
std::array<int, 2> start{ 17,20 }, goal{ 7, 2 };
std::array<int, 2> start{ START_X,START_Y }, goal{ GOAL_X, GOAL_Y };

for (auto& row : maze.grid) {
for (auto& node : row) {
Expand All @@ -36,6 +49,7 @@ int main(){
if (event.type == sf::Event::Closed) {
window.close();
}

if (event.type == sf::Event::MouseButtonPressed) {
int cellX = sf::Mouse::getPosition(window).x / 39;
int cellY = sf::Mouse::getPosition(window).y / 39;
Expand Down Expand Up @@ -82,22 +96,31 @@ int main(){
case Undiscovered: // White
squareColour = sf::Color(255, 255, 255, 255);
break;
case Discovered: // Blue
squareColour = sf::Color(0, 0, 255, 255);
case Discovered: // Gray
squareColour = sf::Color(200, 200, 200, 255);
break;
case Blocking: // Black
squareColour = sf::Color(0, 0, 0, 255);
break;
case Goal: // Red
case Goal: // Blue
squareColour = sf::Color(0, 0, 255, 255);
break;
case Start: // Red
squareColour = sf::Color(255, 0, 0, 255);
break;
case Start: // Yellow
squareColour = sf::Color(255, 255, 0, 255);
case Path: // Green
squareColour = sf::Color(0, 255, 0, 255);
break;
default:
default: // White
squareColour = sf::Color(255, 255, 255, 255);
break;
};

// Force goal node be coloured red since we set it to discovered when we find it
if (node->coords == goalNode->coords) {
squareColour = sf::Color(0, 0, 255, 255);
}

square.setFillColor(squareColour);
square.setPosition(sf::Vector2f(7 + node->coords[0] * 39.4, 7 + node->coords[1] * 39.4));
window.draw(square);
Expand All @@ -120,7 +143,6 @@ int main(){
}

static bool solve(Maze &maze, Node* &start, Node* &goal) {
// Breadth first search
std::deque<Node*> frontier;
frontier.push_back(start);

Expand All @@ -129,19 +151,47 @@ static bool solve(Maze &maze, Node* &start, Node* &goal) {
std::cerr << "No solution\n";
return false;
}
Node* currentNode = frontier.front();
frontier.pop_front();

// BFS
//Node* currentNode = frontier.front();
//frontier.pop_front();

// DFS
//Node* currentNode = frontier.back();
//frontier.pop_back();

// idk what this is but it works better than BFS and DFS
// Get node closest to the goal node in the frontier and use that
int closestNodeIndex = -1;
for (int i = 0; i < frontier.size(); i++) {
if (closestNodeIndex == -1) {
closestNodeIndex = i;
}
else {
if (ManhattanDistance(frontier[i], goal) < ManhattanDistance(frontier[closestNodeIndex], goal)) {
closestNodeIndex = i;
}
}
}
Node* currentNode = frontier[closestNodeIndex];
frontier.erase(frontier.begin() + closestNodeIndex);

if (currentNode->state == Goal) {
std::cout << "Solved!\n";

while (currentNode->parent != nullptr) {
currentNode->state = Path;
currentNode = currentNode->parent; // Backtracking
}
return true;
}

std::array<Node*, 4> neighbors = maze.getNeighbours(currentNode);

for (auto neighbor : neighbors) {
if (neighbor != NULL && (neighbor->state == Undiscovered || neighbor->state == Goal)) {
if (neighbor != NULL && (neighbor->state == Undiscovered || neighbor->state == Goal || neighbor->state == Path)) {
neighbor->parent = currentNode;
frontier.push_back(neighbor);
if (neighbor->state == Undiscovered)
if (neighbor->state == Undiscovered || neighbor->state == Path)
neighbor->state = Discovered;
}
}
Expand Down
Loading

0 comments on commit 7da0046

Please sign in to comment.