Skip to content

Commit 4f65c48

Browse files
committed
Add sample programs to demonstrate usage
1 parent 82e18fb commit 4f65c48

File tree

6 files changed

+149
-0
lines changed

6 files changed

+149
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* A basic example. */
2+
3+
#include<iostream>
4+
#include"../src/sudoku_suite.h"
5+
6+
int main() {
7+
sudoku::Grid grid;
8+
grid.set_initial_state_from_file("samples/sample1.txt");
9+
std::cout << grid << std::endl;
10+
11+
sudoku::solve(&grid);
12+
std::cout << grid << std::endl;
13+
14+
return 0;
15+
}

samples/initialising_grids.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* A basic example. */
2+
3+
#include<iostream>
4+
#include"../src/sudoku_suite.h"
5+
6+
int main() {
7+
// Method 1: Use the constructor and pass in a
8+
// 2D matrix of values.
9+
sudoku::Grid grid({{
10+
{{ 0, 0, 0, 0, 0, 0, 6, 8, 0 }},
11+
{{ 0, 0, 0, 0, 7, 3, 0, 0, 9 }},
12+
{{ 3, 0, 9, 0, 0, 0, 0, 4, 5 }},
13+
{{ 4, 9, 0, 0, 0, 0, 0, 0, 0 }},
14+
{{ 8, 0, 3, 0, 5, 0, 9, 0, 2 }},
15+
{{ 0, 0, 0, 0, 0, 0, 0, 3, 6 }},
16+
{{ 9, 6, 0, 0, 0, 0, 3, 0, 8 }},
17+
{{ 7, 0, 0, 6, 8, 0, 0, 0, 0 }},
18+
{{ 0, 2, 8, 0, 0, 0, 0, 0, 0 }}
19+
}});
20+
std::cout << grid << std::endl;
21+
22+
// Method 2: Declare a 2D array and pass it
23+
// into the set_initial_state() method.
24+
25+
std::array<std::array<int, 9>, 9> values = {{
26+
{{ 9, 7, 8, 4, 1, 0, 0, 0, 5 }},
27+
{{ 3, 0, 0, 8, 0, 0, 0, 6, 4 }},
28+
{{ 6, 0, 0, 3, 5, 0, 0, 9, 0 }},
29+
{{ 2, 6, 9, 0, 3, 0, 0, 0, 7 }},
30+
{{ 5, 0, 7, 1, 0, 0, 0, 0, 0 }},
31+
{{ 1, 0, 3, 0, 0, 2, 0, 8, 9 }},
32+
{{ 7, 1, 0, 2, 0, 5, 0, 0, 3 }},
33+
{{ 0, 0, 2, 7, 0, 3, 1, 5, 8 }},
34+
{{ 0, 0, 5, 9, 4, 1, 0, 0, 0 }}
35+
}};
36+
grid.set_initial_state(values);
37+
std::cout << grid << std::endl;
38+
39+
// Method 3: Read the initial state of the puzzle
40+
// from a file.
41+
42+
grid.set_initial_state_from_file("samples/sample1.txt");
43+
std::cout << grid << std::endl;
44+
45+
return 0;
46+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* A basic example. */
2+
3+
#include<iostream>
4+
#include"../src/sudoku_suite.h"
5+
6+
int main() {
7+
8+
sudoku::Grid sample_grid_1({{
9+
{{ 1, 7, 2, 5, 4, 9, 6, 8, 3 }},
10+
{{ 6, 4, 5, 8, 7, 3, 2, 1, 9 }},
11+
{{ 3, 8, 9, 2, 6, 1, 7, 4, 5 }},
12+
{{ 4, 9, 6, 3, 2, 7, 8, 5, 1 }},
13+
{{ 8, 1, 3, 4, 5, 6, 9, 7, 2 }},
14+
{{ 2, 5, 7, 1, 9, 8, 4, 3, 6 }},
15+
{{ 9, 6, 4, 7, 1, 5, 3, 2, 8 }},
16+
{{ 7, 3, 1, 6, 8, 2, 5, 9, 4 }},
17+
{{ 5, 2, 8, 9, 3, 4, 1, 6, 7 }}
18+
}});
19+
20+
sudoku::Grid sample_grid_2({{
21+
{{ 1, 7, 2, 5, 4, 9, 6, 8, 3 }},
22+
{{ 6, 4, 5, 8, 7, 3, 2, 1, 9 }},
23+
{{ 3, 8, 9, 2, 6, 1, 7, 4, 5 }},
24+
{{ 4, 9, 6, 3, 2, 7, 8, 5, 1 }},
25+
{{ 8, 1, 3, 4, 5, 6, 9, 7, 2 }},
26+
{{ 2, 5, 7, 1, 9, 8, 4, 3, 6 }},
27+
{{ 9, 6, 4, 7, 1, 5, 3, 2, 8 }},
28+
{{ 7, 3, 1, 6, 8, 2, 5, 9, 4 }},
29+
{{ 5, 2, 8, 9, 3, 4, 1, 6, 7 }}
30+
}});
31+
32+
if (sample_grid_1 == sample_grid_2) std::cout << "They're the same! \n\n";
33+
34+
// Get grid value for coordinate.
35+
std::pair<int, int> last_coord = std::make_pair(8, 8);
36+
int last_value = sample_grid_1.get(last_coord);
37+
38+
// Print the grid to the console.
39+
std::cout << sample_grid_2 << std::endl;
40+
41+
return 0;
42+
}

samples/sample1.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
5 3 0 0 7 0 0 0 0
2+
6 0 0 1 9 5 0 0 0
3+
0 9 8 0 0 0 0 6 0
4+
8 0 0 0 6 0 0 0 3
5+
4 0 0 8 0 3 0 0 1
6+
7 0 0 0 2 0 0 0 6
7+
0 6 0 0 0 0 2 8 0
8+
0 0 0 4 1 9 0 0 5
9+
0 0 0 0 8 0 0 7 9

samples/sudoku_generator.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* A basic example. */
2+
3+
#include<iostream>
4+
#include"../src/sudoku_suite.h"
5+
6+
int main() {
7+
sudoku::Grid grid = sudoku::generate_puzzle();
8+
std::cout << grid << std::endl;
9+
10+
return 0;
11+
}

samples/sudoku_solver.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* A basic example. */
2+
3+
#include<iostream>
4+
#include"../src/sudoku_suite.h"
5+
6+
int main() {
7+
sudoku::Grid grid({{
8+
{{ 0, 0, 0, 0, 0, 0, 6, 8, 0 }}, // The 0s represent blank cells.
9+
{{ 0, 0, 0, 0, 7, 3, 0, 0, 9 }},
10+
{{ 3, 0, 9, 0, 0, 0, 0, 4, 5 }},
11+
{{ 4, 9, 0, 0, 0, 0, 0, 0, 0 }},
12+
{{ 8, 0, 3, 0, 5, 0, 9, 0, 2 }},
13+
{{ 0, 0, 0, 0, 0, 0, 0, 3, 6 }},
14+
{{ 9, 6, 0, 0, 0, 0, 3, 0, 8 }},
15+
{{ 7, 0, 0, 6, 8, 0, 0, 0, 0 }},
16+
{{ 0, 2, 8, 0, 0, 0, 0, 0, 0 }}
17+
}});
18+
19+
sudoku::solve(&grid);
20+
21+
std::cout << "Solution is valid? --> ";
22+
std::cout << sudoku::is_valid_solution(grid) << std::endl;
23+
std::cout << grid << std::endl;
24+
25+
return 0;
26+
}

0 commit comments

Comments
 (0)