Author Topic: 25 line Sliding Block Puzzle  (Read 1118 times)

B+

  • Guest
25 line Sliding Block Puzzle
« on: January 04, 2017, 05:01:24 PM »
Uses arrow keys to move number "blocks":
Code: [Select]
' 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
Still readable, I think!  :)