User Tools

Site Tools


script_sliding_puzzle

====== About the script ====== * Purpose : This script converts any shortcut in one of those puzzles where there is a hole and you need to move tiles to make a picture. * Author : [[https://plus.google.com/u/1/105066926163073195690|TrianguloY]] * Link: https://plus.google.com/u/1/105066926163073195690/posts/WxnVvQnzTmv (with video) ====== How to use the script ====== Instructions: * Run the script from the item, if it is the first time it will override the default item with the custom one, otherwise it will recover that saved item. * Choose the grid size and the color of the piece (hole). It will automatically set the script to the four swipe directions, swipe from the item and solve the item. Optionally it will disable the tap to launch action. At the beginning there is the function that runs when solved, by default it launches the item, you can modify it if prefer. Note 1: the randomize function is random, it won't never leave the puzzle resolved, but can leave it with a one-move-to-solve. Note 2: it uses some bitmap copies, for me it works but maybe it freezes in devices with less memory. Note 3: I tested it personally in my main screen, and it works well, however I didn't add checks, so be sure the inputs in the setup are ok. If it crash or makes an error write a comment. I know it is just another mini-game made with scripts and no really useful, but: you can set it as a lock screen! No need to remember patterns, only resolve a not so easy puzzle. Or set it to protect apps, or even as a way to self control if you don't want to open an app a lot of times ;D ====== Script code ====== <sxh javascript> //this is launched when resolved, modify as you want function completed(){ //alert("yes!"); it.launch(); } var e=LL.getEvent(); var it=e.getItem(); if(it==null){alert("no item");return;} var img=it.getCustomIcon(); if(img==null){ alert("No custom icon, assigning default one"); it.setCustomIcon(copyImage(it.getDefaultIcon())); img=it.getCustomIcon(); } var bit=img.getBitmap(); var grid=[null,null]; var orig=[null,null]; var hole=[null,null]; var size=[null,null]; var data=JSON.parse(it.getTag("puzzle")); if(data==null){ initialize(true); return; } grid=data.g; hole=data.h; orig=data.o; size=[ bit.getWidth()/grid[0] , bit.getHeight()/grid[1] ]; var dir=e.getSource(); if(dir=="MENU_ITEM"){initialize(false);return;} if(dir=="I_RESUMED"||dir=="bug I_PAUSED"){randomize();return;} var to=null; if(dir=="I_SWIPE_LEFT"&&hole[0]<grid[0]-1)to=[1,0]; if(dir=="I_SWIPE_RIGHT"&&hole[0]>0)to=[-1,0]; if(dir=="I_SWIPE_UP"&&hole[1]<grid[1]-1)to=[0,1]; if(dir=="I_SWIPE_DOWN"&&hole[1]>0)to=[0,-1]; if(to==null)return; var newhole=sum(hole,to); //exchange var one=getSlot(hole,bit); var two=getSlot(newhole,bit); setSlot(hole,two,bit); setSlot(newhole,one,bit); hole=newhole; img.update(); img.save(); //save it.setTag("puzzle",JSON.stringify({g:grid,o:orig,h:hole})); //check var def=it.getDefaultIcon(); var original=def.getBitmap(); var current=copyImage(img).getBitmap(); setSlot(hole,getSlot(orig,original),current); if(current.sameAs(original)){ //save and set the original img=copyImage(img); bit=img.getBitmap(); it.setCustomIcon(def); setTimeout(function(){ completed(); //restore the saved (randomizated) setTimeout(function(){randomize(); it.setCustomIcon(img);},0); },250); } function randomize(){ var a=[];//virtual image for(var i=0;i<grid[0];++i){ a[i]=[]; for(var j=0;j<grid[1];++j){ a[i][j]=[i,j]; } } var h=hole; //randomize for(var t=0;t<size[0]*size[1]*3||(h[0]==orig[0]&&h[1]==orig[1]);++t){ var d=[[0,-1],[-1,0],[0,1],[1,0]][Math.floor(Math.random()*22)%4]; //this choose negative directions with more probability var n=sum(h,d);//newhole if(n[0]<0||n[0]>=grid[0]||n[1]<0||n[1]>=grid[1])continue; var temp=a[h[0]][h[1]]; a[h[0]][h[1]]=a[n[0]][n[1]]; a[n[0]][n[1]]=temp; h=n; } hole=h; //virtual->real var original=copyImage(img).getBitmap(); for(var i=0;i<grid[0];++i){ for(var j=0;j<grid[1];++j){ setSlot([i,j],getSlot(a[i][j],original),bit); } } //update img.update(); img.save(); //save it.setTag("puzzle",JSON.stringify({g:grid,o:orig,h:hole})); } function initialize(first){ //initialize item if(!confirm("This will:\n"+(first? "-Override the original icon and replace it with the custom one.\n":"-Recover the previously saved icon\n")+"-Set this script to all four swipe directions.\nDo you want to continue?\n(To recover the item and delete the puzzle choose a 1x1 grid)"))return; grid[0]=LL.pickNumericValue("Grid size:horizontal",grid[0]||3,"INT",1,10,1,null); if(grid[0]==undefined)return; grid[1]=LL.pickNumericValue("Grid size:vertical",grid[1]||3,"INT",1,10,1,null); if(grid[1]==undefined)return; if(grid[0]==1&&grid[0]==1){ it.setCustomIcon(it.getDefaultIcon()); it.setTag("puzzle",null); var prop=it.getProperties().edit(); for(var t=0;t<4;++t){ prop.setEventHandler(["i.swipeLeft","i.swipeRight","i.swipeUp","i.swipeDown"][t],EventHandler.UNSET,null); } prop.setEventHandler("i.tap",EventHandler.UNSET,null); prop.commit(); alert("uninstalled from this item"); return; } var color=LL.pickColor("Color of the piece:",0xFF000000,true); if(color==null)return; var flag=confirm("Do you want to disable the tap action in this item to avoid launch it? (will be launched when resolved by default)"); orig=sum(grid,[-1,-1]);//May change hole=orig; if(first){ it.setDefaultIcon(copyImage(img)); }else{ img=copyImage(it.getDefaultIcon()); it.setCustomIcon(img); img=it.getCustomIcon(); bit=img.getBitmap(); } size=[ bit.getWidth()/grid[0] , bit.getHeight()/grid[1] ]; var piece=[]; for(var t=0;t<=size[0]*size[1];++t){ piece[t]=color; } setSlot(hole,piece,bit); img.save(); img.update(); it.setTag("puzzle",JSON.stringify({g:grid,o:orig,h:hole})); var id=LL.getCurrentScript().getId(); var prop=it.getProperties().edit(); for(var t=0;t<4;++t){ prop.setEventHandler(["i.swipeLeft","i.swipeRight","i.swipeUp","i.swipeDown"][t],EventHandler.RUN_SCRIPT,id); } if(flag)prop.setEventHandler("i.tap",EventHandler.NOTHING,null); prop.commit(); randomize(); //it.setCustomIcon(img); } //all this functions need a global variable 'size=[x,y]' function getSlot(slot,from){ var x=Math.floor(slot[0]*size[0]) var y=Math.floor(slot[1]*size[1]) var w=Math.floor(size[0]) var h=Math.floor(size[1]) //because the original function doesn't support reference arrays var colors=[]; for(var a=0;a<h;++a){ for(var b=0;b<w;++b){ colors[a*w+b]=from.getPixel(x+b,y+a); } } return colors; } function setSlot(slot,from,to){ to.setPixels(from,0,size[0],slot[0]*size[0],slot[1]*size[1],size[0],size[1]); } //helpers function copyImage(src){ var bmp_orig = src.getBitmap(); var img_copy = LL.createImage(bmp_orig.getWidth(), bmp_orig.getHeight()); img_copy.draw().drawBitmap(bmp_orig, 0, 0, null); img_copy.update(); return img_copy; } function sum(a,b){ return [a[0]+b[0],a[1]+b[1]]; } </sxh>

script_sliding_puzzle.txt · Last modified: 2014/11/08 22:43 by trianguloy