[Question]
  [
Bubble-wraps are maximum-level entertainment. Everyone can agree to that.
Now, you will make even computers enjoy bubble-wraps.
# Specs
You will be given two integers, w, and h.(each are responsively width and height)
Your program should output all w\*h phases waiting 1 second between each one and terminate.
Every bubble-wrap starts with all cells full.
For example, a 4\*6 bubble-wrap starts like:
```
O_O_
_O_O
O_O_
_O_O
O_O_
_O_O
```
And each phase, a random non-popped cell is popped.For example,
```
O_O_
_O_O
O_X_
_O_O
O_O_
_O_O
```
The program should terminate when all the cells are popped. aka.
```
X_X_
_X_X
X_X_
_X_X
X_X_
_X_X
```
# Examples
```
(4,6)
(5,5)
(6,2)
(10,10)
(7,9)
```
  
[Answer]
# C (Windows), ~~260~~ 248 bytes
```
#import<windows.h>
i,j,l,r;b(w,h){char*s=malloc(l=w*h+h);for(i=h;i--;*s++=10)for(j=w;j--;*s++=i%2^j%2?79:45);*(s-1)=0;s-=l;for(srand(time(0));j>system("cls")+puts(s)-2;j>-1?s[j]=88:0)for(Sleep(1000),r=rand(),j=-2,i=r+l*2;--i-r;j=s[i%l]==79?i%l:j);}
```
[![enter image description here](https://i.stack.imgur.com/8nWg1.gif)](https://i.stack.imgur.com/8nWg1.gif)
[Answer]
# [Python 3](https://docs.python.org/3/), ~~222~~ 220 bytes
This is my first time answering, so please be gentle (and point out mistakes that I have made).
```
import time,random as t
def f(c,r):
 p=print;a='0_'*c;d=((a[:c]+'\n'+a[1:c+1]+'\n')*r)[:-~c*r]
 for i in[1]*((r*c+r%2*c%2)//2):
  p(d);k=1
  while d[k]!='0':k=t.randrange(len(d))
  d=d[:k]+'X'+d[k+1:];time.sleep(1)
 p(d)
```
[Try it online!](https://tio.run/nexus/python3#JY/LqsMgEIb3eYo5i@KtN7M0@B4FK0XU9EgSI1Ohu756auhi4B/4@C9bWsqKFWpa4hFdDusC7gW1C3GEkeLRM9VB0QVTroPT5Pog3A9BU@qM8laQeybCGam8kL@PcWRGnT6eo@1gXBESpGyk5ZQi9wIPPfeHnl0u/e4NhQY2TFo2@f5Pc4RgJvvXkoiadD3vpdo9I51jbihrXNDBqKnF3YhotJDKDvuC82uOsVDZmN11274 "Python 3 – TIO Nexus")
How it works:
1. Chain a lot of '0\_''s together
2. Chop into '0\_0\_...\n' and '\_0\_0...\n' parts and concatenate
3. Generate random indices until the char at the index is a '0'
4. Create new string with the char at the generated index is replaced with a 'X' (Damn you python for non-mutable strings!)
5. Repeat `r*c+r%2*c%2` times: There are `r*c` bubbles in the pattern, unless r and c are both odd, in which case there are `r*c+1`.
[Answer]
## Mathematica (145 bytes)
Anonymous function, takes height and width as input (in that order — if that's a problem, replace `{##}` with `{#2,#}` in the middle of the code for an extra 2 bytes).
Code:
```
Monitor[Do[Pause@1,{i,NestList[RandomChoice@StringReplaceList[#,"O"->"X"]&,""<>Riffle["_"["O"][[Mod[#+#2,2]]]&~Array~{##},"
"],Floor[##/2]]}],i]&
```
Explanation:
* `""<>Riffle[Array["_"["O"][[Mod[#+#2,2]]]&,{##}],"\n"]` creates the initial, unpopped bubble-wrap, by making an array of "\_"s and "O"s and then StringJoining them between newlines.
* `NestList[RandomChoice@StringReplaceList[#,"O"->"X"]&,..., Floor[##/2]]` repeatedly chooses one of the "O"s to replace with an "X", as many times as there are "O"s (which is Floor[width \* height / 2] — thanks to @JonathanAllan for the idea of putting "\_" instead of "O" in the top left corner, otherwise this would be `Ceiling` instead and thus 2 bytes more).
* `Monitor[Do[Pause@1,{i,...}],i]` makes `i` take the values in the list we just calculated, for 1 second each, and dynamically prints `i`.
Example output:
[![GIF of Mathematica popping bubble-wrap](https://i.stack.imgur.com/icXHF.gif)](https://i.stack.imgur.com/icXHF.gif)
[Answer]
# [MATL](https://github.com/lmendo/MATL), 37 bytes
```
:!i:+o`T&Xxt3:q'_OX'XEcD2y1=ft&v1Zr(T
```
The upper-left corner is always an underscore (allowed by the challenge).
The screen is cleared between phases. I could save a byte by not clearing the screen, but it looks better this way.
The program exits with an error ([allowed by default](http://meta.codegolf.stackexchange.com/q/4780/36398)) after displaying all phases.
Try it at [**MATL Online!**](https://matl.io/?code=%3A%21i%3A%2Bo%60T%26Xxt3%3Aq%27_OX%27XEcD2y1%3Dft%26v1Zr%28T&inputs=4%0A5&version=19.8.0) (If it doesn't work after a couple of seconds, please refresh the page and try again).
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~30~~ 29 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
=”OTX
+þ⁹++µị⁾_OYµṄœS1”XÇ¦µÐL
```
[![Example run](https://i.stack.imgur.com/OLEPx.gif)](https://i.stack.imgur.com/OLEPx.gif)
Calls the link as a dyad with the program arguments and then quits with a message (the code for which is `çṛ“\'=ṙMḋḌẓ(ėo»`)
*Nuance:* the bottom-right "cell" will always be a bubble (rather than the top-left like the example in the question), this is to ensure that when all bubbles are popped the random choice returns 0 which will be the `"X"` at the end of the list - replacing that then makes no change to the value and breaks the loop.
*Note:* does not clear the screen (was not specified, and I'm not sure how to do so).
### How?
```
=”OTX - Link 1, choose a random O index or 0: string   e.g. "_O_\nX_O"
 ”O   - "O"
=     - equals (vectorises over the string)            e.g. [0,1,0,0,0,0,1]
   T  - truthy indexes                                 e.g. [2,7]
    X - random choice (if empty this returns 0)
+þ⁹++µị⁾_OYµṄœS1”XÇ¦µÐL - Main link: w, h              e.g. 3, 2
                        - left argument (implicit), w  e.g. 3
  ⁹                     - right argument, h            e.g. 2
 þ                      - outer product with
+                       -     addition                 e.g. [[2,3,4],[3,4,5]]
                        - left argument (implicit), w  e.g. 3
   +                    - add                          e.g. [[5,6,7],[6,7,8]]
                        - right argument (implicit), h e.g. 2
    +                   - add                          e.g. [[7,8,9],[8,9,10]]
     µ                  - monadic chain separation
       ⁾_O              - "_O"
      ị                 - index into (1-based & mod)   e.g. [['_','O','_'],['O','_','O']]
                        -     note: the additions above assure the last entry is an 'O'.
          Y             - join with line feeds         e.g. ['_','O','_','\n','O','_','O']
           µ        µ   - monadic chain separations
                     ÐL - loop until the results are no longer unique:
            Ṅ           -     print with a trailing line feed and yield
             œS1        -     sleep for 1 second and yield
                   ¦    -     apply to index
                  Ç     -         given by calling the last link (1) as a monad 
                        -                 (a random O index or 0 if none exists)
                ”X      -         an "X"  (      ... which will be an X already)
```
[Answer]
# [Scala](https://www.scala-lang.org/), 764 bytes
```
object B{
  def main(a: Array[String]):Unit={
    val v=false
    val (m,l,k,r,n)=(()=>print("\033[H\033[2J\n"),a(0)toInt,a(1)toInt,scala.util.Random,print _)
    val e=Seq.fill(k, l)(v)
    m()
    (0 to (l*k)/2-(l*k+1)%2).foldLeft(e){(q,_)=>
      val a=q.zipWithIndex.map(r => r._1.zipWithIndex.filter(c=>
        if(((r._2 % 2) + c._2)%2==0)!c._1 else v)).zipWithIndex.filter(_._1.length > 0)
      val f=r.nextInt(a.length)
      val s=r.nextInt(a(f)._1.length)
      val i=(a(f)._2,a(f)._1(s)._2)
      Thread.sleep(1000)
      m()
      val b=q.updated(i._1, q(i._1).updated(i._2, !v))
      b.zipWithIndex.map{r=>
        r._1.zipWithIndex.map(c=>if(c._1)n("X")else if(((r._2 % 2)+c._2)%2==0)n("O")else n("_"))
        n("\n")
      }
      b
    }
  }
}
```
### How it works
The algorithm first fills a 2D Sequence with false values. It determines how many iterations (open boxes) exist based on the command line arguments put in. It creates a fold with this value as the upper bound. The integer value of the fold is only used implicitly as a way to count how many iterations the algorithm should run for. The filled sequence we created previously is the starting sequence for the fold. This is used in generating a new 2D sequence of false values with their cooresponding indecies.
For example,
```
[[false, true],
 [true, false],
 [true, true]]
```
Will be turned into
```
[[(false, 0)], [(false, 1)]]
```
Note that all lists that are completely true (have a length of 0) are omitted from the result list. The algorithm then takes this list and picks a random list in the outermost list. The random list is chosen to be the random row we pick. From that random row, we again find a random number, a column index. Once we find these two random indices, we sleep the thread we are on for 1000 miliseconds.
After we're done sleeping, we clear the screen and create a new board with a `true` value updated in the random indices we have created.
To print this out properly, we use `map` and zip it with the index of the map so we have that in our context. We use the truth value of the sequence as to whether we should print an `X` or either an `O` or `_`. To chose the latter, we use the index value as our guide.
### Interesting things to note
To figure out if it should print an `O` or an `_`, the conditional  `((r._2 % 2) + c._2) % 2 == 0` is used. `r._2` refers to the current row index while `c._2` refers to the current column. If one is on an odd row, `r._2 % 2` will be 1, therefore offsetting `c._2` by one in the conditional. This ensures that on odd rows, columns are moved over by 1 as intended.
Printing out the string `"\033[H\033[2J\n"`, according to some Stackoverflow answer I read, clears the screen. It's writing bytes to the terminal and doing some funky stuff I don't really understand. But I've found it to be the easiest way to go about it. It doesn't work on Intellij IDEA's console emulator, though. You'll have to run it using a regular terminal.
Another equation one might find strange to see when first looking at this code is `(l * k) / 2 - (l * k + 1) % 2`. First, let's demystify the variable names. `l` refers to the first arguments passed into the program while `k` refers to the second one. To translate it, `(first * second) / 2 - (first * second + 1) % 2`. The goal of this equation is to come up with the exact amount of iterations needed to get a sequence of all X's. The first time I did this, I just did `(first * second) / 2` as that made sense. For every `n` elements in each sublist, there are `n / 2` bubbles we can pop. However, this breaks when dealing with inputs such as `(11 13)`. We need to compute the product of the two numbers, make it odd if it's even, and even if it's odd, and then take the mod of that by 2. This works because rows and columns that are odd are going to require one less iteration to get to the final result.
`map` is used instead of a `forEach` because it has less characters.
### Things that can probably be improved
One thing that really bugs me about this solution is the frequent use of `zipWithIndex`. It's taking up so many characters. I tried to make it so that I could define my own one character function that would just perform `zipWithIndex` with the value passed in. [But it turns out that Scala does not allow an anonymous function to have type parameters.](https://stackoverflow.com/questions/2554531/how-can-i-define-an-anonymous-generic-scala-function) There is probably another way to do what I'm doing without using `zipWithIndex` but I haven't thought too much about a clever way to do it.
Currently, the code runs in two passes. The first generates a new board while the second pass prints it out. I think that if one were to combine these two passes into one pass, that would save a couple of bytes.
This is the first code golf I've done so I'm sure there is a lot of room for improvement. [If you'd like to see the code before I optimized for bytes as much as possible, here it is.](https://gist.github.com/ColdSauce/332f89c84f06cceca20c4a473536457a)
[Answer]
## JavaScript (ES6), ~~246~~ 229 bytes
```
document.write(`<pre id=o></pre>`)
setInterval(_=>{(a=o.innerHTML.split(/(O)/))[1]?a[Math.random()*~-a.length|1]=`X`:0;o.innerHTML=a.join``},1e3)
f=(w,h)=>o.innerHTML=[...Array(h)].map((_,i)=>`O_`.repeat(w+h).substr(i,w)).join`
`
```
```
<div oninput=f(+w.value,+h.value)><input id=w type=number min=1><input id=h type=number min=1>
```
[Answer]
# Python - 290 bytes
I've never done one of these before - so any constructive criticism would be appreciated :)
Main trick here is just annoyingly nested list comprehensions. I could save a few characters by not having a newline between the pops but that just looks ugly. 
```
r=range
q=print
import random as n,time
def f(H,W):
    def p(b):
        q("\n".join(["".join(["O"if(i,j)in b else"X"if(i,j)in X else"_"for j in r(H)])for i in r(W)]));time.sleep(1);q()
    b=[(i,h)for h in r(H)for i in r(h%2,W,2)];n.shuffle(b);X=[]
    while len(b)>0:
        p(b);X+=[b.pop()]
    p(b)
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~49~~ ~~46~~ 39 bytes (noncompeting)
```
ＵＯＮＮO_¶_OＡＫＡαＡ№αOβＨＷψβ«Ａ§α§⌕ＡαO‽βXＡ№αOβ
```
## Verbose
```
Oblong(InputNumber(), InputNumber(), "O_\n_O")
Assign(PeekAll(), a)
Assign(Count(a, "O"), b)
RefreshWhile (k, b) {
    AssignAtIndex(a, AtIndex(FindAll(a, "O"), Random(b)), "X")
    Assign(Count(a, "O"), b)
}
```
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), ~~61~~ 59 bytes
```
⎕←m←'O_'[2|-/¨⍳⎕]
(b/,m)[?+/b←'O'=,m]←'X'
⎕DL 1
→2/⍨'O'∊⎕←m
```
`⎕←` output
  
`m←` *m*, where *m* is
  
`'O_'[`…`]` these characters indexed by…
  
 `2|` the division-remainder-when-divided-by-two of
  
 `-/¨` the difference between each of
  
 `⍳` all the coordinates (indices) in an array of shape
  
 `⎕` numeric input (the number of rows and columns)
`(`…`)[`…`]←'X'` assign the character X to one of the…
  
 `b/` filtered-by-*b* (to be defined)
  
 `,m` raveled elements of m, specifically…
  
 `?` a random element (lit. number) in the range one to
  
 `+/` the sum of
  
 `b←` *b*, where *b* is
  
 `'O'=` Boolean for where the letter equals
  
 `,m` *m* raveled
`⎕DL 1` **D**e**l**ay one second
`→2` Go to line 2
  
`/⍨` if (lit. filtered by)
  
`'O'∊` whether the letter is a member of
  
`⎕←m` the outputted value, where the outputted value is *m*
  
[Try it online!](https://tio.run/nexus/apl-dyalog#NY1BSwJREMfv71MMBL2i1U2LbhKhBYHhQYNCJN7qbC3te2/Z90yjvImJsRJBx05ePHTrG/RR3hexWbPDDDP/@f3/47JPONNxrAeRuoU4UghS3KOByAIKE2EKVkNf9TA1Vqge2DsE3bdJ33qQ75EymFoDAmwkkRiZgEiSVA@LgA@YPoLBrlY9D4yGAUJXKFJwnUOO/CuGIXYt23LZnLv5R/WacyNiyzm41zE8jVCJIMbmSb21EQhqnhYvqxc1zgM9BK2gEFbybipSDP@NhNXqUHKzBU1u8ka91Ry5bFE62HbZ1xoi7bxBt33OmJu@QLj6YyUVb9zwdvm54P8sXfZNeoftBL4nd9vHe36wBnjFk518uuJs8465yXvZd9mSrm4628StKHwVsiM4/AU "APL (Dyalog Unicode) – TIO Nexus")
---
From version 16.0 it will be shorter:
`{0::→⋄'X'@(⊂(?∘≢⊃⊢)⍸'O'=⍵⊣⎕DL 1)⊢⎕←⍵}⍣≡'O_'[2|-/¨⍳⎕]`
[Answer]
# Python 3, ~~195~~ 188 bytes
```
import time,random
def f(w,h):
 a=bytearray(b'0-'*w*h);b=[*range(0,w*h,2)];random.shuffle(b);
 while 1:print(*(a.decode()[w*i:w*i+w]for i in range(h)),sep='\n');a[b.pop()]=88;time.sleep(1)
```
Using `bytearray` and `decode` seems to be shorter than slicing and reassembling a string a la `a[:i]+'X'+a[i+1:]`.
```
import time,random
def f(w,h):
 x=[*range(1,h*w,2)];random.shuffle(x)
 while 1:
  for i in range(w*h):
   print('-X0'[(i%w%2!=i//w%2)+(i in x)],end='\n'[i%w<w-1:])
  print();time.sleep(1);x.pop()
```
[Answer]
# Java 7, 317 bytes
```
void c(int w,int h)throws Exception{String r="";int x=0,j=0,i;for(;j++<h;x^=1,r+="\n")for(i=0;i<w;r+=(i+++x)%2<1?"_":"O");for(System.out.println(r);r.contains("O");System.out.println(r=r.substring(0,x)+'X'+r.substring(x+1))){Thread.sleep(1000);for(x=0;r.charAt(x)!='O';x=new java.util.Random().nextInt(r.length()));}}
```
**Explanation:**
```
void c(int w, int h) throws Exception{                     // Method with two integer parameters (throws Exception is required for the Thread.sleep)
  String r = "";                                           //  String we build to print
  int x=0, j=0, i;                                         //  Some temp values and indexes we use
  for(; j++<h;                                             //  Loop over the height 
      x^=1,                                                //    After every iteration change the flag `x` every iteration from 0 to 1, or vice-versa
      r += "\n")                                           //    And append the String with a new-line
    for(i=0; i<w;                                          //   Inner loop over the width
        r += (i++ + x)%2 < 1 ? "_" : "O")                  //    Append the String with either '_' or 'O' based on the row and flag-integer
    ;                                                      //   End inner width-loop (implicit / no body)
                                                           //  End height-loop (implicit / single-line body)
  for(                                                     //  Loop
    System.out.println(r);                                 //   Start by printing the starting wrap
    r.contains("O");                                       //   Continue loop as long as the String contains an 'O'
    System.out.println(r=                                  //   Print the changed String after every iteration
        r.substring(0,x)+'X'+r.substring(x+1))){           //    After we've replaced a random 'O' with 'X'
      Thread.sleep(1000);                                  //   Wait 1 second
      for(x=0; r.charAt(x) != 'O';                         //   Loop until we've found a random index containing still containing an 'O'
          x = new java.util.Random().nextInt(r.length()))  //    Select a random index in the String
      ;                                                    //   End loop that determines random index containing 'O' (implicit / no body)
  }                                                        //  End loop
}                                                          // End method
```
**Test gif (4,6)**
[![enter image description here](https://i.stack.imgur.com/aIi9x.gif)](https://i.stack.imgur.com/aIi9x.gif)
[Answer]
## Perl, 148 bytes
146 bytes of code + `-pl` flags.
```
$_=O x($x=$_+1);s/O\K./_/g;for$i(1..($y=<>)){$b.=($i%2?$_:_.s/.$//r).$/}}print$_="\e[H$b";while(/O/){$r=0|rand$y*$x+3;s/.{$r}\KO/X/s||redo;sleep 1
```
To run it:
```
perl -ple '$_=O x($x=$_+1);s/O\K./_/g;for$i(1..($y=<>)){$b.=($i%2?$_:_.s/.$//r).$/}}print$_="\e[H$b";while(/O/){$r=0|rand$y*$x+3;s/.{$r}\KO/X/s||redo;sleep 1' <<< "6
4"
```
[Answer]
# MATLAB (R2016b), 172 bytes
**Code:**
```
x=input('');m=[eye(x(2),x(1)) ''];m(:)='O';m(1:2:end,2:2:end)='_';m(2:2:end,1:2:end)='_';o=find(m=='O');r=o(randperm(nnz(o)));disp(m);for i=r';pause(1);m(i)='X';disp(m);end
```
Recommendations are always welcome! [Try it online!](https://tio.run/nexus/octave#TY27CsMwDEV/xZsk8GJns9E3dC2EUAJ2wIMfOA24@XnXoaV00tVB96g3DqkcTwQgG3n2L48NNcmGikgALDaiIYYbjKCMNj45qT9z4MeFv6tU/zjzFpLDyFeXbOWMdU2u@BoxpRMzEVkX9oKR7JarCFzBlvXY/Xg9rGF47vA7GeLe50lMyxs)
**Program Output:**
[![enter image description here](https://i.stack.imgur.com/WM2fX.gif)](https://i.stack.imgur.com/WM2fX.gif)
**Explanation:**
```
x = input( '' );                    % Input
m = [ eye( x( 2 ), x( 1 ) ) '' ];   % Character Matrix
m( : ) = 'O';                       % Fill Matrix with "Bubbles"
m( 1:2:end, 2:2:end ) = '_';        % Alternate Spaces Between Bubbles (Part 1)
m( 2:2:end, 1:2:end ) = '_';        % Alternate Spaces Between Bubbles (Part 2)
o = find( m == 'O' );               % Index Bubble Locations
r = o( randperm( nnz( o ) ) );      % Randomize Bubble Locations
disp( m );                          % Display Initial Bubble Wrap Phase
for i = r'
    pause( 1 );                     % Pause for 1 Second
    m( i ) = 'X';                   % Pop Bubble
    disp( m );                      % Display Subsequent Bubble Wrap Phase
end
```
]