-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1 lines (1 loc) · 20.1 KB
/
index.js
1
var C=0,d=class{#e;#t;#i;#n;constructor(){C++}get pending(){return this.#e===void 0}#s(e){try{e(this.#e,this.#t)}catch(t){F.emit(t)}}onSettle(e){if(this.#e!==void 0)return this.#s(e);if(this.#n===void 0)this.#i===void 0?this.#i=e:this.#n=e;else if(this.#i===void 0)this.#n.push(e);else{let t=this.#n;this.#n=[this.#i,t,e],this.#i=void 0}}settle(e,t){if(!(this.#e!==void 0||e===void 0))if(this.#e=e,this.#t=t,C--,this.#i!==void 0)this.#s(this.#i),this.#i=void 0,this.#n!==void 0&&(this.#s(this.#n),this.#n=void 0);else{let n=this.#n;if(n!==void 0){for(;n.length!=0;)this.#s(n.shift());this.#n=void 0}}}then(e,t){let n=new d,i=this;return n.onSettle((r,l)=>{i.settle(r,l)}),this.onSettle((r,l)=>{let u=r=="resolved"?e:r=="rejected"?t:null;if(u==null)n.settle(r,l);else try{let a=u(l);a!=null&&typeof a.then=="function"?(i=J(a),i.onSettle(n.settle.bind(n))):n.settle("resolved",a)}catch(a){n.settle("rejected",a)}}),n}cancel(){this.settle("cancelled")}*[Symbol.iterator](){return yield this}},D=Symbol(),w=class{#e;#t;#i;#n;#s;get state(){return this.#s}get result(){return this.#t}constructor(e){this.#e=e,this.#s="ready"}start(){if(this.#s!="ready")return this;this.#s="running";let e=y;y=this;try{typeof this.#e=="function"&&(this.#e=this.#e()),this.#r(this.#e.next())}catch(t){this.#r({done:"faulted",value:t})}finally{y=e}return this}abort(){if(this.#s=="ready")this.#r({done:"aborted"});else if(this.#s=="running"){let e=this.#t;if(e.pending)e.cancel();else throw new TypeError("Fiber is already running")}}#o(e,t){this.#s=="finished"?e.settle("resolved",this.#t):t!==void 0?e.settle("resolved",void 0):this.#s=="faulted"?e.settle("rejected",this.#t):e.cancel()}#u=(e,t)=>{let n=y;y=this;try{if(e=="resolved")this.#r(this.#e.next(t));else if(e=="rejected")this.#r(this.#e.throw(t));else{for(;;){let{done:i,value:r}=this.#e.return();if(i)break;if(r!=null&&typeof r.then=="function"&&typeof r.cancel=="function")try{r.cancel()}catch(l){F.emit(l)}}this.#r({done:"aborted"})}}catch(i){this.#r({done:"faulted",value:i})}finally{y=n}};#l(){let e=this.#s=="faulted";if(this.#i!==void 0){let t=this.#n;e&&=t===!1,this.#o(this.#i,t)}else if(this.#n!==void 0){for(let[t,n]of this.#n)e&&=n===!1,this.#o(t,n);this.#n=void 0}e&&F.emit(this.#t)}#r({done:e,value:t}){if(e)this.#t=t,this.#s=e===!0?"finished":e,this.#e=void 0,this.#l();else try{if(t==null||typeof t.then!="function")this.#r(this.#e.next(t));else try{let n=J(t);this.#t=n,n.onSettle(this.#u)}catch(n){this.#r(this.#e.throw(n))}}catch(n){this.#r({done:"faulted",value:n})}}join(e,t){let n=new d;return this.#s=="running"||this.#s=="ready"?(this.#i===void 0?this.#n===void 0?(this.#i=n,this.#n=e):this.#n.set(n,e):(this.#n=new Map().set(this.#i,this.#n).set(n,e),this.#i=void 0),n.onSettle(()=>{this.#i===void 0?this.#n.delete(n):(this.#i=void 0,this.#n=void 0),t===D&&this.abort()})):this.#o(n,e),n}},c=class{#e=new Map;#t=0;#i=!1;get size(){return this.#e.size}add(e,t=!1){if(this.#e.has(e))return;let n=++this.#t;this.#e.set(e,t?-n:n)}remove(e){this.#e.delete(e)}interrupt(){this.#i=!0}emit=e=>{let t=this.#i;this.#i=!1;let n=this.#t;for(let[i,r]of this.#e){if(this.#i||r>n||-r>n)break;try{i(e)}catch(l){this!==F&&F.emit(l)}finally{r<0&&this.remove(i)}}this.#i=t}},_=function*(){},R=new w(_),y=R;R.start();var F=new c,v=class{#e;#t=R;get fiber(){return this.#t}constructor(e){this.#e=e}start(...e){let t=typeof this.#e=="function"?this.#e(...e):e[0];this.#t.abort(),this.#t=new w(t),this.#t.start()}startJoin(e,t=!0){if(y==this.#t)return typeof e=="function"?e():e;{this.#t.abort(),this.#t=new w(e);let n=this.#t,i=n.join(t,D)[Symbol.iterator]();return n.start(),i}}get running(){return this.#t.state=="running"}abort(){y!=this.#t&&this.#t.abort()}};function G(s,e){let t=new d;return t.settle(s,e),t}function g(s,e){let t=new w(s),n=t.join(e,D);return t.start(),n}function J(s){if(s instanceof d)return s;let e=new d,t=!1;return s.then(n=>{t=!0,e.settle("resolved",n)},n=>{t=!0,e.settle("rejected",n)}),typeof s.cancel=="function"&&e.onSettle(()=>{t||s.cancel()}),e}function p(...s){let e=new d,t=e.settle.bind(e);for(let n of s)n.onSettle(t);return e.onSettle(()=>{for(let n of s)n.cancel()}),e}function f(s){let e=new d,t=!1;if(typeof s=="number"){let n=setTimeout(()=>{t=!0,e.settle("resolved")},s);e.onSettle(()=>{t||clearTimeout(n)})}else if(s==="micro")queueMicrotask(()=>{e.settle("resolved")});else if(s==="frame"){let n=requestAnimationFrame(i=>{t=!0,e.settle("resolved",i)});e.onSettle(()=>{t||cancelAnimationFrame(n)})}return e}function z(s,e,t,n,i){t===null?(e.add(n),s.onSettle(()=>e.remove(n))):typeof e.removeEventListener=="function"?(e.addEventListener(t,n,i),s.onSettle(()=>e.removeEventListener(t,n,i))):(e.on(t,n),s.onSettle(()=>e.off(t,n)))}function m(s,e,t,n){typeof e!="string"&&(t=e,e=null);let i=new d;return z(i,s,e,t,n),i}function o(s,e,t,n){typeof e!="string"&&(t=e,e=null);let i=new d,r=typeof t=="function";return z(i,s,e,l=>{try{if(r&&!t(l))return;i.settle("resolved",l)}catch(u){i.settle("rejected",u)}},n),i}var H=class extends c{#e=!1;reset(){this.#e=!1}set(){this.#e||(this.#e=!0,this.emit())}},b=class extends c{#e=new Set;#t=G("resolved");reset(){this.#t.cancel(),this.#e.clear()}push(e){this.#t.pending||(this.#t=f("micro").then(()=>{this.emit(this.#e),this.#e.clear()})),this.#e.add(e)}};var T=30,P=new Image;P.src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeAAAAAeCAYAAAD0O81IAAAAAXNSR0IArs4c6Q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jz95HDaAQ3iYVVOmlROH/YWD/3Db48vfxu563ka0zA5BG2FFRhkkgIsS7zcMvqIgEsI+ezdDZ2en8JENYHO+cNhAYnwQPDjNBpIHxtNxqW1nHcby4FQXAOOEjZlb4sTtgVzFS1EuxsflHpivr6/wgmM/P+fbQow++vTpI1QnwVA0HkMTEoxWwSRPBPCphJ7tPeitGnlUj328R1yeszi7CGCl8UXbe3Kg1yse8vHV0uuJTvm1t4peG8BGjqqyLBsM/G2MGmw7m2dnKQSNxaNxYzFLEWl3mtfV1aUY/sTamvHx8Zq1Nd3RR16DEHY4HJYDmIQveqk8js7JxIu48zZKADZ7fEUAe4teG8A87mZXmTYY+NvYBrA5NhbtLAAY1wbR+1XyUHk2JygoSEjnDgsLA5/u90ry1IeyrfKAyff59t2zidtarGC/hxZLZkwv2yes/Zo9vghgb9JrA5j3k2t7Zvwt7NRgf9Exx9JSCLq6uhrwn9lHdHQ04D8zD8sB3B0a5t7nbggjgK0YXwSwN+nlPp62AtsCtgVuOwv8Hw29grKSAvDPAAAAAElFTkSuQmCC";await o(P,"load");var L=class{get tilesToReveal(){return this.#o}get minesToFlag(){return this.#u}get size(){return this.#n.length}#e;#t;#i;#n;#s;#o;#u;#l;#r;#h;mouseEvents;stateChanged;constructor(e,t,n,i){this.#e=t,this.#t=n,this.#i=i,this.#l=e,this.stateChanged=new b,this.mouseEvents=new c;let r=l=>{let{clientX:u,clientY:a,type:U,button:W}=l,{top:K,left:I}=this.#l.getBoundingClientRect(),V=Math.floor((u-I)/T),Z=Math.floor((a-K)/T);this.mouseEvents.emit({x:V,y:Z,type:U,button:W})};this.#h=p(m(this.#l,"contextmenu",l=>l.preventDefault()),m(this.#l,"mousedown",r),m(this.#l,"mouseup",r)),this.#r=new Set,this.reset()}resize(e,t,n){this.#e=e,this.#t=t,this.#i=n,this.reset()}reset(){this.#n=new Array(this.#e*this.#t).fill(0),this.#s=new Array(this.#e*this.#t).fill("default");let e=this.#n.map((n,i)=>i);for(let n=0;n<this.#i;n++){let i=Math.random()*e.length|0,r=e[i];e[i]=e.at(-1),e.pop(),this.#n[r]=-100;for(let l of this.getAdjacentTileIndexes(r))this.#n[l]+=1}this.#l.width=this.#e*T,this.#l.height=this.#t*T;let t=this.#l.getContext("2d");for(let n=0;n<this.#n.length;n++)this.#d(n,t);this.#o=this.#n.length-this.#i,this.#u=this.#i,this.stateChanged.push("mines")}dispose(){this.#h.cancel(),this.#f.cancel(),this.stateChanged.reset(),this.#n=void 0,this.#s=void 0}getMines(e){return this.#n[e]}getTile(e){return this.#s[e]}setTile(e,t){let n=this.#s[e];n!==t&&(this.#s[e]=t,this.tileUpdated(e,n))}setTiles(e,t){for(let n of e)this.setTile(n,t)}revealAll(){for(let e=0;e<this.#n.length;e++)this.getMines(e)<0?this.getTile(e)=="cleared"?this.setTile(e,"detonated"):this.getTile(e)!="flagged"&&this.setTile(e,"mine"):this.getTile(e)=="flagged"&&this.setTile(e,"wrong")}#f=G("resolved");tileUpdated(e,t){let n=this.getTile(e)==="cleared",i=this.getTile(e)==="flagged";n!==(t==="cleared")&&(this.#o+=n?-1:1),i!==(t==="flagged")&&(this.#u+=i?-1:1,this.stateChanged.push("mines")),this.#f.pending||(this.#f=f("micro").then(()=>{let r=this.#l.getContext("2d");for(let l of this.#r)this.#d(l,r);this.#r.clear()})),this.#r.add(e)}getAdjacentTileIndexes(e){let t=[],n=e/this.#e|0,i=e%this.#e;for(let r=-1;r<=1;r++)for(let l=-1;l<=1;l++){if(r==0&&l==0)continue;let u=i+l,a=n+r;u<0||a<0||u>=this.#e||a>=this.#t||t.push(this.getTileIndex(u,a))}return t}getTileIndex(e,t){return this.#e*t+e}#a(e){switch(this.getTile(e)){case"pushed":return 1;case"detonated":return 2;case"wrong":return 3;case"question":return 4;case"flagged":return 5;case"mine":return 6;case"cleared":return 7+this.getMines(e);default:return 0}}#d(e,t){let n=T*this.#a(e),i=T*(e/this.#e|0),r=T*(e%this.#e);t.drawImage(P,n,0,T,T,r,i,T,T)}};var M=class{#e;stateChanged;firstAction;#t;#i;#n;constructor(e,t,n,i){this.stateChanged=new b,this.firstAction=new H,this.#e=new L(e,t,n,i),this.#e.stateChanged.add(this.stateChanged.emit),this.#t="pending",this.#i="play",this.#n=new v(()=>this.#o()),this.#n.start()}get minesToFlag(){return this.#e.minesToFlag}get result(){return this.#t}set result(e){this.#t!=e&&(this.#t=e,this.stateChanged.push("result"))}get mode(){return this.#i}set mode(e){this.#i!=e&&(this.#i=e,this.stateChanged.push("mode"))}dispose(){this.#n.abort(),this.#e.dispose()}restart(e,t,n){this.#n.abort(),this.#e.resize(e,t,n),this.mode="play",this.#n.start()}#s=new c;switchMode(e){this.result=="pending"?this.#s.emit(e):(this.#n.abort(),this.#e.reset(),this.mode="auto",this.#n.start())}*#o(){try{for(this.result="pending",this.firstAction.reset();;)if(this.mode=="help")this.mode=yield*p(o(this.#s,e=>e!="help"),o(this.#e.mouseEvents).then(()=>"play"),g(this.#r()).then(()=>"play"));else if(this.mode=="auto"){if(this.mode=yield*p(o(this.#s,e=>e!="auto"),o(this.#e.mouseEvents).then(()=>"play"),g(function*(e){for(;;)try{e.firstAction.reset(),yield*e.#T()}catch(t){e.#e.revealAll(),e.result=t,yield f(2e3),e.#e.reset(),e.result="pending"}}(this))),this.result!="pending")return}else this.mode=="play"&&(this.mode=yield*p(o(this.#s,e=>e!="play"),g(this.#d())))}catch(e){this.#e.revealAll(),this.result=e}}*#u(e){let t=this.#e;if(t.getTile(e)=="cleared")return;let n=new Set().add(e);if(t.setTile(e,"cleared"),t.getMines(e)<0)throw"lost";let i=n.size,r=0,l=!1;for(let u of n){if(!l&&r++>=i){i=n.size;try{try{l=!0,yield f(30),l=!1}finally{if(l)throw null}}catch{}}t.getMines(u)>0||t.getAdjacentTileIndexes(u).forEach(a=>{t.setTile(a,"cleared"),n.add(a)})}l&&(yield null)}*#l(e){let t=this.#e;try{for(;;)t.setTiles(e,"question"),yield f(300),t.setTiles(e,"default"),yield f(300)}finally{t.setTiles(e,"default")}}*#r(){this.firstAction.set();let e=this.#e,t=!0;for(;t;){t=!1;for(let n=0;n<e.size;n++){if(e.getTile(n)!="cleared"||e.getMines(n)==0)continue;let{empty:i,flagged:r,dubious:l}=this.#a(n);if(i.length+l.length!=0&&i.length+l.length+r.length==e.getMines(n))t=!0,e.setTiles(i,"flagged"),e.setTiles(l,"flagged"),yield f(30);else if(i.length!=0&&l.length==0&&r.length==e.getMines(n)){t=!0;for(let u of i)if(yield*this.#u(u),e.tilesToReveal==0)throw"won";yield f(30)}}}}*#h(e){let t=this.#e;if(t.getTile(e)=="default"){t.setTile(e,"pushed");let n;try{n=yield o(t.mouseEvents)}finally{t.setTile(e,"default")}if(n.type=="mouseup"&&n.button==0&&(yield*this.#u(e),t.tilesToReveal==0))throw"won"}else if(t.getTile(e)=="cleared"&&t.getMines(e)>0){let{empty:n,flagged:i,dubious:r}=this.#a(e);if(n.length!=0)if(r.length==0&&i.length==t.getMines(e)){t.setTiles(n,"pushed");let l;try{l=yield o(t.mouseEvents)}finally{t.setTiles(n,"default")}if(l.type=="mouseup"&&l.button==0){for(let u of n)if(yield*this.#u(u),t.tilesToReveal==0)throw"won"}}else yield p(o(t.mouseEvents),g(this.#l(n)))}}*#f(e){let t=this.#e;if(t.getTile(e)=="default")t.setTile(e,"flagged");else if(t.getTile(e)=="flagged")t.setTile(e,"question");else if(t.getTile(e)=="question")t.setTile(e,"default");else if(t.getTile(e)=="cleared"&&t.getMines(e)>0){let{empty:n,flagged:i,dubious:r}=this.#a(e);r.length+n.length+i.length==t.getMines(e)?(t.setTiles(n,"flagged"),t.setTiles(r,"flagged")):yield p(o(t.mouseEvents),g(this.#l(n)))}}#a(e){let t=this.#e,n=t.getAdjacentTileIndexes(e),i=n.filter(u=>t.getTile(u)=="default"),r=n.filter(u=>t.getTile(u)=="flagged"),l=n.filter(u=>t.getTile(u)=="question");return{empty:i,flagged:r,dubious:l}}*#d(){let e=this.#e;if(e.tilesToReveal==0)throw"won";for(;;){let t=yield*o(e.mouseEvents,i=>i.type=="mousedown");this.firstAction.set();let n=e.getTileIndex(t.x,t.y);t.button==0?yield*this.#h(n):t.button==2&&(yield*this.#f(n))}}*#T(){let e=this.#e;if(e.tilesToReveal==0)throw"won";for(;;){yield*this.#r(),yield f(300);let t=[];for(let i=0;i<e.size;i++)e.getTile(i)=="default"&&t.push(i);if(t.length==0)throw"lost";let n;for(let i=0;i<100&&t.length;i++){let r=Math.random()*t.length|0;if(n=t[r],e.getAdjacentTileIndexes(n).every(u=>e.getTile(u)!=="cleared"))break;t[r]=t.at(-1),t.pop()}try{e.setTile(n,"pushed"),yield f(200)}finally{e.setTile(n,"default")}if(yield*this.#u(n),e.tilesToReveal==0)throw"won";yield*f(300)}}};F.add(console.warn);setInterval(()=>console.log(C),1e4);var $=20,ee=10,te=30,h=new M(document.querySelector("#mine-sweeper > canvas"),$,ee,te),E=document.querySelector("#restart"),N=document.querySelector("#helper"),Q=document.querySelector("#autoplay"),x=document.querySelector("#start"),X=document.querySelector("#menu-bar"),A=document.querySelector("#options-panel"),j=A.querySelector("#width-input"),B=A.querySelector("#height-input"),Y=A.querySelector("#mines-input"),ne=A.querySelector("#mine-density"),k=document.querySelector("#options-wrapper"),ie=document.querySelector("#flag-count"),q=document.querySelector("#stopwatch");g(function*(){let s=null;for(;;)(!s||s.has("mines"))&&(ie.textContent=String(h.minesToFlag)),(!s||s.has("result"))&&(X.classList.toggle("won",h.result=="won"),X.classList.toggle("lost",h.result=="lost")),(!s||s.has("mode"))&&(Q.classList.toggle("open",h.mode=="auto"),N.classList.toggle("open",h.mode=="help")),s=yield*o(h.stateChanged)});var O=new v(function*(){let s=0,e=Date.now();try{for(;;)q.textContent=String(s),s++,yield f(e+s*1e3-Date.now())}catch{q.textContent="0"}});function se(){O.running&&O.fiber.result.settle("rejected")}m(h.firstAction,()=>{O.start()});m(h.stateChanged,s=>{s.has("result")&&h.result!="pending"&&O.abort()});E.addEventListener("mousedown",s=>{k.classList.toggle("open"),E.classList.toggle("open")});g(function*(){for(j.value="20",B.value="10",Y.value="30";;)for(yield*o(E,"mousedown"),yield f(0),k.classList.add("open"),E.classList.add("open");;){let s=Number.parseInt(j.value),e=Number.parseInt(B.value),t=Number.parseInt(Y.value);x.disabled=!1,isNaN(s)||s<1||s>300?(j.classList.add("error"),x.disabled=!0):j.classList.remove("error"),isNaN(e)||e<1||e>300?(B.classList.add("error"),x.disabled=!0):B.classList.remove("error"),isNaN(t)||t<1||t>s*e?(Y.classList.add("error"),x.disabled=!0):Y.classList.remove("error"),ne.textContent=(s*e/t).toFixed(1);let n=yield*p(o(A,"input").then(()=>null),o(x,"mousedown").then(()=>!0),o(document.body,"mousedown",i=>!A.contains(i.target)).then(()=>!1));if(n!=null){k.classList.remove("open"),E.classList.remove("open"),n&&(h.restart(s,e,t),se());break}}});m(N,"mousedown",()=>{h.switchMode(h.mode=="help"?"play":"help")});m(Q,"mousedown",()=>{h.switchMode(h.mode=="auto"?"play":"auto")});