JavaScript에서 마작 관련 함수를 작성하고 있습니다.이 마작 알고리즘의 속도를 높이는 데 도움이됩니다.
다음은 테스트 사례 코드입니다.
- 0 요소는 손 각 유형의 타일의 개수 인
- 요소를 손으로 1 (34)이 타일의 총수 인 : 손 마작
참고로, 배열로 표시되는
- 제 craks 다음 점 후 BAMS 후 바람, 최종적 용
대기를 찾는 함수가 실제로 느리게 실행됩니다. 어떻게 속도를 높일 수 있습니까?
// tiles are indexed as follows:
// 1..9 = 1 crak .. 9 crak
// 10..18 = 1 dot .. 9 dot
// 19..27 = 1 bam .. 9 bam
// 28..34 = east, south, west, north, white, green, red
var wall = new Array();
set_up_wall();
function set_up_wall() {
for (var i=1; i<=34; i++) wall[i] = 4;
wall[0]=136;
}
// draw tile from wall
function draw() {
var fudge = 1-(1e-14);
var n = Math.floor(Math.random()*wall[0]*fudge);
var i = 1;
while (n>=wall[i]) n-=wall[i++];
wall[i]--;
wall[0]--;
return i;
}
// get number of a tile (or 0 if honor)
// e.g. 8 bams = 8
function tilenum(i) {
if (i>27) return 0;
if (i%9==0) return 9;
return i%9;
}
// get suit of a tile (or 0 if honor)
function tilesuit(i) {
var eps = 1e-10;
return Math.ceil(i/9-eps)%4;
}
// is this a well-formed hand?
function well_formed(h) {
// this function is recursive
if (h[0]==2) return only_pairs(h);
if (h[0]==14) {
if (only_pairs(h)) return true;
if (thirteen_orphans(h)) return true;
}
if (h[0]%3 != 2) return false; // wrong no. of tiles in hand
// now we start splitting up the hand
// look for three of a kind
for (var i=1; i<=34; i++) {
if (h[i]>=3) {
// create new hand minus the three of a kind
hh = new Array();
for (var j=0; j<=34; j++) hh[j]=h[j];
hh[0]-=3;
hh[i]-=3;
if (well_formed(hh)) return true;
}
}
// look for a run of three
for (var i=1; i<=25; i++) {
if (tilenum(i)<=7) {
if (h[i]>=1 && h[i+1]>=1 && h[i+2]>=1) {
// create new hand minus the run
hh = new Array();
for (var j=0; j<=34; j++) hh[j]=h[j];
hh[0]-=3;
hh[i]--; hh[i+1]--; hh[i+2]--;
if (well_formed(hh)) return true;
}
}
}
// if we reach here, we have exhausted all possibilities
return false;
}
// is this hand all pairs?
function only_pairs(h) {
for (var i=1; i<=34; i++) if (h[i]==1 || h[i]>=3) return false;
return true;
}
// thirteen orphans?
function thirteen_orphans(h) {
var d=0;
var c=new Array(14, 1,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1);
for (var i=0; i<=34; i++) {
if (c[i]==0 && h[i]>0) return false;
if (h[i]!=c[i]) d++;
}
return d==1;
}
// this is inefficient
function waits(h) {
var w=new Array();
for (var j=0; j<=34; j++) w[j]=false;
if (h[0]%3!=1) return w; // wrong no. of tiles in hand
// so we don't destroy h
var hh = new Array();
for (var j=0; j<=34; j++) hh[j]=h[j];
for (var i=1; i<=34; i++) {
// add the tile we are trying to test
hh[0]++; hh[i]++;
if (hh[i]<5) { // exclude hands waiting for a nonexistent fifth tile
if (well_formed(hh)) {
w[0] = true;
w[i] = true;
}
}
hh[0]--; hh[i]--;
}
return w;
}
function tiles_to_string(t) { // strictly for testing purposes
var n;
var ss="";
var s = "x 1c 2c 3c 4c 5c 6c 7c 8c 9c 1d 2d 3d 4d 5d 6d 7d 8d 9d ";
s += "1b 2b 3b 4b 5b 6b 7b 8b 9b Ew Sw Ww Nw Wd Gd Rd";
s=s.split(" ");
for (var i=1; i<=34; i++) {
n=t[i]*1; // kludge
while (n--) ss+=(" "+s[i]);
}
return ss;
}
// tests
var x;
x = new Array(13, 0,0,0,0,0,1,2,2,2, 2,2,2,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0);
document.write("Hand: "+tiles_to_string(x)+"<br />");
document.write("Waits: "+tiles_to_string(waits(x))+"<br /><br />");
x = new Array(13, 1,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1);
document.write("Hand: "+tiles_to_string(x)+"<br />");
document.write("Waits: "+tiles_to_string(waits(x))+"<br /><br />");
x = new Array(13, 0,0,0,0,0,0,0,0,0, 3,1,1,1,1,1,1,1,3, 0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0);
document.write("Hand: "+tiles_to_string(x)+"<br />");
document.write("Waits: "+tiles_to_string(waits(x))+"<br /><br />");
x = new Array(13, 4,0,0,3,3,3,0,0,0, 0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0);
document.write("Hand: "+tiles_to_string(x)+"<br />");
document.write("Waits: "+tiles_to_string(waits(x))+"<br /><br />");
x = new Array(13, 0,0,4,3,3,3,0,0,0, 0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0);
document.write("Hand: "+tiles_to_string(x)+"<br />");
document.write("Waits: "+tiles_to_string(waits(x))+"<br /><br />");
우리가 mah-jong이 무엇인지, 벽, 대기 등과 같은 용어가 무엇인지 알지 못한다면 아마 더 나은 응답을 얻게 될 것입니다. 어쩌면 당신에게 기능적 요구 사항을 설명 할 수 있습니다. – RBarryYoung
두 개의 다른 의견 - well_formed 함수는 3 개 (pungs) 세트를 먼저 잡아서 손을 놓치게됩니다. 예를 들어, 같은 수트의 345, 456, 567은 즉시 5 초를 모두 잃게됩니다. 더구나, 당신은 kongs (4 개의 종류의) 여기에서 고려하지 않고있다. 그러나 아마이 마지막 것이 고의적입니까? –
kongs에 관해서는 : 나는 그들을 아직 다루지 않기로 결정했다. 그리고 어떤 경우 에나, * 선언 된 * kong이 우리의 목적을위한 손은 단지 11 개의 타일 (kong의 일부가 아닌 것들) 만 가진다. 두 번째 : 345, 456, 567과 마찬가지로 : 344555667과 손이 있다면, 먼저 555를 제거한 다음 344667을 확인합니다. 그러면 작동하지 않는다는 것을 알게 될 것입니다. 345를 제거하고, 455667을 남겨두고 작동하는지 확인하십시오. "퀸즈 퀸즈 (Eight Queens)"문제를 해결하는 표준 방식과 같습니다. –