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

Merge branch '4-add-check-solved-board-feature' into 'master'

Resolve "Add check solved board feature"

Closes #4

See merge request !4
parents 94c64839 4ddadc7b
No related branches found
No related tags found
1 merge request!4Resolve "Add check solved board feature"
Pipeline #1378 passed
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
app.versionName=0.0.4
app.versionCode=4
app.versionName=0.0.5
app.versionCode=5
......@@ -179,7 +179,7 @@ class Home extends StatelessWidget {
List cells = myProvider.cells;
int size = myProvider.size;
Color borderColor = _checkBoardIsSolved(cells) ? Colors.green : Colors.orange;
Color borderColor = _checkBoardIsSolved(myProvider) ? Colors.green : Colors.orange;
return Container(
margin: EdgeInsets.all(2),
......@@ -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;
}
}
}
print('ok no block with duplicates');
print('-> ok sudoku solved!');
return true;
}
@override
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