forked from naoyat/solitaire-solver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
KlondikeSolver.cc
734 lines (636 loc) · 21.9 KB
/
KlondikeSolver.cc
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
#include <vector>
#include <string>
#include <sstream>
#include <queue>
#include <algorithm>
using namespace std;
#include "common.h"
#include "cout11.h"
#undef NDEBUG
#include <cassert>
#define NUM_LINES 7
#define NUM_SUITES 4
#define LINE_BASE 0
#define GOAL NUM_LINES
/*
priority_queue<pair<pair<int, int>, string> > _pq;
set<string> _visited;
map<string, pair<int, string> > _last_step;
vector<vector<Card> > _board;
*/
bool option_best = false;
bool option_single_turn = false;
bool option_verbose = false;
inline char num_serialize(int n) {
// printf("num_serialize(n=%d)\n", n);
assert(-1 <= n && n < 26);
return 'a' + n;
}
inline int num_deserialize(char ch) {
assert(0x60 <= ch && ch <= 'z');
return (int)(ch - 'a');
}
class Board {
public:
Board(vector<vector<Card> >& lines, vector<Card>& deck) {
// this->lines.assign(all(lines));
assert(lines.size() == NUM_LINES);
this->lines.assign(NUM_LINES, vector<pair<Card, bool> >());
int cnt = 0;
for (int i=0; i<NUM_LINES; ++i) {
int L = lines[i].size();
assert(1 <= L && L <= 7);
for (int j=0; j<L; ++j) {
this->lines[i].push_back(make_pair(lines[i][j], j == L-1));
}
cnt += L;
}
assert(cnt == 28);
this->deck.assign(all(deck));
assert(deck.size() == 24);
this->goal.assign(4, -1);
this->deck_mask = 0xffffff;
this->deck_ofs = -1;
this->tops.assign(NUM_LINES, -1);
refresh(true);
// cout << "<ctor> " << dump() << endl;
}
~Board() {}
public:
string _serialize() {
stringstream ss;
// goal[]
for (int g=0; g<NUM_SUITES; ++g) {
ss << card_serialize(goal[g]);
}
// lines[]
sort(lines.begin(), lines.end());
for (int l=0; l<NUM_LINES; ++l) {
int L = lines[l].size();
ss << num_serialize(L);
for (int i=0; i<L; ++i) {
ss << card_serialize(lines[l][i].first);
}
int cnt = 0;
for (int i=L-1; i>=0; --i) {
if (lines[l][i].second == true) ++cnt;
else break;
}
ss << num_serialize(cnt);
}
// deck_ofs (0-24)
ss << num_serialize(deck_ofs); // -1〜23 : [a-y]
// deck_mask (24bit)
ss << hex << deck_mask << dec;
return ss.str();
}
int deserialize(string encoded) {
// serialized = encoded;
int i = 0;
// goal[]
for (int g=0; g<NUM_SUITES; ++g) {
goal[g] = card_deserialize(encoded[i++]);
}
// lines[]
assert(lines.size() == NUM_LINES);
for (int l=0; l<NUM_LINES; ++l) {
lines[l].clear();
int L = num_deserialize(encoded[i++]);
for (int j=0; j<L; ++j) {
Card c = card_deserialize(encoded[i++]);
lines[l].push_back(make_pair(c, false));
}
int cnt = num_deserialize(encoded[i++]);
assert(0 <= cnt && cnt <= L);
for (int j=0; j<cnt; ++j) {
lines[l][(L-1)-j].second = true;
}
}
// deck_ofs (0-24)
deck_ofs = num_deserialize(encoded[i++]);
assert(-1 <= deck_ofs && deck_ofs <= 24);
// deck_mask (24bit)
deck_mask = strtol(encoded.c_str()+i, NULL, 16);
assert(0 <= deck_mask && deck_mask <= 0xffffff);
// refresh(false);
refresh(true);
assert(encoded == serialized);
return 0;
}
string dump() {
stringstream ss;
// deck
if (deck_ofs == -1) {
ss << "-";
} else if (deck_ofs < 24) {
ss << _full(deck_top);
} else {
ss << "#";
}
ss << " || ";
// lines[]
for (int l=0; l<NUM_LINES; ++l) {
int L = lines[l].size();
for (int i=0; i<L; ++i) {
if (lines[l][i].second)
ss << _full(lines[l][i].first);
else
ss << '#';
}
if (l < NUM_LINES-1)
ss << " / ";
}
ss << " || ";
// deck_ofs (0-24)
ss << "ofs=" << deck_ofs;
// deck_mask (24bit)
ss << " mask=" << hex << deck_mask << dec;
ss << " || ";
// goal[]
for (int g=0; g<NUM_SUITES; ++g) {
ss << _full(goal[g]);
}
return ss.str();
// cout << ss.str() << endl;
}
void debug() {
cout << "[TOPS:";
for (int l=0; l<NUM_LINES; ++l) {
cout << " " << _full(tops[l]);
}
cout << "] [GOAL:";
for (int g=0; g<NUM_SUITES; ++g) {
cout << " " << _full(goal[g]);
}
cout << "]" << endl;
}
int take() {
if (deck_ofs == -1) {
return -1;
} else {
int m = 1 << deck_ofs;
assert((deck_mask & m) == m);
Card c = deck[deck_ofs];
deck_mask &= ~m;
--deck_ofs;
m >>= 1;
while (deck_ofs >= 0) {
if (deck_mask & m) break;
--deck_ofs;
m >>= 1;
}
return c;
}
}
int turn() {
++deck_ofs;
int last_ofs=-1;
for (int turned=0, m=1<<deck_ofs; deck_ofs<24; ++deck_ofs,m<<=1) {
if (deck_mask & m) {
++turned;
last_ofs=deck_ofs;
}
if (turned == (option_single_turn ? 1 : 3)) break;
}
if (deck_ofs == 24) {
deck_ofs = last_ofs;
}
refresh(true);
return deck_ofs;
}
void refresh(bool do_serialize) {
for (int l=0; l<NUM_LINES; ++l) {
int L = lines[l].size();
if (L == 0) {
tops[l] = -1;
continue;
} else {
assert(lines[l].back().second == true);
tops[l] = lines[l].back().first;
}
}
if (deck_ofs == -1) {
deck_top = -1;
} else if (deck_ofs < 24) {
deck_top = deck[deck_ofs];
} else {
deck_top = -1;
}
score = 0;
for (int g=0; g<NUM_SUITES; ++g) {
#ifdef CLUB_ONLY
if (g == CLUB)
score += card_num(goal[g]) + 1; // 0 or 1-13
else
score += 13;
#else
score += card_num(goal[g]) + 1; // 0 or 1-13
#endif
}
if (do_serialize) {
serialized = _serialize();
}
}
public:
vector<vector<pair<Card, bool> > > lines;
vector<Card> deck;
vector<Card> goal;
vector<Card> tops;
int deck_mask;
int deck_ofs;
Card deck_top;
bool dirty;
int score;
string serialized;
};
struct cmp{
bool operator() ( pair<int, pair<pair<int, int>, string> > a, pair<int, pair<pair<int, int>, string> > b )
{
int a_score = a.first;
int a_distance = -a.second.first.first;
int b_score = b.first;
int b_distance = -b.second.first.first;
int ratio_score = 2;
int ratio_distance = 1;
if (option_best) {
return (a_score * ratio_score - a_distance * ratio_distance) < (b_score * ratio_score - b_distance * ratio_distance);
} else {
return (a_score < b_score) || (a_score == b_score && -a_distance < -b_distance);
}
}
};
priority_queue<pair<int, pair<pair<int, int>, string> >, vector<pair<int, pair<pair<int, int>, string> > >, cmp > _pq;
set<string> _visited;
map<string, pair<int, string> > _best;
inline void add_to_queue(Board *board, int curr_step, int curr_big_turn, string current) {
if (!found(board->serialized, _visited)) {
_pq.push(make_pair(board->score, make_pair(make_pair(-(curr_step + 1), curr_big_turn), board->serialized)));
if (found(board->serialized, _best)) {
if (_best[board->serialized].first > curr_step) {
_best[board->serialized] = make_pair(curr_step, current);
}
} else {
_best[board->serialized] = make_pair(curr_step, current);
}
}
if (current != "") {
board->deserialize(current);
}
}
void _show(int st, vector<vector<Card> >& lines, vector<Card>& goal) {
printf("[%d] ", st);
#if 0
for (int a=0; a<NUM_LINES; ++a) {
int L = lines[a].size();
for (int j=0; j<L; ++j) {
cout << _full(lines[a][j]);
}
cout << " / ";
}
cout << "[";
for (int j=0; j<NUM_SUITES; ++j) {
cout << " " << _full(goal[j]);
}
cout << " ] ";
#endif
}
void show_solution(Board *board, vector<vector<Card> >& initial_lines, string& current) {
vector<string> history;
while (true) {
if (!found(current, _best)) break;
history.push_back(current);
current = _best[current].second;
}
history.pop_back();
reverse(history.begin(), history.end());
vector<int> last_goal(4, -1);
vector<vector<Card> > last_lines(all(initial_lines));
int last_ofs = -1, last_mask = 0xffffff;
int big_turn = 0;
for (int i=0; i<history.size(); ++i) {
board->deserialize(history[i]);
vector<vector<Card> > curr_lines(NUM_LINES, vector<Card>());
for (int l=0; l<NUM_LINES; ++l) {
int L = board->lines[l].size();
for (int j=0; j<L; ++j) {
curr_lines[l].push_back(board->lines[l][j].first);
}
}
vector<int> curr_empty, last_empty;
for (int j=0; j<NUM_LINES; ++j) {
if (last_lines[j].size() == 0) {
last_empty.push_back(j);
}
if (curr_lines[j].size() == 0) {
curr_empty.push_back(j);
}
}
vector<int> mapping(NUM_LINES, -1);
int mapping_mask = 0;
for (int a=0; a<NUM_LINES; ++a) {
if (last_lines[a].size() == 0) continue;
for (int b=0; b<NUM_LINES; ++b) {
if (curr_lines[b].size() == 0) continue;
if (last_lines[a][0] == curr_lines[b][0]) {
mapping[a] = b;
mapping_mask |= (1 << b);
}
}
}
int M = min(curr_empty.size(), last_empty.size());
for (int j=0; j<M; ++j) {
int a = last_empty.back(); last_empty.pop_back();
int b = curr_empty.back(); curr_empty.pop_back();
mapping[a] = b;
mapping_mask |= (1 << b);
}
assert(__builtin_popcount(mapping_mask) >= 6);
for (int a=0; a<NUM_LINES; ++a) {
if (mapping[a] == -1) {
for (int b=0,m=1; b<NUM_LINES; ++b,m<<=1) {
if (mapping_mask & m) continue;
mapping[a] = b;
break;
}
break;
}
}
set<int> tmp(all(mapping));
assert(tmp.size() == 7);
int incl = -1, decl = -1, nb = 0;
vector<vector<Card> > curr_lines_2(7);
for (int a=0; a<NUM_LINES; ++a) {
int b = mapping[a];
curr_lines_2[a] = curr_lines[b];
int len_diff = curr_lines_2[a].size() - last_lines[a].size();
if (len_diff > 0) {
incl = a;
nb = len_diff;
} else if (len_diff < 0) {
decl = a;
nb = -len_diff;
}
}
// show
if (board->deck_mask == last_mask && board->deck_ofs != last_ofs && board->deck_ofs == -1) ++big_turn;
_show(1+i-big_turn, curr_lines_2, board->goal);
bool turned = false;
if (board->deck_mask != last_mask) {
// deck -> somewhere
assert(board->deck_ofs != last_ofs);
assert(0 <= last_ofs && last_ofs < 24);
cout << "(DECK->) " << _full(board->deck[last_ofs]);
nb = 1;
} else {
// deck is not consumed
if (board->deck_ofs != last_ofs) {
// just turned
turned = true;
if (board->deck_ofs == -1)
cout << "(BIGTURN)";
else
cout << "(TURN =>" << _full(board->deck_top) << ")";
} else {
// line -> somewhere
printf("(LINE#%d->) ", 1+decl);
int L = last_lines[decl].size();
for (int j=L-nb; j<L; ++j) {
cout << _full(last_lines[decl][j]);
}
}
}
if (!turned) {
if (board->goal != last_goal) {
cout << " (->GOAL)";
} else {
int bottom = last_lines[incl].size() > 0 ? last_lines[incl].back() : -1;
cout << " (->LINE#" << (1+incl) << " on " << _full(bottom) << ")";
}
}
cout << endl;
// cout << "/ " << board->dump() << endl;
last_goal = board->goal;
last_lines = curr_lines_2;
last_ofs = board->deck_ofs;
last_mask = board->deck_mask;
}
}
int solve(vector<vector<Card> >& initial_lines, vector<Card>& initial_deck) {
Board *board = new Board(initial_lines, initial_deck);
#if 0
string s = board->serialize();
board->deserialize(s);
cout << "ORIG:";
board->dump();
board->turn();
cout << "TURNED:";
board->dump();
board->deserialize(s);
board->turn();
cout << _full(board->deck_top()) << endl;
board->turn();
cout << _full(board->deck_top()) << endl;
cout << "TAKEN:";
board->dump();
#endif
add_to_queue(board, 0, 0, "");
int iter_count = 0;
while (!_pq.empty()) {
int score = _pq.top().first, step = -_pq.top().second.first.first, big_turn = _pq.top().second.first.second;
int next_step = step + 1;
string current = _pq.top().second.second;
_pq.pop();
if (found(current, _visited)) continue;
_visited.insert(current);
board->deserialize(current);
#ifdef DEBUG
// cout << endl;
// cout << score << " " << step << ") " << board->dump() << endl;
//putchar('\r');
//for (int i=0; i<score; ++i) putchar('#');
//fflush(stdout);
#endif
if (board->score == 13*4) {
cout << "*** SOLVED ***" << endl;
cout << "count: " << iter_count << " score: " << board->score << " steps: " << step-1 << " big_turns: " << big_turn << endl;
show_solution(board, initial_lines, current);
break;
}
++iter_count;
if (option_verbose && iter_count % 2000 == 0) {
cout << "count: " << iter_count << " score: " << board->score << " steps: " << step-1 << " big_turns: " << big_turn << endl;
}
// cout << "1"; board->debug();
// suppliers
vector<pair<pair<int, int>, Card> > supplies;
for (int l=0; l<NUM_LINES; ++l) {
int L = board->lines[l].size();
if (L == 0) continue;
for (int j=L-1; j>=0; --j) {
if (!board->lines[l][j].second) break;
Card c = board->lines[l][j].first;
supplies.push_back(make_pair(make_pair(l, L-j), c));
}
}
if (board->deck_top != -1) {
supplies.push_back(make_pair(make_pair(-1, 1), board->deck_top));
}
// cout << "2"; board->debug();
// find all possibilities
tr(it, supplies) {
int from = it->first.first, nb = it->first.second;
Card c = it->second;
assert(c != -1);
bool already_moved_to_vacant_line = false;
for (int to=0; to<NUM_LINES; ++to) {
if (to == from) continue;
Card ct = board->tops[to];
// cout << "to=" << to; board->debug();
// cout << "// " << from << " " << _full(c) << " : " << to << " " << _full(ct) << endl;
if ((ct == -1 && card_num(c)==12) || pilable(ct, c)) {
if (ct == -1) {
if (already_moved_to_vacant_line) continue;
already_moved_to_vacant_line = true;
}
int orig_from_len = -1;
int orig_to_len = board->lines[to].size();
if (from == -1) {
#ifdef DEBUG_VERBOSE
cout << "Deck";
#endif
board->take();
board->lines[to].push_back(make_pair(c, true));
assert(board->lines[to].size() == orig_to_len + 1);
} else {
orig_from_len = board->lines[from].size();
#ifdef DEBUG_VERBOSE
cout << "L" << from << "." << nb;
#endif
board->lines[to].insert(board->lines[to].end(),
board->lines[from].end()-nb, board->lines[from].end());
board->lines[from].erase(board->lines[from].end()-nb, board->lines[from].end());
if (board->lines[from].size() >= 1) {
board->lines[from].back().second = true;
}
assert(board->lines[from].size() == orig_from_len - nb);
assert(board->lines[to].size() == orig_to_len + nb);
}
// cout << "!" << board->tops << endl;
// cout << "!"; board->debug();
#ifdef DEBUG_VERBOSE
cout << "(" << _full(c) << ") on L" << to << "." << orig_to_len << "(" << _full(ct) << ")" << endl;
#endif
board->refresh(true);
add_to_queue(board, step, big_turn, current);
// board->deserialize(current);
// cout << "@"; board->debug();
if (from != -1) {
assert(board->lines[from].size() == orig_from_len);
}
assert(board->lines[to].size() == orig_to_len);
}
}
if (nb == 1) {
int num = card_num(c), suite = card_suite(c);
Card cg = board->goal[suite];
if (card_num(cg) == num - 1) {
int orig_from_len = -1;
if (from == -1) {
#ifdef DEBUG_VERBOSE
cout << "Deck";
#endif
board->take();
} else {
orig_from_len = board->lines[from].size();
#ifdef DEBUG_VERBOSE
cout << "L" << from << "." << nb;
#endif
board->lines[from].pop_back();
if (board->lines[from].size() >= 1) {
board->lines[from].back().second = true;
}
assert(board->lines[from].size() == orig_from_len - 1);
}
#ifdef DEBUG_VERBOSE
cout << "(" << _full(c) << ") on G" << suite << "(" << _full(cg) << ")" << endl;
#endif
board->goal[suite] = c;
board->refresh(true);
add_to_queue(board, step, big_turn, current);
// board->deserialize(current);
if (from != -1) {
// printf("[%d] %d ==? %d\n", from, board->lines[from].size(), orig_from_len);
assert(board->lines[from].size() == orig_from_len);
}
}
}
}
// turn
{
#ifdef DEBUG_VERBOSE
printf("Turn (ofs=%d ->)\n", board->deck_ofs);
#endif
board->turn();
add_to_queue(board, step, board->deck_ofs == -1 ? big_turn + 1 : big_turn, current);
// board->deserialize(current);
}
// break;
}
delete board;
return 0;
}
#include <unistd.h>
void show_usage() {
printf("./KlondikeSolver [-b] [-s] [-v] <game-file>\n");
}
int load(char *path, vector<vector<Card> >& lines, vector<Card>& deck) {
FILE *fp = fopen(path, "r");
if (fp == NULL) return -1;
deck.clear();
lines.clear();
set<Card> s;
for (int i=0; i<7; ++i) {
lines.push_back(read_cards(fp));
// cout << _full_cards(lines[i]) << endl;
s.insert(all(lines[i]));
}
deck = read_cards(fp);
// cout << _full_cards(deck) << endl;
s.insert(all(deck));
// printf("%d\n", s.size());
assert(s.size() == 13*4);
fclose(fp);
return 0;
}
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "bsv")) != -1) {
switch (opt) {
case 'b': // best move (try find minimal count)
option_best = true;
break;
case 's':
option_single_turn = true;
break;
case 'v': // verbose
option_verbose = true;
break;
default:
show_usage();
return 0;
}
}
if (optind == argc) {
show_usage();
return 0;
}
char *path = argv[optind];
vector<vector<Card> > lines; // 1+2+3+4+5+6+7=28
vector<Card> deck; // 24=8*3
if (load(path, lines, deck) == -1) {
show_usage();
return 0;
}
solve(lines, deck);
return 0;
}