summaryrefslogtreecommitdiff
path: root/w
diff options
context:
space:
mode:
authorn <@>2023-11-14 15:48:09 +0200
committern <@>2023-11-14 15:48:09 +0200
commit2f1552eceeae536519b734a8393e607df0279f0d (patch)
tree78cb62feb9df2b50576c5c89bed06e447c615db6 /w
parentffccf2e74dd96622c989e2c112b08776f9235374 (diff)
w/x/mines.k
Diffstat (limited to 'w')
-rw-r--r--w/k.js4
-rw-r--r--w/x/mines.k44
2 files changed, 24 insertions, 24 deletions
diff --git a/w/k.js b/w/k.js
index bdfa24d1..40d55fe0 100644
--- a/w/k.js
+++ b/w/k.js
@@ -85,8 +85,10 @@ hgr=_=>{if(g)return;doc.body.appendChild(cnv=doc.createElement('canvas'));g=cnv.
onresize();g.font='0.05px monospace';iid=setInterval(tick,tickPeriod);raf()
onkeydown=onkeyup=e=>g&&K('k'+e.type[3]+'@'+e.keyCode)
onkeypress=e=>{if(g){let c=e.charCode;K('kx@'+c);(c===10||c===13)&&K('kr@10');c===8&&K('kb@8')}}
+ cnv.oncontextmenu=_=>!1
cnv.onmousedown=cnv.onmouseup=cnv.onmousemove=
- e=>{const r=cnv.getBoundingClientRect();K('m'+e.type[5]+'@'+[e.clientX-r.x,e.clientY-r.y])}},
+ e=>{const r=cnv.getBoundingClientRect(),x=(e.clientX-r.x)/r.width,y=(e.clientY-r.y)/r.height
+ K('m'+e.type[5]+'@`xy`buttons!('+x+' '+y+';'+e.buttons+')')}},
txt=_=>{if(!g)return;cnv.parentNode.removeChild(cnv);clearInterval(iid);clearTimeout(tid);cancelAnimationFrame(aid)
cnv=g=iid=tid=aid=null}
diff --git a/w/x/mines.k b/w/x/mines.k
index 8f8ad655..4a3810a3 100644
--- a/w/x/mines.k
+++ b/w/x/mines.k
@@ -1,35 +1,33 @@
-D:10 10 / shape
-C:15 / number of bombs
-Ds:`c$"0"+!10
-W:1
-`prng@0
-N:(!*/2+D)+/:(~~:)#(2+D)/+,/,/:\:/2#,-1+!3
-gen:{-,/4(+|0N,)/D#@[2+&d;(-C)?-1+d:*/D;:;1]}
-fld:{@[x;&(~^x)&|/((~0>x)&~+/-1=n:x@N)@N;:;0]}
-cnt:{+/(+/-1=x@N;x)*~:\~0>x}
+D:10 10 /dimensions
+C:15 /bomb count
+W:1 /0=lose 1=win
+`prng 0
+N:(!*/2+D)+/:(~~:)#(2+D)/+,/,/:\:/2#,-1+!3 /neighbour cells
+gen:{-,/4(+|0N,)/D#@[2+&d;(-C)?-1+d:*/D;:;1]} /generate a grid
+fld:{@[x;&(~^x)&|/((~0>x)&~+/-1=n:x N)N;:;0]} /one level of flood fill
+cnt:{+/(+/-1=x N;x)*~:\~0>x}
-tick:{G::cnt@fld@G;$[~+/-2=G;tick::(::);]}
-b:0.9*B:1%*D
-u:0.3 0.6
+tick:{G::cnt fld G;$[~+/-2=G;tick::(::);]}
+b:0.9*B:1%*D /inner(b) and outer(B) box size
+u:0.3 0.6 /position of win/lose message
-c:{"g.fillStyle='",($x),"';"} /set colour
-w:{"g.fillText(",(","/(,`k[x]),$y),");"} /write text
-q:{"g.fillRect(",(","/$(B*x),2#b),");"}
+c:{"g.fillStyle='",($x),"';"} /set colour
+w:{"g.fillText(",(","/(,`j x),$y),");"} /write text
+q:{"g.fillRect(",(","/$(B*x),2#b),");"} /draw box
-md:{ $[~tick;:tick
- -1~G[i::(2+D)/1+I::p:_(x%e)%B]; [W::0;tick::(::)]
- -2~G[i];G::cnt@@[G;i;:;0]]}
+md:{$[~tick;:tick
+ -1~G[i::(2+D)/1+I::_x[`xy]%B]; [W::0;tick::(::)]
+ -2~G[i];G::cnt@@[G;i;:;0]]}
-d:{$[0>i:G[(2+D)/1+x];(c`green;q x);(c`blue;q x;c`white;w[Ds@i;B*u+x])]}
+d:{$[0>i:G[(2+D)/1+x];(c`green;q x);(c`blue;q x;c`white;w[(~i)_$i;B*u+x])]}
draw:{cc:d'+!D;m:"you ",("lost";"won")W
`js@,//("g.clearRect(0,0,1,1);";d'+!D)
$[tick;:tick;~W;`js@,//(c`red;q I)]
b:(c`white),/:w["X"]'+B*u+-1+(2+D)\&-1=G
`js@,//(b;c`white;w[m;1%4 2])}
-`js"tickPeriod=150"; /tick[] will be called every half second
-`js"hgr()"; /enter graphics mode
-e:.`js@"[cnv.width,cnv.height]"
-G:gen[]
+`js"tickPeriod=150"; /how often tick[] will be called
+`js"hgr()"; /enter graphics mode
+G:gen[] /grid (including 1-cell padding around it)
/thanks @hermetian