The Bomberman Game

Sort by

recency

|

87 Discussions

|

  • + 0 comments

    Flowing is my java 8 solution

      public static List<String> bomberMan(int n, List<String> grid) {
        // Write your code here
            List<String> holeGrid = new ArrayList<String>();
            int row = grid.size();
            int col = grid.get(0).length();
            char [][] newGrid = new char[row][col];
            
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    newGrid[i][j] = 'O';
                }
            }
            for(char[] line: newGrid){
                String newLine = new String(line);
                holeGrid.add(newLine);
            }
            
            List<String> grid1 = nextState(grid);
            List<String> grid2 = nextState(grid1);
            List<String> grid3 = nextState(grid2);
            
            if(n == 1){
                return grid;
            }
            if(n == 3){
                return grid1;
            }
            if(n%2 == 0){
                return holeGrid;
            }
            
            int flipTime = (n-1)/2;
            if(flipTime%2 == 1){
                return grid3;
            }
            
            return grid2;
            
        }
        
        public static List<String> nextState(List<String> grid){
            List<String> nextGrid = new ArrayList<String>();
            int row = grid.size();
            int col = grid.get(0).length();
            char [][] newGrid = new char[row][col];
            int[][] directions = {{0, 0},{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
            
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    newGrid[i][j] = 'O';
                }
            }
            
            for(int i=0; i<grid.size(); i++){
                for(int j=0; j<grid.get(0).length(); j++){
                    if(grid.get(i).charAt(j) == 'O'){
                        for(int[] direction: directions){
                            int j_new = j + direction[0];
                            int i_new = i + direction[1];
                            if(j_new<col && j_new>=0 && i_new<row && i_new>=0){
                                newGrid[i_new][j_new] = '.';
                            }
                        }
                    }
                }
            }
            for(char[] line: newGrid){
                String newLine = new String(line);
                nextGrid.add(newLine);
            }
            return nextGrid;
        }
    
  • + 0 comments
    def bomberMan(n, g):
        x = lambda g: g if n < 2 else ['O' * len(g[0]) for _ in g] if n % 2 == 0 else [''.join(['.' if g[y][x] == 'O' or (g[y][x] == '.' and 'O' in (g[max(y-1, 0)][x], g[y][max(x-1, 0)], g[y][min(x+1, len(g[0])-1)], g[min(y+1, len(g)-1)][x])) else '.' if g[y][x] == 'O' else 'O' for x in range(len(g[0]))]) for y in range(len(g))]
        return x(g) if n % 4 == 3 else x(x(g))
    
  • + 0 comments

    I wasn't a huge fan of this question. The first solution someone may come to has little in relation to the actual solution this question expects; yet developing the first, intuitive, solution is vital to seeing the pattern that allows the pattern based solution. I didn't appreciate the fact I was made to create an entire solution to discover a pattern that would allow me to create the real solution all because the example only went to n = 3.

  • + 2 comments

    Do some examples on paper and then observe this patter:

    1. The grid is always full with bombs if n is even
    2. The grid at 3 seconds is the detonation of the initial state bombs
    3. The grid at 5 seconds it the detonation of the grid at 3 seconds
    4. Grids 3, 7, 11, ... are repeating
    5. Grids 5, 9, 13, ... are repeating

    So the algorithm becomes:

    1. If n is even, return an all bomb grid
    2. Calculate grid at three seconds
    3. Calculate grid at five seconds
    4. If n is 3, 7, 11, ... return three second grid
    5. If n is 5, 9, 13, ... return five second grid
    6. if n < 3, return initial grid

    Solution

    def get_detonated_grid(bomb_grid):
        rows = len(bomb_grid)
        cols = len(bomb_grid[0])
        
        detonated_grid = []
        for i in range(rows):
            detonated_grid.append([])
            for j in range(cols):
                detonated_grid[i].append('O')
        
        for i in range(rows):
            for j in range(cols):
                if bomb_grid[i][j] == '.':
                    continue
                detonated_grid[i][j] = '.'
                if i > 0:
                    detonated_grid[i-1][j] = '.'
                if i < rows - 1:
                    detonated_grid[i+1][j] = '.'
                if j > 0:
                    detonated_grid[i][j-1] = '.'
                if j < cols - 1:
                    detonated_grid[i][j+1] = '.'
        
        return detonated_grid
    
    def all_bomb_grid(r, c):
        return [['O'] * c] * r
        
    def to_output(grid):
        return [''.join(row) for row in grid]
    
    def bomberMan(n, grid):
        if n == 1:
            return grid
            
        all_bombs = all_bomb_grid(r, c)
        
        if n % 2 == 0:
            return to_output(all_bombs)
        
        # The grid at three seconds is the detonation from initial state
        three_second_grid = get_detonated_grid(grid)
        
        # The grid at five seconds is the detonation from the bombs
        # on the three second grid
        
        # The 3, 7, 11, ... grids repeat
        # The 5, 9, 13, ... grids repeat
        
        if n == 3 or (n - 3) % 4 == 0:
            return to_output(three_second_grid)
        else:
            return to_output(get_detonated_grid(three_second_grid))
    
  • + 0 comments

    Java8 solution, just switch between the two states of the grid:

        public static List<String> bomberMan(int n, List<String> grid) {
        // Write your code here
        if(n==1){return grid;}
        if(n%2==0) {
            int length = grid.get(0).length();
            
            String oString = repeatChar('O', length);
            for(int i=0; i<grid.size(); i++){
                grid.set(i, oString);
            }
            return grid;
        }
        
        
        grid = switchStates(grid);
        if(n%4==3){return grid;}
        return switchStates(grid);
        
        }
        
        
        public static String repeatChar(char c, int times) {
            StringBuilder sb = new StringBuilder(times);
            for (int i = 0; i < times; i++) {
                sb.append(c);
            }
            return sb.toString();
        }
        
        // function to switch between the 2 states -
        // from seconds 3-7-11-15---n%4==3
        // to seconds 5-9-13-17---n%4==1
        public static List<String> switchStates(List<String> grid){
        char[][] charArray = new char[grid.size()][];
    
        for (int i = 0; i < grid.size(); i++) {
                charArray[i] = grid.get(i).toCharArray(); // Convert each String to a char[]
            }
        for(int i=0; i<grid.size(); i++){
          second: for(int j=0; j<charArray[0].length; j++){
              int[][] positions = {{i+1, j}, {i-1, j}, {i, j+1}, {i, j-1}};
              if (grid.get(i).charAt(j) == '.'){
                  for(int[] pos : positions){
                      int x = pos[0];
                      int y = pos[1];
                      if(x>=0 && x < grid.size() && y >= 0 && y < charArray[0].length){
                          if(grid.get(x).charAt(y) == 'O'){
                              continue second;
                          }
                      }
                  }
                  charArray[i][j] = 'O';
              }
              else {
                  
                  charArray[i][j] = '.';
                  for(int[] pos : positions){
                      int x = pos[0];
                      int y = pos[1];
                      if(x>=0 && x < grid.size() && y >= 0 && y < charArray[0].length){
                          charArray[x][y] = '.';
                      }
                  } 
              }
          }  
        }
        for(int i =0 ; i < charArray.length; i++){
            String s = new String(charArray[i]);
            grid.set(i, s);
        }
        return grid;
        }