From 41ca97427082571f84082bbcb133659b5fc0a547 Mon Sep 17 00:00:00 2001 From: Ruben Barkow-Kuder Date: Sun, 14 Apr 2024 13:26:39 +0200 Subject: [PATCH] for each save game a unique color as background (see issue #7) --- CHANGELOG.md | 5 +-- CHANGELOG_de.md | 9 ++++ app/build.gradle | 4 +- .../main/java/roboyard/eclabs/MapObjects.java | 43 ++++++++++++++++++- .../java/roboyard/eclabs/SaveGameScreen.java | 22 ++++++++-- 5 files changed, 72 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 829c2c8..30fa420 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,10 @@ CHANGELOG ========= -### Version 13.4 +### Version 14 - fix game levels -- show unique string above every savegame -- +- show a unique string above every savegame and give it a unique background color ### Version 13.2 diff --git a/CHANGELOG_de.md b/CHANGELOG_de.md index 8f3e30c..9e7769a 100644 --- a/CHANGELOG_de.md +++ b/CHANGELOG_de.md @@ -1,6 +1,15 @@ Changelog Deutsch ================= +### Version 14 + +- Level-Games repariert +- Eindeutige Zeichenfolge über jedem Spielstand und eine eindeutige Hintergrundfarbe hinzugefügt + +### Version 13.2 + +- `distributionSha256Sum` zu den Build-Optionen hinzugefügt + ### Version 13.1 - Nicht benötigte INTERNET permission entfernt diff --git a/app/build.gradle b/app/build.gradle index f7ea5fb..9986cef 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,8 +7,8 @@ android { applicationId "de.z11.roboyard" minSdkVersion 19 targetSdkVersion 34 - versionCode 47 - versionName "13.2" + versionCode 48 + versionName "14" } buildTypes { release { diff --git a/app/src/main/java/roboyard/eclabs/MapObjects.java b/app/src/main/java/roboyard/eclabs/MapObjects.java index 7985a9a..fd6a115 100644 --- a/app/src/main/java/roboyard/eclabs/MapObjects.java +++ b/app/src/main/java/roboyard/eclabs/MapObjects.java @@ -97,7 +97,7 @@ public static String createStringFromList( ArrayList data, boolean String stringContent; if (shortString) { - stringContent = generateUniqueString(content.toString()); + stringContent = generateUnique5LetterFromString(content.toString()); }else{ stringContent = content.toString(); } @@ -105,12 +105,51 @@ public static String createStringFromList( ArrayList data, boolean return stringContent; } + /* + * set a color for the unique string depending on the value of the string, + * the color is generated from the hex value of the string + * - there are only generated light colors + * + * @param input The input string + * @return A light color in hexadecimal format + */ + public static String generateHexColorFromString(String input) { + try { + // Create a SHA-256 message digest instance + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + + // Get the hash bytes for the input string + byte[] hashBytes = digest.digest(input.getBytes()); + + // Convert the hash bytes to a 6-letter hexadecimal string + StringBuilder color = new StringBuilder("#"); + for (int i = 0; i < 6; i++) { + // Convert each byte to a positive integer and take modulo 16 to get a hexadecimal digit + int index = Math.abs(hashBytes[i]) % 16; + // if it is the first, third or 5th digit, the color should be lighter + if (i % 2 == 0 && index <= 4) { + index += 5; + } + // Map the index to a hexadecimal digit + char digit = (char) (index < 10 ? '0' + index : 'A' + index - 10); + + // Append the digit to the color string + color.append(digit); + } + return color.toString(); + } catch (NoSuchAlgorithmException e) { + // Handle NoSuchAlgorithmException if the specified algorithm is not available + e.printStackTrace(); + return null; // or throw an exception + } + } + /* * Generate a unique string from the input string * @param input The input string * @return A unique 5-letter string altering between vowels and consonants */ - public static String generateUniqueString(String input) { + public static String generateUnique5LetterFromString(String input) { try { // Create a SHA-256 message digest instance MessageDigest digest = MessageDigest.getInstance("SHA-256"); diff --git a/app/src/main/java/roboyard/eclabs/SaveGameScreen.java b/app/src/main/java/roboyard/eclabs/SaveGameScreen.java index 11ca58c..936641c 100644 --- a/app/src/main/java/roboyard/eclabs/SaveGameScreen.java +++ b/app/src/main/java/roboyard/eclabs/SaveGameScreen.java @@ -14,6 +14,7 @@ public class SaveGameScreen extends GameScreen { private int[] buttonPositionsX; private int[] buttonPositionsY; private String[] mapUniqueString; + private String[] mapUniqueColor; private int autosaveButtonX; private int autosaveButtonY; private int backButtonX; @@ -70,6 +71,7 @@ public void init() { // load all saved maps to create a unique string from the mapElements mapUniqueString = new String[cols * rows]; + mapUniqueColor = new String[cols * rows]; for (int i = 0; i < cols * rows; i++) { String mapPath = getMapPath(i); SaveManager saver = new SaveManager(gameManager.getActivity()); @@ -77,8 +79,10 @@ public void init() { String saveData = FileReadWrite.readPrivateData(gameManager.getActivity(), mapPath); gridElements = MapObjects.extractDataFromString(saveData); mapUniqueString[i] = MapObjects.createStringFromList(gridElements, true); + mapUniqueColor[i] = MapObjects.generateHexColorFromString(mapUniqueString[i]); } else { mapUniqueString[i] = ""; + mapUniqueColor[i] = "#000000"; } } } @@ -145,10 +149,20 @@ public void draw(RenderManager renderManager) { if (i == 0) { renderManager.drawText((int) (20 * ratioW), (int) ((42 + ts) * ratioH)-5, "Autosave"); } else { - renderManager.setColor(Color.BLACK); - renderManager.drawText(buttonPositionsX[i], buttonPositionsY[i]-5, (i<10?" ":"") + i + "."); - renderManager.setColor(Color.parseColor("#222222")); - renderManager.drawText(buttonPositionsX[i]+33, buttonPositionsY[i]-5, mapUniqueString[i]); + renderManager.setColor(Color.parseColor(mapUniqueColor[i])); + int moveleft=16; + + if (mapUniqueString[i].length() > 0){ + // unicode string with 11 filled squares + String bar = "\u2588\u2588\u2588\u2588\u2588\u2588\u2588"; + for (int j = 0; j < 8; j++) { + renderManager.drawText(buttonPositionsX[i] - moveleft, buttonPositionsY[i] - 5 + j * 15, bar); + } + + } + + renderManager.setColor(Color.parseColor("#000000")); + renderManager.drawText(buttonPositionsX[i] - moveleft + 1 + (i<10? 8 : 0), buttonPositionsY[i] - 5, i + ". " + mapUniqueString[i]); } }