-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudokustate.cpp
158 lines (146 loc) · 3.5 KB
/
sudokustate.cpp
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Author: LOPES Marco
* Purpose: Stock a sudoku state
* Date : november 2016
*/
#include "sudokustate.h"
#include <iostream>
#include <QStack>
/*
* Constructor, Initialise sudoku without any constraint
*/
SudokuState::SudokuState()
{
for (int i = 0; i < 9; ++i) {
for (int j = 1; j <= 9; ++j) {
this->linesConstraints[i].insert(j);
this->columnsConstraints[i].insert(j);
this->squaresConstraints[i].insert(j);
}
}
}
/*
* Read sudoku configuration from console and add constraints
*
* returns: void
*/
void SudokuState::init()
{
for (int line = 0; line < 9; ++line) {
for (int column = 0; column < 9; ++column) {
int number;
std::cin >> number;
this->cases[line][column] = number;
this->addConstraint(line, column, number);
}
}
}
/*
* Display the disposition of the sudoku
*
* returns: void
*/
void SudokuState::display() const
{
std::cout << std::endl;
for (int line = 0; line < 9; ++line) {
if(line % 3 == 0)
std::cout << "+-------+-------+-------+" << std::endl;
for (int column = 0; column < 9; ++column) {
if(column % 3 == 0)
std::cout << "| ";
if(this->cases[line][column] != 0)
std::cout << this->cases[line][column] << " ";
else
std::cout << "- ";
}
std::cout << "| ";
std::cout << std::endl;
}
std::cout << "+-------+-------+-------+" << std::endl;
std::cout << std::endl;
}
/*
* Add a constraint in line, column and subsquare
*
* line: where make the constraint
* column: where make the constraint
* value: the constraint
*
* returns: void
*/
void SudokuState::addConstraint(int line, int column, int value)
{
this->linesConstraints[line].remove(value);
this->columnsConstraints[column].remove(value);
this->squaresConstraints[this->squareNumber(line, column)].remove(value);
}
/*
* Make the intersection between line, column and square constraints
*
* line: the line of the case
* column: the column of the case
*
* returns: QSet<int> which contain the possibilities for one case
*/
QSet<int> SudokuState::getIntersect(int line, int column) const
{
return this->linesConstraints[line] & this->columnsConstraints[column] & this->squaresConstraints[this->squareNumber(line, column)];
}
/*
* Get a value of one case
*
* line: the line of the case
* column: the column of the case
*
* returns: the value of the case
*/
int SudokuState::getCase(int line, int column) const
{
return this->cases[line][column];
}
/*
* Set a value of one case
*
* line: the line of the case
* column: the column of the case
* value: the value to set
*
* returns: void
*/
void SudokuState::setCase(int line, int column, int value)
{
this->cases[line][column] = value;
}
/*
* Get the number of the subsquare
*
* line: the line of the case
* column: the column of the case
*
* returns: the number of the subsquare
*/
int SudokuState::squareNumber(int line, int column) const
{
return line/3*3+column/3;
}
/*
* Get backtracking value(used or not yet)
*
* returns: bool the value of backtracking
*/
bool SudokuState::getBacktracking() const
{
return backtracking;
}
/*
* Set the value of backtracking
*
* value: the new value for backtracking
*
* returns: void
*/
void SudokuState::setBacktracking(bool value)
{
backtracking = value;
}