-
Notifications
You must be signed in to change notification settings - Fork 7
/
sol.c
1996 lines (1899 loc) · 58.7 KB
/
sol.c
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _DEFAULT_SOURCE /* for getopt, sigaction, usleep */
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <time.h>
#include <termios.h>
#include <unistd.h>
#include "sol.h"
#include "schemes.h"
struct playfield f;
struct opts op;
// action table {{{
/* stores a function pointer for every takeable action; called by game loop */
int (*action[NUM_PLACES][10])(int,int,int) = {
#ifdef KLONDIKE
/* 1 2 3 4 5 6 7 stk wst fnd*/
/* 1 */ { t2f, t2t, t2t, t2t, t2t, t2t, t2t, nop, nop, t2f },
/* 2 */ { t2t, t2f, t2t, t2t, t2t, t2t, t2t, nop, nop, t2f },
/* 3 */ { t2t, t2t, t2f, t2t, t2t, t2t, t2t, nop, nop, t2f },
/* 4 */ { t2t, t2t, t2t, t2f, t2t, t2t, t2t, nop, nop, t2f },
/* 5 */ { t2t, t2t, t2t, t2t, t2f, t2t, t2t, nop, nop, t2f },
/* 6 */ { t2t, t2t, t2t, t2t, t2t, t2f, t2t, nop, nop, t2f },
/* 7 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2f, nop, nop, t2f },
/*stk*/ { nop, nop, nop, nop, nop, nop, nop, nop, s2w, nop },
/*wst*/ { w2t, w2t, w2t, w2t, w2t, w2t, w2t, w2s, w2f, w2f },
/*fnd*/ { f2t, f2t, f2t, f2t, f2t, f2t, f2t, nop, nop, nop },
#elif defined SPIDER
/* 1 2 3 4 5 6 7 8 9 10*/
/* 1 */ { t2f, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t },
/* 2 */ { t2t, t2f, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t },
/* 3 */ { t2t, t2t, t2f, t2t, t2t, t2t, t2t, t2t, t2t, t2t },
/* 4 */ { t2t, t2t, t2t, t2f, t2t, t2t, t2t, t2t, t2t, t2t },
/* 5 */ { t2t, t2t, t2t, t2t, t2f, t2t, t2t, t2t, t2t, t2t },
/* 6 */ { t2t, t2t, t2t, t2t, t2t, t2f, t2t, t2t, t2t, t2t },
/* 7 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2f, t2t, t2t, t2t },
/* 8 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2f, t2t, t2t },
/* 9 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2f, t2t },
/*10 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2f },
/*stk*/ { s2t, s2t, s2t, s2t, s2t, s2t, s2t, s2t, s2t, s2t },
#elif defined FREECELL
/* 1 2 3 4 5 6 7 8 cll fnd*/
/* 1 */ { t2f, t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2c, t2f },
/* 2 */ { t2t, t2f, t2t, t2t, t2t, t2t, t2t, t2t, t2c, t2f },
/* 3 */ { t2t, t2t, t2f, t2t, t2t, t2t, t2t, t2t, t2c, t2f },
/* 4 */ { t2t, t2t, t2t, t2f, t2t, t2t, t2t, t2t, t2c, t2f },
/* 5 */ { t2t, t2t, t2t, t2t, t2f, t2t, t2t, t2t, t2c, t2f },
/* 6 */ { t2t, t2t, t2t, t2t, t2t, t2f, t2t, t2t, t2c, t2f },
/* 7 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2f, t2t, t2c, t2f },
/* 8 */ { t2t, t2t, t2t, t2t, t2t, t2t, t2t, t2f, t2c, t2f },
/*cll*/ { c2t, c2t, c2t, c2t, c2t, c2t, c2t, c2t, c2f, c2f },
/*fnd*/ { f2t, f2t, f2t, f2t, f2t, f2t, f2t, f2t, f2c, nop },
#endif
};
// }}}
// argv parsing, game loops, cleanup {{{
int main(int argc, char** argv) {
/* opinionated defaults: */
op.s = &unicode_large_color;
op.v = 1; /* enable fake visbell by default */
#ifdef SPIDER
op.m = MEDIUM;
#endif
int optget;
opterr = 0; /* don't print message on unrecognized option */
while ((optget = getopt (argc, argv, "+:hs:vbcmMV")) != -1) {
switch (optget) {
#ifdef SPIDER
case 's': /* number of suits */
switch (optarg[0]) {
case '1': op.m = EASY; break;
case '2': op.m = MEDIUM; break;
case '4': op.m = NORMAL; break;
default: goto error;
} break;
#endif
case 'b': op.s = &unicode_large_mono; break;
case 'c': op.s = &unicode_large_color; break;
case 'm': op.s = &unicode_small_mono; break; /* "mini, monochrome" */
case 'M': op.s = &unicode_small_color; break; /* "mini, colorful" */
case 'V': op.v = 0; break; /* WARN: experimental; might change */
case 'h': default: goto error;
error:
fprintf (stderr, SHORTHELP LONGHELP KEYHELP, argv[0]);
return optget != 'h';
}
}
signal_setup();
atexit (*quit);
signal_handler(SIGWINCH); /* initialize window size */
newgame:
screen_setup(1);
switch(sol()) {
case GAME_NEW: goto newgame;
case GAME_WON:
print_table(NO_HI, NO_HI);
win_anim();
if (getch(NULL)=='q') return 0;
goto newgame;
case GAME_QUIT: return 0;
}
}
#define is_tableu(where) (where <= TAB_MAX) /* "card games helper functions" */
int sol(void) {
long seed = time(NULL);
restart:
free_undo(f.u);
deal(seed);
int from, to, opt;
int ret;
for(;;) {
switch (get_cmd(&from, &to, &opt)) {
case CMD_MOVE:
ret = action[from][to](from,to,opt);
#ifdef FREECELL
if (ret == ERR && is_tableu(from) && to == from)
/* t2f failed? try t2c! */
ret = t2c(from, STOCK, 0);
#endif
#ifdef INVERSE_MOVE
if (ret == ERR && is_tableu(from) && is_tableu(to))
/* try again with from/to swapped: */
ret = action[to][from](to,from,opt);
#endif
switch (ret) {
case OK: break;
case ERR: visbell(); break;
case WON: return GAME_WON;
}
break;
case CMD_JOIN:
switch (join(to)) {
case OK: break;
case ERR: visbell(); break;
case WON: return GAME_WON;
}
break;
case CMD_HINT: break;//TODO: show a possible (and sensible) move. if possible, involve active cursor
case CMD_FIND:
f.h[0] = getchar(); /* NOTE: not using getch(), so f,esc clears hls */
f.h[1] = '\0';
break;
case CMD_SEARCH:
raw_mode(0);
printf("\r/"); fflush(stdout);
fgets(f.h, 3, stdin);
if (f.h[0] != '\n' && f.h[1] != '\n') while(getchar()!='\n'); // note: when we read 1 byte, it is followed by CR, NUL. if we read two bytes (or more), it is only followed by NUL, since there is no space for the CR. TODO: cleanup
raw_mode(1);
f.h[2] = '\0';
break;
case CMD_UNDO: undo_pop(f.u); break;
case CMD_INVAL: visbell(); break;
case CMD_NEW: return GAME_NEW;
case CMD_AGAIN: goto restart;
case CMD_QUIT: return GAME_QUIT;
case CMD_HELP:
printf (KEYHELP "\nPress any key to continue.");
getch(NULL);
break;
}
}
}
void quit(void) {
screen_setup(0);
free_undo(f.u);
}
//}}}
// card games helper functions {{{
#define get_suit(card) \
((card-1) % NUM_SUITS)
#define get_rank(card) \
((card-1) / NUM_SUITS)
#define get_color(card) \
((get_suit(card) ^ get_suit(card)>>1) & 1)
int find_top(card_t* pile) {
// TODO: on use, must check that result != -1
int i;
for(i=PILE_SIZE-1; i>=0 && !pile[i]; i--);
return i;
}
int first_movable(card_t* pile) {
/* NOTE: in FREECELL this does not take max_move into account! */
int i = 0;
for (;pile[i] && !is_movable(pile, i); i++);
return i;
}
int turn_over(card_t* pile) {
int top = find_top(pile);
if (pile[top] < 0) {
pile[top] *= -1;
return 1;
} else return 0;
}
int check_won(void) {
for (int pile = 0; pile < NUM_DECKS*NUM_SUITS; pile++)
if (f.f[pile][NUM_RANKS-1] == NO_CARD) return 0;
return 1;
}
int rank_next (card_t a, card_t b) {
return get_rank(a) == get_rank(b)-1;
}
int color_ok (card_t a, card_t b) {
#if defined KLONDIKE || defined FREECELL
/* color opposite? */
return (get_color(a) != get_color(b));
#elif defined SPIDER
/* same suit? */
return (get_suit(a) == get_suit(b));
#endif
}
int is_consecutive (card_t* pile, int pos) {
if (pos+1 >= PILE_SIZE) return 1; /* card is last */
if (pile[pos+1] == NO_CARD) return 1; /* card is first */
/* ranks consecutive? */
if (!rank_next(pile[pos+1], pile[pos])) return 0;
/* color/suit OK? */
if (!color_ok(pile[pos+1], pile[pos])) return 0;
return 1;
}
int is_movable(card_t* pile, int n) {
#ifdef KLONDIKE
return(pile[n] > NO_CARD); /*non-movable cards don't exist in klondike*/
#elif defined SPIDER || defined FREECELL
int top = find_top(pile);
for (int i = top; i >= 0; i--) {
if (pile[i] <= NO_CARD) return 0; /*no card or card face down?*/
if (!is_consecutive(pile, i)) return 0;
if (i == n) return 1; /* card reached, must be movable */
}
return 0;
#endif
}
int hls(card_t card, char* hi) {
/* checks if a card matches a highlight search. a hilight search might be a rank, a suit, a color or both. */
// TODO: now we use rankletters in keyboard input and here. that's ugly.
int ok = 0; /* prevent an invalid highlight from matching everything */
for (; *hi; hi++) {
switch(*hi) {
/* letter ranks: */
case 'a': case 'A': if (get_rank(card)!=RANK_A) return 0; ok++; break;
case '0':
case 'x': case 'X': if (get_rank(card)!=RANK_X) return 0; ok++; break;
case 'j': case 'J': if (get_rank(card)!=RANK_J) return 0; ok++; break;
case 'q': case 'Q': if (get_rank(card)!=RANK_Q) return 0; ok++; break;
case 'k': case 'K': if (get_rank(card)!=RANK_K) return 0; ok++; break;
/* suits: */
case 'c': case 'C': if (get_suit(card)!=CLUBS) return 0; ok++; break;
case 'd': case 'D': if (get_suit(card)!=DIAMONDS)return 0;ok++; break;
case 'h': case 'H': if (get_suit(card)!=HEARTS) return 0; ok++; break;
case 's': case 'S': if (get_suit(card)!=SPADES) return 0; ok++; break;
/* colours: */
case 'r': case 'R': if (get_color(card)!=RED) return 0; ok++; break;
case 'b': case 'B': if (get_color(card)!=BLK) return 0; ok++; break;
/* special: */
#if defined KLONDIKE || defined FREECELL
case 'f': case 'F': { /* highlight cards that go on the foundation next */
card_t* foundation = f.f[get_suit(card)];
int top = find_top(foundation);
if (top >= 0 && foundation[top]) {
if (rank_next(foundation[top], card) &&
get_suit(card) == get_suit(foundation[top]))
return 1;
} else {
if (get_rank(card) == RANK_A) return 1;
}
return 0;}
#endif // NOTE: makes no sense in SPIDER
/* number ranks: */
default:
if (*hi < '1' || *hi > '9') continue;
if (get_rank(card) != *hi - '1') return 0;
ok++;
}
}
return ok;
}
//}}}
// takeable actions {{{
#ifdef KLONDIKE
card_t stack_take(void) { /*NOTE: assert(f.w >= 0) */
card_t card = f.s[f.w];
/* move stack one over, so there are no gaps in it: */
for (int i = f.w; i < f.z-1; i++)
f.s[i] = f.s[i+1];
f.z--;
f.w--; /* make previous card visible again */
return card;
}
int t2f(int from, int to, int opt) { /* tableu to foundation */
(void) to; (void) opt; /* don't need */
int top_from = find_top(f.t[from]);
to = get_suit(f.t[from][top_from]);
int top_to = find_top(f.f[to]);
if ((top_to < 0 && get_rank(f.t[from][top_from]) == RANK_A)
|| (top_to >= 0 && rank_next(f.f[to][top_to],f.t[from][top_from]))) {
f.f[to][top_to+1] = f.t[from][top_from];
f.t[from][top_from] = NO_CARD;
undo_push(from, FOUNDATION, to,
turn_over(f.t[from]));
if (check_won()) return WON;
return OK;
} else return ERR;
}
int w2f(int from, int to, int opt) { /* waste to foundation */
(void) from; (void) to; (void) opt; /* don't need */
if (f.w < 0) return ERR;
to = get_suit(f.s[f.w]);
int top_to = find_top(f.f[to]);
if ((top_to < 0 && get_rank(f.s[f.w]) == RANK_A)
|| (top_to >= 0 && rank_next(f.f[to][top_to], f.s[f.w]))) {
undo_push(WASTE, FOUNDATION, f.w | to<<16, 0);//ugly encoding :|
f.f[to][top_to+1] = stack_take();
if (check_won()) return WON;
return OK;
} else return ERR;
}
int s2w(int from, int to, int opt) { /* stock to waste */
(void) from; (void) to; (void) opt; /* don't need */
if (f.z == 0) return ERR;
f.w++;
if (f.w == f.z) f.w = -1;
return OK;
}
int w2s(int from, int to, int opt) { /* waste to stock (undo stock to waste) */
(void) from; (void) to; (void) opt; /* don't need */
if (f.z == 0) return ERR;
f.w--;
if (f.w < -1) f.w = f.z-1;
return OK;
}
int f2t(int from, int to, int opt) { /* foundation to tableu */
(void) from; /* don't need */
int top_to = find_top(f.t[to]);
from = opt;
int top_from = find_top(f.f[from]);
if (color_ok(f.t[to][top_to], f.f[from][top_from])
&& (rank_next(f.f[from][top_from], f.t[to][top_to]))) {
f.t[to][top_to+1] = f.f[from][top_from];
f.f[from][top_from] = NO_CARD;
undo_push(FOUNDATION, to, from, 0);
return OK;
} else return ERR;
}
int w2t(int from, int to, int opt) { /* waste to tableu */
(void) from; (void) opt; /* don't need */
if (f.w < 0) return ERR;
int top_to = find_top(f.t[to]);
if ((color_ok(f.t[to][top_to], f.s[f.w])
&& (rank_next(f.s[f.w], f.t[to][top_to])))
|| (top_to < 0 && get_rank(f.s[f.w]) == RANK_K)) {
undo_push(WASTE, to, f.w, 0);
f.t[to][top_to+1] = stack_take();
return OK;
} else return ERR;
}
int t2t(int from, int to, int opt) { /* tableu to tableu */
(void) opt; /* don't need */
int top_to = find_top(f.t[to]);
int top_from = find_top(f.t[from]);
for (int i = top_from; i >=0; i--) {
if ((color_ok(f.t[to][top_to], f.t[from][i])
&& (rank_next(f.t[from][i], f.t[to][top_to]))
&& f.t[from][i] > NO_CARD) /* card face up? */
|| (top_to < 0 && get_rank(f.t[from][i]) == RANK_K)) {
/* move cards [i..top_from] to their destination */
int count = 0;
for (;i <= top_from; i++) {
top_to++;
f.t[to][top_to] = f.t[from][i];
f.t[from][i] = NO_CARD;
count++;
}
undo_push(from, to, count,
turn_over(f.t[from]));
return OK;
}
}
return ERR; /* no such move possible */
}
#elif defined SPIDER
int remove_if_complete (int pileno) { //cleanup!
card_t* pile = f.t[pileno];
/* test if K...A complete; move to foundation if so */
int top_from = find_top(pile);
if (get_rank(pile[top_from]) != RANK_A) return 0;
for (int i = top_from; i>=0; i--) {
if (!is_consecutive (pile, i)) return 0;
if (i+RANK_K == top_from /* if ace to king: remove it */
&& get_rank(pile[top_from-RANK_K]) == RANK_K) {
for(int i=top_from, j=0; i>top_from-NUM_RANKS; i--,j++){
f.f[f.w][j] = pile[i];
pile[i] = NO_CARD;
}
undo_push(pileno, FOUNDATION, f.w,
turn_over(pile));
f.w++;
return 1;
}
}
return 0;
}
int t2t(int from, int to, int opt) { //in dire need of cleanup
int top_from = find_top(f.t[from]);
int top_to = find_top(f.t[to]);
int empty_to = (top_to < 0)? opt: -1; /* empty pile? */
for (int i = top_from; i >= 0; i--) {
if (!is_consecutive(f.t[from], i)) break;
/* is consecutive OR to empty pile and rank ok? */
if (rank_next(f.t[from][i], f.t[to][top_to])
|| (empty_to >= RANK_A && get_rank(f.t[from][i]) == empty_to)) {
int count = 0;
for (;i <= top_from; i++) {
top_to++;
f.t[to][top_to] = f.t[from][i];
f.t[from][i] = NO_CARD;
count++;
}
undo_push(from, to, count,
turn_over(f.t[from]));
remove_if_complete(to);
if (check_won()) return WON;
return OK;
}
}
return ERR; /* no such move possible */
}
int s2t(int from, int to, int opt) {
(void) from; (void) to; (void) opt; /* don't need */
if (f.z <= 0) return ERR; /* stack out of cards */
for (int pile = 0; pile < NUM_PILES; pile++)
if (f.t[pile][0]==NO_CARD) return ERR; /*no piles may be empty*/
undo_push(STOCK, TABLEU, 1, 0); /* NOTE: before remove_if_complete()! */
for (int pile = 0; pile < NUM_PILES; pile++) {
f.t[pile][find_top(f.t[pile])+1] = f.s[--f.z];
remove_if_complete(pile);
if (check_won()) return WON;
}
return OK;
}
int t2f(int from, int to, int opt) {
(void) to; (void) opt; /* don't need */
/* manually retrigger remove_if_complete() (e.g. after undo_pop) */
return remove_if_complete(from)?OK:ERR;
}
#elif defined FREECELL
int max_move(int from, int to) {
/* returns the maximum number of cards that can be moved */
/* see also: https://boardgames.stackexchange.com/a/45157/26498 */
int free_tabs = 0, free_cells = 0;
for (int i = 0; i < NUM_PILES; i++) free_tabs += f.t[i][0] == NO_CARD;
for (int i = 0; i < NUM_CELLS; i++) free_cells += f.s[i] == NO_CARD;
/* don't count the tableau we are moving to: */
if (to >= 0 && f.t[to][0] == NO_CARD) free_tabs--;
/* theoretic maximum is limited by the number of cards on the pile */
int max_theory = (1<<free_tabs) * (free_cells + 1);
int max_effective = 1 + find_top(f.t[from]) - first_movable(f.t[from]);
return max_effective < max_theory? max_effective : max_theory;
}
//TODO FREECELL: auto move to tableu after each move (not all cards possible, only when it is the smallest rank still on the board)
int t2t(int from, int to, int opt) {
int top_to = find_top(f.t[to]);
int top_from = find_top(f.t[from]);
int cards = max_move(from, to);
if (top_to < 0) { /* moving to empty pile? */
if (opt > cards)
return ERR; /* cannot execute move */
cards = opt; /* user wants to move n cards*/
}
for (int i = top_from; i >=0; i--) {
if (cards-->0/*enough space and not more attempted than wanted*/
&& ((top_to >= 0 /* if destn. not empty: check rank/color */
&& (color_ok(f.t[to][top_to], f.t[from][i])
&& (rank_next(f.t[from][i], f.t[to][top_to]))))
|| (top_to < 0 && !cards))) {/*if dest empty and right # cards*/
/* move cards [i..top_from] to their destination */
int count = 0;
for (;i <= top_from; i++) {
top_to++;
f.t[to][top_to] = f.t[from][i];
f.t[from][i] = NO_CARD;
count++;
}
undo_push(from, to, count, 0);
return OK;
}
}
return ERR; /* no such move possible */
}
int t2f(int from, int to, int opt) { /* 1:1 copy from KLONDIKE */
(void) to; (void) opt; /* don't need */
int top_from = find_top(f.t[from]);
to = get_suit(f.t[from][top_from]);
int top_to = find_top(f.f[to]);
if ((top_to < 0 && get_rank(f.t[from][top_from]) == RANK_A)
|| (top_to >= 0 && rank_next(f.f[to][top_to],f.t[from][top_from]))) {
f.f[to][top_to+1] = f.t[from][top_from];
f.t[from][top_from] = NO_CARD;
undo_push(from, FOUNDATION, to, 0);
if (check_won()) return WON;
return OK;
} else return ERR;
}
int f2t(int from, int to, int opt) {
(void) from; /* don't need */
int top_to = find_top(f.t[to]);
from = opt;
int top_from = find_top(f.f[from]);
if (top_to < 0 /* empty tableu? */
||(color_ok(f.t[to][top_to], f.f[from][top_from])
&& (rank_next(f.f[from][top_from], f.t[to][top_to])))) {
f.t[to][top_to+1] = f.f[from][top_from];
f.f[from][top_from] = NO_CARD;
undo_push(FOUNDATION, to, from, 0);
return OK;
} else return ERR;
}
int t2c(int from, int to, int opt) {
(void) to; (void) opt; /* don't need */
/* is a cell free? */
if (f.w == (1<<NUM_CELLS)-1)
return ERR;
for (to = 0; to < NUM_CELLS; to++)
if (!(f.w>>to&1)) break;
/* move 1 card */
int top_from = find_top(f.t[from]);
f.s[to] = f.t[from][top_from];
f.t[from][top_from] = NO_CARD;
f.w |= 1<<to; /* mark cell as occupied */
undo_push(from, STOCK, to, 0);
return OK;
}
int c2t(int from, int to, int opt) {
(void) from; /* don't need */
int top_to = find_top(f.t[to]);
from = opt;
if (top_to < 0 /* empty tableu? */
||(color_ok(f.t[to][top_to], f.s[from])
&& (rank_next(f.s[from], f.t[to][top_to])))) {
f.t[to][top_to+1] = f.s[from];
f.s[from] = NO_CARD;
f.w &= ~(1<<from); /* mark cell as free */
undo_push(STOCK, to, from, 0);
return OK;
} else return ERR;
return ERR;
}
int c2f(int from, int to, int opt) {
(void) from; (void) to; /* don't need */
from = opt;
if (f.s[from] == NO_CARD) return ERR;
to = get_suit(f.s[from]);
int top_to = find_top(f.f[to]);
if ((top_to < 0 && get_rank(f.s[from]) == RANK_A)
|| (top_to >= 0 && rank_next(f.f[to][top_to],f.s[from]))) {
f.f[to][top_to+1] = f.s[from];
f.s[from] = NO_CARD;
f.w &= ~(1<<from); /* mark cell as free */
undo_push(STOCK, FOUNDATION, from | to<<16, 0);
if (check_won()) return WON;
return OK;
} else return ERR;
}
int f2c(int from, int to, int opt) {
(void) from; (void) to; /* don't need */
/* is a cell free? */
if (f.w == (1<<NUM_CELLS)-1)
return ERR;
for (to = 0; to < NUM_CELLS; to++)
if (!(f.w>>to&1)) break;
/* move 1 card */
from = opt;
int top_from = find_top(f.f[from]);
f.s[to] = f.f[from][top_from];
f.f[from][top_from] = NO_CARD;
f.w |= 1<<to; /* mark cell as occupied */
undo_push(FOUNDATION, STOCK, from | to<<16, 0);
return OK;
}
#define w2f c2f /* for join()'s "to foundation" */
#endif
//TODO: generalize prediction engine for CMD_HINT
#ifdef KLONDIKE
#define would_complete(pile) 0
#elif defined SPIDER
#define would_complete(pile) \
(get_rank(f.t[pile][r[pile].top]) == RANK_A \
&& get_rank(f.t[to][bottom_to]) == RANK_K)
#elif defined FREECELL
#define would_complete(pile) 0
#endif
#define would_turn(pile) \
(f.t[pile][r[pile].pos-1] < 0)
#define would_empty(pile) \
(r[pile].pos == 0)
int join(int to) {
int top_to = find_top(f.t[to]);
#ifdef SPIDER
int bottom_to = first_movable(f.t[to]);
#endif
#if defined KLONDIKE || defined FREECELL
if (to == WASTE || to == STOCK) return ERR; /*why would you do that!?*/
if (to == FOUNDATION) {
int status = ERR;
for (int i = 0; i < NUM_PILES+NUM_CELLS; i++)
switch (is_tableu(i)?t2f(i, FOUNDATION, 0)
:w2f(STOCK,FOUNDATION,i-NUM_PILES)){
case WON: return WON;
case OK: status = OK;
case ERR: /* nop */;
}
return status;
}
#endif
#ifdef KLONDIKE
if (top_to < 0) { /* move a king to empty pile: */
for (int i = 0; i <= TAB_MAX; i++) {
if (f.t[i][0] < 0) /* i.e. would turn? */
if (t2t(i, to, 0) == OK) return OK;
}
return w2t(WASTE, to, 0);
}
#elif defined FREECELL
if (top_to < 0) { /* move longest cascade to empty tableu: */ //TODO FREECELL:
int longest = -1;
int length = -1;
for (int i = 0; i <= TAB_MAX; i++) {
int m = max_move(i, to);
/*longest cascade that won't uncover another free pile*/
//TODO: don't rip apart cascades
if (m >= length && m <= find_top(f.t[i]))
length = m, longest = i;
}
if (longest < 0) return ERR;
return t2t(longest, to, length);
}
#endif
struct rating {
int ok:1; /* card to move in pile? */
int above; /* number of movable cards above */
int below; /* number of cards below ours */
int pos; /* where the card to move is in the pile */
int top; /* find_top() */
} r[NUM_PILES] = {{0}};
int complete = 0;/* SPIDER: true if any pile would complete a stack */
int turn = 0; /* SPIDER: true if any pile would turn_over */
int empty = 0; /* true if any pile would become empty */
/* 1. rate each pile: */
#ifdef SPIDER
if (top_to < 0) {
for (int pile = 0; pile < NUM_PILES; pile++) {
if (pile == to) continue;
int top = find_top(f.t[pile]);
int bottom = first_movable(f.t[pile]);
r[pile].pos = bottom; /* need for would_empty */
if (top < 0) continue; /* no cards to move */
if (would_empty(pile)) continue; /* doesn't help */
r[pile].ok++;
r[pile].above = 0; /* always take as many as possible */
r[pile].below = top - bottom;
r[pile].top = top;
complete |= would_complete(pile); /* never happens */
turn |= would_turn(pile);
empty |= would_empty(pile);
}
} else
#endif
for (int pile = 0; pile < NUM_PILES; pile++) {
r[pile].top = r[pile].pos = find_top(f.t[pile]);
/* backtrack until we find a compatible-to-'to'-pile card: */
#ifdef FREECELL
int maxmove = max_move(pile, -1);
#endif
while (r[pile].pos >= 0 && is_movable(f.t[pile], r[pile].pos)) {
int rankdiff = get_rank(f.t[pile][r[pile].pos])
- get_rank(f.t[to][top_to]);
if (rankdiff >= 0) break; /* past our card */
#ifdef FREECELL
if (!maxmove--) break; /* can't move this many cards */
#endif
if (rankdiff == -1 && /* rank matches */
color_ok(f.t[pile][r[pile].pos], f.t[to][top_to])
) {
r[pile].ok++;
complete |= would_complete(pile);
turn |= would_turn(pile);
empty |= would_empty(pile);
for (int i = r[pile].pos; i >= 0; i--)
if (is_movable(f.t[pile], i-1))
r[pile].above++;
else break;
break;
}
r[pile].pos--;
r[pile].below++;
}
}
/* 2. find optimal pile: (optimized for spider) */
//todo: in spider, prefer longest piles if above==0 (faster completions)
int from = -1;
for (int pile = 0, above = 99, below = 99; pile < NUM_PILES; pile++) {
if (!r[pile].ok) continue;
/* don't bother if another pile would be better: prefer ... */
/* ... to complete a stack: */
if (!would_complete(pile) && complete) continue;
/* ... emptying piles: */
if (!would_empty(pile) && empty && !complete) continue;
/* ... to turn_over: */
if (!would_turn(pile) && turn && !complete && !empty) continue;
/* ... not to rip apart too many cards: */
if (r[pile].above > above) continue;
/* if tied, prefer ... */
if (r[pile].above == above
/* ... larger pile if destination is empty */
&& (top_to < 0? r[pile].below < below
/* ... shorter pile otherwise */
: r[pile].below > below))
continue;
from = pile;
above = r[pile].above;
below = r[pile].below;
}
/* 3. move cards over and return: */
#ifdef KLONDIKE
/* prefer waste if it wouldn't turn_over: */
/* NOTE: does not attempt to take from froundation */
if (!empty && !turn && w2t(WASTE, to, 0) == OK)
return OK;
if (from < 0) /* nothing found */
return ERR;
return t2t(from, to, 0);
#elif defined SPIDER
if (from < 0) /* nothing found */
return ERR;
int bottom = first_movable(f.t[from]);
return t2t(from, to, get_rank(f.t[from][bottom]));
#elif defined FREECELL
//TODO: if would rip apart, try freecells first (instead after)
if (from < 0) /* no tableu move found */ {
/* try all free cells before giving up: */
for (int i = 0; i < NUM_CELLS; i++)
if (c2t(STOCK, to, i) == OK) return OK;
return ERR;
}
return t2t(from, to, 0);
#endif
}
#undef would_empty
#undef would_turn
#undef would_complete
int nop(int from, int to, int opt) { (void)from;(void)to;(void)opt;return ERR; }
// }}}
// keyboard input handling {{{
// cursor functions{{{
#ifdef KLONDIKE
void cursor_left (struct cursor* cursor) {
op.h = 1;
if (is_tableu(cursor->pile)) {
if (cursor->pile > 0) cursor->pile--;
cursor->opt = 0;
} else { /* stock/waste/foundation*/
switch (cursor->pile) {
case WASTE: cursor->pile = STOCK; cursor->opt = 0; break;
case FOUNDATION:
if (cursor->opt <= 0)
cursor->pile = WASTE;
else
cursor->opt--;
}
}
}
void cursor_down (struct cursor* cursor) {
op.h = 1;
if (!is_tableu(cursor->pile)) {
switch (cursor->pile) {
case STOCK: cursor->pile = TAB_1; break;
case WASTE: cursor->pile = TAB_2; break;
case FOUNDATION:
cursor->pile = TAB_4 + cursor->opt;
}
cursor->opt = 0;
}
}
void cursor_up (struct cursor* cursor) {
op.h = 1;
if (is_tableu(cursor->pile)) {
switch (cursor->pile) { //ugly :|
case TAB_1: cursor->pile = STOCK; break;
case TAB_2: cursor->pile = WASTE; break;
case TAB_3: cursor->pile = WASTE; break;
case TAB_4: case TAB_5: case TAB_6: case TAB_7:
cursor->opt=cursor->pile-TAB_4;
cursor->pile = FOUNDATION;
break;
}
}
}
void cursor_right (struct cursor* cursor) {
op.h = 1;
if (is_tableu(cursor->pile)) {
if (cursor->pile < TAB_MAX) cursor->pile++;
cursor->opt = 0;
} else {
switch (cursor->pile) {
case STOCK: cursor->pile = WASTE; break;
case WASTE: cursor->pile = FOUNDATION;cursor->opt = 0; break;
case FOUNDATION:
if (cursor->opt < NUM_SUITS-1)
cursor->opt++;
}
}
}
#elif defined SPIDER
/*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
void cursor_left (struct cursor* cursor) {
op.h = 1;
if (cursor->pile > 0) cursor->pile--;
cursor->opt = 0;
}
void cursor_down (struct cursor* cursor) {
op.h = 1;
int first = first_movable(f.t[cursor->pile]);
int top = find_top(f.t[cursor->pile]);
if (first + cursor->opt < top)
cursor->opt++;
}
void cursor_up (struct cursor* cursor) {
op.h = 1;
if (cursor->opt > 0) cursor->opt--;
}
void cursor_right (struct cursor* cursor) {
op.h = 1;
if (cursor->pile < TAB_MAX) cursor->pile++;
cursor->opt = 0;
}
#elif defined FREECELL
void cursor_left (struct cursor* cursor) {
op.h = 1;
if (is_tableu(cursor->pile)) {
if (cursor->pile > 0) cursor->pile--;
cursor->opt = 0;
} else { /* cells/foundation*/
switch (cursor->pile) {
case STOCK:
if (cursor->opt > 0)
cursor->opt--;
break;
case FOUNDATION:
if (cursor->opt <= 0) {
cursor->pile = STOCK;
cursor->opt = 3;
} else {
cursor->opt--;
}
}
}
}
void cursor_down (struct cursor* cursor) {
op.h = 1;
if (is_tableu(cursor->pile)) {
if (cursor->opt < max_move(cursor->pile, -1)-1)
cursor->opt++;
} else {
cursor->pile = cursor->opt+NUM_CELLS*(cursor->pile==FOUNDATION);
cursor->opt = 0;
}
}
void cursor_up (struct cursor* cursor) {
op.h = 1;
if (is_tableu(cursor->pile)) {
if (cursor->opt > 0) {
cursor->opt--;
} else {
switch (cursor->pile) {
case TAB_1: case TAB_2: case TAB_3: case TAB_4:
cursor->opt = cursor->pile; /*assumes TAB_1==0*/
cursor->pile = STOCK;
break;
case TAB_5: case TAB_6: case TAB_7: case TAB_8:
cursor->opt = cursor->pile - NUM_CELLS;
cursor->pile = FOUNDATION;
}
}
}
}
void cursor_right (struct cursor* cursor) {
op.h = 1;
if (is_tableu(cursor->pile)) {
if (cursor->pile < TAB_MAX) cursor->pile++;
cursor->opt = 0;
} else {
switch (cursor->pile) {
case STOCK:
if (cursor->opt < NUM_SUITS-1) {
cursor->opt++;
} else {
cursor->pile = FOUNDATION;
cursor->opt = 0;
} break;
case FOUNDATION:
if (cursor->opt < NUM_SUITS-1)
cursor->opt++;
}
}
}
#endif
void cursor_to (struct cursor* cursor, int pile) {
op.h = 1;
cursor->pile = pile;
cursor->opt = 0;
}
int set_mouse(int pile, int* main, int* opt) {
//TODO: this should set cursor.opt, so card selector choice dialog does not trigger!
op.h = 0;
if (pile < 0) return 1;
*main = pile;
#ifdef KLONDIKE
if (pile >= FOUNDATION)//TODO: check upper bound!
*main = FOUNDATION,
*opt = pile - FOUNDATION;
#elif defined SPIDER
(void)opt;
#elif defined FREECELL
if (pile > TAB_MAX) {
*main = pile-STOCK < NUM_CELLS? STOCK : FOUNDATION;
*opt = (pile-STOCK) % 4;
}
#endif
return 0;
}
//}}}
int get_cmd (int* from, int* to, int* opt) {
int _f, t;
unsigned char mouse[6] = {0}; /* must clear [3]! */
struct cursor inactive = {-1,-1};
static struct cursor active = {0,0};
static char last_successful_action[2] = {0,0}; //TODO: dot implementation should be in main game loop (CMD_AGAIN)
if (is_tableu(active.pile))
active.opt = 0;
/***/
from_l: print_table(&active, &inactive);
_f = getch(mouse);
switch (_f) {
/* direct addressing: */
case '1': *from = TAB_1; break;