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
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
......@@ -57,7 +57,7 @@ class Home extends StatelessWidget {
}
// TODO: shuffle/flip/rotate grid
return cells;
}
......@@ -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,8 +242,73 @@ class Home extends StatelessWidget {
);
}
bool _checkBoardIsSolved(List cells) {
return false;
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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment