package main.testgen;

public class Stringifier {

    public static String makeProblemName(int sx, int sy, int ex, int ey) {
        return sx + "-" + sy + "_" + ex + "-" + ey;
    }

    public static String defaultToString(int seed, int sizeX, int sizeY, int unblockedRatio) {
        StringBuilder sb = new StringBuilder("def_");
        sb.append(intToStr(seed)).append("_");
        sb.append(intToStr(sizeX)).append("_");
        sb.append(intToStr(sizeY)).append("_");
        sb.append(intToStr(unblockedRatio));
        return sb.toString();
    }
    
    public static String automataToString(long seed, int sizeX, int sizeY, int unblockedRatio, int iterations, float resolutionMultiplier, int cutoffOffset, boolean bordersAreBlocked) {
        StringBuilder sb = new StringBuilder("gen_");
        sb.append(sizeX).append("x");
        sb.append(sizeY).append("_");
        sb.append(unblockedRatio).append("_");
        sb.append(iterations).append("_");
        sb.append((int)(resolutionMultiplier*1000)).append("_");
        sb.append(cutoffOffset).append("_");
        sb.append(bordersAreBlocked ? "T" : "F").append("_");
        sb.append(longToStr(seed));
        return sb.toString();
    }
    
    public static String automataDCToString(long seed, int sizeX, int sizeY, float percentBlocked, int iterations, float resolutionMultiplier, boolean bordersAreBlocked) {
        StringBuilder sb = new StringBuilder("autodc_");
        sb.append(sizeX).append("x");
        sb.append(sizeY).append("_");
        sb.append((int)(percentBlocked*1000)).append("_");
        sb.append(iterations).append("_");
        sb.append((int)(resolutionMultiplier*1000)).append("_");
        sb.append(bordersAreBlocked ? "T" : "F").append("_");
        sb.append(longToStr(seed));
        return sb.toString();
    }
    
    public static String mazeMapToString(long seed, int sizeX, int sizeY, int corridorWidth, float connectednessRatio) {
        StringBuilder sb = new StringBuilder("maze_");
        sb.append(sizeX).append("x");
        sb.append(sizeY).append("_");
        sb.append(corridorWidth).append("_");
        sb.append("e").append((int)(-Math.log(connectednessRatio+0.00000001f)*10)).append("_");
        sb.append(longToStr(seed));
        return sb.toString();
    }

    public static String defaultToStringReadable(int seed, int sizeX, int sizeY, int unblockedRatio) {
        StringBuilder sb = new StringBuilder("gen_");
        sb.append(sizeX).append("x");
        sb.append(sizeY).append("_");
        sb.append(unblockedRatio).append("_");
        sb.append(intToStr(seed));
        return sb.toString();
    }
    
    public static int[] defaultToParameters(String input) {
        String[] args = input.split("_");
        if (!args[0].equals("def"))
            throw new UnsupportedOperationException("Invalid string: " + input);
        int[] pars = new int[args.length-1];
        for (int i=0; i<pars.length; i++) {
            pars[i] = strToInt(args[i+1]);
        }
        return pars;
    }

    public static String intToStr(int i) {
        StringBuilder sb = new StringBuilder("i");
        while (i != 0) {
            sb.append(digitToChar((i%32+32)%32));
            i = i>>>5;
        }
        return sb.toString();
    }
    
    public static String longToStr(long i) {
        StringBuilder sb = new StringBuilder("i");
        while (i != 0) {
            sb.append(digitToChar((int)(i%32+32)%32));
            i = i>>>5;
        }
        return sb.toString();
    }
    
    public static int strToInt(String s) {
        int num = 0;
        for (int i=1; i<s.length(); i++) {
            num += charToDigit(s.charAt(i)) << (5*(i-1));
        }
        return num;
    }
    
    public static char digitToChar(int d) {
        if (d < 0) {
            throw new UnsupportedOperationException("Exceeds range: " + d);
        } else if (d < 26) {
            return (char)('A' + d);
        } else if (d < 36) {
            return (char)('0' + (d-26));
        } else {
            throw new UnsupportedOperationException("Exceeds range: " + d);
        }
    }
    
    public static int charToDigit(char c) {
        if (c >= 'A' && c <= 'Z') {
            return (int)(c-'A');
        } else if (c >= '0' && c <= '9') {
            return (int)(c-'0')+26;
        } else {
            throw new UnsupportedOperationException("Exceeds range: " + c);
        }
        
    }
    
}
@@ -2142,35 +2142,38 @@
 end(
-%22e%22).append((int)(-Math.log
+floatToOneDigitStandardFormStr
 (con
@@ -2191,24 +2191,8 @@
 atio
-+0.00000001f)*10
 )).a
@@ -3500,32 +3500,444 @@
 oString();%0A    %7D
+%0A%0A    public static String floatToOneDigitStandardFormStr(float f) %7B%0A        float EPS = 0.0000001f;%0A%0A        if (f == 0) return %220E0%22;%0A        int digits = 0;%0A    %0A        while (Math.abs(f) %3E 1+EPS) %7B%0A            f /= 10;%0A            digits++;%0A        %7D%0A        while (Math.abs(f) %3C 1-EPS) %7B%0A            f *= 10;%0A            digits--;%0A        %7D%0A        return (int)(f %3E 0 ? f+EPS : f-EPS) + %22E%22 + digits;%0A    %7D
 %0A    %0A    public

Change name format of maze maps to something more intuitive (e.g. 1E-3 to represent 0.001)