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
90 changes: 63 additions & 27 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@
#include <iostream>
#include "sortings.cpp"
#include "mazes.cpp"
#include <conio.h>

void clear_input_buffer() {
while (_kbhit()) {
_getch();
}
}

using namespace std;

class Menu {
private:
int enter_n() {
int enter_permutation() {
system("cls");
cout << "Enter size of permutation to be sort:\n";
int n = -1;
clear_input_buffer();
cin >> n;
while (cin.fail() || n <= 0 || n > 200) {
system("cls");
Expand All @@ -23,6 +31,23 @@ class Menu {
}
return n;
}
pair<int, int> enter_maze() {
system("cls");
cout << "Enter size of maze (height width):\n";
int height = -1, width = -1;
cin >> height >> width;
clear_input_buffer();
while (cin.fail() || height <= 0 || width <= 0 || height > 50 || width > 50) {
system("cls");
cout << "Invalid size. Please enter height (1-50) and width (1-50).\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> height >> width;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return {height, width};
}

void sort_selection_menu() {
string menuItems[8] = { "Bubble sort", "Selection sort", "Merge sort", "Quick sort",
Expand Down Expand Up @@ -63,7 +88,7 @@ class Menu {
case 0: {
system("cls");
SortVisualizer sorter;
int n = enter_n();
int n = enter_permutation();
system("cls");
sorter.random_array(n);
sorter.bubble_sort();
Expand All @@ -73,7 +98,7 @@ class Menu {
case 1: {
system("cls");
SortVisualizer sorter;
int n = enter_n();
int n = enter_permutation();
system("cls");
sorter.random_array(n);
sorter.selection_sort();
Expand All @@ -83,7 +108,7 @@ class Menu {
case 2: {
system("cls");
SortVisualizer sorter;
int n = enter_n();
int n = enter_permutation();
system("cls");
sorter.random_array(n);
sorter.merge_sort();
Expand All @@ -93,7 +118,7 @@ class Menu {
case 3: {
system("cls");
SortVisualizer sorter;
int n = enter_n();
int n = enter_permutation();
system("cls");
sorter.random_array(n);
sorter.quick_sort();
Expand All @@ -103,7 +128,7 @@ class Menu {
case 4: {
system("cls");
SortVisualizer sorter;
int n = enter_n();
int n = enter_permutation();
system("cls");
sorter.random_array(n);
sorter.heap_sort();
Expand All @@ -113,7 +138,7 @@ class Menu {
case 5: {
system("cls");
SortVisualizer sorter;
int n = enter_n();
int n = enter_permutation();
system("cls");
sorter.random_array(n);
sorter.bogo_sort();
Expand All @@ -123,7 +148,7 @@ class Menu {
case 6: {
system("cls");
SortVisualizer sorter;
int n = enter_n();
int n = enter_permutation();
system("cls");
sorter.random_array(n);
sorter.miracle_sort();
Expand All @@ -135,6 +160,8 @@ class Menu {
cout << "Exiting...\n";
Sleep(1000);
main_menu_display();
menuActive = false;
return;
}
}
}
Expand All @@ -144,14 +171,14 @@ class Menu {
}

void maze_selection_menu() {
string menuItems[2] = { "Generate maze", "Exit" };
string menuItems[4] = { "DFS", "Kruskal", "Wilson", "Exit" };
int selected = 0;
bool menuActive = true;

while (menuActive) {
system("cls");
cout << "=== MAZE MENU ===\n\n";
for (int i = 0; i < 2; i++) {
for (int i = 0; i < 4; i++) {
if (i == selected) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11); // cyan
cout << "> " << menuItems[i] << " <" << endl;
Expand All @@ -168,41 +195,50 @@ class Menu {
while (menuActive) {
if (GetAsyncKeyState(VK_UP) & 1) {
selected--;
if (selected < 0) selected = 2;
if (selected < 0) selected = 3;
break;
}
else if (GetAsyncKeyState(VK_DOWN) & 1){
selected++;
if (selected > 2) selected = 0;
if (selected > 3) selected = 0;
break;
}
else if (GetAsyncKeyState(VK_RETURN) & 1){
switch (selected){
case 0: {
system("cls");
cout << "Enter size of maze (height width): ";
int height = -1, width = -1;
cin >> height >> width;
while (cin.fail() || height <= 0 || width <= 0 || height > 50 || width > 50) {
system("cls");
cout << "Invalid size. Please enter height (1-50) and width (1-50).\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> height >> width;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
int height, width;
tie(height, width) = enter_maze();
system("cls");
MazeVisualizer mazeVis;
mazeVis.generate_maze(height, width);
mazeVis.dfs(height, width);
system("pause");
return;
}
case 1: {
int height, width;
tie(height, width) = enter_maze();
system("cls");
MazeVisualizer mazeVis;
mazeVis.kruskal(height, width);
system("pause");
return;
}
case 2: {
int height, width;
tie(height, width) = enter_maze();
system("cls");
MazeVisualizer mazeVis;
mazeVis.wilson(height, width);
system("pause");
return;
}
case 3: {
system("cls");
cout << "Exiting...\n";
Sleep(1000);
main_menu_display();
menuActive = false;
return;
}
}
}
Expand Down Expand Up @@ -282,4 +318,4 @@ int main()
{
Menu menu;
menu.main_menu_display();
}
}
Loading