The Bomberman Game

  • + 0 comments

    I found the other python solutions here a little hard to understand. Hopefully this will be helpful to anyone who's stuck!

    def bomberMan(n, grid):
        if n == 1: 
            return grid
            
        # The grid is totally full on every other turn
        if n % 2 == 0:
            return ["O" * len(grid[0]) for _ in range(len(grid))]
        
        countdown_grid = []
        for row in grid:
            countdown_row = [2 if cell == "O" else -1 for cell in row]
            countdown_grid.append(countdown_row)
        
        planting_this_turn = True
    
        num_iterations = ((n-2) % 4) + 1 
        for i in range(num_iterations):
            # planting
            if planting_this_turn:
                new_countdown_grid = []
                for row in countdown_grid:
                    # 4 because we will decrement the counter this turn
                    new_countdown_row = [4 if cell == -1 else cell for cell in row]
                    new_countdown_grid.append(new_countdown_row)
                countdown_grid = new_countdown_grid
            
            # countdown
            bombs_to_detonate = []
            for row in range(len(countdown_grid)):
                for col in range(len(countdown_grid[0])):
                    countdown_grid[row][col] -= 1
                    if countdown_grid[row][col] == 0:
                        bombs_to_detonate.append((row, col))
                    
            # explosion
            for bomb in bombs_to_detonate:
                row, col = bomb
                countdown_grid[row][col] = -1
                
                if 0 < col: # left
                    countdown_grid[row][col-1] = -1
                    
                if 0 < row: # top
                    countdown_grid[row - 1][col] = -1
                
                if col < len(countdown_grid[row]) - 1: # right
                    countdown_grid[row][col + 1] = -1
                
                if row < len(countdown_grid) - 1: # bottom
                    countdown_grid[row + 1][col] = -1
            
            planting_this_turn = not planting_this_turn
            
        final_grid = []
        for row in countdown_grid:
            str_array = ["." if cell == -1 else "O" for cell in row]
            s = "".join(str_array)
            final_grid.append(s)
        
        return final_grid