Skip to content

Commit 8db8943

Browse files
authored
Merge pull request #15 from ArjArav98/separated_hosts
Separated hosts
2 parents a9be3bf + 3a34e59 commit 8db8943

19 files changed

+684
-564
lines changed

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ A C++17-compatible header that provides useful functions which help with the sol
1818
## Usage
1919

2020
* Simply download the `src/sudoku_suite.h` file and move it to your project's directory.
21-
* Include the header file, as shown below in the examples, and use the functions you need!
21+
* Include the required header files, as shown below in the examples, and use the functions you need!
2222
* **NOTE:** The code is incompatible with pre-C++17 versions. While compiling, you'll have to compile with the `--std=c++17` flag.
2323
* For example; when using the clang compiler, the compile command would be `c++ --std=c++17 /path/to/file.cpp`
2424

@@ -40,7 +40,7 @@ There are **three functions** that Sudoku-Suite provides the developer, along wi
4040

4141
## Examples
4242

43-
**NOTE:** The following examples are also present in the repository in the `samples/` directory.
43+
**NOTE:** The following examples are also present in the repository in the `examples/` directory.
4444

4545
* [Solving and validating a Sudoku puzzle](#solving-and-validating-sudoku-puzzle)
4646
* [Generating a Sudoku puzzle](#generating-a-sudoku-puzzle)
@@ -51,7 +51,8 @@ There are **three functions** that Sudoku-Suite provides the developer, along wi
5151
#### Solving and validating Sudoku puzzle
5252
```
5353
#include<iostream>
54-
#include"/path/to/sudoku_suite.h"
54+
#include"/path/to/src/sudoku_solver.h"
55+
#include"/path/to/src/sudoku_validator.h"
5556
5657
int main() {
5758
sudoku::Grid grid({{
@@ -80,7 +81,7 @@ int main() {
8081
#### Generating a Sudoku puzzle
8182
```
8283
#include<iostream>
83-
#include"/path/to/sudoku_suite.h"
84+
#include"/path/to/src/sudoku_generator.h"
8485
8586
int main() {
8687
sudoku::Grid grid = sudoku::generate_puzzle();
@@ -93,7 +94,10 @@ int main() {
9394
#### Initialising and reusing Grid objects
9495
```
9596
#include<iostream>
96-
#include"/path/to/sudoku_suite.h"
97+
#include"/path/to/src/grid.h"
98+
99+
/* NOTE: If you only need to utilise the Grid object,
100+
* you can just import the "grid.h" header alone. */
97101
98102
int main() {
99103
//================
@@ -162,7 +166,10 @@ File: `sample1.txt`
162166

163167
```
164168
#include<iostream>
165-
#include"/path/to/sudoku_suite.h"
169+
#include"/path/to/src/grid.h"
170+
171+
/* NOTE: If you only need to utilise the Grid object,
172+
* you can just import the "grid.h" header alone. */
166173
167174
int main() {
168175
sudoku::Grid grid;
@@ -179,7 +186,10 @@ int main() {
179186
#### Operations on Grid objects
180187
```
181188
#include<iostream>
182-
#include"/path/to/sudoku_suite.h"
189+
#include"/path/to/src/grid.h"
190+
191+
/* NOTE: If you only need to utilise the Grid object,
192+
* you can just import the "grid.h" header alone. */
183193
184194
int main() {
185195
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
/* A basic example. */
1+
/* Copyright 2023 Arjun Aravind */
2+
/* A basic example as seen in the project's README. */
23

34
#include<iostream>
4-
#include"../src/sudoku_suite.h"
5+
#include"../src/sudoku_solver.h"
6+
7+
/* NOTE: If you only need to utilise the Grid object,
8+
* you can just import the "grid.h" header alone. */
59

610
int main() {
711
sudoku::Grid grid;
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
/* A basic example. */
1+
/* Copyright 2023 Arjun Aravind */
2+
/* A basic example as seen in the project's README. */
23

34
#include<iostream>
4-
#include"../src/sudoku_suite.h"
5+
#include"../src/grid.h"
6+
7+
/* NOTE: If you only need to utilise the Grid object,
8+
* you can just import the "grid.h" header alone. */
59

610
int main() {
711
// Method 1: Use the constructor and pass in a
@@ -18,7 +22,7 @@ int main() {
1822
{{ 0, 2, 8, 0, 0, 0, 0, 0, 0 }}
1923
}});
2024
std::cout << grid << std::endl;
21-
25+
2226
// Method 2: Declare a 2D array and pass it
2327
// into the set_initial_state() method.
2428

@@ -38,7 +42,7 @@ int main() {
3842

3943
// Method 3: Read the initial state of the puzzle
4044
// from a file.
41-
45+
4246
grid.set_initial_state_from_file("samples/sample1.txt");
4347
std::cout << grid << std::endl;
4448

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
/* A basic example. */
1+
/* Copyright 2023 Arjun Aravind */
2+
/* A basic example as seen in the project's README. */
23

34
#include<iostream>
4-
#include"../src/sudoku_suite.h"
5+
#include"../src/grid.h"
56

6-
int main() {
7+
/* NOTE: If you only need to utilise the Grid object,
8+
* you can just import the "grid.h" header alone. */
79

10+
int main() {
811
sudoku::Grid sample_grid_1({{
912
{{ 1, 7, 2, 5, 4, 9, 6, 8, 3 }},
1013
{{ 6, 4, 5, 8, 7, 3, 2, 1, 9 }},
@@ -16,7 +19,7 @@ int main() {
1619
{{ 7, 3, 1, 6, 8, 2, 5, 9, 4 }},
1720
{{ 5, 2, 8, 9, 3, 4, 1, 6, 7 }}
1821
}});
19-
22+
2023
sudoku::Grid sample_grid_2({{
2124
{{ 1, 7, 2, 5, 4, 9, 6, 8, 3 }},
2225
{{ 6, 4, 5, 8, 7, 3, 2, 1, 9 }},
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
/* A basic example. */
1+
/* Copyright 2023 Arjun Aravind */
2+
/* A basic example as seen in the project's README. */
23

34
#include<iostream>
4-
#include"../src/sudoku_suite.h"
5+
#include"../src/sudoku_generator.h"
56

67
int main() {
78
sudoku::Grid grid = sudoku::generate_puzzle();
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
/* A basic example. */
1+
/* Copyright 2023 Arjun Aravind */
2+
/* A basic example as seen in the project's README. */
23

34
#include<iostream>
4-
#include"../src/sudoku_suite.h"
5+
#include"../src/sudoku_solver.h"
6+
#include"../src/sudoku_validator.h"
57

68
int main() {
79
sudoku::Grid grid({{
8-
{{ 0, 0, 0, 0, 0, 0, 6, 8, 0 }}, // The 0s represent blank cells.
10+
{{ 0, 0, 0, 0, 0, 0, 6, 8, 0 }}, // The 0s represent blank cells.
911
{{ 0, 0, 0, 0, 7, 3, 0, 0, 9 }},
1012
{{ 3, 0, 9, 0, 0, 0, 0, 4, 5 }},
1113
{{ 4, 9, 0, 0, 0, 0, 0, 0, 0 }},
@@ -18,9 +20,13 @@ int main() {
1820

1921
sudoku::solve(&grid);
2022

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

25+
if (sudoku::is_valid_solution(grid)) {
26+
std::cout << "Solution is valid!\n";
27+
} else {
28+
std::cout << "DEFCON 4! Something isn't working.\n";
29+
}
30+
2531
return 0;
2632
}

src/coord.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Copyright 2023 Arjun Aravind */
2+
#ifndef SRC_COORD_H_
3+
#define SRC_COORD_H_
4+
5+
#include<utility>
6+
7+
namespace sudoku {
8+
9+
typedef std::pair<int, int> Coord;
10+
int GRID_LEN = 9;
11+
12+
} // namespace sudoku
13+
14+
#endif // SRC_COORD_H_

src/coord_utils.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* Copyright 2023 Arjun Aravind */
2+
#ifndef SRC_COORD_UTILS_H_
3+
#define SRC_COORD_UTILS_H_
4+
5+
#include<chrono>
6+
#include<random>
7+
#include<set>
8+
#include<utility>
9+
#include<vector>
10+
11+
#include"../src/coord.h"
12+
13+
namespace sudoku {
14+
15+
Coord get_next_cell_coord(Coord coord) {
16+
/* Function which returns the next successive coordinate for a
17+
* Sudoku grid, given a current coordinate. */
18+
if (coord.second == GRID_LEN-1 && coord.first == GRID_LEN-1)
19+
return coord;
20+
else if (coord.second == GRID_LEN-1)
21+
return std::make_pair(coord.first+1, 0);
22+
return std::make_pair(coord.first, coord.second + 1);
23+
}
24+
25+
std::set<Coord> get_N_random_cell_coords(int n) {
26+
std::random_device rd; // obtain a random number from hardware
27+
std::mt19937 gen(rd()); // seed the generator
28+
std::uniform_int_distribution<> distr(0, GRID_LEN-1); // define the range
29+
30+
std::set<Coord> random_cell_coords;
31+
32+
while (n > 0) {
33+
auto cell_coord = std::make_pair(distr(gen), distr(gen));
34+
auto inserted = random_cell_coords.insert(cell_coord);
35+
36+
if (!inserted.second) continue; // Element already existed.
37+
n--;
38+
}
39+
40+
return random_cell_coords;
41+
}
42+
43+
} // namespace sudoku
44+
45+
#endif // SRC_COORD_UTILS_H_

0 commit comments

Comments
 (0)