The Bomberman Game

Sort by

recency

|

93 Discussions

|

  • + 0 comments

    Easy Javascript solution:

           const bombGameBoard = (initialState) => {
               const bombedField = initialState.map((row, index) => Array.from({ length: row.length }).fill(BOMB))
               for (let r = 0; r < initialState.length; r++) {
                   for (let c = 0; c < initialState[r].length; c++) {
                       if (initialState[r][c] === BOMB) {
                           bombedField[r][c] = EMPTY_FIELD;
                           if (bombedField[r+1]) {
                               bombedField[r+1][c] = EMPTY_FIELD;
                           }
    
                           if (bombedField[r-1]) {
                               bombedField[r-1][c] = EMPTY_FIELD;
                           }
    
                           if (bombedField[r-1]) {
                               bombedField[r-1][c] = EMPTY_FIELD;
                           }
    
                           if (bombedField[r]?.[c+1]) {
                               bombedField[r][c+1] = EMPTY_FIELD;
                           }
    
                           if (bombedField[r]?.[c-1]) {
                               bombedField[r][c-1] = EMPTY_FIELD;
                           }
                       }
                   }
               }
    
               return bombedField;
           }
    
    
           const BOMB = 'O';
           const EMPTY_FIELD = '.';
    
           function bomberMan(seconds, grid) {
               let initialState = grid.map((row) => Array.from(row));
               const firstCycleBoardState = bombGameBoard(initialState); // 3 7 11
               const secondCycleBoardState = bombGameBoard(firstCycleBoardState); // 5 9 13
               if (seconds === 1) {
                   return grid;
               }
               if (seconds % 4 === 3) {
                   return firstCycleBoardState.map((arr) => arr.join(''));
               }
    
               if (seconds % 4 === 1) {
                   return secondCycleBoardState.map((arr) => arr.join(''));
               }
    
               if (seconds % 2 === 0) {
                   return grid.map((row, index) => Array.from({ length: row.length }).fill(BOMB)).map((arr) => arr.join(''));
               }
           }
    
  • + 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 ;
        }
    
    }
    
  • + 0 comments

    my solution: https://github.com/liweinan/java-snippets/blob/master/src/main/java/io/weli/hackerrank/BomberMan.java

  • + 1 comment

    As mentioned by others, this was a PITA question. I finally got it after carefully running it step-by-step in my own IDE. This took many days, largely due to my dissatisifaction with leaving something undone.

    Sure, interviewers might ask this question, but only if they have decided they expect to fail you before even starting.

  • + 0 comments

    c++

    void tryExplode(std::vector<string>& src, int i, int j){
        size_t rows = src.size();
        size_t columns = src[0].size();
        
        
        if(i < 0 || j < 0 || i >= rows || j >= columns){
            return;
        }
        
        src[i][j] = '.';
    
    
    }
    
    vector<string> makeFullGrid(size_t rows, size_t columns){
        std::string fullRow;
        fullRow.insert(0, columns, 'O');
        
        return {rows, fullRow};
        
    }
    
    std::vector<string> detonate(std::vector<string> const& src){
        size_t rows = src.size();
        size_t columns = src[0].size();
        
        auto dst = makeFullGrid(rows, columns);
        
        for(int i = 0; i < rows; ++i){
            for(int j = 0; j < columns; ++j){
                if(src[i][j] == 'O'){
                    tryExplode(dst, i + 1, j);
                    tryExplode(dst, i - 1, j);
                    tryExplode(dst, i, j + 1);
                    tryExplode(dst, i, j - 1);
                    tryExplode(dst, i, j);
                }
            }
        } 
        return dst;
    }
    
    
    
    vector<string> bomberMan(int n, vector<string> grid) {
    
        size_t rows = grid.size();
        size_t columns = grid[0].size();
        
        if(n <= 1){
            return grid;
        }
        
        if(n%2==0){
            return makeFullGrid(rows, columns);
        }
        
        if(n%4==3){
            return detonate(grid);
         }
         
        if(n%4==1){
            auto result = detonate(grid);
            return detonate(result);
        }
    
        throw std::runtime_error("could not solve");
    }