// SPDX-License-Identifier: MIT // Copyright (c) 2026 Incoprea // Sliding eight-tile puzzle // Not compiled or tested in-world // Setup: Owner-touch to generate a puzzle by valid random moves from a solved board. Enter the number of a tile next to the underscore. // Stop / restore: Stop responding and the listener closes after 90 seconds, or remove/disable the script. Owner-touch restarts the exercise. // Notes: Owner-only local exercise; no prizes, visitor records, network service, or persistent scores. The scramble is always reachable from the solved state. A random walk can occasionally finish near or at solved; restart for another scramble. // License: see LICENSE in this distribution. // Original Incoprea. MIT. Not compiled or tested in-world. integer channel; integer handle; close() { if(handle)llListenRemove(handle); handle=0; llSetTimerEvent(0.0); } ask(string prompt) { if(!handle) { channel=-1000-(integer)llFrand(1000000.0); handle=llListen(channel,"",llGetOwner(),""); } llTextBox(llGetOwner(),prompt,channel); llSetTimerEvent(90.0); } integer digit(string text,integer min,integer max) { if(llStringLength(text)!=1)return -1; integer n=llSubStringIndex("0123456789",text); if(nmax)return -1; return n; } list board; integer blank; string view() { return llDumpList2String(llList2List(board,0,2)," ")+"\n"+llDumpList2String(llList2List(board,3,5)," ")+"\n"+llDumpList2String(llList2List(board,6,8)," "); } integer adjacent(integer p) { return llAbs(p/3-blank/3)+llAbs(p%3-blank%3)==1; } move(integer p) { string value=llList2String(board,p); board=llListReplaceList(board,[value],blank,blank); board=llListReplaceList(board,["_"],p,p); blank=p; } start() { board=["1","2","3","4","5","6","7","8","_"]; blank=8; integer i; for(i=0;i<100;++i) { integer p=(integer)llFrand(9.0); if(adjacent(p))move(p); } ask(view()+"\nEnter adjacent tile 1-8."); } answer(string s) { if(digit(s,1,8)<0) { ask(view()+"\nEnter a tile number 1-8."); return; } integer p=llListFindList(board,[s]); if(p<0||!adjacent(p)) { ask(view()+"\nChoose a tile beside the blank."); return; } move(p); if(llDumpList2String(board,"")=="12345678_") { close(); llOwnerSay("Puzzle solved."); return; } ask(view()+"\nSlide another tile."); } default { touch_start(integer count) { if(llDetectedKey(0)!=llGetOwner())return; close(); start(); } listen(integer ch,string name,key id,string message) { if(!handle||ch!=channel||id!=llGetOwner())return; answer(llStringTrim(message,STRING_TRIM)); } timer() { close(); llOwnerSay("Exercise paused. Touch to restart."); } changed(integer change) { if(change&CHANGED_OWNER)llResetScript(); } }