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

Mark conflicts in board

parent 93222b5b
Branches
Tags
1 merge request!11Resolve "Show conflicts in grid"
Pipeline #1405 passed
org.gradle.jvmargs=-Xmx1536M org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true android.useAndroidX=true
android.enableJetifier=true android.enableJetifier=true
app.versionName=0.0.12 app.versionName=0.0.13
app.versionCode=12 app.versionCode=13
assets/icons/button_back.png

3.65 KiB

assets/icons/button_show_conflicts.png

2.86 KiB

...@@ -54,6 +54,8 @@ function build_icon_for_skin() { ...@@ -54,6 +54,8 @@ function build_icon_for_skin() {
} }
# Game icons # Game icons
build_icon ${CURRENT_DIR}/button_back.svg ${BASE_DIR}/assets/icons/button_back.png
build_icon ${CURRENT_DIR}/button_show_conflicts.svg ${BASE_DIR}/assets/icons/button_show_conflicts.png
build_icon ${CURRENT_DIR}/button_start.svg ${BASE_DIR}/assets/icons/button_start.png build_icon ${CURRENT_DIR}/button_start.svg ${BASE_DIR}/assets/icons/button_start.png
build_icon ${CURRENT_DIR}/difficulty_easy.svg ${BASE_DIR}/assets/icons/difficulty_easy.png build_icon ${CURRENT_DIR}/difficulty_easy.svg ${BASE_DIR}/assets/icons/difficulty_easy.png
build_icon ${CURRENT_DIR}/difficulty_medium.svg ${BASE_DIR}/assets/icons/difficulty_medium.png build_icon ${CURRENT_DIR}/difficulty_medium.svg ${BASE_DIR}/assets/icons/difficulty_medium.png
......
<?xml version="1.0" encoding="UTF-8"?>
<svg enable-background="new 0 0 100 100" version="1.1" viewBox="0 0 93.665 93.676" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><path d="m46.834 11.248c-19.624 0-35.588 15.964-35.588 35.592 0 19.624 15.964 35.588 35.588 35.588s35.584-15.964 35.584-35.588c0-19.629-15.96-35.592-35.584-35.592zm0 63.607c-15.471 0-28.02-12.544-28.02-28.014 0-15.477 12.549-28.019 28.02-28.019s28.018 12.541 28.018 28.019c0 15.471-12.547 28.014-28.018 28.014z" stroke-width=".75985"/><path d="m55.403 64.285c0.85468 0.85879 3.0403 0.85879 3.0403 0v-34.893c0-0.85985-2.1908-0.85985-3.0486 0l-28.641 15.895c-0.8578 0.85468-0.87224 2.2454-0.0165 3.1032z" fill="#ed9138" stroke-width="1.031"/></svg>
<?xml version="1.0" encoding="UTF-8"?>
<svg enable-background="new 0 0 100 100" version="1.1" viewBox="0 0 91.389 91.821" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><rect width="91.389" height="91.821" ry="0" fill="#e2b300"/><path d="m79.855 13.662a1.4083 1.4083 0 0 0-1.8775-0.11982l-26.365 20.293-1.1585-12.723a1.4009 1.4009 0 0 0-1.0985-1.2583 1.4367 1.4367 0 0 0-1.558 0.65904l-6.8708 11.644-9.6071-9.5472a1.4562 1.4562 0 0 0-1.6578-0.25965 1.4737 1.4737 0 0 0-0.77887 1.2782 0.972 0.972 0 0 0 0.02006 0.23979l2.177 13.182-21.052-3.4553a1.4314 1.4314 0 0 0-0.99866 2.6166l17.576 11.345-10.825 7.6299a1.4314 1.4314 0 0 0 0.81885 2.5965l12.783 0.13983-13.981 21.751a1.4236 1.4236 0 0 0 2.0972 1.8775l22.23-17.956 5.5725 6.4713a1.4509 1.4509 0 0 0 1.3582 0.45946 1.3606 1.3606 0 0 0 1.0786-0.9588l2.3967-7.1703 12.483 5.7723a1.4376 1.4376 0 0 0 1.6579-0.31951 1.451 1.451 0 0 0 0.19979-1.6777l-6.6512-11.784 13.062-3.5353a1.4274 1.4274 0 0 0 1.0586-1.3182 1.4045 1.4045 0 0 0-0.91882-1.3983l-12.583-4.7337 21.531-27.863a1.4082 1.4082 0 0 0-0.11982-1.8775z" stroke-width="2.0684"/></svg>
...@@ -8,6 +8,7 @@ class Cell { ...@@ -8,6 +8,7 @@ class Cell {
final int col; final int col;
final int row; final int row;
bool isFixed; bool isFixed;
int conflictsCount = 0;
Cell( Cell(
@required this.value, @required this.value,
...@@ -28,7 +29,7 @@ class Cell { ...@@ -28,7 +29,7 @@ class Cell {
Color borderDarkColor = Colors.black; Color borderDarkColor = Colors.black;
Color borderLightColor = Colors.grey; Color borderLightColor = Colors.grey;
Color borderSelectedColor = Colors.red; Color borderSelectedColor = Colors.red;
Color backgroundColor = this.isFixed ? Colors.grey[300] : Colors.grey[100]; Color backgroundColor = this.getBackgroundColor(myProvider);
Border borders = Border( Border borders = Border(
top: BorderSide(width: borderWidth, color: ((this.row % size) == 0) ? borderDarkColor : borderLightColor), top: BorderSide(width: borderWidth, color: ((this.row % size) == 0) ? borderDarkColor : borderLightColor),
...@@ -67,6 +68,29 @@ class Cell { ...@@ -67,6 +68,29 @@ class Cell {
); );
} }
Color getBackgroundColor(Data myProvider) {
Color editableCellColor = Colors.grey[100];
Color editableCellColorConflict = Colors.pink[100];
Color fixedCellColor = Colors.grey[300];
Color fixedCellColorConflict = Colors.pink[200];
Color backgroundColor = editableCellColor;
if (this.isFixed) {
backgroundColor = fixedCellColor;
}
if (myProvider.showConflicts && (this.conflictsCount != 0)) {
if (this.isFixed) {
backgroundColor = fixedCellColorConflict;
} else {
backgroundColor = editableCellColorConflict;
}
}
return backgroundColor;
}
Container widgetUpdateValue(Data myProvider) { Container widgetUpdateValue(Data myProvider) {
String imageAsset = 'assets/skins/empty.png'; String imageAsset = 'assets/skins/empty.png';
if (this.value > 0) { if (this.value > 0) {
......
...@@ -15,6 +15,7 @@ class Data extends ChangeNotifier { ...@@ -15,6 +15,7 @@ class Data extends ChangeNotifier {
String _level = 'medium'; String _level = 'medium';
int _size = 3; int _size = 3;
String _skin = 'default'; String _skin = 'default';
bool _showConflicts = false;
// Game data // Game data
bool _stateRunning = false; bool _stateRunning = false;
...@@ -40,6 +41,12 @@ class Data extends ChangeNotifier { ...@@ -40,6 +41,12 @@ class Data extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
bool get showConflicts => _showConflicts;
set updateShowConflicts(bool showConflicts) {
_showConflicts = showConflicts;
notifyListeners();
}
getParameterValue(String parameterCode) { getParameterValue(String parameterCode) {
switch(parameterCode) { switch(parameterCode) {
case 'difficulty': { return _level; } case 'difficulty': { return _level; }
......
...@@ -318,45 +318,65 @@ class Home extends StatelessWidget { ...@@ -318,45 +318,65 @@ class Home extends StatelessWidget {
bool _checkBoardIsSolved(Data myProvider) { bool _checkBoardIsSolved(Data myProvider) {
List cells = myProvider.cells; List cells = myProvider.cells;
int size = myProvider.size; int size = myProvider.size;
int sideLength = pow(size, 2);
bool isSolved = true;
// reset conflict states
for (var row = 0; row < sideLength; row++) {
for (var col = 0; col < sideLength; col++) {
cells[row][col].conflictsCount = 0;
}
}
// check grid is fully completed // check grid is fully completed
for (var row = 0; row < pow(size, 2); row++) { for (var row = 0; row < sideLength; row++) {
for (var col = 0; col < pow(size, 2); col++) { for (var col = 0; col < sideLength; col++) {
if (cells[row][col].value == 0) { if (cells[row][col].value == 0) {
print('grid not complete'); isSolved = false;
return false;
} }
} }
} }
print('ok grid complete');
// check lines does not contains a value twice // check lines does not contains a value twice
for (var row = 0; row < pow(size, 2); row++) { for (var row = 0; row < sideLength; row++) {
List values = []; List values = [];
for (var col = 0; col < pow(size, 2); col++) { for (var col = 0; col < sideLength; col++) {
values.add(cells[row][col].value); int value = cells[row][col].value;
if (value != 0) {
values.add(value);
}
} }
List distinctValues = values.toSet().toList(); List distinctValues = values.toSet().toList();
if (values.length != distinctValues.length) { if (values.length != distinctValues.length) {
print('line ' + row.toString() + ' contains duplicates'); print('line ' + row.toString() + ' contains duplicates');
return false; // Add line to cells in conflict
for (var col = 0; col < sideLength; col++) {
cells[row][col].conflictsCount++;
}
isSolved = false;
} }
} }
print('ok no line with duplicates');
// check columns does not contains a value twice // check columns does not contains a value twice
for (var col = 0; col < pow(size, 2); col++) { for (var col = 0; col < sideLength; col++) {
List values = []; List values = [];
for (var row = 0; row < pow(size, 2); row++) { for (var row = 0; row < sideLength; row++) {
values.add(cells[row][col].value); int value = cells[row][col].value;
if (value != 0) {
values.add(value);
}
} }
List distinctValues = values.toSet().toList(); List distinctValues = values.toSet().toList();
if (values.length != distinctValues.length) { if (values.length != distinctValues.length) {
print('column ' + col.toString() + ' contains duplicates'); print('column ' + col.toString() + ' contains duplicates');
return false; // Add column to cells in conflict
for (var row = 0; row < sideLength; row++) {
cells[row][col].conflictsCount++;
}
isSolved = false;
} }
} }
print('ok no column with duplicates');
// check blocks does not contains a value twice // check blocks does not contains a value twice
for (var blockRow = 0; blockRow < size; blockRow++) { for (var blockRow = 0; blockRow < size; blockRow++) {
...@@ -367,37 +387,78 @@ class Home extends StatelessWidget { ...@@ -367,37 +387,78 @@ class Home extends StatelessWidget {
for (var colInBlock = 0; colInBlock < size; colInBlock++) { for (var colInBlock = 0; colInBlock < size; colInBlock++) {
int row = (blockRow * size) + rowInBlock; int row = (blockRow * size) + rowInBlock;
int col = (blockCol * size) + colInBlock; int col = (blockCol * size) + colInBlock;
values.add(cells[row][col].value); int value = cells[row][col].value;
if (value != 0) {
values.add(value);
}
} }
} }
List distinctValues = values.toSet().toList(); List distinctValues = values.toSet().toList();
if (values.length != distinctValues.length) { if (values.length != distinctValues.length) {
print('block [' + blockCol.toString() + ',' + blockRow.toString() + '] contains duplicates'); print('block [' + blockCol.toString() + ',' + blockRow.toString() + '] contains duplicates');
return false; // Add blocks to cells in conflict
for (var rowInBlock = 0; rowInBlock < size; rowInBlock++) {
for (var colInBlock = 0; colInBlock < size; colInBlock++) {
int row = (blockRow * size) + rowInBlock;
int col = (blockCol * size) + colInBlock;
cells[row][col].conflictsCount++;
}
}
isSolved = false;
} }
} }
} }
print('ok no block with duplicates');
if (isSolved) {
print('-> ok sudoku solved!'); print('-> ok sudoku solved!');
return true; }
return isSolved;
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
Data myProvider = Provider.of<Data>(context); Data myProvider = Provider.of<Data>(context);
return Scaffold( List<Widget> menuActions = [];
appBar: AppBar(
title: Text('🔢'), if (myProvider.stateRunning) {
actions: [ menuActions.add(
if (myProvider.stateRunning)
FlatButton( FlatButton(
child: Text('◀️'), child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
border: Border.all(
color: myProvider.showConflicts ? Colors.white : Colors.blue,
width: 4,
),
),
margin: EdgeInsets.all(8),
child: Image(
image: AssetImage('assets/icons/button_show_conflicts.png'),
fit: BoxFit.fill
),
),
onPressed: () { myProvider.updateShowConflicts = !myProvider.showConflicts; },
)
);
menuActions.add(
FlatButton(
child: Image(
image: AssetImage('assets/icons/button_back.png'),
fit: BoxFit.fill
),
onPressed: () => resetGame(myProvider), onPressed: () => resetGame(myProvider),
), ),
], );
}
return Scaffold(
appBar: AppBar(
title: Text('🔢'),
actions: menuActions,
), ),
body: SafeArea( body: SafeArea(
child: Center( child: Center(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment