Skip to content
Snippets Groups Projects
Commit 4ddadc7b authored by Benoît Harrault's avatar Benoît Harrault
Browse files

Check if soduku is solved

parent 94c64839
No related branches found
No related tags found
1 merge request!4Resolve "Add check solved board feature"
Pipeline #1375 passed
This commit is part of merge request !4. Comments created here will be created in the context of that merge request.
org.gradle.jvmargs=-Xmx1536M org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true android.useAndroidX=true
android.enableJetifier=true android.enableJetifier=true
app.versionName=0.0.4 app.versionName=0.0.5
app.versionCode=4 app.versionCode=5
...@@ -179,7 +179,7 @@ class Home extends StatelessWidget { ...@@ -179,7 +179,7 @@ class Home extends StatelessWidget {
List cells = myProvider.cells; List cells = myProvider.cells;
int size = myProvider.size; int size = myProvider.size;
Color borderColor = _checkBoardIsSolved(cells) ? Colors.green : Colors.orange; Color borderColor = _checkBoardIsSolved(myProvider) ? Colors.green : Colors.orange;
return Container( return Container(
margin: EdgeInsets.all(2), margin: EdgeInsets.all(2),
...@@ -242,9 +242,74 @@ class Home extends StatelessWidget { ...@@ -242,9 +242,74 @@ class Home extends StatelessWidget {
); );
} }
bool _checkBoardIsSolved(List cells) { bool _checkBoardIsSolved(Data myProvider) {
List cells = myProvider.cells;
int size = myProvider.size;
// check grid is fully completed
for (var row = 0; row < pow(size, 2); row++) {
for (var col = 0; col < pow(size, 2); col++) {
if (cells[row][col].value == 0) {
print('grid not complete');
return false;
}
}
}
print('ok grid complete complete');
// check lines does not contains a value twice
for (var row = 0; row < pow(size, 2); row++) {
List values = [];
for (var col = 0; col < pow(size, 2); col++) {
values.add(cells[row][col].value);
}
List distinctValues = values.toSet().toList();
if (values.length != distinctValues.length) {
print('line ' + row.toString() + ' contains duplicates');
return false;
}
}
print('ok no line with duplicates');
// check columns does not contains a value twice
for (var col = 0; col < pow(size, 2); col++) {
List values = [];
for (var row = 0; row < pow(size, 2); row++) {
values.add(cells[row][col].value);
}
List distinctValues = values.toSet().toList();
if (values.length != distinctValues.length) {
print('column ' + col.toString() + ' contains duplicates');
return false;
}
}
print('ok no column with duplicates');
// check blocks does not contains a value twice
for (var blockRow = 0; blockRow < size; blockRow++) {
for (var blockCol = 0; blockCol < size; blockCol++) {
List values = [];
for (var rowInBlock = 0; rowInBlock < size; rowInBlock++) {
for (var colInBlock = 0; colInBlock < size; colInBlock++) {
int row = (blockRow * size) + rowInBlock;
int col = (blockCol * size) + colInBlock;
values.add(cells[row][col].value);
}
}
List distinctValues = values.toSet().toList();
if (values.length != distinctValues.length) {
print('block [' + blockCol.toString() + ',' + blockRow.toString() + '] contains duplicates');
return false; return false;
} }
}
}
print('ok no block with duplicates');
print('-> ok sudoku solved!');
return true;
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment