-
Notifications
You must be signed in to change notification settings - Fork 1
/
patterns.h
467 lines (392 loc) · 11.5 KB
/
patterns.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#ifndef PATTERNS
#define PATTERNS
#include<iostream>
#include<cstdlib>
#include<map>
#include<string>
#include<cstdio>
#include<vector>
#include<queue>
#include<time.h>
#include<cmath>
#include<stack>
#include<typeinfo>
#define NUM_CARD 15
#define NUM_NUM 4
#define PAIR_LENGTH 2
#define TRI_LENGTH 3
#define QUA_LENGTH 4
#define TWO 12
#define joker 13
#define JOKER 14
using namespace std;
static double alpha = 100.0;
static double beta = 0.1;
double Power(int p){
return alpha * exp(-beta * p);
}
/** the pattern class that has the basic characteristic of a pattern**/
/** variables: String name: the name of the pattern
int len: the length of the pattern **/
class pattern{
private:
string name;
int len;
public:
pattern(string nam, int l){
this->name = nam;
this->len = l;
}
string getName() const{
return name;
}
int getSize() const{
return len;
}
void setSize(int length){
len = length;
}
virtual vector<int> toVec() = 0;
};
/** the class representing single pattern
variable: int num: the number value of the single card
constructor: (int number): the number value **/
class Single: public pattern{
int num;
public:
Single(int number):pattern("S", 1){
this->num = number;
}
int getNum() const{
return num;
}
bool operator >(const Single& s){
return num > s.getNum();
}
static bool ifExist(int card[], int index){
return card[index] > 0;
}
double getPow(const int counter[]){
int p = 0;
for (int i = num + 1; i != NUM_CARD; i++)
if(counter[i] > 0)
p++;
return Power(p);
}
vector<int> toVec(){
vector<int> vec;
vec.push_back(num);
return vec;
}
};
/** the class representing pair pattern
variable: int num: the number value of the pair
constructor: (int number): the number value **/
class Pair: public pattern{
int num;
public:
Pair(int number):pattern("P", PAIR_LENGTH){
this->num = number;
}
int getNum() const{
return num;
}
bool operator >(const Pair& p){
return num > p.getNum();
}
static bool ifExist(int card[], int index){
return card[index] > 1;
}
double getPow(const int counter[]){
int p = 0;
for (int i = num + 1; i != NUM_CARD; i++)
if(counter[i] >= PAIR_LENGTH)
p++;
return getSize() * Power(p);
}
vector<int> toVec(){
vector<int> vec;
vec.push_back(num);
vec.push_back(num);
return vec;
}
};
/** the class representing triple pattern
variable: int tri: the number value of the triple part
int appd: the number value of the appendix part
constructor: (int triple, int appendix, int length) **/
class Triple:public pattern{
int tri, appd;
public:
Triple(int triple, int appendix, int length):pattern("T", length){
this->tri = triple;
this-> appd = appendix;
}
int getTriple() const{
return tri;
}
int getAppendix() const{
return appd;
}
bool operator >(Triple& t){
if(this->getSize() != t.getSize())
return false;
else
return this->tri > t.getTriple();
}
static bool ifExist(int card[], int tri_index, int appd_index, int appd_length){
return (card[tri_index] >= TRI_LENGTH) && (card[appd_index] >= appd_length);
}
double getPow(const int counter[]){
int p = 0;
for (int i = tri + 1; i != NUM_CARD; i++)
if(counter[i] >= TRI_LENGTH)
p++;
return getSize() * Power(p);
}
vector<int> toVec(){
vector<int> vec;
for (int i = 0; i != TRI_LENGTH; i++)
vec.push_back(tri);
for (int i = 0; i != getSize() - TRI_LENGTH; i++)
vec.push_back(appd);
return vec;
}
};
/** the class representing straight pattern
variable: int st: the number value of the start number of a straight
constructor: (int start, int length): the number value and the length **/
class Straight:public pattern{
int st;
public:
Straight(int start, int length):pattern("ST", length){
this->st = start;
}
int getStart() const{
return st;
}
bool operator >(const Straight& st){
if(this->getSize() != st.getSize())
return false;
else
return this->st > st.getStart();
}
static bool ifExist(int card[], int start, int length){
for (int i = start; i != start + length; i++)
if (card[i] == 0)
return false;
return true;
}
double getPow(const int counter[]){
int p = 0;
for (int i = st + 1; i <= TWO - this->getSize(); i++){
bool sw = true;
for (int j = 0; j != this->getSize(); j++){
if(counter[i+j] == 0){
sw = false;
break;
}
}
if (sw)
p++;
}
return getSize() * Power(p);
}
vector<int> toVec(){
vector<int> vec;
for (int i = st; i != st + getSize(); i++)
vec.push_back(i);
return vec;
}
};
/** the class representing straight pairs pattern
variable: int start: the number value of the start card
constructor: (int start, int length): the number value and the length **/
class StraightPair:public pattern{
int st;
public:
StraightPair(int start, int length):pattern("SP", length){
this->st = start;
}
int getStart() const{
return st;
}
bool operator >(const StraightPair& sp){
if(this->getSize() != sp.getSize())
return false;
else
return this->st > sp.getStart();
}
static bool ifExist(int card[], int start, int length){
for (int i = start; i != start + length; i++)
if (card[i] < PAIR_LENGTH)
return false;
return true;
}
double getPow(const int counter[]){
int p = 0;
for (int i = st; i <= TWO - this->getSize(); i++){
bool sw = true;
for (int j = 0; j != this->getSize(); j++){
if(counter[i+j] < PAIR_LENGTH){
sw = false;
break;
}
}
if (sw)
p++;
}
return getSize() * 2 * Power(p);
}
vector<int> toVec(){
vector<int> vec;
for (int i = st; i != st + getSize(); i++){
vec.push_back(i);
vec.push_back(i);
}
return vec;
}
};
/** the class representing plane pattern. **/
class Plane:public pattern{
int st, tri_num;
vector<Triple> tri_set;
public:
Plane(int startTriple, int triples_num, vector<Triple> TripleSet, int length): pattern("PL", length){
this->st = startTriple;
this->tri_num = triples_num;
this->tri_set = TripleSet;
}
int getStartTriple() const{
return st;
}
vector<Triple> getTripleSet() const{
return tri_set;
}
bool operator >(const Plane& plane){
if (this->getSize() != plane.getSize())
return false;
else{
return this->st > plane.getStartTriple();
}
}
static bool ifExist(int card[], int st, int length){
for (int i = st; i != st + length; i++)
if (card[i] < TRI_LENGTH)
return false;
return true;
}
double getPow(const int counter[]){
int p = 0;
for (int i = st + 1; i < TWO - tri_num; i++){
if(counter[i] >= TRI_LENGTH){
for (int j = 1; j != tri_num; j++){
if (counter[i + j] < TRI_LENGTH)
break;
if (j == tri_num - 1)
p++;
}
}
}
return getSize() * Power(p);
}
vector<int> toVec(){
vector<int> vec;
for (int i = 0; i != tri_set.size(); i++){
vector<int> temp = tri_set[i].toVec();
for (int j = 0; j != temp.size(); j++)
vec.push_back(temp[j]);
}
return vec;
}
};
/** the class representing bomb pattern
variable: int num: the number value of the bomb
constructor: (int number, int length): the number value and length **/
class Bomb:public pattern{
int num;
public:
Bomb(int number, int length):pattern("B", length){
num = number;
}
int getNum() const{
return num;
}
bool operator >(const Bomb& b){
return num > b.getNum();
}
static bool ifExist(int card[], int index){
if(index == JOKER || index == joker)
return card[joker] && card[JOKER];
else
return card[index] == QUA_LENGTH;
}
double getPow(const int counter[]){
int p = 0;
for (int i = num + 1 ; i != NUM_CARD - 1; i++){
if(i == joker && counter[joker] && counter[JOKER])
p++;
else if (counter[i] == QUA_LENGTH)
p++;
}
return getSize() * 2 * Power(p);
}
vector<int> toVec(){
vector<int> vec;
if (num == joker){
vec.push_back(joker);
vec.push_back(JOKER);
}
else{
for (int i = 0; i != QUA_LENGTH; i++)
vec.push_back(num);
}
return vec;
}
};
/** the class representing quadruple pattern
variable: int quad: the number value of the quadruple part
int appd1: the number value of the appendix part 1
int appd2: the number value of the appendix part 2 **/
class Quadruple:public pattern{
int quad, appd1, appd2;
public:
Quadruple(int quadruple, int appd1, int appd2, int length):pattern("Q", length){
this->quad = quadruple;
this->appd1 = appd1;
this->appd2 = appd2;
}
int getQuad() const{
return quad;
}
bool operator >(const Quadruple& qua){
if (this->getSize() != qua.getSize())
return false;
else
return quad > qua.getQuad();
}
static bool ifExist(int card[], int qua_index, int appd1_index, int appd2_index,
int appd_length){
return (card[qua_index] == QUA_LENGTH) && (card[appd1_index] >= appd_length) &&
(card[appd2_index] >= appd_length);
}
double getPow(const int counter[]){
int p = 0;
for (int i = quad + 1 ; i != NUM_CARD; i++){
if (counter[i] == QUA_LENGTH)
p++;
}
return getSize() * 2 * Power(p);
}
vector<int> toVec(){
vector<int> vec;
for (int i = 0; i != QUA_LENGTH; i++)
vec.push_back(quad);
for (int i = 0; i != (getSize() - QUA_LENGTH) / 2; i++)
vec.push_back(appd1);
for (int i = 0; i != (getSize() - QUA_LENGTH) / 2; i++)
vec.push_back(appd2);
return vec;
}
};
#endif // PATTERNS