The Bomberman Game

  • + 0 comments

    java

    class Result {
    
        /*
         * Complete the 'bomberMan' function below.
         *
         * The function is expected to return a STRING_ARRAY.
         * The function accepts following parameters:
         *  1. INTEGER n
         *  2. STRING_ARRAY grid
         */
    
        public static List<String> bomberMan(int n, List<String> grid) {
            // Write your code here
            if(n == 1) return grid ;
            
            int row = grid.size() ;
            int col = grid.get(0).length() ;
            if(n%2 == 0)
            {
                return createZeroGrid(row, col);
            }
            
             // For odd values of n greater than 1, we simulate explosions
            // There are only two explosion patterns:
            // - one that occurs at time = 3 (and every 4 seconds after that)
            // - another that occurs at time = 5 (and every 4 seconds after that)
            
            List<String> after3 = afterExplosion(grid, row, col);
            List<String> after5 = afterExplosion(after3, row, col) ;
            
            if(n % 4 == 3)
            {
                return after3 ;
            }else{
                return after5 ;
            }
            
    
        }
        
        public static List<String> createZeroGrid(int row , int col)
        {
            List<String> result = new ArrayList<>();
            String s = "O".repeat(col);
            for(int i=0 ; i < row ; i++)
            {
                result.add(s);
            }
            return result ;
        }
        
        public static List<String> afterExplosion(List<String> grid , int row , int col)
        {
            char[][] resultCharArr = new char[row][col];
            for (int i = 0; i < row; i++) {
                Arrays.fill(resultCharArr[i],'O');
            }
            
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col ; j++) {
                    if(grid.get(i).charAt(j) == 'O' )//if it's a bomb
                    {
                        //blast that index and i+1,j and  i-1,j
                        // and i,j+1 and i,j-1 
                        // total 5 blast from 1 bomb 
                        resultCharArr[i][j] = '.';
                        
                        if(i > 0) resultCharArr[i-1][j] = '.';
                        if(i < row -1) resultCharArr[i+1][j] = '.';
                        
                        if(j > 0) resultCharArr[i][j-1] = '.';
                        if(j < col-1) resultCharArr[i][j+1] = '.';
                    }
                }
            }
            
            // Convert the 2D char grid back into a list of strings
            List<String> result = new ArrayList<>();
            for(char[] rowOFchar:resultCharArr)
            {
                   result.add(new String(rowOFchar));
            }
            
            return result ;
        }
    
    }