From d0dbb4543f0a60dab0528be91b95b1236da5b7c2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Beno=C3=AEt=20Harrault?= <benoit@harrault.fr>
Date: Mon, 17 Feb 2025 16:19:16 +0100
Subject: [PATCH] Fix compute/display conflicts

---
 .../metadata/android/en-US/changelogs/8.txt   |  1 +
 .../metadata/android/fr-FR/changelogs/8.txt   |  1 +
 lib/cubit/activity/activity_cubit.dart        |  2 +
 lib/models/activity/activity.dart             | 82 +++++++------------
 pubspec.yaml                                  |  2 +-
 5 files changed, 33 insertions(+), 55 deletions(-)
 create mode 100644 fastlane/metadata/android/en-US/changelogs/8.txt
 create mode 100644 fastlane/metadata/android/fr-FR/changelogs/8.txt

diff --git a/fastlane/metadata/android/en-US/changelogs/8.txt b/fastlane/metadata/android/en-US/changelogs/8.txt
new file mode 100644
index 0000000..19bc39d
--- /dev/null
+++ b/fastlane/metadata/android/en-US/changelogs/8.txt
@@ -0,0 +1 @@
+Fix compute/display conflicts.
diff --git a/fastlane/metadata/android/fr-FR/changelogs/8.txt b/fastlane/metadata/android/fr-FR/changelogs/8.txt
new file mode 100644
index 0000000..38224f1
--- /dev/null
+++ b/fastlane/metadata/android/fr-FR/changelogs/8.txt
@@ -0,0 +1 @@
+Correction sur calcul/affichage des conflits.
diff --git a/lib/cubit/activity/activity_cubit.dart b/lib/cubit/activity/activity_cubit.dart
index 5f19af7..acf3f1c 100644
--- a/lib/cubit/activity/activity_cubit.dart
+++ b/lib/cubit/activity/activity_cubit.dart
@@ -93,6 +93,8 @@ class ActivityCubit extends HydratedCubit<ActivityState> {
       refresh();
     }
 
+    state.currentActivity.updateConflictsInBoard();
+
     if (state.currentActivity.checkBoardIsSolved()) {
       BoardAnimate.startAnimation(this, 'win');
       state.currentActivity.isFinished = true;
diff --git a/lib/models/activity/activity.dart b/lib/models/activity/activity.dart
index ab1f8ae..dad9137 100644
--- a/lib/models/activity/activity.dart
+++ b/lib/models/activity/activity.dart
@@ -137,37 +137,20 @@ class Activity {
     return false;
   }
 
-  ConflictsCount computeConflictsInBoard() {
+  void updateConflictsInBoard() {
     final BoardCells cells = board.cells;
-    final ConflictsCount conflicts = boardConflicts;
 
     // reset conflict states
-    for (int row = 0; row < board.boardSizeVertical; row++) {
-      for (int col = 0; col < board.boardSizeHorizontal; col++) {
-        conflicts[row][col] = 0;
-      }
+    for (CellLocation location in board.getCellLocations()) {
+      boardConflicts[location.row][location.col] = 0;
     }
 
     // check siblings
-    for (int row = 0; row < board.boardSizeVertical; row++) {
-      for (int col = 0; col < board.boardSizeHorizontal; col++) {
-        final int value = cells[row][col].value;
-        if (value != 0) {
-          for (int deltaRow = -1; deltaRow <= 1; deltaRow++) {
-            for (int deltaCol = -1; deltaCol <= 1; deltaCol++) {
-              if (row + deltaRow >= 0 &&
-                  row + deltaRow < board.boardSizeHorizontal &&
-                  col + deltaCol >= 0 &&
-                  col + deltaCol < board.boardSizeVertical &&
-                  (deltaRow * deltaCol != 0)) {
-                final int siblingValue = cells[row + deltaRow][col + deltaCol].value;
-
-                if (siblingValue == value) {
-                  conflicts[row][col]++;
-                }
-              }
-            }
-          }
+    for (CellLocation location in board.getCellLocations()) {
+      final int value = board.get(location).value;
+      if (value != 0) {
+        if (board.cellHasSiblingWithSameValue(location)) {
+          boardConflicts[location.row][location.col]++;
         }
       }
     }
@@ -176,33 +159,27 @@ class Activity {
     for (String blockId in board.getBlockIds()) {
       List<int> values = [];
       List<int> duplicateValues = [];
-      for (int row = 0; row < board.boardSizeVertical; row++) {
-        for (int col = 0; col < board.boardSizeHorizontal; col++) {
-          if (cells[row][col].blockId == blockId) {
-            final int value = cells[row][col].value;
-            if (value != 0) {
-              if (!values.contains(value)) {
-                values.add(value);
-              } else {
-                duplicateValues.add(value);
-              }
+      for (CellLocation location in board.getCellLocations()) {
+        if (board.get(location).blockId == blockId) {
+          final int value = board.get(location).value;
+          if (value != 0) {
+            if (!values.contains(value)) {
+              values.add(value);
+            } else {
+              duplicateValues.add(value);
             }
           }
         }
       }
       for (int duplicateValue in duplicateValues) {
-        for (int row = 0; row < board.boardSizeVertical; row++) {
-          for (int col = 0; col < board.boardSizeHorizontal; col++) {
-            if (cells[row][col].blockId == blockId &&
-                cells[row][col].value == duplicateValue) {
-              conflicts[row][col]++;
-            }
+        for (CellLocation location in board.getCellLocations()) {
+          if (board.get(location).blockId == blockId &&
+              board.get(location).value == duplicateValue) {
+            boardConflicts[location.row][location.col]++;
           }
         }
       }
     }
-
-    return conflicts;
   }
 
   void showTip(ActivityCubit activityCubit) {
@@ -277,12 +254,11 @@ class Activity {
 
     List<CellLocation> cellsWithWrongValue = [];
 
-    for (int row = 0; row < board.boardSizeVertical; row++) {
-      for (int col = 0; col < board.boardSizeHorizontal; col++) {
-        if (cells[row][col].value != 0 &&
-            cells[row][col].value != cellsSolved[row][col].value) {
-          cellsWithWrongValue.add(CellLocation.go(row, col));
-        }
+    for (CellLocation location in board.getCellLocations()) {
+      if (cells[location.row][location.col].value != 0 &&
+          cells[location.row][location.col].value !=
+              cellsSolved[location.row][location.col].value) {
+        cellsWithWrongValue.add(location);
       }
     }
 
@@ -292,11 +268,9 @@ class Activity {
   List<CellLocation> getCellsWithConflicts() {
     List<CellLocation> cellsWithConflict = [];
 
-    for (int row = 0; row < board.boardSizeVertical; row++) {
-      for (int col = 0; col < board.boardSizeHorizontal; col++) {
-        if (boardConflicts[row][col] != 0) {
-          cellsWithConflict.add(CellLocation.go(row, col));
-        }
+    for (CellLocation location in board.getCellLocations()) {
+      if (boardConflicts[location.row][location.col] != 0) {
+        cellsWithConflict.add(location);
       }
     }
 
diff --git a/pubspec.yaml b/pubspec.yaml
index b308120..6d4f6ae 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -3,7 +3,7 @@ description: A suguru game application.
 
 publish_to: "none"
 
-version: 0.0.7+7
+version: 0.0.8+8
 
 environment:
   sdk: "^3.0.0"
-- 
GitLab