import java.util.*;
import java.lang.*;
import java.io.*;

class Main {
    public static int solve(int width, int height, int nbBlocks, String[] grid) {
        int valueBlockToRemove=0;
        int ind=0;
        for(int i=1; i<height-1; i++){
            char [] t = grid[i].toCharArray();
            for(int j=width-1; t[j]!='X'; j--) {
                //System.err.println(t[j]);
                if(t[j]-'0'>=0 && t[j]-'0'<nbBlocks && j>=ind){
                    ind=j;
                    valueBlockToRemove=t[j]-'0';
                    System.err.println("Le bloc " + (t[j]-'0') + " est a l'indice " + j);
                    break;
                }
            }
        }
        System.err.println("Block to remove : " + valueBlockToRemove);
        return valueBlockToRemove;
    }

    public static boolean isInterBloque(String[] grid){
        for (String line : grid){
            char[] t = line.toCharArray();
            for(char c: t){
                if(c!='X' && c!='.')
                    return true;
            }
        }
        return false;
    }

    public static void updateGrid(int blocValue, String[] grid){
        for (int j=0; j<grid.length; j++){
            char[] t = grid[j].toCharArray();
            String line="";
            for (char c : t) {
                if (c - '0' == blocValue)
                    line += ".";
                else
                    line += c + "";
            }
            grid[j]= line;
        }
    }

    public static void show (String[] grid){
        for (String s : grid)
            System.err.println(s);
    }

    public static void main(String[] args){
        //String[] grid={"XXXX","X.11","X02.","XXXX"}; //test 1
        String[] grid={"XXXXXXXXXX",
                       "X.1111111.",
                       "X....0002.",
                       "XXXXXXXXXX"}; //test 2
        show(grid);
        while(isInterBloque(grid)){
            //updateGrid(solve(4,4,3, grid),grid); // test 1
            updateGrid(solve(10,4,3, grid),grid); //test 2
            show(grid);
        }
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: