-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolitaire.cpp
More file actions
522 lines (439 loc) · 18.1 KB
/
Copy pathsolitaire.cpp
File metadata and controls
522 lines (439 loc) · 18.1 KB
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
#include <iostream>
#include <random>
#include <string>
#include <fstream>
#include <ostream>
#include "solitaire.h"
const char* Solitaire::SYMBOL_STR[] = {"♠ ", "♥ ", "♣ ", "◆ "};
bool Solitaire::is_sussesive(const Card& top, const Card& bottom) const {
return (top.number == bottom.number - 1) &&
((static_cast<int>(top.symbol) + static_cast<int>(bottom.symbol)) % 2 == 1);
}
void Solitaire::print_each(const Card& moved_card,
const std::string from_position,
const std::string to_position,
const bool count_increment) {
if (count_increment) {
solve_count++;
}
// terminal
std::cout << solve_count
<< ": ";
if (moved_card.symbol % 2 == 1) {
std::cout << "\033[1;31m";
}
std::cout << SYMBOL_STR[moved_card.symbol] <<NUMBER[moved_card.number] << "\033[0m"
<< " ["
<< from_position
<< " → "
<< to_position
<< "] ";
if (to_position == "output") {
std::cout << "(score: " << score << " → " << score + EARNING_SCORE << ")";
}
std::cout << "\n";
// file
if (output_file.is_open()) {
output_file << solve_count
<< ": "
<< SYMBOL_STR[moved_card.symbol] <<NUMBER[moved_card.number]
<< " ["
<< from_position
<< " → "
<< to_position
<< "] ";
if (to_position == "output") {
output_file << "(score: " << score << " → " << score + EARNING_SCORE << ")";
}
output_file << "\n";
}
}
bool Solitaire::equal_to_the_last_move(const CardPile& cards, int from, int to) const {
return last_moving.cards == cards && last_moving.from == to && last_moving.to == from;
}
bool Solitaire::equal_to_the_last_move(const Card& card, int from, int to) const {
CardPile tmp_cardpile = CardPile();
tmp_cardpile.push(card);
return equal_to_the_last_move(tmp_cardpile, from, to);
}
void Solitaire::update_last_moving(const CardPile& cards, int from, int to) {
last_moving.cards = cards;
last_moving.from = from;
last_moving.to = to;
}
void Solitaire::update_last_moving(const Card& card, int from, int to) {
CardPile tmp_cardpile = CardPile();
tmp_cardpile.push(card);
update_last_moving(tmp_cardpile, from, to);
}
Solitaire::Solitaire(int seed, std::string path): Solitaire::Solitaire(seed) {
output_file.open(path);
}
Solitaire::Solitaire(int seed) {
std::mt19937_64 gen(seed);
// shuffle cards
int tmp_card_sequence[52];
for (int i = 0; i < 52; i++) {
tmp_card_sequence[i] = i;
}
std::uniform_int_distribution<int> dis(0, 51);
for (int i = 0; i < 100; i++) {
int first_pos = dis(gen);
int second_pos = dis(gen);
int tmp_num = tmp_card_sequence[first_pos];
tmp_card_sequence[first_pos] = tmp_card_sequence[second_pos];
tmp_card_sequence[second_pos] = tmp_num;
}
int count = 0;
// insert to playing piles
for (int i = 0; i < PLAYING_LINES; i++) {
playing_piles[i] = CardPile(i + 1);
for (int j = 0; j < i + 1; j++) {
Card tmp_card = Card();
tmp_card.symbol = static_cast<Card::Symbols>(tmp_card_sequence[count] / 13);
tmp_card.number = tmp_card_sequence[count] % 13 + 1;
playing_piles[i].push(tmp_card);
count++;
}
playing_piles[i].set_closed_pos(playing_piles[i].size() - 2);
}
// insert to the stock
for (int i = 0; i < 24; i++) {
Card tmp_card = Card();
tmp_card.symbol = static_cast<Card::Symbols>(tmp_card_sequence[count] / 13);
tmp_card.number = tmp_card_sequence[count] % 13 + 1;
stock.push(tmp_card);
count++;
}
stock.set_closed_pos(24 - 1);
}
Solitaire::~Solitaire() {
output_file.close();
}
bool Solitaire::solve() {
bool is_moved = false;
bool game_over = false;
while (true) {
print_board();
// game clear
if (score == 208) {
break;
}
// (a)
// at playing piles
for (int i = 0; i < PLAYING_LINES; i++) {
if (!playing_piles[i].is_empty() && playing_piles[i].top_is_opened()) {
const Card& top_card = playing_piles[i].top();
int top_card_number = top_card.number;
int top_card_symbol = static_cast<int>(top_card.symbol);
// can move
if (top_card_number == output_piles[top_card_symbol].size() + 1 &&
!equal_to_the_last_move(top_card, i + 1, last_moving.Output)) {
output_piles[top_card_symbol].push(top_card);
playing_piles[i].pop();
// print the description for the moving
update_last_moving(top_card, i + 1, last_moving.Output);
std::cout << "-------------------------------------------------\n";
if (output_file.is_open()) output_file << "-------------------------------------------------\n";
print_each(top_card, "playing #" + std::to_string(i + 1), "output");
std::cout << "\n";
if (output_file.is_open()) output_file << "\n";
is_moved = true;
score += EARNING_SCORE;
break;
}
}
}
if (is_moved) {
is_moved = false;
continue;
}
// at waste pile
if (!is_moved && !waste_pile.is_empty()) {
const Card& top_card = waste_pile.top();
int top_card_number = top_card.number;
int top_card_symbol = static_cast<int>(top_card.symbol);
// can move
if (top_card_number == output_piles[top_card_symbol].size() + 1) {
output_piles[top_card_symbol].push(top_card);
waste_pile.pop();
// print the description for the moving
update_last_moving(top_card, last_moving.Waste, last_moving.Output);
std::cout << "-------------------------------------------------\n";
if (output_file.is_open()) output_file << "-------------------------------------------------\n";
print_each(top_card, "waste", "output");
std::cout << "\n";
if (output_file.is_open()) output_file << "\n";
is_moved = true;
score += EARNING_SCORE;
}
}
if (is_moved) {
is_moved = false;
continue;
}
// (b)
if (!stock.is_empty()) {
// move a card from the stock to the waste
if (waste_pile.is_empty()) {
waste_pile.push(stock.top());
update_last_moving(stock.top(), last_moving.Stock, last_moving.Waste);
std::cout << "-------------------------------------------------\n";
if (output_file.is_open()) output_file << "-------------------------------------------------\n";
print_each(stock.top(), "stock", "waste");
std::cout << "\n";
if (output_file.is_open()) output_file << "\n";
stock.pop();
stock.set_closed_pos(stock.size() - 1);
is_moved = true;
}
}
if (is_moved) {
is_moved = false;
continue;
}
// waste to any playing piles
if (!waste_pile.is_empty()) {
const Card& waste_card = waste_pile.top();
const int waste_card_number = waste_card.number;
const int waste_card_symbol = static_cast<int>(waste_card.symbol);
for (int i = 0; i < PLAYING_LINES; i++) {
if (playing_piles[i].is_empty()) {
if (waste_card_number == Card::King) {
playing_piles[i].push(waste_card);
waste_pile.pop();
update_last_moving(waste_card, last_moving.Waste, i + 1);
std::cout << "-------------------------------------------------\n";
if (output_file.is_open()) output_file << "-------------------------------------------------\n";
print_each(waste_card, "waste", "playing #" + std::to_string(i + 1));
std::cout << "\n";
if (output_file.is_open()) output_file << "\n";
is_moved = true;
break;
}
} else {
const Card& top_card = playing_piles[i].top();
const int top_card_number = top_card.number;
const int top_card_symbol = static_cast<int>(top_card.symbol);
// the different color → can move
if (is_sussesive(waste_card, top_card)) {
playing_piles[i].push(waste_card);
waste_pile.pop();
update_last_moving(waste_card, last_moving.Waste, i + 1);
std::cout << "-------------------------------------------------\n";
if (output_file.is_open()) output_file << "-------------------------------------------------\n";
print_each(waste_card, "waste", "playing #" + std::to_string(i + 1));
std::cout << "\n";
if (output_file.is_open()) output_file << "\n";
is_moved = true;
break;
}
}
// a king → empty plie
if (playing_piles[i].is_empty() && waste_card_number == Card::King) {
playing_piles[i].push(waste_card);
waste_pile.pop();
update_last_moving(waste_card, last_moving.Waste, i + 1);
std::cout << "-------------------------------------------------\n";
if (output_file.is_open()) output_file << "-------------------------------------------------\n";
print_each(waste_card, "waste", "playing #" + std::to_string(i + 1));
std::cout << "\n";
if (output_file.is_open()) output_file << "\n";
is_moved = true;
break;
}
}
}
if (is_moved) {
is_moved = false;
continue;
}
// (c)
for (int i = 0; i < PLAYING_LINES; i++) {
CardPile tmp_stack;
if (playing_piles[i].is_empty()) {
continue;
}
// find the longest card sequence
for (int j = playing_piles[i].size() - 1; ; j--) {
tmp_stack.push(playing_piles[i].view(j));
const Card& tmp_stack_top = tmp_stack.top();
const int tmp_stack_top_number = tmp_stack_top.number;
const int tmp_stack_top_symbol = static_cast<int>(tmp_stack_top.symbol);
// if not incresing numbers or not alternating color → the longest sequence
if (j <= 0 ||
!playing_piles[i].is_opened(j - 1) ||
!is_sussesive(tmp_stack_top, playing_piles[i].view(j - 1))) {
break;
}
}
// compare top of the other lines
const Card& tmp_stack_top = tmp_stack.top();
const int tmp_stack_top_number = tmp_stack_top.number;
const int tmp_stack_top_symbol = static_cast<int>(tmp_stack_top.symbol);
// pile which to move the cards
int suitable_pile = -1;
for (int j = 0; j < PLAYING_LINES; j++) {
if (i == j) {
continue;
}
if (playing_piles[j].is_empty()) {
if (tmp_stack_top_number == Card::King) {
suitable_pile = j;
}
continue;
}
const Card& playing_pile_top = playing_piles[j].top();
const int playing_pile_top_number = playing_pile_top.number;
const int playing_pile_top_symbol = static_cast<int>(playing_pile_top.symbol);
// if incresing numbers and alternating color → suitable
if (is_sussesive(tmp_stack_top, playing_pile_top)) {
suitable_pile = j;
}
}
if (suitable_pile == -1 || equal_to_the_last_move(tmp_stack, i + 1, suitable_pile + 1)) {
continue;
}
// move to the other pile → move
// try to move king and the other cards from empty pile to the other empty pile → unnecessary moving
if (playing_piles[i].size() == tmp_stack.size() &&
tmp_stack_top.number == Card::King &&
playing_piles[suitable_pile].size() == 0) {
continue;
}
// cut from the orig playing piles
for (int j = 0; j < tmp_stack.size(); j++) {
playing_piles[i].pop();
}
update_last_moving(tmp_stack, i + 1, suitable_pile + 1);
// paste to the dest playing piles
std::cout << "-------------------------------------------------\n";
if (output_file.is_open()) output_file << "-------------------------------------------------\n";
solve_count++;
while (!tmp_stack.is_empty()) {
playing_piles[suitable_pile].push(tmp_stack.top());
print_each(tmp_stack.top(),
"playing #" + std::to_string(i + 1),
"playing #" + std::to_string(suitable_pile + 1),
false);
tmp_stack.pop();
}
std::cout << "\n";
if (output_file.is_open()) output_file << "\n";
is_moved = true;
break;
}
if (is_moved) {
is_moved = false;
continue;
}
// (e)
if (!stock.is_empty()) {
waste_pile.push(stock.top());
update_last_moving(stock.top(), last_moving.Stock, last_moving.Waste);
std::cout << "-------------------------------------------------\n";
if (output_file.is_open()) output_file << "-------------------------------------------------\n";
print_each(stock.top(), "stock", "waste");
std::cout << "\n";
if (output_file.is_open()) output_file << "\n";
stock.pop();
stock.set_closed_pos(stock.size() - 1);
is_moved = true;
}
if (is_moved) {
is_moved = false;
continue;
}
// fallback (no move while (a) to (e))
game_over = true;
break;
}
if (game_over) {
std::cout << "game over!\n"
<< "score: " << score << "\n";
if (output_file.is_open()) output_file << "game over!\n"
<< "score: " << score << "\n";
if (score > 150) {
std::cout <<"???";
}
} else {
std::cout << "game clear!\n"
<< "score: " << score << "\n";
if (output_file.is_open()) output_file << "game clear!\n"
<< "score: " << score << "\n";
}
return !game_over;
}
void Solitaire::print_board() {
int max_length = 0;
for (int i = 0; i < PLAYING_LINES; i++) {
if (max_length < playing_piles[i].size()) {
max_length = playing_piles[i].size();
}
}
Card** board = new Card*[max_length + 2];
bool** is_hidden = new bool*[max_length + 2];
for (int i = 0; i < max_length + 2; i++) {
board[i] = new Card[PLAYING_LINES > 7 ? PLAYING_LINES : 7];
is_hidden[i] = new bool[PLAYING_LINES > 7 ? PLAYING_LINES : 7];
for (int j = 0; j < (PLAYING_LINES > 7 ? PLAYING_LINES : 7); j++) {
board[i][j].number = -1;
}
}
// stock
if (!stock.is_empty()) {
board[0][0] = stock.top();
is_hidden[0][0] = !stock.top_is_opened();
}
// waste
if (!waste_pile.is_empty()) {
board[0][1] = waste_pile.top();
is_hidden[0][1] = !waste_pile.top_is_opened();
}
// output
for (int i = 0; i < 4; i++) {
if (output_piles[i].is_empty()) {
continue;
}
board[0][i + 3] = output_piles[i].top();
is_hidden[0][i + 3] = !output_piles[i].top_is_opened();
}
// playing piles
for (int i = 0; i < PLAYING_LINES; i++) {
if (playing_piles[i].is_empty()) {
continue;
}
CardPile tmp_pile = playing_piles[i];
for (int j = tmp_pile.size() - 1; j >= 0; j--) {
board[j + 2][i] = tmp_pile.view(j);
is_hidden[j + 2][i] = !tmp_pile.is_opened(j);
}
}
for (int i = 0; i < max_length + 2; i++) {
for (int j = 0; j < (PLAYING_LINES > 7 ? PLAYING_LINES : 7); j++) {
if (board[i][j].number == -1) {
std::cout << " ";
if (output_file.is_open()) output_file << " ";
continue;
}
if (is_hidden[i][j]) {
std::cout << "\033[1;34m";
} else if (board[i][j].symbol % 2 == 1) {
std::cout << "\033[1;31m";
}
std::cout << SYMBOL_STR[board[i][j].symbol] << NUMBER[board[i][j].number] << " ";
if (output_file.is_open()) output_file << SYMBOL_STR[board[i][j].symbol] << NUMBER[board[i][j].number] << " ";
std::cout << "\033[0m";
}
std::cout << "\n";
if (output_file.is_open()) output_file << "\n";
}
std::cout << "\n";
if (output_file.is_open()) output_file << "\n";
for (int i = 0; i < max_length + 2; i++) {
delete board[i];
delete is_hidden[i];
}
delete board;
delete is_hidden;
}