Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: CI

on: [push, pull_request]

defaults:
run:
shell: bash

jobs:
build:
name: ${{ matrix.platform.name }} ${{ matrix.config.name }}
runs-on: ${{ matrix.platform.os }}

strategy:
fail-fast: false
matrix:
platform:
- { name: Windows VS2019, os: windows-2019 }
- { name: Windows VS2022, os: windows-2022 }
- { name: Linux GCC, os: ubuntu-latest }
- { name: Linux Clang, os: ubuntu-latest, flags: -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ }
- { name: macOS, os: macos-latest }
config:
- { name: Shared, flags: -DBUILD_SHARED_LIBS=TRUE }
- { name: Static, flags: -DBUILD_SHARED_LIBS=FALSE }

steps:
- name: Install Linux Dependencies
if: runner.os == 'Linux'
run: sudo apt-get update && sudo apt-get install libxrandr-dev libxcursor-dev libudev-dev libopenal-dev libflac-dev libvorbis-dev libgl1-mesa-dev libegl1-mesa-dev

- name: Checkout
uses: actions/checkout@v4

- name: Configure
run: cmake -B build ${{matrix.platform.flags}} ${{matrix.config.flags}}

- name: Build
run: cmake --build build --config Release
37 changes: 37 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
cmake_minimum_required(VERSION 3.28)
project(Project3)

set(CMAKE_CXX_STANDARD 17)
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)

include_directories(src)


include(FetchContent)
FetchContent_Declare(SFML
GIT_REPOSITORY https://github.com/SFML/SFML.git
GIT_TAG 2.6.x
GIT_SHALLOW ON
EXCLUDE_FROM_ALL
SYSTEM)
FetchContent_MakeAvailable(SFML)

add_executable(Project3
src/maze.cpp
src/maze.h
main.cpp
src/dfs.cpp
src/dfs.h
src/bfs.cpp
src/bfs.h)
target_link_libraries(Project3 PRIVATE sfml-graphics)
target_compile_features(Project3 PRIVATE cxx_std_17)
include_directories(${SFML_INCLUDE_DIRS})

if(WIN32)
add_custom_command(
TARGET Project3
COMMENT "Copy OpenAL DLL"
PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${SFML_SOURCE_DIR}/extlibs/bin/$<IF:$<EQUAL:${CMAKE_SIZEOF_VOID_P},8>,x64,x86>/openal32.dll $<TARGET_FILE_DIR:Project3>
VERBATIM)
endif()
177 changes: 58 additions & 119 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,142 +1,81 @@
#include "bfs.h"
#include "Maze.h"
using namespace std;

// maze size, will be changed later but is 20x20 for ease of use
const int width = 50;
const int height = 50;

#include <iostream>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <stack>
#include <random>
#include <dfs.h>

using namespace std;

// maze size, will be changed later but is 20x20 for ease of use
const int width = 50;
const int height = 50;

// directions to move in
vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

// random num gen credit: https://stackoverflow.com/questions/7560114/random-number-c-in-some-range
//
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> distr(0, 3);

// VISITABLE CELL must check 3 co
bool visitableCell(int currX, int currY, const vector<vector<int>>& maze) {
//make sure the DFS doesn't run into a wall
if (currX <= 0 || currX >= width - 1 || currY <= 0 || currY >= height - 1) {
return false;
}
int numWalls = 0;
//make sure the cell is surrounded by walls
for (int i = 0; i < 4; i++) {
int newX = currX + directions[i].first;
int newY = currY + directions[i].second;

if (maze[newX][newY] == 0 && newX >= 0 && newX < width && newY >= 0 && newY < height) {
numWalls++;
}
}
if (numWalls >= 3) {
return true;
}
else {
return false;
}

}

string generateMaze() {

//TODO: loop to continue while stack is not empty,
// pop the top of the stack and check if it is a dead end, if it is,
// remove the wall and add the adjacent cell to the stack, if it is not,
//add the adjacent cell to the stack
// // random num gen credit: https://stackoverflow.com/questions/7560114/random-number-c-in-some-range

// 0 is a wall, 1 is a path
vector<vector<int>> maze(width, vector<int>(height, 0));
// stringstream to print the maze
stringstream ss;

// initialize the maze with walls
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
maze[x][y] = 0;
}
}

// start and end points
int middleY = height / 2;
maze[0][middleY] = 1;
maze[width - 1][middleY] = 1;

// stack to keep track of current position
stack<pair<int, int>> currPos;
pair<int, int> startCell = {0, middleY};
currPos.push(startCell);

// Choose a random direction. Call visitableCell() on the cell in that direction.
// If visitableCell returns true, then we add it to the stack, remove the wall and the while loop repeats.
// It becomes our new currPosStack.
// If it doesn't, we choose a different random direction & call visitableCell on it.
// We do this until
//
while (!currPos.empty()) {
int currX = currPos.top().first;
int currY = currPos.top().second;

currPos.pop();

bool directions_guessed[4] = {false, false, false, false};
bool allTried = false;

while (!allTried) {
int dirIndex = distr(gen);

directions_guessed[dirIndex] = true;

int newX = currX + directions[dirIndex].first;
int newY = currY + directions[dirIndex].second;

if (visitableCell(newX, newY, maze)) {
maze[newX][newY] = 1;

currPos.push({newX, newY});
break;
}

allTried = true;
for (int i = 0; i < 4; i++) {
if (!directions_guessed[i]) {
allTried = false;
break;
}
int main() {
//create a window with text about what this is with
// string maze = generateMaze();
// cout << maze;
Maze m = Maze(width, height);
m.generate_maze();
m.remove_wall(m.height-1,m.width-1,2);
auto window = sf::RenderWindow({(int)WINDOW_WIDTH, (int)WINDOW_HEIGHT}, "CMake SFML Project");
m.show(window);

window.setFramerateLimit(144);
int frame = 0;
dfs d = dfs(&m);
bfs b = bfs(&m);

sf::Text text;
sf::Font font;
font.loadFromFile("src/NotoSans-VariableFont_wdth,wght.ttf");
text.setFont(font);
text.setCharacterSize(40);
text.setFillColor(sf::Color::Black);

while (window.isOpen()) {
for (auto event = sf::Event(); window.pollEvent(event);)
{
if (event.type == sf::Event::Closed)
{
window.close();
}

if(event.type == sf::Event::MouseButtonPressed) {
// m.reset();
// m.generate_maze();
// m.remove_wall(m.height-1,m.width-1,2);
//std :: cout << b.step_forward() << std::endl;
frame++;
}
}
}
}


if(frame != 0) {
b.step_forward();
d.step_forward();

// print the maze
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (maze[x][y] == 1) {
ss << " ";
} else {
ss << "██";
}
}
ss << "\n";
m.show(window);
text.setString(to_string(b.steps_taken));
text.setPosition(sf::Vector2f(1200,50));
window.draw(text);
text.setString(to_string(d.steps_taken));
text.setPosition(sf::Vector2f(1400,50));
window.draw(text);
window.display();
}

// return the maze
return ss.str();
}



int main() {
string maze = generateMaze();
cout << maze;

return 0;
}
Loading
Loading