AvGalen
Premium Member
bonus points for being totally awesome!
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Superbump.
Just looked at this, and improved the 15-puzzle sim from the original down to 414 bytes. http://codepen.io/molarmanful/pen/OVEYEv
- No need for charCodeAt: strings can be parsed like arrays with [].
- Never use brackets with any loops/conditionals in general unless you REALLY need to.
- Sometimes, conditionals can be replaced with && or ||.
- window.onkeydown should be written as onkeydown.
- Use e.which instead e.keyCode.
- In for loops, use -- instead of ++.
Just some of the stuff I did. There's probably more that I did and more that can be done.
a=document.getElementById('a');a[w="innerHTML"]='1234\n5678\n9ABC\nDEF ';h=" ";function r(f,g){a[w]=a[w][s='substring'](0,f)+g+a[w][s](f+1)}onkeyup=d=function(e){f=(x=a[w]).indexOf(h);q=e?e.which&3:0|Math.random()*4;y=f+[-5,1,5,-1][q];if((q==2&&f<14)||(!q&&f>4))r(f,x[y]),r(y,h);if(q%2){g=x.charAt(y);if(g!="\n")r(f,g),r(y,h)}},onclick=onload=function(){for(i=99;i--;)d()}
a=document.getElementById('a');a[w="innerHTML"]='1234\n5678\n9ABC\nDEF ';r=(f,g)=>a[w]=a[w].slice(0,f)+g+a[w].slice(f+1);onkeyup=d=e=>{f=(x=a[w]).search(" ");if((z=x[y=f+[-5,1,5,-1][e?e.which&3:0|Math.random()*4]])&&z!='\n')r(f,z),r(y," ")},onclick=onload=e=>{for(i=1e4;i--;)d()}
h=[...' 123\n5674\n9AB8\nDEFC'];y=0;onkeyup=e=>d(e.keyCode%5);d=q=>{if(q==6)for(i=1e4;i--;d(0|Math.random()*5));m=h[y],h[y]=h[f=h[w=y+[-5,,1,5,-1][q]]&&h[w]!='\n'?w:y],h[y=f]=m;document.body.innerHTML=h.join("")};d(6)
a=document.getElementById('a');a[w="innerHTML"]='1234\n5678\n9ABC\nDEF ';function r(f,g){a[w]=a[w].slice(0,f)+g+a[w].slice(f+1)}onkeyup=d=function(e){f=(x=a[w]).search(" ");if((z=x[y=f+[-5,1,5,-1][e?e.which&3:0|Math.random()*4]])&&z!='\n')r(f,z),r(y," ")},onclick=function(e){for(i=1e4;i--;)d()}
h=' 123\n5674\n9AB8\nDEFC'.split("");y=0;onkeyup=function(e){d(e.keyCode%5)};d=function(q){if(q==6)for(i=1e4;i--;d(0|Math.random()*5));m=h[y],h[y]=h[f=h[w=y+[-5,,1,5,-1][q]]&&h[w]!='\n'?w:y],h[y=f]=m;document.body.innerHTML=h.join("")};d(6)
OH MY GOD! I got the 15 puzzle sim down to 372 bytes!
EDIT: I updated an old script from this topic: http://jsfiddle.net/pq6VK/9/ The javascript is only 217 (!!) bytes.
Code:h=[...' 123\n5674\n9AB8\nDEFC'];y=0;onkeyup=e=>d(e.keyCode%5);d=q=>{if(q==6)for(i=1e4;i--;d(0|Math.random()*5));m=h[y],h[y]=h[f=h[w=y+[-5,,1,5,-1][q]]&&h[w]!='\n'?w:y],h[y=f]=m;document.body.innerHTML=h.join("")};d(6)
I didn't read the previous posts, just thought, " this could be better" and edited the code.I don't understand the excitement. If I'm not mistaken, [post=906615]we were at least down to 313[/post], including random-state scrambling (that's just the latest posted version, maybe earlier ones were even shorter). Does yours have more/better functionality?
h=' 123\n5674\n9AB8\nDEFC'.split("");y=0;onkeyup=function(e){d(e.which%5)};function d(q){if(q>5)for(i=1e4;i--;d(0|Math.random()*5));m=h[y],h[y]=h[f=h[w=y+[-5,,1,5,-1][q]]&&h[w]!='\n'?w:y],h[y=f]=m;a.innerHTML=h.join("")}d(6)
I don't think this is valid. The date just doesn't move fast enough to be used as a random number inside such a tight loop, so you will get a lot of UUUU... moves and it won't be a decent scramble anymore.- Change 0|Math.random()*5 to new Date%5. This isn't as great, but it works.
h=' 123\n5674\n9AB8\nDEFC'.split("");y=0;onkeyup=function(e){d(e.which)};function d(q){m=h[y],h[y]=h[f=h[w=y+[-5,1,5,-1][q&3]]&&h[w]!='\n'?w:y],h[y=f]=m;a.innerHTML=h.join("")}for(i=1e4;i--;)d(Math.random()*4)
h=[...' 123\n5674\n9AB8\nDEFC'];y=0;onkeyup=e=>d(e.which);d=q=>{m=h[y],h[y]=h[f=h[w=y+[-5,1,5,-1][q&3]]&&h[w]!='\n'?w:y],h[y=f]=m,a.innerHTML=h.join("")};for(i=1e4;i--;)d(Math.random()*4)
I noticed the script now only provides scrambles when you load the scrambler, which means you can't press a key (e.g. v) to do it anymore.
h=[...' 123\n5674\n9AB8\nDEFC'];y=0;d=q=>{m=h[y],h[y]=h[f=h[w=y+[-5,1,5,-1][q&3]]>'\n'?w:y],h[y=f]=m,a.innerHTML=h.join("")};for(i=1e4;i--;onkeyup=e=>d(e.which))d(Math.random()*4)
h=[...' 123\n5674\n9AB8\nDEFC'];y=0;d=q=>{m=h[y],h[y]=h[f=h[w=y+[-5,1,5,-1][q&3]]>' '?w:y],h[y=f]=m,a.innerHTML=h.join("")};for(i=1e4;i--;onkeyup=e=>d(e.which))d(Math.random()*4)
Ah, niceIf we're doing that, we can also change >'\n' to >' ' for 178 (since y is the space, the other piece is always 0-9A-F)
h=[...' 123\n5674\n9AB8\nDEFC'];y=0;d=q=>{h[y]=h[f=h[w=y+[-5,1,5,-1][q&3]]>' '?w:y],h[y=f]=' ',a.innerHTML=h.join("")};for(i=1e4;i--;onkeyup=e=>d(e.which))d(Math.random()*4)
onkeyup=function(e){d(e.which)};function d(q){h[y]=h[f=h[w=y+[-5,1,5,-1][q&3]=]=>' '?w:y],h[y=f]=' ',a.innerHTML=h.join("")}for(h=' 123\n5674\n9AB8\nDEFC'.split(""),y=0,i=1e4;i--;)d(Math.random()*4)
Haha, of course... Well, that's what I get for not looking at the big pictureEdit: Lol. 173.
Code:h=[...' 123\n5674\n9AB8\nDEFC'];y=0;d=q=>{h[y]=h[f=h[w=y+[-5,1,5,-1][q&3]]>' '?w:y],h[y=f]=' ',a.innerHTML=h.join("")};for(i=1e4;i--;onkeyup=e=>d(e.which))d(Math.random()*4)