-
Notifications
You must be signed in to change notification settings - Fork 40
/
HuPaiMJ_32.h
357 lines (320 loc) · 9 KB
/
HuPaiMJ_32.h
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/********************************************************************
created: pinorr
file base: HuPaiMJ.h
author: pinorr Q 505971450
purpose: 麻将胡牌提示算法(第2版)
*********************************************************************/
#ifndef __HU_PAI_MJ_H__
#define __HU_PAI_MJ_H__
#include <map>
#include <set>
#include <unordered_set>
#include <string>
#include <vector>
#include <list>
#include <windows.h>
#include <iostream>
#include "DefineHuTip.h"
using namespace std;
#define MAX_KEY_NUM (MAX_VAL_NUM+1) //9+赖子
#define MAX_NAI_NUM 4 //赖子最大个数
#define BIT_VAL_NUM 3 //一个值占的bit数
#define BIT_VAL_FLAG 0x07 //
struct stCorOut
{
BYTE byCorType; //
bool byNum; // 总张数
BYTE byValNum[MAX_KEY_NUM]; //
};
inline void getIndexByKey2(int llVal, BYTE byIndex[MAX_KEY_NUM])
{
for (int i = 0; i<MAX_KEY_NUM; ++i)
{
byIndex[i] = (llVal >> (BIT_VAL_NUM*i))&BIT_VAL_FLAG;
}
}
inline int getKey2ByIndex(BYTE byIndex[MAX_KEY_NUM])
{
int nKey = 0;
for (int i = 0; i<MAX_KEY_NUM; ++i)
nKey |= (int)(byIndex[i] & BIT_VAL_FLAG) << (BIT_VAL_NUM*i);
return nKey;
}
inline bool isValidKey(int llVal)
{
BYTE byIndex[MAX_KEY_NUM] = {};
getIndexByKey2(llVal, byIndex);
if (byIndex[9] > MAX_NAI_NUM)
return false;
int nNum = 0;
for (int i = 0; i<MAX_KEY_NUM; ++i)
{
nNum += byIndex[i];
if (byIndex[i] > 4 || nNum > 14) //
return false;
}
return nNum > 0;
}
inline BYTE getNumByKey(int llVal)
{
BYTE byIndex[MAX_KEY_NUM] = {};
getIndexByKey2(llVal, byIndex);
BYTE nNum = 0;
for (int i = 0; i < MAX_KEY_NUM; ++i)
nNum += byIndex[i];
return nNum;
}
set<int> g_setSingle; //单个顺子+刻子 50个
set<int> g_setSingleFZ; //单个顺子+刻子 22个
set<int> g_setSingleJiang; //单个将 19个
set<int> g_setSingleJiangFZ; //单个将 15个
unordered_set<int> g_usetHuAll[15]; //
unordered_set<int> g_usetHuAllFZ[15]; // 风、字牌
class CHuPaiMJ
{
public:
CHuPaiMJ(){};
~CHuPaiMJ(){};
public:
static void TrainSingle() // 1.三张单独组合
{
BYTE byTemp[MAX_KEY_NUM] = {};
byTemp[9] = 3;
g_setSingle.insert(getKey2ByIndex(byTemp));
g_setSingleFZ.insert(getKey2ByIndex(byTemp));
// 1.1 刻子
for (int i = 0; i<MAX_VAL_NUM; ++i)
{
memset(byTemp, 0, MAX_KEY_NUM);
for (int n = 0; n<3; ++n)
{
byTemp[i] = 3 - n;
byTemp[9] = n;
g_setSingle.insert(getKey2ByIndex(byTemp));
if (i < 7) //风、字牌最多7张
g_setSingleFZ.insert(getKey2ByIndex(byTemp));
}
}
// 1.2 顺子 没赖子
for (int i = 0; i<MAX_VAL_NUM - 2; ++i)
{
memset(byTemp, 0, MAX_KEY_NUM);
byTemp[i] = 1;
byTemp[i + 1] = 1;
byTemp[i + 2] = 1;
g_setSingle.insert(getKey2ByIndex(byTemp));
}
// 1.2 顺子 1个赖子 (2个赖子时也就是刻子)
for (int i = 0; i<MAX_VAL_NUM - 2; ++i)
{
memset(byTemp, 0, MAX_KEY_NUM);
byTemp[i] = 1;
byTemp[i + 1] = 1;
byTemp[9] = 1;
g_setSingle.insert(getKey2ByIndex(byTemp));
memset(byTemp, 0, MAX_KEY_NUM);
byTemp[9] = 1;
byTemp[i + 1] = 1;
byTemp[i + 2] = 1;
g_setSingle.insert(getKey2ByIndex(byTemp));
memset(byTemp, 0, MAX_KEY_NUM);
byTemp[i] = 1;
byTemp[9] = 1;
byTemp[i + 2] = 1;
g_setSingle.insert(getKey2ByIndex(byTemp));
}
// 1.3 将牌
memset(byTemp, 0, MAX_KEY_NUM);
byTemp[9] = 2;
g_setSingleJiang.insert(getKey2ByIndex(byTemp));
g_setSingleJiangFZ.insert(getKey2ByIndex(byTemp));
for (int i = 0; i<MAX_VAL_NUM; ++i)
{
memset(byTemp, 0, MAX_KEY_NUM);
for (int n = 0; n<2; ++n)
{
byTemp[i] = 2 - n;
byTemp[9] = n;
g_setSingleJiang.insert(getKey2ByIndex(byTemp));
if (i < 7) //风、字牌最多7张
g_setSingleJiangFZ.insert(getKey2ByIndex(byTemp));
}
}
};
static void TrainAll()
{
TrainSingle();
int nAll = 0;
int nAllFZ = 0;
int nSingle[100] = {};
int nSingleFZ[50] = {};
set<int>::iterator iter = g_setSingle.begin();
for (; iter != g_setSingle.end(); ++iter)
nSingle[nAll++] = *iter;
iter = g_setSingleFZ.begin();
for (; iter != g_setSingleFZ.end(); ++iter)
nSingleFZ[nAllFZ++] = *iter;
int nAllJiang = 0;
int nAllJiangFZ = 0;
int nSingleJiang[20] = {};
int nSingleJiangKe[20] = {};
iter = g_setSingleJiang.begin();
for (; iter != g_setSingleJiang.end(); ++iter)
nSingleJiang[nAllJiang++] = *iter;
iter = g_setSingleJiangFZ.begin();
for (; iter != g_setSingleJiangFZ.end(); ++iter)
nSingleJiangKe[nAllJiangFZ++] = *iter;
for (int i1 = 0; i1<nAllFZ; ++i1)
{
g_usetHuAllFZ[getNumByKey(nSingleFZ[i1])].insert(nSingleFZ[i1]);
for (int i2 = i1; i2<nAllFZ; ++i2)
{
int nTemp = nSingleFZ[i1] + nSingleFZ[i2];
if (!isValidKey(nTemp))
continue;
g_usetHuAllFZ[getNumByKey(nTemp)].insert(nTemp);
for (int i3 = i2; i3<nAllFZ; ++i3)
{
int nTemp = nSingleFZ[i1] + nSingleFZ[i2] + nSingleFZ[i3];
if (!isValidKey(nTemp))
continue;
g_usetHuAllFZ[getNumByKey(nTemp)].insert(nTemp);
for (int i4 = i3; i4<nAllFZ; ++i4)
{
int nTemp = nSingleFZ[i1] + nSingleFZ[i2] + nSingleFZ[i3] + nSingleFZ[i4];
if (!isValidKey(nTemp))
continue;
g_usetHuAllFZ[getNumByKey(nTemp)].insert(nTemp);
}
}
}
}
for (int i = 0; i<nAllJiangFZ; ++i)
{
for (int j = 0; j < 15; ++j)
{
if (j % 3 != 0)
continue;
g_usetHuAllFZ[getNumByKey(nSingleJiangKe[i])].insert(nSingleJiangKe[i]);
unordered_set<int>::iterator iter_u = g_usetHuAllFZ[j].begin();
for (; iter_u != g_usetHuAllFZ[j].end(); ++iter_u)
{
int nTemp = nSingleJiangKe[i] + *iter_u;
if (isValidKey(nTemp))
g_usetHuAllFZ[getNumByKey(nTemp)].insert(nTemp);
}
}
}
cout << (int)sizeof(*(g_setSingleFZ.begin())) << endl;
// cout << "pino:" << dec << g_setSingleFZ.size() << endl;
// cout << "pino:" << dec << g_usetHuAllFZ.size() << endl;
// cout << "pino:" << dec << g_usetHuAllJiangFZ.size() << endl;
for (int i1 = 0; i1<nAll; ++i1)
{
g_usetHuAll[getNumByKey(nSingle[i1])].insert(nSingle[i1]);
for (int i2 = i1; i2<nAll; ++i2)
{
int nTemp = nSingle[i1] + nSingle[i2];
if (!isValidKey(nTemp))
continue;
g_usetHuAll[getNumByKey(nTemp)].insert(nTemp);
for (int i3 = i2; i3<nAll; ++i3)
{
int nTemp = nSingle[i1] + nSingle[i2] + nSingle[i3];
if (!isValidKey(nTemp))
continue;
g_usetHuAll[getNumByKey(nTemp)].insert(nTemp);
for (int i4 = i3; i4<nAll; ++i4)
{
int nTemp = nSingle[i1] + nSingle[i2] + nSingle[i3] + nSingle[i4];
if (!isValidKey(nTemp))
continue;
g_usetHuAll[getNumByKey(nTemp)].insert(nTemp);
}
}
}
}
for (int i = 0; i<nAllJiang; ++i)
{
for (int j = 0; j < 15; ++j)
{
if (j%3 != 0)
continue;
g_usetHuAll[getNumByKey(nSingleJiang[i])].insert(nSingleJiang[i]);
unordered_set<int>::iterator iter_u = g_usetHuAll[j].begin();
for (; iter_u != g_usetHuAll[j].end(); ++iter_u)
{
int nTemp = nSingleJiang[i] + *iter_u;
if (isValidKey(nTemp))
g_usetHuAll[getNumByKey(nTemp)].insert(nTemp);
}
}
}
cout << endl;
cout << "pino:" << dec << g_setSingle.size() << endl;
//cout << "pino:" << dec << g_usetHuAll.size() << endl;
//cout << "pino:" << dec << g_usetHuAllJiang.size() << endl;
}
static bool CheckCanHuSingle(stColorData &stColor, BYTE &byNaiNum, BYTE byNaiMax)
{
if (stColor.byNum == 0)
return true;
bool bSuc = false;
int nVal = getKey2ByIndex(stColor.byCount);
for (byNaiNum = 0; byNaiNum <= byNaiMax; ++byNaiNum)
{
BYTE byAll = stColor.byNum + byNaiNum;
BYTE byLeftNum = byAll % 3;
if (byLeftNum == 1)
continue;
int nKey = nVal + ((int)(byNaiNum & BIT_VAL_FLAG) << (BIT_VAL_NUM * 9));
if (stColor.byCorType == enColorMJ_FenZi)
bSuc = g_usetHuAllFZ[byAll].find(nKey) != g_usetHuAllFZ[byAll].end();
else
bSuc = g_usetHuAll[byAll].find(nKey) != g_usetHuAll[byAll].end();
if (bSuc)
return true;
}
byNaiNum = 0;
return false;
}
static bool CheckCanHu(stCardData &stData, BYTE byNaiIndex)
{
if (stData.byNum % 3 != 2)
return false;
int nNaiZiNum = 0;
if (byNaiIndex != INVALID_VAL)
{
nNaiZiNum = stData.byCardNum[byNaiIndex];
stData.byCardNum[byNaiIndex] = 0;
stData.byNum -= nNaiZiNum;
}
stColorData stColorTemp[enColorMJ_Max];
for (int cor = 0; cor < enColorMJ_Max; ++cor)
{
stColorTemp[cor].byCorType = cor;
int nAll = (cor == enColorMJ_FenZi) ? 7 : MAX_VAL_NUM;
if (cor*MAX_VAL_NUM + nAll <= MAX_TOTAL_TYPE)
{
memcpy(stColorTemp[cor].byCount, stData.byCardNum + cor*MAX_VAL_NUM, nAll);
for (int i = 0; i < nAll; ++i)
stColorTemp[cor].byNum += stColorTemp[cor].byCount[i];
}
}
BYTE byJiangNum = 0;
BYTE nNaiTry[4] = {};
for (int cor = 0; cor < enColorMJ_Max; ++cor)
{
if (stColorTemp[cor].byNum == 0)
continue;
if (!CheckCanHuSingle(stColorTemp[cor], nNaiTry[cor], nNaiZiNum))
return false;
nNaiZiNum -= nNaiTry[cor];
byJiangNum += (stColorTemp[cor].byNum + nNaiTry[cor]) == 2;
if (byJiangNum > nNaiZiNum + 1)
return false;
}
return true;
}
};
#endif //__CPLAYER_H__