-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyramid.cpp
648 lines (573 loc) · 22.6 KB
/
pyramid.cpp
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#include "pyramid.hpp"
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <cassert>
/*+- Card Representation -+
| -1 : - |
| 1 : Ace |
| 2-10 : 2-10 |
| 11 : Jack |
| 12 : Queen |
| 13 : King |
+-----------------------+*/
// Retrieves the card
char Pyramid::get_card(int representation) {
switch(representation) {
case NO_CARD: return '-';
case 1: return 'A';
case 10: return 'T';
case 11: return 'J';
case 12: return 'Q';
case 13: return 'K';
default: return (char)(representation + '0');
}
}
// Previous card
int Pyramid::previous(int index, int mask) {
if(mask == NO_MASK) mask = deck_waste_mask;
if(index < TOTAL_PYRAMID_CARDS - 1 || index >= TOTAL_CARDS) return NO_CARD;
do {
index--;
if(index < TOTAL_PYRAMID_CARDS - 1) return NO_CARD;
} while(!get_deck_waste_mask_value(index, mask));
return index;
}
// Next card
int Pyramid::next(int index, int mask) {
if(mask == NO_MASK) mask = deck_waste_mask;
if(index < TOTAL_PYRAMID_CARDS - 1 || index >= TOTAL_CARDS) return NO_CARD;
do {
index++;
if(index >= TOTAL_CARDS) return NO_CARD;
} while(!get_deck_waste_mask_value(index, mask));
return index;
}
// Check card on (row, col) is covered or not
bool Pyramid::check_covered(int row, int col, int mask) {
if(row > 7 || row < 1 || col > row || col < 1) return true;
if(row == 7 && col <= row) return false;
if(mask == NO_MASK) mask = pyramid_mask;
int index = row * (row + 1) / 2 + col - 1;
return get_pyramid_mask_value(index, mask) || get_pyramid_mask_value(index + 1, mask);
}
// Set pyramid mask
void Pyramid::set_pyramid_mask_value(int n, int *mask, bool bit_value) {
if(mask == NULL) mask = &pyramid_mask;
(*mask) = (*mask) ^ ((!bit_value) << (TOTAL_PYRAMID_CARDS - n - 1));
}
// Get pyramid mask
bool Pyramid::get_pyramid_mask_value(int n, int mask) {
if(mask == NO_MASK) mask = pyramid_mask;
return (mask >> (TOTAL_PYRAMID_CARDS - n - 1)) & 1;
}
// Set deck and waste mask
void Pyramid::set_deck_waste_mask_value(int n, int *mask, bool bit_value) {
if(mask == NULL) mask = &deck_waste_mask;
(*mask) = (*mask) ^ ((!bit_value) << (TOTAL_CARDS - n - 1));
}
// Get pyramid mask
bool Pyramid::get_deck_waste_mask_value(int n, int mask) {
if(mask == NO_MASK) mask = deck_waste_mask;
return (mask >> (TOTAL_CARDS - n - 1)) & 1;
}
// Constructor
Pyramid::Pyramid() {
srand(time(NULL) * time(NULL));
int cards[13], no_cards = 0, c, card_rep;
for(int i=0; i<13; i++) cards[i] = 4;
while(no_cards < 52) {
c = rand() % (52 - no_cards) + 1;
card_rep = 0;
while(c > cards[card_rep]) {
c -= cards[card_rep];
card_rep++;
}
cards[card_rep]--;
pyramid[no_cards] = card_rep + 1;
no_cards++;
}
top_deck = TOTAL_PYRAMID_CARDS;
top_waste = NO_CARD;
total_reset_deck = 0;
pyramid_mask = PYRAMID_MASK_DEFAULT;
deck_waste_mask = DECK_WASTE_MASK_DEFAULT;
}
Pyramid::Pyramid(Pyramid *_pyramid) {
for(int i=0; i<TOTAL_CARDS; i++) pyramid[i] = _pyramid->pyramid[i];
top_deck = _pyramid->top_deck;
top_waste = _pyramid->top_waste;
total_reset_deck = _pyramid->total_reset_deck;
pyramid_mask = _pyramid->pyramid_mask;
deck_waste_mask = _pyramid->deck_waste_mask;
}
Pyramid::Pyramid(int *pyramid_and_deck) {
for(int i=0; i<TOTAL_CARDS; i++) pyramid[i] = pyramid_and_deck[i];
top_deck = TOTAL_PYRAMID_CARDS;
top_waste = NO_CARD;
total_reset_deck = 0;
pyramid_mask = PYRAMID_MASK_DEFAULT;
deck_waste_mask = DECK_WASTE_MASK_DEFAULT;
}
// Destructor
Pyramid::~Pyramid() {}
// Functions
// Get state of pyramid
State Pyramid::get_state() {
State current_state;
current_state.pyramid_mask = pyramid_mask;
current_state.deck_waste_mask = deck_waste_mask;
current_state.top_deck_index = top_deck;
current_state.top_waste_index = top_waste;
current_state.total_reset_deck_count = total_reset_deck;
current_state.recalculate();
return current_state;
}
// Get local pyramid mask
int Pyramid::get_local_pyramid_mask() {
return pyramid_mask;
}
// Get local deck waste mask
int Pyramid::get_local_deck_waste_mask() {
return deck_waste_mask;
}
// Retrieves the card on the top of the deck
char Pyramid::get_top_deck_card(int mask, int top_deck_index) {
if(mask == NO_MASK) mask = deck_waste_mask;
if(top_deck_index == NO_CARD_INDEX) top_deck_index = top_deck;
if(top_deck_index == NO_CARD || !get_deck_waste_mask_value(top_deck_index, mask)) return '-';
return get_card(pyramid[top_deck_index]);
}
// Retrieves the card on the top of the waste
char Pyramid::get_top_waste_card(int mask, int top_waste_index) {
if(mask == NO_MASK) mask = deck_waste_mask;
if(top_waste_index == NO_CARD_INDEX) top_waste_index = top_waste;
if(top_waste_index == NO_CARD || !get_deck_waste_mask_value(top_waste_index, mask)) return '-';
return get_card(pyramid[top_waste_index]);
}
// Retrieves the cards on pyramid
char * Pyramid::get_pyramid(int mask) {
char * pyramid_cards = (char *)malloc(sizeof(char) * TOTAL_PYRAMID_CARDS);
if(mask == NO_MASK) mask = pyramid_mask;
for(int i = 0; i < TOTAL_PYRAMID_CARDS; i++) {
if(get_pyramid_mask_value(i, mask)) {
pyramid_cards[i] = get_card(pyramid[i]);
} else {
pyramid_cards[i] = get_card(NO_CARD);
}
}
return pyramid_cards;
}
// Draw a card from deck
// If the deck is empty, the cards in the waste
// is going back to deck in order
void Pyramid::draw_deck(int mask, int *top_deck_index, int *top_waste_index, int *total_reset_deck_count) {
if(mask == NO_MASK) mask = deck_waste_mask;
if(top_deck_index == NULL) top_deck_index = &top_deck;
if(top_waste_index == NULL) top_waste_index = &top_waste;
if(total_reset_deck_count == NULL) total_reset_deck_count = &total_reset_deck;
int temporary;
if(*top_deck_index == NO_CARD) {
*top_deck_index = next(TOTAL_PYRAMID_CARDS - 1, mask);
*top_waste_index = NO_CARD;
(*total_reset_deck_count)++;
} else {
temporary = next(*top_deck_index, mask);
*top_waste_index = *top_deck_index;
*top_deck_index = temporary;
}
}
// Pair 2 cards from pyramid
bool Pyramid::pair_cards_in_pyramid(int row1, int col1, int row2, int col2, int *mask) {
if(mask == NULL) mask = &pyramid_mask;
if(row1 == row2 && col1 == col2) return false;
if(row1 > 7 || row1 < 1 || col1 > row1 || col1 < 1) return false;
if(row2 > 7 || row2 < 1 || col2 > row2 || col2 < 1) return false;
if(check_covered(row1, col1, *mask) || check_covered(row2, col2, *mask)) return false;
row1--; col1--; row2--; col2--;
int index1, index2;
index1 = row1 * (row1 + 1) / 2 + col1;
index2 = row2 * (row2 + 1) / 2 + col2;
if(get_pyramid_mask_value(index1, *mask) && get_pyramid_mask_value(index2, *mask) && pyramid[index1] + pyramid[index2] == 13) {
set_pyramid_mask_value(index1, mask);
set_pyramid_mask_value(index2, mask);
return true;
} else return false;
}
// Pair card from top of deck and card from top of waste
bool Pyramid::pair_cards_deck_and_waste(int *mask, int *top_deck_index, int *top_waste_index) {
if(mask == NULL) mask = &deck_waste_mask;
if(top_deck_index == NULL) top_deck_index = &top_deck;
if(top_waste_index == NULL) top_waste_index = &top_waste;
if(*top_deck_index == NO_CARD || *top_waste_index == NO_CARD) return false;
if(get_deck_waste_mask_value(*top_deck_index, *mask) && get_deck_waste_mask_value(*top_waste_index, *mask) && pyramid[*top_deck_index] + pyramid[*top_waste_index] == 13) {
set_deck_waste_mask_value(*top_deck_index, mask);
set_deck_waste_mask_value(*top_waste_index, mask);
*top_deck_index = next(*top_deck_index);
*top_waste_index = previous(*top_waste_index);
return true;
} else return false;
}
// Pair card from top of deck and card from pyramid
bool Pyramid::pair_cards_deck_and_pyramid(int row, int col, int *mask, int *top_deck_index, int *deck_mask) {
if(mask == NULL) mask = &pyramid_mask;
if(top_deck_index == NULL) top_deck_index = &top_deck;
if(deck_mask == NULL) deck_mask = &deck_waste_mask;
if(row > 7 || row < 1 || col > row || col < 1) return false;
if(*top_deck_index == NO_CARD || check_covered(row, col, *mask)) return false;
row--; col--;
int index;
index = row * (row + 1) / 2 + col;
if(get_pyramid_mask_value(index, *mask) && get_deck_waste_mask_value(*top_deck_index, *deck_mask) && pyramid[index] + pyramid[*top_deck_index] == 13) {
set_pyramid_mask_value(index, mask);
set_deck_waste_mask_value(*top_deck_index, deck_mask);
*top_deck_index = next(*top_deck_index);
return true;
} else return false;
}
// Pair card from top of waste and card from pyramid
bool Pyramid::pair_cards_waste_and_pyramid(int row, int col, int *mask, int *top_waste_index, int *deck_mask) {
if(mask == NULL) mask = &pyramid_mask;
if(top_waste_index == NULL) top_waste_index = &top_waste;
if(deck_mask == NULL) deck_mask = &deck_waste_mask;
if(row > 7 || row < 1 || col > row || col < 1) return false;
if(*top_waste_index == NO_CARD || check_covered(row, col, *mask)) return false;
row--; col--;
int index;
index = row * (row + 1) / 2 + col;
if(get_pyramid_mask_value(index, *mask) && get_deck_waste_mask_value(*top_waste_index, *deck_mask) && pyramid[index] + pyramid[*top_waste_index] == 13) {
set_pyramid_mask_value(index, mask);
set_deck_waste_mask_value(*top_waste_index, deck_mask);
*top_waste_index = previous(*top_waste_index);
return true;
} else return false;
}
// Remove king from the pyramid
bool Pyramid::remove_king(int row, int col, int *mask) {
if(mask == NULL) mask = &pyramid_mask;
if(row > 7 || row < 1 || col > row || col < 1) return false;
if(check_covered(row, col, *mask)) return false;
row--; col--;
int index;
index = row * (row + 1) / 2 + col;
if(get_pyramid_mask_value(index, *mask) && pyramid[index] == 13) {
set_pyramid_mask_value(index, mask);
return true;
} else return false;
}
// Remove king from the top of the deck
bool Pyramid::remove_king_from_deck(int *mask, int *top_deck_index) {
if(mask == NULL) mask = &deck_waste_mask;
if(top_deck_index == NULL) top_deck_index = &top_deck;
if(*top_deck_index != NO_CARD && get_deck_waste_mask_value(*top_deck_index, *mask) && pyramid[*top_deck_index] == 13) {
set_deck_waste_mask_value(*top_deck_index, mask);
*top_deck_index = next(*top_deck_index);
return true;
} else return false;
}
// Remove king from the top of the waste
bool Pyramid::remove_king_from_waste(int *mask, int *top_waste_index) {
if(mask == NULL) mask = &deck_waste_mask;
if(top_waste_index == NULL) top_waste_index = &top_waste;
if(*top_waste_index != NO_CARD && get_deck_waste_mask_value(*top_waste_index, *mask) && pyramid[*top_waste_index] == 13) {
set_deck_waste_mask_value(*top_waste_index, mask);
*top_waste_index = previous(*top_waste_index);
return true;
} else return false;
}
// Get all possible actions
std::vector< std::pair<int, std::vector<int> > > Pyramid::get_all_possible_actions(int mask, int deck_mask, int top_deck_index, int top_waste_index, int total_reset_deck_count) {
if(mask == NO_MASK) mask = pyramid_mask;
if(deck_mask == NO_MASK) deck_mask = deck_waste_mask;
if(top_deck_index == NO_CARD_INDEX) top_deck_index = top_deck;
if(top_waste_index == NO_CARD_INDEX) top_waste_index = top_waste;
if(total_reset_deck_count == NO_CARD_INDEX) total_reset_deck_count = total_reset_deck;
int row, col, index;
std::vector< std::pair<int, int> > uncovered_card[TOTAL_POSSIBLE_UNCOVERED_CARDS + 1];
std::vector< std::pair<int, std::vector<int> > > actions;
if(!is_finished(mask, total_reset_deck_count)) {
// Clear all uncovered card
for(int i = 0; i <= TOTAL_POSSIBLE_UNCOVERED_CARDS; i++)
uncovered_card[i].clear();
for(row = 1; row <= TOTAL_ROW; row++) {
for(col = 1; col <= row; col++) {
index = (row - 1) * row / 2 + col - 1;
if(get_pyramid_mask_value(index, mask) && !check_covered(row, col, mask)) {
//-----------------------
// Check King in pyramid
//-----------------------
if(check_remove_king(row, col, mask)) {
std::vector<int> location;
location.push_back(row);
location.push_back(col);
actions.push_back(std::make_pair(ACTION_REMOVE_KING_IN_PYRAMID, location));
//std::cout<<"king \n";
}
//------------------------------------
// Get all uncovered card except King
//------------------------------------
else {
uncovered_card[pyramid[index]].push_back(std::make_pair(row, col));
//std::cout<<"uncovered card \n";
}
}
}
}
//----------------------------------------------------------
// Check pair of uncovered cards in pyramid
//----------------------------------------------------------
int check_size = TOTAL_POSSIBLE_UNCOVERED_CARDS / 2;
std::vector< std::pair<int, int> >::iterator fc, sc;
for(int i = 1; i <= check_size; i++) {
int pc = 13 - i;
if(uncovered_card[i].size() > 0 && uncovered_card[pc].size() > 0) {
std::vector<int> location;
for(fc = uncovered_card[i].begin(); fc != uncovered_card[i].end(); fc++) {
for(sc = uncovered_card[pc].begin(); sc != uncovered_card[pc].end(); sc++) {
location.clear();
location.push_back(fc->first);
location.push_back(fc->second);
location.push_back(sc->first);
location.push_back(sc->second);
actions.push_back(std::make_pair(ACTION_PAIR_CARDS_PYRAMID, location));
}
}
}
}
//---------------------------
// Check King on top of deck
//---------------------------
if(check_remove_king_from_deck(deck_mask, top_deck_index)) {
std::vector<int> empty_vector;
actions.push_back(std::make_pair(ACTION_REMOVE_KING_ON_DECK, empty_vector));
}
//---------------------------
// Check King on top of waste
//---------------------------
if(check_remove_king_from_waste(deck_mask, top_waste_index)) {
std::vector<int> empty_vector;
actions.push_back(std::make_pair(ACTION_REMOVE_KING_ON_WASTE, empty_vector));
}
//----------------------------------------------------------
// Check pair on top of deck with uncovered card in pyramid
//----------------------------------------------------------
if(get_top_deck_card(deck_mask, top_deck_index) != '-') {
int pc = 13 - pyramid[top_deck_index];
for(int i = 0; i < uncovered_card[pc].size(); i++) {
std::vector<int> location;
location.push_back(uncovered_card[pc][i].first);
location.push_back(uncovered_card[pc][i].second);
actions.push_back(std::make_pair(ACTION_PAIR_CARD_PYRAMID_DECK, location));
}
}
//----------------------------------------------------------
// Check pair on top of waste with uncovered card in pyramid
//----------------------------------------------------------
if(get_top_waste_card(deck_mask, top_waste_index) != '-') {
int pc = 13 - pyramid[top_waste_index];
for(int i = 0; i < uncovered_card[pc].size(); i++) {
std::vector<int> location;
location.push_back(uncovered_card[pc][i].first);
location.push_back(uncovered_card[pc][i].second);
actions.push_back(std::make_pair(ACTION_PAIR_CARD_PYRAMID_WASTE, location));
}
}
//----------------------------------------------------------
// Check pair on top of deck with waste
//----------------------------------------------------------
if(check_pair_cards_deck_and_waste(deck_mask, top_deck_index, top_waste_index)) {
std::vector<int> empty_vector;
actions.push_back(std::make_pair(ACTION_PAIR_CARD_DECK_WASTE, empty_vector));
}
//----------------------------------------------------------
// Action draw card
//----------------------------------------------------------
std::vector<int> empty_vector;
actions.push_back(std::make_pair(ACTION_DRAW, empty_vector));
}
return actions;
}
// Check if action valid
int Pyramid::check_pair(int mask) {
int temp_i=0;
int temp_j=0;
if(check_remove_king_from_deck()) return 3;
else if(check_remove_king_from_waste())return 4;
for (int i=7; i>=1;i--){
for(int j=i;j>=1;j--){
i--;j--;
int post = i * (i + 1) / 2 + j;
//std::cout<<"Check "<<get_card(pyramid[post])<<"\n";
i++;j++;
if(!check_covered(i, j, mask)){
if(get_card(pyramid[post])=='K') return 2;
if(temp_i==0 && temp_j==0){
temp_i=i;
temp_j=j;
for (int m=7; m>=1;m--){
for(int n=7;n>=1;n--){
if(!check_covered(m, n, mask))
if(check_pair_cards_in_pyramid(temp_i, temp_j, m, n, mask)) return 5;
}
}
if(check_pair_cards_deck_and_pyramid(temp_i, temp_j, mask)) return 6;
if(check_pair_cards_waste_and_pyramid(temp_i, temp_j, mask)) return 7;
temp_i=0;
temp_j=0;
}
}
}
}
if(check_pair_cards_deck_and_waste())return 8;
else return 1;
}
// Check Pair 2 cards from pyramid
bool Pyramid::check_pair_cards_in_pyramid(int row1, int col1, int row2, int col2, int mask) {
if(mask == NO_MASK) mask = pyramid_mask;
if(row1 == row2 && col1 == col2) return false;
if(row1 > 7 || row1 < 1 || col1 > row1 || col1 < 1) return false;
if(row2 > 7 || row2 < 1 || col2 > row2 || col2 < 1) return false;
if(check_covered(row1, col1, mask) || check_covered(row2, col2, mask)) return false;
row1--; col1--; row2--; col2--;
int index1, index2;
index1 = row1 * (row1 + 1) / 2 + col1;
index2 = row2 * (row2 + 1) / 2 + col2;
if(get_pyramid_mask_value(index1, mask) && get_pyramid_mask_value(index2, mask) && pyramid[index1] + pyramid[index2] == 13) {
return true;
} else return false;
}
// Check Pair card from top of deck and card from top of waste
bool Pyramid::check_pair_cards_deck_and_waste(int mask, int top_deck_index, int top_waste_index) {
if(mask == NO_MASK) mask = deck_waste_mask;
if(top_deck_index == NO_CARD_INDEX) top_deck_index = top_deck;
if(top_waste_index == NO_CARD_INDEX) top_waste_index = top_waste;
if(top_deck_index == NO_CARD || top_waste_index == NO_CARD) return false;
if(get_deck_waste_mask_value(top_deck_index, mask) && get_deck_waste_mask_value(top_waste_index, mask) && pyramid[top_deck_index] + pyramid[top_waste_index] == 13) {
return true;
} else return false;
}
// Check Pair card from top of deck and card from pyramid
bool Pyramid::check_pair_cards_deck_and_pyramid(int row, int col, int mask, int top_deck_index, int deck_mask) {
if(mask == NO_MASK) mask = deck_waste_mask;
if(top_deck_index == NO_CARD_INDEX) top_deck_index = top_deck;
if(deck_mask = NO_MASK) deck_mask = deck_waste_mask;
if(row > 7 || row < 1 || col > row || col < 1) return false;
if(top_deck_index == NO_CARD || check_covered(row, col, mask)) return false;
row--; col--;
int index;
index = row * (row + 1) / 2 + col;
if(get_pyramid_mask_value(index, mask) && get_deck_waste_mask_value(top_deck_index, deck_mask) && pyramid[index] + pyramid[top_deck_index] == 13) {
return true;
} else return false;
}
// Check Pair card from top of waste and card from pyramid
bool Pyramid::check_pair_cards_waste_and_pyramid(int row, int col, int mask, int top_waste_index, int deck_mask) {
if(mask == NO_MASK) mask = deck_waste_mask;
if(top_waste_index == NO_CARD_INDEX) top_waste_index = top_waste;
if(deck_mask = NO_MASK) deck_mask = deck_waste_mask;
if(row > 7 || row < 1 || col > row || col < 1) return false;
if(top_waste_index == NO_CARD || check_covered(row, col, mask)) return false;
row--; col--;
int index;
index = row * (row + 1) / 2 + col;
if(get_pyramid_mask_value(index, mask) && get_deck_waste_mask_value(top_waste_index, deck_mask) && pyramid[index] + pyramid[top_waste_index] == 13) {
return true;
} else return false;
}
// Check Remove king from the pyramid
bool Pyramid::check_remove_king(int row, int col, int mask) {
if(row > 7 || row < 1 || col > row || col < 1) return false;
if(row !=7 ){
if(!check_covered(row, col))return false;
}
row--; col--;
int index;
index = row * (row + 1) / 2 + col;
if(get_pyramid_mask_value(index, mask) && pyramid[index] == 13) {
return true;
} else return false;
}
// Check Remove king from the top of the deck
bool Pyramid::check_remove_king_from_deck(int mask, int top_deck_index) {
if(mask == NO_MASK) mask = deck_waste_mask;
if(top_deck_index == NO_CARD_INDEX) top_deck_index = top_deck;
if(top_deck_index != NO_CARD && get_deck_waste_mask_value(top_deck_index, mask) && pyramid[top_deck_index] == 13) {
return true;
} else return false;
}
// Check Remove king from the top of the waste
bool Pyramid::check_remove_king_from_waste(int mask, int top_waste_index) {
if(mask == NO_MASK) mask = deck_waste_mask;
if(top_waste_index == NO_CARD_INDEX) top_waste_index = top_waste;
if(top_waste_index != NO_CARD && get_deck_waste_mask_value(top_waste_index, mask) && pyramid[top_waste_index] == 13) {
return true;
} else return false;
}
// Check if that state is winnable
bool Pyramid::is_winnable_state(const State &state) {
std::vector< std::pair<int, int> > available_card[TOTAL_POSSIBLE_UNCOVERED_CARDS + 1];
int count_in_deck[TOTAL_POSSIBLE_UNCOVERED_CARDS + 1];
int mask = state.pyramid_mask;
int deck_mask = state.deck_waste_mask;
// Clear all uncovered card
for(int i = 0; i <= TOTAL_POSSIBLE_UNCOVERED_CARDS; i++) {
available_card[i].clear();
count_in_deck[i] = 0;
}
for(int i = TOTAL_PYRAMID_CARDS; i < TOTAL_CARDS; i++) {
if(get_deck_waste_mask_value(i, deck_mask)) {
count_in_deck[pyramid[i]]++;
}
}
/* for(int i = 0; i <= TOTAL_POSSIBLE_UNCOVERED_CARDS; i++) {
std::cout << count_in_deck[i] << " ";
}
std::cout << std::endl;
*/
for(int row = 1; row <= TOTAL_ROW; row++) {
for(int col = 1; col <= row; col++) {
int index = (row - 1) * row / 2 + col - 1;
if(get_pyramid_mask_value(index, mask)) {
available_card[pyramid[index]].push_back(std::make_pair(row, col));
}
}
}
/* for(int i = 1; i < TOTAL_POSSIBLE_UNCOVERED_CARDS; i++) {
std::cout << "Card #" << i << ": ";
for(int j = 0; j < available_card[i].size(); j++) {
std::cout << "(" << available_card[i][j].first << "," << available_card[i][j].second << ") ";
}
std::cout << std::endl;
}
*/
//----------------------------------------------------------
// Check pair of uncovered cards in pyramid
//----------------------------------------------------------
std::vector< std::pair<int, int> >::iterator fc, sc;
int row1, col1, row2, col2;
for(int i = 1; i < TOTAL_POSSIBLE_UNCOVERED_CARDS; i++) {
int pc = 13 - i;
if(available_card[i].size() > 0 && available_card[pc].size() > 0) {
for(fc = available_card[i].begin(); fc != available_card[i].end(); fc++) {
int available_count = count_in_deck[pc];
for(sc = available_card[pc].begin(); sc != available_card[pc].end(); sc++) {
row1 = fc->first;
col1 = fc->second;
row2 = sc->first;
col2 = sc->second;
if(!(row2 > row1 && col2 >=col1 && col2 <= (row2 - (row1 - col1)))) available_count++;
}
assert(available_count <= 4);
if(available_count == 0) return false;
}
}
}
return true;
}
// Obtain the status of the game
// Finished when the total_reset_deck equals to 3
bool Pyramid::is_finished(int mask, int total_reset_deck_count) {
if(mask == NO_MASK) mask = pyramid_mask;
if(total_reset_deck_count == NO_CARD_INDEX) total_reset_deck_count = total_reset_deck;
return total_reset_deck_count == 3 || mask == 0;
}