Classic Sliding Blocks Puzzle

shorter version (40 lines):

' Classic Sliding Blocks Puzzle.bas SmallBASIC 0.12.8 [B+=MGA] 2016-12-28
while size < 3 or size > 9
? "Sliding Block Puzzle" : input "Enter your number of blocks per side 3-9"; s
size = int(val(s)) : if size = 0 then end
wend
color 15, 9 : rect 0, 0, xmax, ymax, 0 filled 'cls but not with back color
dim board(size, size)
for
r = 1 to size : for c = 1 to size : board(c, r) = c + (r - 1) * size : next : next
board(size, size) = 0 : c0 = size : r0 = size
for
i = 0 to 50 * size * size ' mix blocks
select case mid("lrud", int(rnd * 4) + 1, 1)
case "l" : if c0 + 1 <= size then board(c0, r0) = board(c0+1, r0) : board(c0+1, r0) = 0 : c0 = c0+1
case "r" : if c0 - 1 > 0 then board(c0, r0) = board(c0-1, r0) : board(c0-1, r0) = 0 : c0 = c0-1
case "u" : if r0 + 1 <= size then board(c0, r0) = board(c0, r0+1) : board(c0, r0+1) = 0 : r0 = r0+1
case "d" : if r0 - 1 > 0 then board(c0, r0) = board(c0, r0-1) : board(c0, r0-1) = 0 : r0 = r0-1
end select
next
pen on
while not solved
solved = 1
for r = 1 to size : for c = 1 to size
if board(c, r) then
if board(c, r) <> (r-1)*size + (c-1) mod size + 1 then solved=0
rect (c-1) * 60, (r - 1) * 60, c * 60, r * 60, 9 filled
rect (c-1) * 60, (r - 1) * 60, c * 60, r * 60, 15
at (c - 1) * 60 + 20, (r - 1) * 60 + 20 : ? board(c, r)
else
rect (c-1) * 60+1, (r - 1) * 60+1, c * 60-2, r * 60-1, 0 filled
end if
next : next
if pen(3) and solved = 0 then
mx = pen(4) : my = pen(5) : delay 50
bx = int(mx / 60) + 1 : by = int(my / 60) + 1
if bx = c0 + 1 and by = r0 then board(c0, r0) = board(c0+1, r0) : board(c0+1, r0) = 0 : c0 = c0+1
if bx = c0 - 1 and by = r0 then board(c0, r0) = board(c0-1, r0) : board(c0-1, r0) = 0 : c0 = c0-1
if bx = c0 and by = r0 + 1 then board(c0, r0) = board(c0, r0+1) : board(c0, r0+1) = 0 : r0 = r0+1
if bx = c0 and by = r0 - 1 then board(c0, r0) = board(c0, r0-1) : board(c0, r0-1) = 0 : r0 = r0-1
end if
wend
pen off : color 15, 0 : at 50, size * 60 + 40 : ? "Solved!": delay 5000

We think we may have contender for Rosetta Code, with 26 line "opaz" version
' OPAZ Classic Sliding Blocks.bas  SmallBASIC 0.12.8 [B+=MGA] 2016-12-28
? " Sliding Blocks Puzzle" : ? : ? "Select Board size from 3 up to 9 ";
while s < 3 or s > 9 : input "";sz : s = int(val(sz)) : wend : dim board(s, s)
for
r = 1 to s : for c = 1 to s : board(c, r) = c + (r - 1) * s : next : next
board(s, s) = 0 : c0 = s : r0 = s
for
i = 0 to 50 * s * s : handle mid("opaz", int(rnd * 4) + 1, 1) : next
cls : locate s + 2, 1 : ? " press: o =left p =right a =up z =down"
while 1
locate 1, 0 : b = space(20) : solved = 1
for r = 1 to s : for c = 1 to s
if board(c, r) then
if board(c, r) <> c + (r - 1) * s then solved = 0 'not solved
b = b + right(" " + board(c, r), 3) + " "
else : b = b + " " : end if
next : ? b : b = space(20) : next : ?
if solved then locate s + 4, 20 : ? "Solved!" : delay 5000 : end
k = inkey : if k = "Q" then cls : end else handle k
wend
sub
handle(d)
select case d
case "o" : if c0 < s then board(c0,r0)=board(c0+1,r0) : board(c0+1,r0)=0 : c0=c0+1
case "p" : if c0 > 1 then board(c0,r0)=board(c0-1,r0) : board(c0-1,r0)=0 : c0=c0-1
case "a" : if r0 < s then board(c0,r0)=board(c0,r0+1) : board(c0,r0+1)=0 : r0=r0+1
case "z" : if r0 > 1 then board(c0,r0)=board(c0,r0-1) : board(c0,r0-1)=0 : r0=r0-1
end select
end
sub


' Sliding Block arrow.bas SmallBASIC 0.12.8 [B+=MGA] 2017-01-04
input "For Sliding Block Puzzle, enter blocks per side 3-9 ";sz
s = int(val(sz)) : if 2 < s and s < 10 then dim board(s, s) else end
for
r = 1 to s : for c = 1 to s : board(c, r) = c + (r - 1) * s : next : next
board(s, s) = 0 : c0 = s : r0 = s : cls
for
i = 0 to 50 * s * s : handle val(mid("3489", int(rnd * 4) + 1, 1)) : next
while 1
locate 1, 0 : b = "" : solved = 1
for r = 1 to s : for c = 1 to s
if board(c, r) then
if board(c, r) <> c + (r - 1) * s then solved = 0 'not solved
b = b + right(" " + board(c, r), 3) + " "
else : b = b + " " : end if
next : ? b : b = "" : next : ?
if solved then locate s + 2, 2 : ? "Solved!" : delay 5000 : end
k = inkey : if len(k)=1 and asc(k)=27 then end else handle asc(right(k,1))-1
wend
sub
handle(d)
select case d
case 3 : if c0 < s then board(c0,r0)=board(c0+1,r0) : board(c0+1,r0)=0 : c0=c0+1
case 4 : if c0 > 1 then board(c0,r0)=board(c0-1,r0) : board(c0-1,r0)=0 : c0=c0-1
case 8 : if r0 < s then board(c0,r0)=board(c0,r0+1) : board(c0,r0+1)=0 : r0=r0+1
case 9 : if r0 > 1 then board(c0,r0)=board(c0,r0-1) : board(c0,r0-1)=0 : r0=r0-1
end select
end