[Community Puzzle] Reverse Minesweeper

Really good puzzle, it’s funny how I see that people write complicated code or just few lines.
I belong to the first group, after submitting i see how i can optimize instead just using IF

Hi, could really use some help (Im using java)
And yes i know it looks like a mess i started debugging and splitting it into separate parts for better debugging…

Problem 1 and 4 are easy, but when i run my code for problem 3 and 4 i get and index out of bounds exception :confused: any ideas why?

And i would also appreciate if someone could give me a little hint on how to tackle the problem of multiple x in one row since my code isnt built for that right now

public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int w = in.nextInt();
        int h = in.nextInt();
        if (in.hasNextLine()) {
            in.nextLine();
        }

        String[] field = new String[h];

        int[] xRow = new int[h];
        String[] xlineRow = new String[h];
        int row = 0;

        for (int i = 0; i < h; i++) {
            String line = in.nextLine();

            System.err.println("WANTED: " + line);

            field[i] = line;
            if(line.contains("x")){
                xRow[row] = i;                //saves row (das was nach unten geht)
                xlineRow[row] = line;          //saves string
            }
            row++;
        }

        //System.err.println(xRow[0] + 1);

        
        //TODO: wenn mehrere x in einer zeile dann durch alle durchgehen und umwandeln



        if(xRow.length != 0 && xRow != null){                           //if there is at least one x
            for(int i = 0; i < h; i++){                                 //for every row
                if(xRow[i] != 0){
                    int temp = xRow[i];                                  //x row
                    int location = xlineRow[i].indexOf("x");    //x location
                    
                    int locationL = location-1; //left same row
                    int locationM = location;   //x loc same row;
                    int locationR = location+1; //right same row

                    String newStr = field[temp];    //make new row to overwrite it
    
                    //for same row
                    //check if already has a number
                    if(Character.getNumericValue(newStr.charAt(locationL)) != -1) {
                        int number = Character.getNumericValue(newStr.charAt(locationL));
                        number += 1;

                        newStr = newStr.substring(0, locationL) + number + newStr.substring(locationL+1);

                    } else {
                        newStr = newStr.substring(0, locationL) + "1" + newStr.substring(locationL+1);
                    }

                    if(Character.getNumericValue(newStr.charAt(locationR)) != -1) {
                        int number = Character.getNumericValue(newStr.charAt(locationR));
                        number += 1;

                        newStr = newStr.substring(0, locationR) + number + newStr.substring(locationR+1);

                    } else {
                        newStr = newStr.substring(0, locationR) + "1" + newStr.substring(locationR+1);
                    }
                    
                    //newStr = newStr.substring(0, locationL) + "1" + newStr.substring(locationL+1);
                    newStr = newStr.substring(0, locationM) + "." + newStr.substring(locationM+1);  //place dot where x was
                    //newStr = newStr.substring(0, locationR) + "1" + newStr.substring(locationR+1);
                    
                    
                    
                    
                    field[temp] = newStr;   //same row
                    
                    
                    
                    //for upper
                    newStr = field[temp-1];

                    //newStr = newStr.substring(0, locationL) + "1" + newStr.substring(locationL+1);
                    //newStr = newStr.substring(0, locationM) + "1" + newStr.substring(locationM+1);
                    //newStr = newStr.substring(0, locationR) + "1" + newStr.substring(locationR+1);

                    if(Character.getNumericValue(newStr.charAt(locationL)) != -1) {
                        int number = Character.getNumericValue(newStr.charAt(locationL));
                        number += 1;

                        newStr = newStr.substring(0, locationL) + number + newStr.substring(locationL+1);

                    } else {
                        newStr = newStr.substring(0, locationL) + "1" + newStr.substring(locationL+1);
                    }
                    

                    if(Character.getNumericValue(newStr.charAt(locationM)) != -1) {
                        int number = Character.getNumericValue(newStr.charAt(locationM));
                        number += 1;

                        newStr = newStr.substring(0, locationM) + number + newStr.substring(locationM+1);

                    } else {
                        newStr = newStr.substring(0, locationM) + "1" + newStr.substring(locationM+1);
                    }
                    

                    if(Character.getNumericValue(newStr.charAt(locationR)) != -1) {
                        int number = Character.getNumericValue(newStr.charAt(locationR));
                        number += 1;

                        newStr = newStr.substring(0, locationR) + number + newStr.substring(locationR+1);

                    } else {
                        newStr = newStr.substring(0, locationR) + "1" + newStr.substring(locationR+1);
                    }
                    
                    
                    field[temp-1] = newStr;    //saves as new row




                    //for lower
                    newStr = field[temp+1];

                    //newStr = newStr.substring(0, locationL) + "1" + newStr.substring(locationL+1);
                    //newStr = newStr.substring(0, locationM) + "1" + newStr.substring(locationM+1);
                    //newStr = newStr.substring(0, locationR) + "1" + newStr.substring(locationR+1);

                    if(Character.getNumericValue(newStr.charAt(locationL)) != -1) {
                        int number = Character.getNumericValue(newStr.charAt(locationL));
                        number += 1;

                        newStr = newStr.substring(0, locationL) + number + newStr.substring(locationL+1);

                    } else {
                        newStr = newStr.substring(0, locationL) + "1" + newStr.substring(locationL+1);
                    }
                    

                    if(Character.getNumericValue(newStr.charAt(locationM)) != -1) {
                        int number = Character.getNumericValue(newStr.charAt(locationM));
                        number += 1;

                        newStr = newStr.substring(0, locationM) + number + newStr.substring(locationM+1);

                    } else {
                        newStr = newStr.substring(0, locationM) + "1" + newStr.substring(locationM+1);
                    }
                    

                    if(Character.getNumericValue(newStr.charAt(locationR)) != -1) {
                        int number = Character.getNumericValue(newStr.charAt(locationR));
                        number += 1;

                        newStr = newStr.substring(0, locationR) + number + newStr.substring(locationR+1);

                    } else {
                        newStr = newStr.substring(0, locationR) + "1" + newStr.substring(locationR+1);
                    }
                    
                    
                    field[temp+1] = newStr;    //saves as new row

                }
            } 
        }
        System.err.println("\n");
        System.err.println("--- Output ---");
        System.err.println("\n");
        for(int i = 0; i < h; i++){
            System.out.println(field[i]);
        }
        
    }

Maybe you have not check the doc

The method throws an IndexOutOfBoundException when :

  • the startIndex is larger than the length of String
  • the startIndex isless than zero
  • startIndex is greater than endIndex
  • endIndex is greater than length of String

for exemple with your code :

If location == 0 then locationL == -1 

and it results as

newStr.substring(0, -1)
1 Like