Skip to content

Instantly share code, notes, and snippets.

@oriSomething
Last active November 3, 2017 10:54
Show Gist options
  • Save oriSomething/8951f762c1a704f88234d3ef546be536 to your computer and use it in GitHub Desktop.
Save oriSomething/8951f762c1a704f88234d3ef546be536 to your computer and use it in GitHub Desktop.
Sokoban clone
<body onload='
K = onkeydown = ({ keyCode: k }) => {
if (k > 36 && k < 41) {
ox = (k -= 38)==2?0:k;
oy = k==-1?0:k-1;
A = M[y][x];
B = M[y + oy][x + ox] || "#";
C = (Y=M[y + oy * 2]||[])[x + ox * 2];
a = A == "+" ? "." : " ";
b = " "==B||B=="$" ? "@" : "+";
c = " "==B||B=="." ? C : C == " " ? "$" : C == "." ? "*" : C;
if (B !== "#" && (!"#*$".includes(C) ||" .".includes(B))) {
M[y][x] = a;
M[y + oy][x + ox] = b;
Y[x + ox * 2] = c;
y += oy;
x += ox;
}
}
P.textContent = M.map(l=>l.join("")).join("\n");
};
M = P.textContent.split("\n").map(l => l.split(""));
x = 3;
y = 2;
K(P);
'><pre id=P style="font-size:3pc">#####
# #
#.$@#
#.$ #
#.$ #
#####
<body onload='
K=onkeydown=d=>{d=d.keyCode;36<d&&41>d&&(ox=2==(d-=38)?0:d,oy=-1==d?0:d-1,A=M[y][x],B=M[y+oy][x+ox]||"#",C=(Y=M[y+2*oy]||[])[x+2*ox],a="+"==A?".":" ",b=" "==B||"$"==B?"@":"+",c=" "==B||"."==B?C:" "==C?"$":"."==C?"*":C,"#"===B||"#*$".includes(C)&&!" .".includes(B)||(M[y][x]=a,M[y+oy][x+ox]=b,Y[x+2*ox]=c,y+=oy,x+=ox));P.textContent=M.map(d=>d.join("")).join("\n")};M=P.textContent.split("\n").map(l=>l.split(""));x=3;y=2;K(P)'><pre id=P style="font-size:3pc">#####
# #
#.$@#
#.$ #
#.$ #
#####
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment