diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index b4250d8..dec51c8 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -1,20 +1,20 @@ { - "configurations": [ - { - "name": "Win32", - "includePath": [ - "${workspaceFolder}/**" - ], - "defines": [ - "_DEBUG", - "UNICODE", - "_UNICODE" - ], - "compilerPath": "C:\\MinGW\\bin\\gcc.exe", - "cStandard": "c11", - "cppStandard": "gnu++14", - "intelliSenseMode": "windows-gcc-x86" - } - ], - "version": 4 + "configurations": [ + { + "name": "windows-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [ + "_DEBUG", + "UNICODE", + "_UNICODE" + ], + "compilerPath": "C:/msys64/mingw64/bin/gcc.exe", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "windows-gcc-x64" + } + ], + "version": 4 } \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..b4d89a5 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": true, + "cwd": "d:/OPCODE/CPP_Mini_Projects/Src/Expense_Tracker", + "program": "d:/OPCODE/CPP_Mini_Projects/Src/Expense_Tracker/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 71a6ea2..cb4258b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,73 +1,130 @@ { - "C_Cpp.errorSquiggles": "disabled", - "files.associations": { - "array": "cpp", - "atomic": "cpp", - "bitset": "cpp", - "cctype": "cpp", - "cfenv": "cpp", - "chrono": "cpp", - "cinttypes": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "codecvt": "cpp", - "complex": "cpp", - "condition_variable": "cpp", - "csetjmp": "cpp", - "csignal": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cuchar": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "forward_list": "cpp", - "list": "cpp", - "unordered_map": "cpp", - "unordered_set": "cpp", - "vector": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "map": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "random": "cpp", - "ratio": "cpp", - "regex": "cpp", - "set": "cpp", - "string": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "fstream": "cpp", - "future": "cpp", - "initializer_list": "cpp", - "iomanip": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "mutex": "cpp", - "new": "cpp", - "ostream": "cpp", - "scoped_allocator": "cpp", - "shared_mutex": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "thread": "cpp", - "typeindex": "cpp", - "typeinfo": "cpp", - "valarray": "cpp" - } + "C_Cpp.errorSquiggles": "disabled", + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "cfenv": "cpp", + "chrono": "cpp", + "cinttypes": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "codecvt": "cpp", + "complex": "cpp", + "condition_variable": "cpp", + "csetjmp": "cpp", + "csignal": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cuchar": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "forward_list": "cpp", + "list": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "map": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "ratio": "cpp", + "regex": "cpp", + "set": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "future": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "ostream": "cpp", + "scoped_allocator": "cpp", + "shared_mutex": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "typeindex": "cpp", + "typeinfo": "cpp", + "valarray": "cpp" + }, + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false } \ No newline at end of file diff --git a/Src/Sudoku_Generator_Solver/Game.cpp b/Src/Sudoku_Generator_Solver/Game.cpp new file mode 100644 index 0000000..02cb9e0 --- /dev/null +++ b/Src/Sudoku_Generator_Solver/Game.cpp @@ -0,0 +1,250 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +class SudokuSolver +{ +private: + vector> grid; + vector> solution; + +public: + SudokuSolver() + { + grid.assign(9, vector(9, 0)); + solution.assign(9, vector(9, 0)); + } + + bool isValid(vector> &g, int row, int col, int num) + { + // Check row + for (int j = 0; j < 9; j++) + { + if (g[row][j] == num) + return false; + } + + // Check column + for (int i = 0; i < 9; i++) + { + if (g[i][col] == num) + return false; + } + + // Check 3x3 box + int boxRow = 3 * (row / 3); + int boxCol = 3 * (col / 3); + for (int i = boxRow; i < boxRow + 3; i++) + { + for (int j = boxCol; j < boxCol + 3; j++) + { + if (g[i][j] == num) + return false; + } + } + + return true; + } + + bool solve(vector> &g) + { + for (int row = 0; row < 9; row++) + { + for (int col = 0; col < 9; col++) + { + if (g[row][col] == 0) + { + for (int num = 1; num <= 9; num++) + { + if (isValid(g, row, col, num)) + { + g[row][col] = num; + if (solve(g)) + return true; + g[row][col] = 0; + } + } + return false; + } + } + } + return true; + } + + void fillDiagonalBoxes() + { + vector numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + for (int box = 0; box < 3; box++) + { + auto rng = std::default_random_engine(std::random_device{}()); + std::shuffle(numbers.begin(), numbers.end(), rng); + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + grid[box * 3 + i][box * 3 + j] = numbers[i * 3 + j]; + } + } + } + } + + int countSolutions(vector> &g, int &count) + { + if (count > 1) + return count; + + for (int row = 0; row < 9; row++) + { + for (int col = 0; col < 9; col++) + { + if (g[row][col] == 0) + { + for (int num = 1; num <= 9; num++) + { + if (isValid(g, row, col, num)) + { + g[row][col] = num; + countSolutions(g, count); + g[row][col] = 0; + } + } + return count; + } + } + } + count++; + return count; + } + + bool hasUniqueSolution(vector> &puzzle) + { + vector> testGrid = puzzle; + int solutionCount = 0; + countSolutions(testGrid, solutionCount); + return solutionCount == 1; + } + + vector> generate(string difficulty = "medium") + { + // Number of clues based on difficulty + int clues; + if (difficulty == "easy") + clues = 50; + else if (difficulty == "medium") + clues = 40; + else if (difficulty == "hard") + clues = 30; + else if (difficulty == "expert") + clues = 20; + else + clues = 40; + + // Generate complete solution + grid.assign(9, vector(9, 0)); + fillDiagonalBoxes(); + solve(grid); + solution = grid; + + // Create puzzle by removing numbers + vector> puzzle = grid; + vector> cells; + for (int i = 0; i < 9; i++) + { + for (int j = 0; j < 9; j++) + { + cells.push_back({i, j}); + } + } + auto rng = std::default_random_engine(std::random_device{}()); + std::shuffle(cells.begin(), cells.end(), rng); + + int removed = 0; + int targetRemovals = 81 - clues; + + for (auto &cell : cells) + { + if (removed >= targetRemovals) + break; + + int row = cell.first; + int col = cell.second; + int backup = puzzle[row][col]; + puzzle[row][col] = 0; + + if (hasUniqueSolution(puzzle)) + { + removed++; + } + else + { + puzzle[row][col] = backup; + } + } + + return puzzle; + } + + void printGrid(const vector> &g) + { + for (int i = 0; i < 9; i++) + { + if (i % 3 == 0 && i != 0) + { + cout << "------+-------+------" << endl; + } + for (int j = 0; j < 9; j++) + { + if (j % 3 == 0 && j != 0) + { + cout << "| "; + } + cout << (g[i][j] != 0 ? to_string(g[i][j]) : ".") << " "; + } + cout << endl; + } + } + + vector> getSolution() + { + return solution; + } +}; + +int main() +{ + srand(time(0)); + + cout << "==================================================" << endl; + cout << "SUDOKU GENERATOR AND SOLVER (C++)" << endl; + cout << "==================================================" << endl; + + SudokuSolver solver; + vector difficulties = {"easy", "medium", "hard", "expert"}; + + for (const auto &difficulty : difficulties) + { + cout << "\n==================================================" << endl; + cout << "PUZZLE (" << difficulty << ")" << endl; + cout << "==================================================" << endl; + + vector> puzzle = solver.generate(difficulty); + solver.printGrid(puzzle); + + cout << "\n==================================================" << endl; + cout << "SOLUTION (" << difficulty << ")" << endl; + cout << "==================================================" << endl; + + solver.printGrid(solver.getSolution()); + } + + cout << "\n==================================================" << endl; + cout << "Code execution completed!" << endl; + cout << "==================================================" << endl; + + return 0; +} \ No newline at end of file diff --git a/Src/Sudoku_Generator_Solver/Game.exe b/Src/Sudoku_Generator_Solver/Game.exe new file mode 100644 index 0000000..ea170a5 Binary files /dev/null and b/Src/Sudoku_Generator_Solver/Game.exe differ