The Bomberman Game

Sort by

recency

|

37 Discussions

|

  • + 0 comments

    I was able to determine that there are only 4 states the grid can be in: The initial grid, filled with bombs, or 1 of 2 alternating states.

    #python 3
    def bomberMan(n, grid):
        if n == 1:
            return grid
        
        allBombs = ['O' * len(grid[0])] * len(grid)
        
        if n % 2 == 0:
            return allBombs
            
        state1 = progress(grid)
        state2 = progress(state1)
        
        return state2 if (n-1) % 4 ==0 else state1
        
    
    def progress(grid):
        h = len(grid)
        w = len(grid[0])
        new = []
        for r in range(h):
            row = ""
            for c in range(w):
                current = grid[r][c]
                bottom = grid[min(r+1,h-1)][c]
                top = grid[max(r-1,0)][c]
                left = grid[r][max(c-1,0)]
                right = grid[r][min(c+1,w-1)]
                
                cross = current + bottom + top + left + right
                
                if cross.count('O') > 0:
                    row +='.'
                else:
                    row +='O'
            new.append(row)
        return new
    
  • + 0 comments

    Observation: There is a pattern: --> The blast at 3rd second, 7th second, 11th second.... will be the same. --> Similarly, the blast at 5th second, 9th second, ... and so on will be the same. What we can do is to create a function that gets us the output grid for the 3rd second (1st blast). Now if we run that function again with the input of the 1st blast, we will get the output for the 5th second (2nd blast)

    My code:

    const filledGrid = (grid) => {
      return grid.map((row) =>
        row
          .split("")
          .map((ele) => "O")
          .join("")
      );
    };
    
    const secondPattern = (grid) => {
      let newGrid = grid.map((row) => row.split("").map((el) => "O"));
      for (let i = 0; i < grid.length; i++) {
        for (let j = 0; j < grid[0].length; j++) {
          if (grid[i][j] === "O") {
            newGrid[i][j] = ".";
            if (i > 0) {
              newGrid[i - 1][j] = ".";
            }
            if (j > 0) {
              newGrid[i][j - 1] = ".";
            }
            if (i < grid.length - 1) {
              newGrid[i + 1][j] = ".";
            }
            if (j < grid[0].length - 1) {
              newGrid[i][j + 1] = ".";
            }
          }
        }
      }
      return newGrid.map((row) => row.join(""));
    };
    
    function bomberMan(n, grid) {
        // Write your code here
        if (n === 1) return grid;
        if(n % 2 === 0) return filledGrid(grid);
        let secondPatternMatch = false;
        for(let i = 2; i <=n; i++)
        {
            if(i % 2 !== 0)
            {
                secondPatternMatch = !secondPatternMatch;
            }
        }
        return secondPatternMatch ? secondPattern(grid) : secondPattern(secondPattern(grid)); 
        
    
    }
    

    What I am doing is using a flag variable to check the seconds.

    
    

    `

  • + 0 comments

    Python3 short but horrible:

    adj = ((0, 0), (-1, 0), (1, 0), (0, -1), (0, 1))
    def detonate(grid, t, i, j):
        return any(t - grid[i + x][j + y] == 3 for x, y in adj if i + x >= 0 and j + y >= 0 and i + x < len(grid) and j + y < len(grid[0]))
        
    def bomberMan(n, grid):
        n = min(n, 2 + (n-2) % 4)
        grid = [[('.', 0)[c == 'O'] for c in r] for r in grid]
        
        for t in range(2, n+1, 2):
            grid = [[t if c == '.' else c for c in r] for r in grid]
            if t+1 <= n:
                grid = [['.' if detonate(grid, t+1, i, j) else grid[i][j] for j in range(len(grid[i]))] for i in range(len(grid))]
        
        return [''.join(('O', '.')[c == '.'] for c in r) for r in grid]
    
  • + 0 comments

    C#:

    public static List<string> bomberMan(int n, List<string> grid)
        {
            if(n == 1)
                return grid;
            List<string> stuffedGrid = new List<string>();
            List<string> detonationGrid = new List<string>();
            string str1 = "";
            for(int j = 0; j < grid[0].Length; j++)
            {
                str1+="O";
            }
            for(int i = 0; i < grid.Count; i++)
            {
                stuffedGrid.Add(str1);
            }
            if(n % 2 == 0)
                return stuffedGrid;
                
            else if(n % 4 == 3)
            {
                detonationGrid = getBombLocations(stuffedGrid, grid);
            }               
            else
            {
                detonationGrid = getBombLocations(stuffedGrid, getBombLocations(stuffedGrid,grid));
            }
            return detonationGrid;
        }
        public static List<string> getBombLocations(List<string> stuffedGrid, List<string> grid)
        {
            List<string> detonatingGrid = new List<string>();
            detonatingGrid.AddRange(stuffedGrid);
            char[] temp = new char[grid[0].Length];
            for(int i = 0; i < grid.Count; i++)
            {
                string str = grid[i];
                for(int  j = 0; j < str.Length; j++)
                {
                    if(str[j] == 'O')
                    {
                         temp = detonatingGrid[i].ToCharArray();
                         temp[j] = '.';
                         string replaceBomb = new string(temp);
                         detonatingGrid[i] = replaceBomb;
                        if(j != str.Length-1){
                            temp = detonatingGrid[i].ToCharArray();
                            temp[j+1] = '.';
                            string replaceRight = new string(temp);
                            detonatingGrid[i] = replaceRight;
                        }
                        if(i != grid.Count-1){
                            temp = detonatingGrid[i+1].ToCharArray();
                            temp[j] = '.';
                            string replaceDown = new string(temp);
                            detonatingGrid[i+1] = replaceDown;
                        }
                        if(j != 0){
                            temp = detonatingGrid[i].ToCharArray();
                            temp[j-1] = '.';
                            string replaceLeft = new string(temp);
                            detonatingGrid[i] = replaceLeft;
                        }
                        if(i != 0){
                            temp = detonatingGrid[i-1].ToCharArray();
                            temp[j] = '.';
                            string replaceUp = new string(temp);
                            detonatingGrid[i-1] = replaceUp;
                        }
                    }
                } 
            }
            return detonatingGrid;
        }
    
  • + 0 comments

    Python - uses a nice 'discard this bomb location if it exists' set operation so you can 'explode' the five bomb squares without worrying about bound checking.

    Classic HR 'hope you know this trick about the problem' feature where you have to know that the solution will bounce between two states in a stable pattern ... else trying to solve it by performing all the steps takes too long for the test cases and hits timeouts.

    def bomberMan(n, grid):
        rows = len(grid)
        cols = len(grid[0])
        
        bombs = set(
            (r_idx, c_idx) 
            for r_idx, row in enumerate(grid)
            for c_idx, element in enumerate(row)
            if element == 'O'
        )
        
        bombs_everywhere = set(
            (r_idx, c_idx) 
            for r_idx, row in enumerate(grid)
            for c_idx, element in enumerate(row)
        )
        
        def bomb(bomb_locations):
            next_state = bombs_everywhere.copy()
            for bomb in bomb_locations:
                r_idx, c_idx = bomb
                next_state.discard((r_idx, c_idx))
                next_state.discard((r_idx - 1, c_idx))
                next_state.discard((r_idx + 1, c_idx))
                next_state.discard((r_idx, c_idx + 1))
                next_state.discard((r_idx, c_idx - 1))
            return next_state
                
        def set_to_grid(inset):
            return [
                ''.join(['O' if (r, c) in inset else '.' for c in range(cols)]) 
                for r in range(rows)
            ]
        
        if n == 1:
            return grid
        if n % 2 == 0:
            return set_to_grid(bombs_everywhere)
        if n % 4 == 3:
            return set_to_grid(bomb(bombs))
        else:
            return set_to_grid(bomb(bomb(bombs)))