-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathshanten.js
189 lines (153 loc) · 4.61 KB
/
shanten.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
let hand = new Array(38);
let completeSets;
let pair;
let partialSets;
let bestShanten;
let minimumShanten;
let hasGivenMinimum;
export {
// module.exports = {
calculateMinimumShanten,
calculateStandardShanten
}
function calculateMinimumShanten(handToCheck, minimumShanten = -2) {
let chiitoiShanten = calculateChiitoitsuShanten(handToCheck);
if (chiitoiShanten < 0) {
return chiitoiShanten;
}
let kokushiShanten = calculateKokushiShanten(handToCheck);
if (kokushiShanten < 3) {
return kokushiShanten;
}
let standardShanten = calculateStandardShanten(handToCheck, minimumShanten);
return Math.min(standardShanten, chiitoiShanten, kokushiShanten);
}
function calculateChiitoitsuShanten(handToCheck) {
hand = handToCheck.slice();
let pairCount = 0, uniqueTiles = 0;
for (let i = 1; i < hand.length; i++) {
if (hand[i] === 0) continue;
uniqueTiles++;
if (hand[i] >= 2) {
pairCount++;
}
}
let shanten = 6 - pairCount;
if (uniqueTiles < 7) {
shanten += 7 - uniqueTiles;
}
return shanten;
}
function calculateKokushiShanten(handToCheck) {
let uniqueTiles = 0;
let hasPair = 0;
for (let i = 1; i < handToCheck.length; i++) {
if (i % 10 === 1 || i % 10 === 9 || i > 30) {
if (handToCheck[i] !== 0) {
uniqueTiles++;
if (handToCheck[i] >= 2) {
hasPair = 1;
}
}
}
}
return 13 - uniqueTiles - hasPair;
}
function calculateStandardShanten(handToCheck, minimumShanten_ = -2) {
hand = handToCheck.slice();
// Initialize variables
hasGivenMinimum = true;
minimumShanten = minimumShanten_;
completeSets = 0;
pair = 0;
partialSets = 0;
bestShanten = 8;
if (minimumShanten_ == -2) {
hasGivenMinimum = false;
minimumShanten = -1;
}
// Loop through hand, removing all pair candidates and checking their shanten
for (let i = 1; i < hand.length; i++) {
if (hand[i] >= 2) {
pair++;
hand[i] -= 2;
removeCompletedSets(1);
hand[i] += 2;
pair--;
}
}
// Check shanten when there's nothing used as a pair
removeCompletedSets(1);
return bestShanten;
}
function removeCompletedSets(i) {
if (bestShanten <= minimumShanten) return;
// Skip to the next tile that exists in the hand.
for (; i < hand.length && hand[i] === 0; i++) { }
if (i >= hand.length) {
// We've gone through the whole hand, now check for partial sets.
removePotentialSets(1);
return;
}
// Pung
if (hand[i] >= 3) {
completeSets++;
hand[i] -= 3;
removeCompletedSets(i);
hand[i] += 3;
completeSets--;
}
// Chow
if (i < 30 && hand[i + 1] !== 0 && hand[i + 2] !== 0) {
completeSets++;
hand[i]--; hand[i + 1]--; hand[i + 2]--;
removeCompletedSets(i);
hand[i]++; hand[i + 1]++; hand[i + 2]++;
completeSets--;
}
// Check all alternative hand configurations
removeCompletedSets(i + 1);
}
function removePotentialSets(i) {
if (bestShanten <= minimumShanten) return;
if (hasGivenMinimum && completeSets < 3 - minimumShanten) return;
// Skip to the next tile that exists in the hand
for (; i < hand.length && hand[i] === 0; i++) { }
if (i >= hand.length) {
// We've checked everything. See if this shanten is better than the current best.
let currentShanten = 8 - (completeSets * 2) - partialSets - pair;
if (currentShanten < bestShanten) {
bestShanten = currentShanten;
}
return;
}
// A standard hand will only ever have four groups plus a pair.
if (completeSets + partialSets < 4) {
// Pair
if (hand[i] === 2) {
partialSets++;
hand[i] -= 2;
removePotentialSets(i);
hand[i] += 2;
partialSets--;
}
// Edge or Side wait protorun
if (i < 30 && hand[i + 1] !== 0) {
partialSets++;
hand[i]--; hand[i + 1]--;
removePotentialSets(i);
hand[i]++; hand[i + 1]++;
partialSets--;
}
// Closed wait protorun
if (i < 30 && i % 10 <= 8 && hand[i + 2] !== 0) {
partialSets++;
hand[i]--; hand[i + 2]--;
removePotentialSets(i);
hand[i]++; hand[i + 2]++;
partialSets--;
}
}
// Check all alternative hand configurations
removePotentialSets(i + 1);
}