Riichi/build/test_hand.js
2025-04-15 20:37:24 +02:00

1 line
No EOL
7.8 KiB
JavaScript

(()=>{"use strict";var t={274:(t,i,s)=>{s.d(i,{F:()=>e});class e{constructor(t,i,s){this.family=t,this.value=i,this.red=s,this.imgFront=new Image,this.imgBack=new Image,this.imgGray=new Image,this.img=new Image,this.imgSrc="",this.tilt=0,this.setImgSrc()}getFamily(){return this.family}getValue(){return this.value}isEqual(t,i){return this.family===t&&this.value===i}isRed(){return this.red}compare(t){return this.family!==t.family?this.family<t.family?-1:1:this.value!==t.value?this.value<t.value?-1:1:0}isLessThan(t){return this.family<t.family||this.family===t.family&&this.value<=t.value}setTilt(){this.tilt=.04*(1-2*Math.random())}drawTile(t,i,s,e,r=!1,l=0,n=!1,o=!0){const h=75*e,a=100*e,u=h/2,m=a/2;t.save(),t.translate(i+u,s+m),t.rotate(l+(o?this.tilt:0));const c=-.92*h/2,g=-.92*a/2;if(t.drawImage(this.imgGray,c,g,h,a),r)t.drawImage(this.imgBack,-u,-m,h,a);else{t.drawImage(this.imgFront,-u,-m,h,a);const i=.9,s=h*i,r=a*i,l=-68*e/2,o=-90*e/2;t.drawImage(this.img,l,o,s,r),n&&t.drawImage(this.imgGray,-u,-m,h,a)}t.restore()}cleanup(){[this.imgFront,this.imgBack,this.imgGray,this.img].forEach((t=>{t.onload=null,t.onerror=null}))}preloadImg(){return t=this,i=void 0,e=function*(){const t=[{img:this.imgFront,src:"img/Regular/Front.svg"},{img:this.imgBack,src:"img/Regular/Back.svg"},{img:this.imgGray,src:"img/Regular/Gray.svg"},{img:this.img,src:this.imgSrc}];yield Promise.all(t.map((({img:t,src:i})=>this.loadImg(t,i))))},new((s=void 0)||(s=Promise))((function(r,l){function n(t){try{h(e.next(t))}catch(t){l(t)}}function o(t){try{h(e.throw(t))}catch(t){l(t)}}function h(t){var i;t.done?r(t.value):(i=t.value,i instanceof s?i:new s((function(t){t(i)}))).then(n,o)}h((e=e.apply(t,i||[])).next())}));var t,i,s,e}loadImg(t,i){return new Promise(((s,e)=>{t.onload=()=>s(),t.onerror=()=>e(),t.src=i}))}setImgSrc(){if(this.imgSrc="img/Regular/",this.family<=3){const t=["","Man","Pin","Sou"];this.imgSrc+=t[this.family]+String(this.value),this.red&&(this.imgSrc+="-Dora")}else if(4===this.family){const t=["","Ton","Nan","Shaa","Pei"];this.imgSrc+=t[this.value]}else if(5===this.family){const t=["","Chun","Hatsu","Haku"];this.imgSrc+=t[this.value]}this.imgSrc+=".svg"}}},305:(t,i,s)=>{function e(t,i){return t?(console.log("%c[SUCCES] "+i,"color: green"),1):(console.log("%c[ECHEC] "+i,"color: red"),0)}s.d(i,{v:()=>e})},879:(t,i,s)=>{s.d(i,{Y:()=>e});class e{constructor(t,i,s){this.tiles=t,this.stolenFrom=i,this.belongsTo=s}push(t){this.tiles.push(t)}pop(){return this.tiles.pop()}getTiles(){return this.tiles}compare(t){const i=this.tiles[0].compare(t.tiles[0]);return 0!==i?i:this.tiles[1].compare(t.tiles[1])}drawGroup(t,i,s,e,r,l,n){t.save(),t.translate(525,525),t.rotate(l),t.translate(-525,-525);const o=75*r,h=90*r,a=25*r/2,u=(this.belongsTo-this.stolenFrom-1+4)%4,m=void 0===n?-1:n.getFamily(),c=void 0===n?0:n.getValue(),g=(i,s,e,l)=>{i.drawTile(t,s,e,r,!1,l,i.isEqual(m,c))},d=Math.PI/2;switch(u){case 0:g(this.tiles[0],i,s+a,d),g(this.tiles[1],i+h,s,0),g(this.tiles[2],i+h+o+e*r,s,0);break;case 1:g(this.tiles[0],i,s,0),g(this.tiles[1],i+h,s+a,-d),g(this.tiles[2],i+h+o+3*e*r,s,0);break;case 2:g(this.tiles[0],i,s,0),g(this.tiles[1],i+o+e*r,s,0),g(this.tiles[2],i+h+o+e*r,s+a,-d);break;default:console.error(`Position non prise en charge: ${u}`)}t.restore()}}},959:(t,i,s)=>{s.d(i,{U:()=>f});var e=s(274),r=s(879);const l="m",n=1,o="p",h=2,a="s",u=3,m="w",c=4,g="d",d=5;class p{constructor(t){this.tiles=t.map((t=>new e.F(t.getFamily(),t.getValue(),t.isRed()))),this.sort()}sort(){this.tiles.sort(((t,i)=>t.isLessThan(i)?-1:1))}find(t,i){const s=this.findTileIndex(t,i);if(-1!==s){[this.tiles[s],this.tiles[0]]=[this.tiles[0],this.tiles[s]];const t=this.tiles.shift();return this.sort(),t}}findTileIndex(t,i){return this.tiles.findIndex((s=>s.getFamily()===t&&s.getValue()===i))}count(t,i){return this.tiles.filter((s=>s.getFamily()===t&&s.getValue()===i)).length}findGroups(t=!1){if(0===this.tiles.length)return[];const i=this.tiles.pop(),s=i.getFamily(),e=i.getValue();if(this.count(s,e)>=1&&!t){const t=this.tryFormPair(i);if(t)return t}if(this.count(s,e)>=2){const s=this.tryFormTriplet(i,t);if(s)return s}if(s<=3){const r=this.count(s,e-1)>0,l=this.count(s,e-2)>0;if(r&&l){const s=this.tryFormSequence(i,t);if(s)return s}}this.tiles.push(i),this.sort()}tryFormPair(t){const i=this.find(t.getFamily(),t.getValue()),s=this.findGroups(!0);if(this.tiles.push(i),this.sort(),void 0!==s)return this.tiles.push(t),this.sort(),s.push(new r.Y([t,i],0,0)),s}tryFormTriplet(t,i){const s=this.find(t.getFamily(),t.getValue()),e=this.find(t.getFamily(),t.getValue()),l=this.findGroups(i);if(this.tiles.push(s),this.tiles.push(e),this.sort(),void 0!==l)return l.push(new r.Y([t,s,e],0,0)),this.tiles.push(t),this.sort(),l}tryFormSequence(t,i){const s=this.find(t.getFamily(),t.getValue()-1),e=this.find(t.getFamily(),t.getValue()-2),l=this.findGroups(i);if(this.tiles.push(s),this.tiles.push(e),this.sort(),void 0!==l)return l.push(new r.Y([e,s,t],0,0)),this.tiles.push(t),this.sort(),l}}class f{constructor(t=""){this.isolate=!1,this.tiles=[],this.initializeFromString(t)}initializeFromString(t){for(let i=0;i<t.length-1;i++){const s=t.substring(i,i+2),e=s[0],r=Number(s[1]);this.isValidTileCode(e,r)&&this.addTileFromCode(e,r)}}isValidTileCode(t,i){return t===l||t===o||t===a||t===m||t===g}addTileFromCode(t,i){const s={[l]:n,[o]:h,[a]:u,[m]:c,[g]:d}[t];void 0!==s&&this.tiles.push(new e.F(s,i,!1))}getTiles(){return this.tiles}length(){return this.tiles.length}push(t){this.tiles.push(t)}pop(){return this.tiles.pop()}find(t,i){const s=this.findTileIndex(t,i);if(-1!==s){[this.tiles[s],this.tiles[0]]=[this.tiles[0],this.tiles[s]];const t=this.tiles.shift();return this.sort(),t}}findTileIndex(t,i){return this.tiles.findIndex((s=>s.getFamily()===t&&s.getValue()===i))}eject(t){if(t<0||t>=this.tiles.length)throw new Error("Invalid tile index");[this.tiles[0],this.tiles[t]]=[this.tiles[t],this.tiles[0]];const i=this.tiles.shift();return this.sort(),i}get(t){if(!(void 0===t||t<0||t>=this.tiles.length))return this.tiles[t]}sort(){this.tiles.sort(((t,i)=>t.isLessThan(i)?-1:1))}count(t,i){return this.tiles.filter((s=>s.getFamily()===t&&s.getValue()===i)).length}toGroup(t=!1){return new p(this.tiles).findGroups(t)}drawHand(t,i,s,e,r,l=void 0,n=!1,o=0){const h=(75+e)*r,a=Math.cos(o)*h,u=Math.sin(o)*h;for(let e=0;e<this.tiles.length;e++){const h=e===this.tiles.length-1&&this.isolate?10:0;let m=i+e*a+h*r*Math.cos(o),c=s+e*u+h*r*Math.sin(o);e===l&&(m+=25*r*Math.sin(o),c-=25*r*Math.cos(o)),this.tiles[e].drawTile(t,m,c,r,n,o)}}preload(){return t=this,i=void 0,e=function*(){yield Promise.all(this.tiles.map((t=>t.preloadImg())))},new((s=void 0)||(s=Promise))((function(r,l){function n(t){try{h(e.next(t))}catch(t){l(t)}}function o(t){try{h(e.throw(t))}catch(t){l(t)}}function h(t){var i;t.done?r(t.value):(i=t.value,i instanceof s?i:new s((function(t){t(i)}))).then(n,o)}h((e=e.apply(t,i||[])).next())}));var t,i,s,e}cleanup(){this.tiles.forEach((t=>t.cleanup())),this.tiles=[]}}}},i={};function s(e){var r=i[e];if(void 0!==r)return r.exports;var l=i[e]={exports:{}};return t[e](l,l.exports,s),l.exports}s.d=(t,i)=>{for(var e in i)s.o(i,e)&&!s.o(t,e)&&Object.defineProperty(t,e,{enumerable:!0,get:i[e]})},s.o=(t,i)=>Object.prototype.hasOwnProperty.call(t,i);var e,r,l,n,o,h=s(959),a=s(305);let u=new h.U("s1s1"),m=new h.U("m1m2m3"),c=new h.U("m2m2m2 p1p1"),g=new h.U("m1m2m3 p1p2p3 s1s2s3 m9m9m9 p9p9"),d=new h.U("m2m2m2 p1p1p1"),p=0,f=0;p+=(0,a.v)(1===(null===(e=u.toGroup())||void 0===e?void 0:e.length),"s11 has 1 group"),p+=(0,a.v)(1===(null===(r=m.toGroup())||void 0===r?void 0:r.length),"m123 has 1 group"),p+=(0,a.v)(2===(null===(l=c.toGroup())||void 0===l?void 0:l.length),"m222 p11 has 2 groups"),(0,a.v)(5===(null===(n=g.toGroup())||void 0===n?void 0:n.length),"m123 p123 s123 m999 p99 has 5 groups"),p+=(0,a.v)(2===(null===(o=d.toGroup())||void 0===o?void 0:o.length),"m222 p111 has 2 groups"),f+=4,console.log("Succès: "+p.toString()+"/"+f.toString())})();