0

So i have this string StringBuilder fenToString = new StringBuilder("1P111Pr1")

now how can i change it to a = "1P3Pr1"?

i tried this

int fenNum = 0;
for(int i = 0; i < fenToString.length(); i++){
        if(Character.isDigit(fenToString.charAt(i))){
            fenNum += 1;
            fenToString.setCharAt(i, (char)(fenNum+'0'));
        }else{
            fenNum = 0;
        }
    }

i get "1P123Pr1" instead of "1P3Pr1"

fauxxi
  • 75
  • 10
  • What happens if there are 10 or more 1s in a row? – Andy Turner Feb 11 '21 at 14:10
  • 2
    @AndyTurner [FEN](https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation) is about positions on a chess board, which is 8x8. A digit represents that many empty spaces, and a letter represents a chess piece, so the count of letters plus the sum of digits will always equal 8. – Andreas Feb 11 '21 at 14:26

2 Answers2

1

So to explicitly state the task: You want to convert a sequence of two or more digits into the sum of those digits. The sum is guaranteed to be 8 or less, i.e. to also be a single digit.

There are many ways to do that, but the closest to what you're trying would likely be:

static String normalizeFen(String fen) {
    StringBuilder buf = new StringBuilder(fen);
    for (int i = 1; i < buf.length(); i++) {
        if (Character.isDigit(buf.charAt(i)) && Character.isDigit(buf.charAt(i - 1))) {
            // Found 2 consecutive digits, so sum them
            int sum = Character.digit(buf.charAt(i - 1), 10)
                    + Character.digit(buf.charAt(i), 10);
            buf.setCharAt(i - 1, Character.forDigit(sum, 10));
            buf.deleteCharAt(i); // Remove second digit
            i--; // Go back to reprocess the same index position again
        }
    }
    return buf.toString();
}

Tests

System.out.println(normalizeFen("1P111Pr1"));
System.out.println(normalizeFen("rnbqkbnr/pp1ppppp/11111111/11p11111/1111P111/11111111/PPPP1PPP/RNBQKBNR"));

Output

1P3Pr1
rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • ah of course i need to check on the index after, not at the first index of "1", and delete the unnecessary "1"s after summing them. thank you so much, that's exactly what i need! – fauxxi Feb 11 '21 at 14:49
0

It is possible to transform the FEN string without removing characters in the StringBuilder:

static String fixFenString(String fen) {
    // debug print
    System.out.print(fen + " -> ");

    StringBuilder fenToString = new StringBuilder(fen.length());
    int fenNum = 0;
    for (int i = 0; i < fen.length(); i++) {
        if(Character.isDigit(fen.charAt(i))) {
            fenNum += fen.charAt(i) - '0';
        } else {
            if (fenNum > 0) {
                fenToString.append(fenNum);
                fenNum = 0;
            }
            fenToString.append(fen.charAt(i));
        }
    }
    // add last digit if available
    if (fenNum > 0) {
        fenToString.append(fenNum);
    }
    return fenToString.toString();
}

Tests:

System.out.println(getFenString("1P111P11"));
System.out.println(getFenString("1P12PP1"));
System.out.println(getFenString("1P131P"));
System.out.println(getFenString("rnbqkbnr/pp1ppppp/11111111/11p11111/1111P111/11111111/PPPP1PPP/RNBQKBNR"));

Output:

1P111P11 -> 1P3P2
1P12PP1 -> 1P3PP1
1P131P -> 1P5P
rnbqkbnr/pp1ppppp/11111111/11p11111/1111P111/11111111/PPPP1PPP/RNBQKBNR ->
rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42