Skip to content

Instantly share code, notes, and snippets.

@gaborbata
Last active Aug 29, 2015
Embed
What would you like to do?
BoxBot4k - Sokoban game in 4 kB of JavaScript (for pouët.net)
<!DOCTYPE html><html><title>B4K</title><canvas id="c" width="448" height="384"></canvas><script>function D(t){var x,o=t.split("|"),e=o.shift();for(x=o[N];x--;)e=e.replace(RegExp(o[x].charAt(0),"g"),o[x].substring(1));return e}function g(t,x,o,e){for(e=o[N];e--;)o[e]=t.charCodeAt(x*o[N]+e)-Z}function l(x,o){for(0>v?v=49:v>49&&(v=0),m=0,h=0,f=!1,g(L,v,t),x=W;x--;)for(o=H;o--;)4==t[W*o+x]&&(t[W*o+x]=1,u.x=x,u.y=o)}function p(x,o,e){for(x=3;x--;){for(o=W;o--;)for(e=H;e--;)q?2!=x||t[W*e+o]>2?1==x&&t[W*e+o]>2?w[M](z[8],o*Z+4,e*Z+4):0==x&&t[W*e+o]>2&&w[M](z[t[W*e+o]],o*Z,e*Z):w[M](z[t[W*e+o]],o*Z,e*Z):w[M](z[0],o*Z,e*Z);1==x&&q?w[M](z[7],u.x*Z+4,u.y*Z+4):0==x&&q&&w[M](z[4],u.x*Z,u.y*Z)}if(w.font="bold 14px sans-serif",w[S]="rgba(255,255,255,.4)",w.textAlign="center",q)f&&w[F](E[0],C,19),w[F](E[11]+(v+1)+E[10]+E[12]+m+E[10]+E[13]+h,C,376);else{for(x=1;x<E[N]-5;x++)w[F](E[x],C,19*x+106);w[F](E[9],C,376)}}function B(x,o,e,r,n,y){for(w=O.c[X]("2d"),z=Array(P[N]),o=Array(256),x=z[N];x--;){for(g(I,P[x][3],o),e=document.createElement("canvas"),e.width=Z,e.height=Z,r=e[X]("2d"),n=16;n--;)for(y=16;y--;)r[S]=P[x][o[16*y+n]],r.fillRect(2*n,2*y,2,2);z[x]=e}t=Array(W*H),v=0,u={x:0,y:0},q=!1,b=!1,l(),p(),O[A]("keydown",function(x,o,e,r,n){if(!b){if(b=!0,q){switch(n={x:0,y:0},x.keyCode){case 38:n.y=-1;break;case 40:n.y=1;break;case 37:n.x=-1;break;case 39:n.x=1;break;case 33:v++,l();break;case 34:v--,l();break;case 8:l();break;case 13:f&&(v++,l());break;default:return}if((0!=n.x||0!=n.y)&&!f){for(o=W*(u.y+n.y)+u.x+n.x,e=W*(u.y+2*n.y)+u.x+2*n.x,1==t[o]||2==t[o]?(u.x+=n.x,u.y+=n.y,m++):5!=t[o]&&6!=t[o]||1!=t[e]&&2!=t[e]||(t[o]=5==t[o]?1:2,t[e]=1==t[e]?5:6,u.x+=n.x,u.y+=n.y,m++,h++),e=!0,r=t[N];r--;)5==t[r]&&(e=!1);f=e}}else q=!0;x.preventDefault(),p()}}),O[A]("keyup",function(){b=!1})}var W=14,H=12,Z=32,C=224,A="addEventListener",F="fillText",M="drawImage",S="fillStyle",X="getContext",N="length",O=window,R="transparent",Q="rgba(0,0,0,.3)",P=[["#21285c","#19204c","#303378",2],["#5c3c20","#4c3018","#785830",2],["#74060c","#640305","#8e1618",2],["#a29b79","#cec491","#7f7543",3],[R,"#ccc","#888",0],["#c4a864","#a88448","#6f4e2a",1],["#c47865","#a85449","#6f2c2b",1],[R,Q,Q,0],[Q,Q,Q,1]],E=D("~ COMPLETED! PRzS ENTER TO CONTINUE.,BOXBOT4K,,GAME CONTROLS:,ARROW KEYS - MOVE,BACKSPACE - RzTART ~,PGUP/PGDN - NEXT/PREVIOUS ~,,PRzS ANY KEY TO START.,(C) 2014 GABOR BATA, ,~: ,MOVz: ,PUSHz: |~LEVEL|zES").split(","),L=D('uu~vpY,j-#WWPF!"=R`wN%$:&hb;mv`jwNkV&JNT8NwsSpjjYt$wEkbZ"Vdfg"#9NTbhkDYR oGCP"&&%$nC=xYd pDb"I":x:FDY0pD8sl"Bt#2EkNkbhws0wpai7w!HCn6"2a%twy;0pgxGCPU#kLxkspTY0fcottwOxeq!/lWt/1hl/pT-xYSfprp$%Z4&lcl%hAoxYMpjpX3X$%kaxwbhjYSobx"EJ8#Q!xbkTsl5S fnplnJ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yynhnyynivtumtumthomthomouhliol~trntrtv ytjtlz"rz"lllwouoywrujpggggssssp"ll~wouoywrujp"v uy ffxiel!v uuyyvelkffxi uyl~uy!jky!vdkdvoyr"!vzq ize~w!jy qykd!r"qyrd~"qqq"!roq!rzkzq z"rze~wukqy!v"!royvokz!vz zriz |~ |z"""|y!!|xzz !~~! |w" y|v~~ |uyyy|t!"|swwwwww|r |q!~"|pzzzzz"|o""|nt~v|mtt~ |l~~|k! |jry|izz|h!z|g"~|fxx|e !|d"y'),z,t,w,m,h,f,v,u,q,b;B();</script></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment