The Bomberman Game

  • [deleted]
    + 1 comment

    Here's my sol


    public class Solution {

    static void blast(char[][] grid,int i,int j,int[][] secPlanted){
        grid[i][j]='.';
        try{
            if(grid[i][j+1]=='O'&&secPlanted[i][j+1]==3){
                blast(grid,i,j+1,secPlanted);
            }
            grid[i][j+1]='.';
            secPlanted[i][j+1]=0;
        }catch(ArrayIndexOutOfBoundsException e){}
        try{
            if(grid[i][j-1]=='O'&&secPlanted[i][j-1]==3){
                blast(grid,i,j-1,secPlanted);
            }
            grid[i][j-1]='.';
            secPlanted[i][j-1]=0;
        }catch(ArrayIndexOutOfBoundsException e){}
        try{
            if(grid[i-1][j]=='O'&&secPlanted[i-1][j]==3){
                blast(grid,i-1,j,secPlanted);
            }
            grid[i-1][j]='.';
            secPlanted[i-1][j]=0;
        }catch(ArrayIndexOutOfBoundsException e){}
        try{
            if(grid[i+1][j]=='O'&&secPlanted[i+1][j]==3){
                blast(grid,i+1,j,secPlanted);
            }
            grid[i+1][j]='.';
            secPlanted[i+1][j]=0;
        }catch(ArrayIndexOutOfBoundsException e){}
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int r=sc.nextInt(),c=sc.nextInt(),n=sc.nextInt();
        String[] gridString = new String[r];
        int sec=0;
        char[][] grid = new char[r][c];
        for(int i=0;i<r;i++){
            gridString[i]=sc.next();
            grid[i]=gridString[i].toCharArray();
        }
        sec++;//1 sec
        sec++;//2 secs
        int[][] secPlanted = new int[r][c];
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                secPlanted[i][j] = grid[i][j]=='O'?2:0; 
            }
        }
        while(sec<=n){
            while(n-sec>4)
                sec+=4;
            //fill empty places
            if(sec%2==0){
                for(int i=0;i<r;i++)
                    for(int j=0;j<c;j++)
                        if(grid[i][j]=='.')
                            grid[i][j]='O';
            }
    
            //bomb blast
            if(sec%2==1){
                for(int i=0;i<r;i++){
                    for(int j=0;j<c;j++){
                        if(secPlanted[i][j]==3){
                            secPlanted[i][j]=0;
                            blast(grid,i,j,secPlanted);
                        }
                    }
                }
            }
            for(int i=0;i<r;i++)
                for(int j=0;j<c;j++)
                    secPlanted[i][j] = grid[i][j]=='O' ? secPlanted[i][j]+1 : 0;
            sec++;
    
        }
    
        //final grid
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++)
                System.out.print(grid[i][j]);
            System.out.println();
        }
    }
    

    }