Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ZERDICORP committed Jul 25, 2021
0 parents commit d0f3e32
Show file tree
Hide file tree
Showing 13 changed files with 409 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.dll
*.exe
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Maze Path Finder

<kbd><img src="https://github.com/ZERDICORP/maze_path_finder/blob/master/screenshots/s1.png?row=true" alt="screenshot" width="250" height="250"></kbd>

### Just 3 steps to get started:
1) **Install [C++ SFML Library](https://www.sfml-dev.org/download.php).**
2) **In the "build.bat" file, specify your path to SFML.**
3) **Run "run.bat".**

### Author's libraries used by this project:
- [athm.h](https://github.com/ZERDICORP/athm-lib.git)
- [file.h](https://github.com/ZERDICORP/file-lib.git)
15 changes: 15 additions & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@echo off

REM ↓↓↓↓↓↓↓↓↓↓↓↓ Path to SFML ↓↓↓↓↓↓↓↓↓↓↓↓

SET sfmlPath="C:/Program Files/SFML-2.5.1"

REM ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

SET includePath=%sfmlPath%/include
SET libPath=%sfmlPath%/lib

g++ ./src/implementation/*.cpp -o ./build/main.exe -O3 -O2 -O1 -DSFML_STATIC -I %includePath% -I "./src/headers" -L %libPath% -lsfml-graphics -lsfml-window -lsfml-system

echo Press any button..
pause > nul
1 change: 1 addition & 0 deletions build/.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
squareWidth = 10
3 changes: 3 additions & 0 deletions run.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off
call build.bat
call start.bat
Binary file added screenshots/s1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/headers/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "include.h"
#include "macros.h"

#ifndef MAIN_CONFIG
#define MAIN_CONFIG
enum class EVENT_CODE
{
NONE,
CLOSE,
RESTART,
};
#endif
6 changes: 6 additions & 0 deletions src/headers/include.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <map>
#include <athm.h>
#include <file.h>
4 changes: 4 additions & 0 deletions src/headers/macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#define mWH 400
#define mWW 400
#define mTitle "maze generation"
#define mConfigPath ".cfg"
18 changes: 18 additions & 0 deletions src/headers/tools.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "config.h"

struct Node
{
bool bCollapse = false;
bool bTopWall = true;
bool bLeftWall = true;
};

void mazeGen(std::vector<std::vector<Node>>& nodes, int iRows, int iCols);
void displayConsoleInformation(std::map<std::string, float>& cfg);

std::map<std::string, float> readConfig(std::string sConfigPath);

EVENT_CODE eventListener(sf::RenderWindow& window);

int init(sf::RenderWindow& window);
int main();
200 changes: 200 additions & 0 deletions src/implementation/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
#include "config.h"
#include "tools.h"

int loop(sf::RenderWindow& window, std::map<std::string, float>& cfg)
{
bool bNeedToUpdateConsole = true;
bool bFinding = true;
bool bRollback = false;

int iSqW = cfg["squareWidth"];
int iRows = mWH / iSqW;
int iCols = mWW / iSqW;

sf::Vector2i startPos(0, 0);
sf::Vector2i finishPos(zer::athm::rand_int(10, iCols - 1), zer::athm::rand_int(10, iRows - 1));
sf::Vector2i currentPos = startPos;

std::vector<std::vector<Node>> nodes(iRows, std::vector<Node>(iCols, Node()));

mazeGen(nodes, iRows, iCols);

for (int iRow = 0; iRow < iRows; ++iRow)
for (int iCol = 0; iCol < iCols; ++iCol)
nodes[iRow][iCol].bCollapse = false;

std::vector<sf::Vector2i> history;
history.push_back(currentPos);

std::vector<sf::Vector2i> vectors({
sf::Vector2i(0, -1),
sf::Vector2i(0, 1),
sf::Vector2i(-1, 0),
sf::Vector2i(1, 0)
});

sf::RectangleShape rectStart(sf::Vector2f(iSqW, iSqW));
rectStart.setFillColor(sf::Color::Yellow);
rectStart.setPosition(0, 0);

sf::RectangleShape rectFinish(sf::Vector2f(iSqW, iSqW));
rectFinish.setFillColor(sf::Color::Red);
rectFinish.setPosition(finishPos.x * iSqW, finishPos.y * iSqW);

sf::VertexArray pathLine(sf::LinesStrip, 2);
pathLine[0].color = sf::Color(15, 255, 252);
pathLine[1].color = sf::Color(15, 255, 252);

sf::VertexArray mazeLine(sf::LineStrip, 2);
mazeLine[0].color = sf::Color::White;
mazeLine[1].color = sf::Color::White;

while (window.isOpen())
{
window.clear();

/*
Maze path finder algorithm body.
*/
if (bFinding)
{
if (currentPos.y == finishPos.y && currentPos.x == finishPos.x)
{
bFinding = false;
rectFinish.setFillColor(sf::Color::Green);
continue;
}

std::vector<int> vectorVariants({0, 1, 2, 3});

while (true)
{
int iRandomVectorVariantIndex = zer::athm::rand_int(vectorVariants.size());
int iRandomVectorIndex = vectorVariants[iRandomVectorVariantIndex];

sf::Vector2i tempPos = currentPos + vectors[iRandomVectorIndex];

if (zer::athm::inRange2D(iRows, iCols, tempPos.y, tempPos.x))
{
bool bWall = ((vectors[iRandomVectorIndex].y < 0 && nodes[currentPos.y][currentPos.x].bTopWall) ||
(vectors[iRandomVectorIndex].y > 0 && nodes[tempPos.y][tempPos.x].bTopWall) ||
(vectors[iRandomVectorIndex].x < 0 && nodes[currentPos.y][currentPos.x].bLeftWall) ||
(vectors[iRandomVectorIndex].x > 0 && nodes[tempPos.y][tempPos.x].bLeftWall));

/*
If there are no walls in the direction and we have not been there yet.
*/
if (!bWall && !nodes[tempPos.y][tempPos.x].bCollapse)
{
if (bRollback)
{
history.push_back(currentPos);
bRollback = false;
}

currentPos = tempPos;

history.push_back(currentPos);

nodes[currentPos.y][currentPos.x].bCollapse = true;

break;
}
else
vectorVariants.erase(vectorVariants.begin() + iRandomVectorVariantIndex);
}
else
vectorVariants.erase(vectorVariants.begin() + iRandomVectorVariantIndex);

if (vectorVariants.size() == 0)
{
currentPos = history[history.size() - 1];

history.erase(history.end() - 1);

bRollback = true;

break;
}
}
}

/*
Drawing start & finish.
*/
window.draw(rectStart);
window.draw(rectFinish);

/*
Drawing path.
*/
if (history.size() > 1)
{
for (int i = 1; i < history.size() - 1; ++i)
{
pathLine[0].position = sf::Vector2f(history[i - 1].x * iSqW + (iSqW / 2), history[i - 1].y * iSqW + (iSqW / 2));
pathLine[1].position = sf::Vector2f(history[i].x * iSqW + (iSqW / 2), history[i].y * iSqW + (iSqW / 2));

window.draw(pathLine);
}

pathLine[0].position = sf::Vector2f(
history[history.size() - 2].x * iSqW + (iSqW / 2), history[history.size() - 2].y * iSqW + (iSqW / 2));
pathLine[1].position = sf::Vector2f(
history[history.size() - 1].x * iSqW + (iSqW / 2), history[history.size() - 1].y * iSqW + (iSqW / 2));

window.draw(pathLine);
}

/*
Drawing maze.
*/
for (int r = 0; r < iRows; ++r)
{
for (int c = 0; c < iCols; ++c)
{
if (nodes[r][c].bTopWall)
{
mazeLine[0].position = sf::Vector2f(c * iSqW, r * iSqW);
mazeLine[1].position = sf::Vector2f(c * iSqW + iSqW, r * iSqW);

window.draw(mazeLine);
}
if (nodes[r][c].bLeftWall)
{
mazeLine[0].position = sf::Vector2f(c * iSqW, r * iSqW);
mazeLine[1].position = sf::Vector2f(c * iSqW, r * iSqW + iSqW);

window.draw(mazeLine);
}
}
}

window.display();

if (bNeedToUpdateConsole)
{
displayConsoleInformation(cfg);
bNeedToUpdateConsole = false;
}

switch (eventListener(window))
{
case EVENT_CODE::CLOSE:
window.close();
break;

case EVENT_CODE::RESTART:
init(window);
break;
}
}
return 0;
}

int init(sf::RenderWindow& window)
{
std::map<std::string, float> cfg = readConfig(mConfigPath);

return loop(window, cfg);
}
Loading

0 comments on commit d0f3e32

Please sign in to comment.