The Bomberman Game

  • + 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");
    }