From b52881af8c7dbdb6709ac3ace6ce4ec362af86f9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Beno=C3=AEt=20Harrault?= <benoit@harrault.fr>
Date: Sat, 3 Feb 2024 00:40:24 +0100
Subject: [PATCH] Shuffle available colors, improve colors theme

---
 android/gradle.properties                     |  4 +-
 .../metadata/android/en-US/changelogs/15.txt  |  1 +
 .../metadata/android/fr-FR/changelogs/15.txt  |  1 +
 lib/cubit/game_cubit.dart                     |  1 +
 lib/models/game.dart                          | 19 +++++++++
 lib/ui/painters/game_board_painter.dart       |  7 ++--
 lib/utils/color_theme.dart                    | 39 ++++++++++++++-----
 pubspec.yaml                                  |  2 +-
 8 files changed, 57 insertions(+), 17 deletions(-)
 create mode 100644 fastlane/metadata/android/en-US/changelogs/15.txt
 create mode 100644 fastlane/metadata/android/fr-FR/changelogs/15.txt

diff --git a/android/gradle.properties b/android/gradle.properties
index 81949df..957c40b 100644
--- a/android/gradle.properties
+++ b/android/gradle.properties
@@ -1,5 +1,5 @@
 org.gradle.jvmargs=-Xmx1536M
 android.useAndroidX=true
 android.enableJetifier=true
-app.versionName=0.0.14
-app.versionCode=14
+app.versionName=0.0.15
+app.versionCode=15
diff --git a/fastlane/metadata/android/en-US/changelogs/15.txt b/fastlane/metadata/android/en-US/changelogs/15.txt
new file mode 100644
index 0000000..aa67307
--- /dev/null
+++ b/fastlane/metadata/android/en-US/changelogs/15.txt
@@ -0,0 +1 @@
+Shuffle available colors, improve colors theme.
diff --git a/fastlane/metadata/android/fr-FR/changelogs/15.txt b/fastlane/metadata/android/fr-FR/changelogs/15.txt
new file mode 100644
index 0000000..253e217
--- /dev/null
+++ b/fastlane/metadata/android/fr-FR/changelogs/15.txt
@@ -0,0 +1 @@
+Mélange les couleurs disponibles, amélioration du thème de couleurs.
diff --git a/lib/cubit/game_cubit.dart b/lib/cubit/game_cubit.dart
index b6096a8..1eb5517 100644
--- a/lib/cubit/game_cubit.dart
+++ b/lib/cubit/game_cubit.dart
@@ -24,6 +24,7 @@ class GameCubit extends HydratedCubit<GameState> {
     final Game game = Game(
       board: state.currentGame.board,
       settings: state.currentGame.settings,
+      shuffledColors: state.currentGame.shuffledColors,
       isRunning: state.currentGame.isRunning,
       isFinished: state.currentGame.isFinished,
       availableBlocksCount: state.currentGame.availableBlocksCount,
diff --git a/lib/models/game.dart b/lib/models/game.dart
index 31f0c9f..94f2538 100644
--- a/lib/models/game.dart
+++ b/lib/models/game.dart
@@ -4,10 +4,12 @@ import 'package:jeweled/models/game_board.dart';
 import 'package:jeweled/models/game_cell.dart';
 import 'package:jeweled/models/cell_location.dart';
 import 'package:jeweled/models/game_settings.dart';
+import 'package:jeweled/utils/color_theme.dart';
 
 class Game {
   final GameBoard board;
   final GameSettings settings;
+  List<int> shuffledColors = [];
   bool isRunning = false;
   bool isFinished = false;
   int availableBlocksCount = 0;
@@ -17,6 +19,7 @@ class Game {
   Game({
     required this.board,
     required this.settings,
+    required this.shuffledColors,
     this.isRunning = false,
     this.isFinished = false,
     this.availableBlocksCount = 0,
@@ -28,6 +31,7 @@ class Game {
     return Game(
       board: GameBoard.createNull(),
       settings: GameSettings.createDefault(),
+      shuffledColors: shuffleColors(),
     );
   }
 
@@ -37,10 +41,18 @@ class Game {
     return Game(
       board: GameBoard.createRandom(settings),
       settings: settings,
+      shuffledColors: shuffleColors(),
       isRunning: true,
     );
   }
 
+  static List<int> shuffleColors() {
+    List<int> values = new List<int>.generate(ColorTheme.getColorsCount(), (i) => i + 1);
+    values.shuffle();
+
+    return values;
+  }
+
   GameCell getCell(CellLocation cellLocation) {
     return this.board.cells[cellLocation.row][cellLocation.col];
   }
@@ -49,6 +61,11 @@ class Game {
     return this.getCell(cellLocation).value;
   }
 
+  int? getCellValueShuffled(CellLocation cellLocation) {
+    final int? value = this.getCell(cellLocation).value;
+    return value != null ? this.shuffledColors[value - 1] : null;
+  }
+
   void updateCellValue(CellLocation locationToUpdate, int? value) {
     this.board.cells[locationToUpdate.row][locationToUpdate.col].value = value;
   }
@@ -250,6 +267,7 @@ class Game {
     print('  movesCount: ' + movesCount.toString());
     print('  score: ' + score.toString());
     print('  availableBlocksCount: ' + availableBlocksCount.toString());
+    print('  shuffledColors: ' + shuffledColors.toString());
     print('');
   }
 
@@ -261,6 +279,7 @@ class Game {
     return <String, dynamic>{
       'board': this.board.toJson(),
       'settings': this.settings.toJson(),
+      'shuffledColors': this.shuffledColors,
       'isRunning': this.isRunning,
       'isFinished': this.isFinished,
       'availableBlocksCount': this.availableBlocksCount,
diff --git a/lib/ui/painters/game_board_painter.dart b/lib/ui/painters/game_board_painter.dart
index 5bb11b4..0b476e5 100644
--- a/lib/ui/painters/game_board_painter.dart
+++ b/lib/ui/painters/game_board_painter.dart
@@ -2,7 +2,6 @@ import 'dart:math';
 
 import 'package:flutter/material.dart';
 
-import 'package:jeweled/models/game_cell.dart';
 import 'package:jeweled/models/game.dart';
 import 'package:jeweled/models/cell_location.dart';
 import 'package:jeweled/utils/color_theme.dart';
@@ -32,9 +31,9 @@ class GameBoardPainter extends CustomPainter {
         final double x = cellSize * col;
 
         final CellLocation cellLocation = CellLocation.go(row, col);
-        final GameCell cell = game.getCell(cellLocation);
-        if (cell.value != null) {
-          final int colorCode = ColorTheme.getColorCode(cell.value);
+        final int? cellValue = game.getCellValueShuffled(cellLocation);
+        if (cellValue != null) {
+          final int colorCode = ColorTheme.getColorCode(cellValue);
 
           final Animation<double>? translation = this.animations[row][col];
           final double y = yOrigin + (translation?.value ?? 0) * cellSize;
diff --git a/lib/utils/color_theme.dart b/lib/utils/color_theme.dart
index 18d2006..c6172db 100644
--- a/lib/utils/color_theme.dart
+++ b/lib/utils/color_theme.dart
@@ -3,26 +3,45 @@ import 'package:flutter/material.dart';
 class ColorTheme {
   static Map<String, List<int>> itemColors = {
     'default': [
-      0xffffff,
-      0xe63a3f,
-      0x708cfd,
-      0x359c35,
-      0xffce2c,
-      0xff6f43,
-      0xa13cb1,
-      0x38ffff,
-      0xf2739d,
+      0x000000,
+      0x9D9D9D,
+      0xFFFFFF,
+      0xBE2633,
+      0xE06F8B,
+      0x493C2B,
+      0xA46422,
+      0xEB8931,
+      0xF7E26B,
+      0x2F484E,
+      0x44891A,
+      0xA3CE27,
+      0x1B2632,
+      0x005784,
+      0x31A2F2,
+      0xB2DCEF,
     ],
   };
   static int defaultItemColor = 0x808080;
 
+  static int getColorsCount() {
+    const skin = 'default';
+
+    if (itemColors.containsKey(skin) && null != itemColors[skin]) {
+      List<int> skinColors = itemColors[skin] ?? [];
+
+      return skinColors.length;
+    }
+
+    return 0;
+  }
+
   static int getColorCode(int? value) {
     const skin = 'default';
 
     if (value != null && itemColors.containsKey(skin) && null != itemColors[skin]) {
       List<int> skinColors = itemColors[skin] ?? [];
       if (skinColors.length > value) {
-        return (skinColors[value]) | 0xFF000000;
+        return (skinColors[value % getColorsCount()]) | 0xFF000000;
       }
     }
     return defaultItemColor | 0xFF000000;
diff --git a/pubspec.yaml b/pubspec.yaml
index e4140c6..caa9a7c 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -3,7 +3,7 @@ description: Jeweled Game
 
 publish_to: 'none'
 
-version: 0.0.14+14
+version: 0.0.15+15
 
 environment:
   sdk: '^3.0.0'
-- 
GitLab