Skip to content

Commit

Permalink
for each save game a unique color as background (see issue #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubo77 committed Apr 14, 2024
1 parent b854e23 commit 41ca974
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 11 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG_de.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "de.z11.roboyard"
minSdkVersion 19
targetSdkVersion 34
versionCode 47
versionName "13.2"
versionCode 48
versionName "14"
}
buildTypes {
release {
Expand Down
43 changes: 41 additions & 2 deletions app/src/main/java/roboyard/eclabs/MapObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,59 @@ public static String createStringFromList( ArrayList<GridElement> data, boolean

String stringContent;
if (shortString) {
stringContent = generateUniqueString(content.toString());
stringContent = generateUnique5LetterFromString(content.toString());
}else{
stringContent = content.toString();
}

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");
Expand Down
22 changes: 18 additions & 4 deletions app/src/main/java/roboyard/eclabs/SaveGameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,15 +71,18 @@ 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());
if(saver.getMapsStateSaved(mapPath, "mapsSaved.txt")) {
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";
}
}
}
Expand Down Expand Up @@ -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]);
}
}

Expand Down

0 comments on commit 41ca974

Please sign in to comment.