Skip to content

Commit

Permalink
Added Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
SahooBishwajeet committed Dec 27, 2023
1 parent eaf8ec2 commit 4671e05
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/cli.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#include "cli.h"

/**
* @brief Command line interface function.
*
* This function handles the command line interface for the Sudogs program.
* It takes command line arguments and performs the corresponding actions.
*
* @param argv The command line arguments.
*/
void cli(const char **argv) {
if(strcmp(argv[1], "--help") == 0) {
std::cout << "Usages: sudogs, sudogs --help, sudogs --gen [difficulty]\n\n"
Expand Down
34 changes: 32 additions & 2 deletions src/fill_grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

std::array<int, 9> value {1, 2, 3, 4, 5, 6, 7, 8, 9};

/**
* @brief Checks the validity of placing a number in a specific position in the Sudoku grid.
*
* This function checks if a number can be placed in a specific position in the Sudoku grid
* without violating the rules of Sudoku, which are:
* (1) The number should not appear more than once in the same row.
* (2) The number should not appear more than once in the same column.
* (3) The number should not appear more than once in the same 3x3 box.
*
* @param sudoku The Sudoku grid.
* @param row The row index of the position.
* @param col The column index of the position.
* @param num The number to be placed.
* @return True if the number can be placed in the position, false otherwise.
*/
bool isValid(int sudoku[][SUDOKU_SIZE], int row, int col, int num) {
// Validity in the Current Row
for(int i = 0; i < SUDOKU_SIZE; i++) {
Expand All @@ -24,6 +39,14 @@ bool isValid(int sudoku[][SUDOKU_SIZE], int row, int col, int num) {
return true;
}

/**
* @brief Checks if the Sudoku grid has any empty cells.
*
* This function checks if the Sudoku grid has any empty cells, i.e., cells with the value EMPTY_CELL.
*
* @param sudoku The Sudoku grid.
* @return True if the grid has empty cells, false otherwise.
*/
bool hasEmptyCell(int sudoku[][SUDOKU_SIZE]) {
for(int i = 0; i < SUDOKU_SIZE; i++) {
for(int j = 0; j < SUDOKU_SIZE; j++) {
Expand All @@ -34,11 +57,18 @@ bool hasEmptyCell(int sudoku[][SUDOKU_SIZE]) {
return false;
}

/**
* @brief Fills the Sudoku grid recursively.
*
* This function fills the Sudoku grid recursively by placing numbers in empty cells.
* It uses backtracking to find a valid solution for the Sudoku grid.
*
* @param sudoku The Sudoku grid.
* @return True if the grid is successfully filled, false otherwise.
*/
bool fillGrid(int sudoku[][SUDOKU_SIZE]) {
int row, col;
for(int i = 0; i < SUDOKU_SIZE * SUDOKU_SIZE; i++) {
// WHY THE FUCK NOT USE NORMAL ITERATOR??
// DON'T ASK ME, I'M HIGH RN
row = floor(i / SUDOKU_SIZE);
col = i % SUDOKU_SIZE;

Expand Down
7 changes: 7 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
#define SUDOKU_SIZE 9
#define EMPTY_CELL 0

/**
* @brief The main function of the program.
*
* @param argc The number of command-line arguments.
* @param argv An array of strings containing the command-line arguments.
* @return int The exit status of the program.
*/
int main(int argc, const char **argv) {

if(strcmp(argv[0], argv[argc - 1]) == 0) {
Expand Down
8 changes: 8 additions & 0 deletions src/sudoku_generator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#include "sudoku_generator.h"
#include "fill_grid.h"

/**
* Generates a Sudoku puzzle with the specified difficulty level.
* The generated puzzle is stored in the given 2D array.
*
* @param sudoku The 2D array to store the generated Sudoku puzzle.
* @param difficulty The difficulty level of the Sudoku puzzle.
* The higher the difficulty level, the fewer empty cells in the puzzle.
*/
void generateSudoku(int sudoku[][SUDOKU_SIZE], int difficulty) {
for (int i = 0; i < SUDOKU_SIZE; i++) {
for (int j = 0; j < SUDOKU_SIZE; j++) {
Expand Down
14 changes: 14 additions & 0 deletions src/sudoku_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
#include "fill_grid.h"
#include "print.h"

/**
* Solves the given Sudoku puzzle.
* Supports finding all possible solutions of the given puzzle.
*
* @param sudoku The Sudoku puzzle to be solved.
* @param allVals Flag indicating whether to generate all possible solutions.
*/
void sudokuSolver(int sudoku[][SUDOKU_SIZE], bool allVals) {
int solved[SUDOKU_SIZE][SUDOKU_SIZE];
for(int i = 0; i < SUDOKU_SIZE; i++) {
Expand All @@ -14,6 +21,13 @@ void sudokuSolver(int sudoku[][SUDOKU_SIZE], bool allVals) {
helper(solved, allVals);
}

/**
* Recursive helper function to solve the Sudoku puzzle.
* Uses backtracking to find the solution.
*
* @param sudoku The Sudoku puzzle grid.
* @param allVals Flag to indicate whether to find all solutions or stop after finding the first solution.
*/
void helper(int sudoku[][SUDOKU_SIZE], bool allVals) {
static int solutionCount = 0;

Expand Down
19 changes: 19 additions & 0 deletions src/tui.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#include "tui.h"

/**
* @brief Gets the user's choice for the desired action.
*
* This function displays a menu to the user and waits for their input.
* The user can choose to generate a Sudoku, solve a Sudoku, or exit the program.
* The function validates the user's input and repeats the menu until a valid choice is made.
*
* @return The user's choice as an integer.
*/
int getChoice() {
int choice;

Expand All @@ -19,6 +28,11 @@ int getChoice() {
return choice;
}

/**
* @brief Prompts the user to enter a difficulty level for generating a Sudoku puzzle.
*
* @return The difficulty level entered by the user (an integer between 1 and 5).
*/
int getDifficulty() {
int difficulty;

Expand All @@ -45,6 +59,11 @@ int getDifficulty() {
return difficulty;
}

/**
* Displays the solution of the Sudoku puzzle.
*
* @param sudoku The Sudoku puzzle grid.
*/
void displaySolution(int sudoku[][SUDOKU_SIZE]) {
char dispAll;

Expand Down

0 comments on commit 4671e05

Please sign in to comment.