Interactive Lights Off game Intro

I guess most people are familiar with the game Lights Off, but in case someone isn't, here is a short description:  


  You start with an NxN grid where each cell in the grid is a light. For this example N=5. There are 2 ways the game can start: either all of the lights are on, or some lights are off. The goal is to turn off all of the lights. If you toggle a light, the lights left, right, above and below it will toggle with it.


All Lights on

O O O O O  
O O O O O  
O O O O O  
O O O O O  
O O O O O


Toggled cells

Toggled(0,0) | Toggled(1,2)| Toggled(2,4)
X X O O O    |  O O X O O  |  O O O O O
X O O O O    |  O X X X O  |  O O O O X
O O O O O    |  O O X O O  |  O O O X X
O O O O O    |  O O O O O  |  O O O O X
O O O O O    |  O O O O O  |  O O O O O


Task

Make an interactive game of Lights Off with as little code as possible.  

Requirements  

There is a setup phase and a play phase.

The setup phase takes user input to determine the grid size (minimum size of 5) and whether the grid starts with all of the lights on. If the grid should not start with all of the lights on, lights must be selected randomly to start off: at least 1 and at most 20% of the lights.

In the play phase, the grid is displayed to the user and moves are taken as input. The application could use a windowed GUI with lights toggled by clicking, or a text UI which takes typed input of the coordinates of the cell to toggle.

When the user reaches a solved state (all lights off), the application must notify them that they have solved the puzzle, and show how many moves it took.

The use of external resources is not allowed.

If there is anything unclear or missing please let me know :)

Related questions


Lights Out - Find the solution asks for a computer-generated solution given the input
Lights Off! is more similar to this one, but non-interactive
C++, 564 bytes

#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;ctime&gt;
#include &lt;cstdlib&gt;
#include &lt;vector&gt;
using namespace std;int main(){int n,p,s=0,y,i=0,b;cin&gt;&gt;n&gt;&gt;b;if(n&lt;5)n=5;vector&lt;int&gt;m(n*n,1);srand(time(0));if(b)for(int&amp;c:m)c=rand()&lt;RAND_MAX/6;for(;i&lt;n*n;i++)cout&lt;&lt;m[i]&lt;&lt;(-~i%n?"":"\n");while(find(m.begin(),m.end(),1)!=m.end()){cin&gt;&gt;p&gt;&gt;y;p+=y*n;if(p&lt;0||p&gt;=n*n)continue;m[p]=!m[p];if(p%n)m[p-1]=!m[p-1];if(p%n+1&lt;n)m[p+1]=!m[p+1];if(p&gt;=n)m[p-n]=!m[p-n];if(p+n&lt;n*n)m[p+n]=!m[p+n];for(i=0;i&lt;n*n;i++)cout&lt;&lt;m[i]&lt;&lt;(-~i%n?"":"\n");s++;}cout&lt;&lt;"Solved in "&lt;&lt;s&lt;&lt;" steps."&lt;&lt;endl;}


In setup phase, the user should input the grid size first. Then if the grid should starts with all of the lights on, the user should input 0; otherwise 1.

In play phase, the user should input the 0-based coordinates of the cell to toggle until all lights off.

Original code:

#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;ctime&gt;
#include &lt;cstdlib&gt;
#include &lt;vector&gt;
using namespace std;
int main() {
    int n, p, s = 0, y, i = 0, b;
    cin &gt;&gt; n &gt;&gt; b;
    if(n &lt; 5) n = 5;
    vector&lt;int&gt; m(n * n, 1);
    srand(time(0));
    if (b) for (int&amp; c : m) c = rand() &lt; RAND_MAX / 6;
    for (; i &lt; n * n; i++) cout &lt;&lt; m[i] &lt;&lt; (-~i % n ? "" : "\n");
    while (find(m.begin(), m.end(), 1) != m.end()) {
        cin &gt;&gt; p &gt;&gt; y;
        p += y * n;
        if (p &lt; 0 || p &gt;= n * n) continue;
        m[p] = !m[p];
        if (p % n) m[p - 1] = !m[p - 1];
        if (p % n + 1 &lt; n) m[p + 1] = !m[p + 1];
        if (p &gt;= n) m[p - n] = !m[p - n];
        if (p + n &lt; n * n) m[p + n] = !m[p + n];
        for (i = 0; i &lt; n * n; i++) cout &lt;&lt; m[i] &lt;&lt; (-~i % n ? "" : "\n");
        s++;
    }
    cout &lt;&lt; "Solved in " &lt;&lt; s &lt;&lt; " steps." &lt;&lt; endl;
}