Lights Out

Instructions:
There are 25 lights in a 5x5 grid. Initially some lights are turned on, others are turned off.
Turn off all of the lights. It is always possible to solve the puzzle in 10 moves (sometimes less).

Keys
Cursor keys
Space bar to turn on/off current light and those around it.

The emulator I used is an online one: JSBeeb - https://bbc.godbolt.org/

Simply copy and paste the source code into it and then type RUN

Technical information:
General:

All variables are global.

Referring to the expanded version of the program:
lines 10 to 32 are for initialisation
lines 33 to 53 are the main game loop
lines 54 to 56 are the end program
lines 60 to 74 is the subroutine that draws the 5x5 grid
lines 80 to 85 is the subroutine that draws/erases an outline around the current cell
lines 90 to 103 is the subroutine that turns the lights on/off.

initialisation
10DIMa(4,4),b(4,4)
set up 2 5x5 arrays: a is the main grid, b is to choose exactly 10 lights initially
11i=RND(-TIME)
seed the pseudo random number generator
12c=0
c is the number of lights lit
13FORm=1TO10
14REPEAT
15x=RND(5)-1
16y=RND(5)-1
20UNTILb(y,x)=0
21b(y,x)=1
22GOSUB90
turn on/off lights
23NEXT
choose 10 lights initially (don't choose the same one twice)

24MODE4
25VDU23,1,0;0;0;0;
disable cursor
26m=0
m is number of moves
27x=0
28y=0
x and y are co-ords of current cell
29*FX4,1
disable cursor keys for editing
30GOSUB60
draw grid
31GCOL0,1
32GOSUB80
draw outline around current cell

main game loop
33REPEAT
34g=GET-136
35GCOL0,0
36GOSUB80
erase outline around current cell
40x=(x-(g=0)*4-(g=1))MOD5
add/subtract 1 to x if right/left pressed, wrap-around applies (e.g. left when x=0 gives x=4)
41y=(y-(g=3)*4-(g=2))MOD5
add/subtract 1 to x if down/up pressed, wrap-around applies
42IFg=-104THENm=m+1:GOSUB90
if space pressed then add 1 to number of moves and turn on/off lights
50GOSUB60
draw grid
51GCOL0,1+(c=0)
52GOSUB80
draw outline around current cell (but not if game over)
53UNTILc=0
until number of lights lit is zero

end program
54VDU23,1,1;0;0;0;
enable cursor
55OSCLI"FX4,0"
enable cursor keys for editing (*FX4,0 didn't like not being at the end of the program line)
56END

draw 5x5 grid and print number of moves taken
60FORj=0TO4
61FORi=0TO4
62GCOL0,a(j,i)
select graphics colour (0=black, 1=white)
63MOVEi*128+4,1020-j*128-4
move to top-left corner of cell
64PLOT0,120,0
PLOT0 moves to relative x,y co-ords (rather than absolute)
70PLOT81,-120,-120
PLOT81 draws a triangle using relative x,y co-ords
71PLOT81,120,0
2 triangles back to back make a square
72NEXT,
73PRINTTAB(0,21);"moves=";m
74RETURN

draw/erase outline around current cell
80MOVEx*128,1020-y*128
81PLOT1,128,0
PLOT1 uses relative x,y co-ords
82PLOT1,0,-128
83PLOT1,-128,0
84PLOT1,0,128
85RETURN

turn on/off lights (at (y,x), (y,x-1), (y,x+1), (y-1,x) and (y+1,x) if these positions are valid)
90FORj=-1TO1
91FORi=-1TO1
92IFi*j<>0ORy+j<0ORy+j>4ORx+i<0ORx+i>4THENGOTO102
100a(y+j,x+i)=a(y+j,x+i)EOR1
if light is on then turn if off and vice-versa
101c=c+(a(y+j,x+i)=0)*2+1
subtract 1 from count if light turned off; add 1 to count if light turned on
102NEXT,
103RETURN
