-
Notifications
You must be signed in to change notification settings - Fork 1
/
solve.h
136 lines (113 loc) · 5.29 KB
/
solve.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* @file
* Sudoku solver interface.
*/
/***********************************************************************************
* Author: Laurent Farhi *
* Name: solve.h *
* Language: C *
* Copyright (C) 2009, All rights reserved. *
* *
* LICENSE: *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
***********************************************************************************/
#pragma once
#ifndef SUDOKU_SOLVE_H
#define SUDOKU_SOLVE_H
#include <stdint.h> // for uintptr_t
const char *sudoku_get_version ();
/// Types of events
typedef enum
{
ON_INIT = 1,
ON_CHANGE = 2,
ON_SOLVED = 4
} sudokuGridEventType;
#ifndef SUDOKU_SIZE
#define SUDOKU_SIZE (3)
#endif
#if SUDOKU_SIZE > 5
#error SUDOKU_SIZE should not exceed 5
#elif SUDOKU_SIZE < 2
#error SUDOKU_SIZE should exceed 1
#endif
// compile-time integer constant expression
enum
{
SQUARE_SIZE = SUDOKU_SIZE,
GRID_SIZE = SQUARE_SIZE * SQUARE_SIZE,
};
typedef struct
{
char *row_name;
char *column_name;
char *value_name;
char empty_code;
} GridReferential;
extern const GridReferential sudoku_grid_referential;
/// Type definition for events
typedef struct sudoku_grid_event_args
{
int grid[GRID_SIZE][GRID_SIZE][GRID_SIZE]; ///< Grid of 9 rows, 9 columns and 9 candidate values
int nbCells; ///< Number of non empty cells (for which the possible value has been found).
} sudoku_grid_event_args;
/// Type definition for callback functions called on events.
typedef void (*sudoku_grid_event_handler) (uintptr_t, sudoku_grid_event_args);
/// Adds a callback function called on events.
/// @param [in] event_type Types (or'ed) of event to which the function is to be added.
/// @param [in] handler Function pointer to be added.
void sudoku_grid_event_handler_add (sudokuGridEventType event_type, sudoku_grid_event_handler handler);
/// Removes a callback function called on events.
/// @param [in] type Types (or'ed) of event to which the function is to be removed.
/// @param [in] handler Function pointer to be added.
void sudoku_grid_event_handler_remove (sudokuGridEventType event_type, sudoku_grid_event_handler handler);
/// Type definition for messages
typedef struct sudoku_message_args
{
const char *rule; ///< Text of the rule
int verbosity; ///< Level of the rule
} sudoku_message_args;
/// Type definition for callback functions called on message.
typedef void (*sudoku_message_handler) (uintptr_t, sudoku_message_args);
/// Adds a callback function called on message.
/// @param [in] handler Function pointer to be added.
void sudoku_message_handler_add (sudoku_message_handler);
/// Removes a callback function called on message.
/// @param [in] handler Function pointer to be added.
void sudoku_message_handler_remove (sudoku_message_handler);
/// Removes all event handlers
void sudoku_all_handlers_clear ();
/// Enumeration of methods avalaible and used for solving
typedef enum
{
NONE, ///< None, when the grid can not be solved.
EXACT_COVER, ///< Exact cover search using dancing links algorithm (brut force)
ELIMINATION, ///< Elimination (human behavior)
BACKTRACKING, ///< Brut force using backtracking
} method;
/// Option to search for the first or all of the possible solutions.
typedef enum
{
FIRST, ///< First only
ALL ///< All of the solutions
} findSolutions;
void sudoku_init (void);
/// Solves the sudoku grid.
/// @param [in] startGrid Grid to be solved
/// @param [in] selected_method Method selected for solving the grid
/// @param [in] option option to choose to search for the first (#FIRST) or all (#ALL) of the possible solutions
/// @returns The method effectively used to solve the grid (promoted to #BACKTRACKING if needed).
method sudoku_solve (int startGrid[GRID_SIZE][GRID_SIZE], method selected_method, findSolutions option);
#endif