Skip to content

Commit

Permalink
Add sample programs to demonstrate usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ArjArav98 committed Apr 9, 2023
1 parent 82e18fb commit 4f65c48
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 0 deletions.
15 changes: 15 additions & 0 deletions samples/initialising_grid_from_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* A basic example. */

#include<iostream>
#include"../src/sudoku_suite.h"

int main() {
sudoku::Grid grid;
grid.set_initial_state_from_file("samples/sample1.txt");
std::cout << grid << std::endl;

sudoku::solve(&grid);
std::cout << grid << std::endl;

return 0;
}
46 changes: 46 additions & 0 deletions samples/initialising_grids.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* A basic example. */

#include<iostream>
#include"../src/sudoku_suite.h"

int main() {
// Method 1: Use the constructor and pass in a
// 2D matrix of values.
sudoku::Grid grid({{
{{ 0, 0, 0, 0, 0, 0, 6, 8, 0 }},
{{ 0, 0, 0, 0, 7, 3, 0, 0, 9 }},
{{ 3, 0, 9, 0, 0, 0, 0, 4, 5 }},
{{ 4, 9, 0, 0, 0, 0, 0, 0, 0 }},
{{ 8, 0, 3, 0, 5, 0, 9, 0, 2 }},
{{ 0, 0, 0, 0, 0, 0, 0, 3, 6 }},
{{ 9, 6, 0, 0, 0, 0, 3, 0, 8 }},
{{ 7, 0, 0, 6, 8, 0, 0, 0, 0 }},
{{ 0, 2, 8, 0, 0, 0, 0, 0, 0 }}
}});
std::cout << grid << std::endl;

// Method 2: Declare a 2D array and pass it
// into the set_initial_state() method.

std::array<std::array<int, 9>, 9> values = {{
{{ 9, 7, 8, 4, 1, 0, 0, 0, 5 }},
{{ 3, 0, 0, 8, 0, 0, 0, 6, 4 }},
{{ 6, 0, 0, 3, 5, 0, 0, 9, 0 }},
{{ 2, 6, 9, 0, 3, 0, 0, 0, 7 }},
{{ 5, 0, 7, 1, 0, 0, 0, 0, 0 }},
{{ 1, 0, 3, 0, 0, 2, 0, 8, 9 }},
{{ 7, 1, 0, 2, 0, 5, 0, 0, 3 }},
{{ 0, 0, 2, 7, 0, 3, 1, 5, 8 }},
{{ 0, 0, 5, 9, 4, 1, 0, 0, 0 }}
}};
grid.set_initial_state(values);
std::cout << grid << std::endl;

// Method 3: Read the initial state of the puzzle
// from a file.

grid.set_initial_state_from_file("samples/sample1.txt");
std::cout << grid << std::endl;

return 0;
}
42 changes: 42 additions & 0 deletions samples/operations_on_grid_objects.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* A basic example. */

#include<iostream>
#include"../src/sudoku_suite.h"

int main() {

sudoku::Grid sample_grid_1({{
{{ 1, 7, 2, 5, 4, 9, 6, 8, 3 }},
{{ 6, 4, 5, 8, 7, 3, 2, 1, 9 }},
{{ 3, 8, 9, 2, 6, 1, 7, 4, 5 }},
{{ 4, 9, 6, 3, 2, 7, 8, 5, 1 }},
{{ 8, 1, 3, 4, 5, 6, 9, 7, 2 }},
{{ 2, 5, 7, 1, 9, 8, 4, 3, 6 }},
{{ 9, 6, 4, 7, 1, 5, 3, 2, 8 }},
{{ 7, 3, 1, 6, 8, 2, 5, 9, 4 }},
{{ 5, 2, 8, 9, 3, 4, 1, 6, 7 }}
}});

sudoku::Grid sample_grid_2({{
{{ 1, 7, 2, 5, 4, 9, 6, 8, 3 }},
{{ 6, 4, 5, 8, 7, 3, 2, 1, 9 }},
{{ 3, 8, 9, 2, 6, 1, 7, 4, 5 }},
{{ 4, 9, 6, 3, 2, 7, 8, 5, 1 }},
{{ 8, 1, 3, 4, 5, 6, 9, 7, 2 }},
{{ 2, 5, 7, 1, 9, 8, 4, 3, 6 }},
{{ 9, 6, 4, 7, 1, 5, 3, 2, 8 }},
{{ 7, 3, 1, 6, 8, 2, 5, 9, 4 }},
{{ 5, 2, 8, 9, 3, 4, 1, 6, 7 }}
}});

if (sample_grid_1 == sample_grid_2) std::cout << "They're the same! \n\n";

// Get grid value for coordinate.
std::pair<int, int> last_coord = std::make_pair(8, 8);
int last_value = sample_grid_1.get(last_coord);

// Print the grid to the console.
std::cout << sample_grid_2 << std::endl;

return 0;
}
9 changes: 9 additions & 0 deletions samples/sample1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
5 3 0 0 7 0 0 0 0
6 0 0 1 9 5 0 0 0
0 9 8 0 0 0 0 6 0
8 0 0 0 6 0 0 0 3
4 0 0 8 0 3 0 0 1
7 0 0 0 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9
11 changes: 11 additions & 0 deletions samples/sudoku_generator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* A basic example. */

#include<iostream>
#include"../src/sudoku_suite.h"

int main() {
sudoku::Grid grid = sudoku::generate_puzzle();
std::cout << grid << std::endl;

return 0;
}
26 changes: 26 additions & 0 deletions samples/sudoku_solver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* A basic example. */

#include<iostream>
#include"../src/sudoku_suite.h"

int main() {
sudoku::Grid grid({{
{{ 0, 0, 0, 0, 0, 0, 6, 8, 0 }}, // The 0s represent blank cells.
{{ 0, 0, 0, 0, 7, 3, 0, 0, 9 }},
{{ 3, 0, 9, 0, 0, 0, 0, 4, 5 }},
{{ 4, 9, 0, 0, 0, 0, 0, 0, 0 }},
{{ 8, 0, 3, 0, 5, 0, 9, 0, 2 }},
{{ 0, 0, 0, 0, 0, 0, 0, 3, 6 }},
{{ 9, 6, 0, 0, 0, 0, 3, 0, 8 }},
{{ 7, 0, 0, 6, 8, 0, 0, 0, 0 }},
{{ 0, 2, 8, 0, 0, 0, 0, 0, 0 }}
}});

sudoku::solve(&grid);

std::cout << "Solution is valid? --> ";
std::cout << sudoku::is_valid_solution(grid) << std::endl;
std::cout << grid << std::endl;

return 0;
}

0 comments on commit 4f65c48

Please sign in to comment.