-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanda.cpp
1647 lines (1285 loc) · 31.1 KB
/
sanda.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
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
// This is the Streets and Alleys Rule set
// Here, we draw our board, handel input, and verify moves.
#include "game.h"
#include "stdafx.h"
#include "debug.h"
#include "cardtools.h"
#include "sanda.h"
#include <time.h>
#include <math.h>
#include "colordefs.h"
#include "winteaser.h"
//using namespace Std;
StreetsAndAlleys::StreetsAndAlleys(HWND hWind, int info) {
int i, j, k, n;
//dprintf("Sucessfully called StreetsAndAlleys Constructor! hWind = %i\n", (int)hWind);
Init(hWind, info);
// clear the board
for (i = 0; i < 5; i++)
for (j = 0; j < 3; j++)
for (k = 0; k < 3; k++)
for (n = 0; n < 3; n++)
table[i][j][k][n] = 0;
startCard = 0;
back = NULL;
place.columb = 0;
place.row = 0;
place.depth = 0;
place.region = 0;
won = true;
return;
}
void StreetsAndAlleys::deal() {
int i, j, k, n;
CARDDIM cardDim = getCardAttributes();
UNDO *del = back;
// clear the board
for (i = 0; i < 5; i++)
for (j = 0; j < 3; j++)
for (k = 0; k < 4; k++)
for (n = 0; n < 3; n++)
table[i][j][k][n] = 0;
n = 0; // Reset n which is our "card in deck" counter.
// Main board (5rows x3columbs x3deep) (zone 1)
for (i = 0; i < 5; i++) {
for (j = 0; j < 3; j++) {
for (k = 0; k < 3; k++) {
table[i][j][k][0] = deck[n];
n++;
}
}
}
// Free Board (3columbs x3deep) (zone 2)
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
table[i][0][j][1] = deck[n];
n++;
}
}
// n should = 51
startCard = deck[n];
table[0][0][0][2] = startCard; // Place Board (2columbs x2rows x1deep (last one))
// (zone 3)
// Deal is effectively our init function.
// We need to initialize our key variables here.
score = 1;
height = (int)(cardDim.Height * 1.30); // rows will want to have a 30% margin
// we put this here so it is not recalculated
// 1000+ times
width = cardDim.Width;
selected.columb = 0;
selected.row = 0;
selected.depth = 0;
selected.region = -1;
loc.columb = 0;
loc.row = 0;
loc.columb = 0;
loc.region = 0;
place.columb = 0;
place.row = 0;
place.depth = 3;
place.region = 0;
won = false;
// Contain a possible memory hole when running multiple games
while (back != NULL) {
back = back->previous;
delete del;
del = back;
}
return;
}
void StreetsAndAlleys::leftClick(int X, int Y) {
int region = getRegion(X, Y);
LOCATION card;
TABLELOC temp;
switch (region) {
case 0:
card = getRegion1(X, Y);
break;
case 1:
card = getRegion2(X, Y);
break;
case 2:
card = getRegion3(X, Y);
break;
}
/*eraseCard(hWnd, 800, 600, 0);
if (card.isValid) {
drawCard(hWnd, 800, 600, table[card.columb][card.row][card.depth][region],
0, false);
}*/
if (!card.isValid)
return;
if (selected.region == -1) {
selectCard(card, region);
return;
} else if (card.columb == selected.columb && card.row == selected.row &&
card.depth == selected.depth && region == selected.region) {
temp.columb = card.columb;
temp.row = card.row;
temp.depth = card.depth;
temp.region = region;
selected.region = -1;
placeCard(temp);
return;
}
// We have a valid selected card and space
if (moveIsValid(card, region))
makeMove(card, region);
return;
}
void StreetsAndAlleys::rightClick(int X, int Y) {
int region = getRegion(X, Y);
LOCATION card;
if (won) // don't do anything if the game has been won
return;
switch (region) {
case 0:
card = getRegion1(X, Y);
break;
case 1:
card = getRegion2(X, Y);
break;
case 2:
card = getRegion3(X, Y);
break;
}
loc.columb = card.columb;
loc.row = card.row;
loc.depth = card.depth;
loc.region = region;
if (card.isValid)
placeCard(loc);
return;
}
int StreetsAndAlleys::load(const char *filename) {
FILE *file = fopen(filename, "rb");
int i, j, k, region;
UNDO *del = NULL, *temp = NULL;
char name[256];
ZeroMemory(name, 256);
if (file == NULL)
return FALSE;
i = fgetc(file);
if (i == EOF) {
fclose(file);
return i;
}
i = fgetc(file);
if (i == EOF) {
fclose(file);
return i;
}
if (fgets(name, i, file) == NULL) {
fclose(file);
return EOF;
}
if (strcmp(name, gameName()) != 0) {
fclose(file);
return INVAL_GAME_TYPE;
}
// There should not be any errors after this as we have validated the file
// First, clear our game
deal();
for(i = 0; i < 52; i++)
deck[i] = fgetc(file);
for (region = 0; region < 3; region++)
for (i = 0; i < 5; i++)
for (j = 0; j < 3; j++)
for (k = 0; k < 3; k++)
table[i][j][k][region] = fgetc(file);
j = fgetc(file); // number of undo structures
for (i = 0; i < j; i++) {
del = new UNDO;
del->source.columb = fgetc(file);
del->source.row = fgetc(file);
del->source.depth = fgetc(file);
del->source.region = fgetc(file);
del->dest.columb = fgetc(file);
del->dest.row = fgetc(file);
del->dest.depth = fgetc(file);
del->dest.region = fgetc(file);
del->previous = temp;
temp = del;
}
temp = NULL;
back = NULL;
// Now we need to swap the structure
while (del != NULL) {
back = new UNDO;
back->previous = temp;
back->source.columb = del->source.columb;
back->source.row = del->source.row;
back->source.depth = del->source.depth;
back->source.region = del->source.region;
back->dest.columb = del->dest.columb;
back->dest.row = del->dest.row;
back->dest.depth = del->dest.depth;
back->dest.region = del->dest.region;
temp = del;
del = del->previous;
delete temp;
temp = back;
}
startCard = fgetc(file);
score = fgetc(file);
i = fgetc(file);
if (i != 255) {
// Something terribly wrong happened
shuffel();
deal();
redrawBoard();
fclose(file);
return i;
}
redrawBoard();
fclose(file);
return TRUE;
}
int StreetsAndAlleys::save(const char *filename) {
FILE *file = fopen(filename, "wb");
int i, j, k, region;
UNDO *del = back;
if (won) { // don't do anything if the game has been won
fclose(file);
return EOF;
}
if (file == NULL)
return FALSE;
// Generic solitaire header
fputc(datum, file);
// Streets and Alleys Specific Header
fputc((int)strlen(gameName()) + 1, file);
fputs(gameName(), file);
for (i = 0; i < 52; i++)
fputc(deck[i], file);
for (region = 0; region < 3; region++)
for (i = 0; i < 5; i++)
for (j = 0; j < 3; j++)
for (k = 0; k < 3; k++)
fputc(table[i][j][k][region], file);
i = 0;
while (del != NULL) {
del = del->previous;
i++;
}
fputc(i, file); // Number of undo structures
del = back;
while (del != NULL) {
fputc(del->source.columb, file);
fputc(del->source.row, file);
fputc(del->source.depth, file);
fputc(del->source.region, file);
fputc(del->dest.columb, file);
fputc(del->dest.row, file);
fputc(del->dest.depth, file);
fputc(del->dest.region, file);
del = del->previous;
}
fputc(startCard, file);
fputc(score, file);
fputc(255, file); // Last data to close the file
i = fclose(file);
if (i != 0)
return i;
return TRUE;
}
void StreetsAndAlleys::redrawBoard() {
int i, j, k, x, y;
TABLELOC card;
if (won) // don't do anything if the game has been won
return;
clearBoard(hWnd);
card.region = 0;
// Main Board
for (i = 0; i < 5; i++) {
card.columb = i;
for (j = 0; j < 3; j++) {
card.row = j;
for (k = 0; k < 3; k++) {
card.depth = k;
placeCard(card);
}
}
}
// Free Board
card.region = 1;
card.row = 0;
for (i = 0; i < 3; i++) {
card.columb = i;
for (j = 0; j < 3; j++) {
card.depth = j;
placeCard(card);
}
}
// Place Board
card.region = 2;
card.depth = 0;
for (i = 0; i < 2; i++) {
card.columb = i;
for (j = 0; j < 2; j++) {
card.row = j;
placeCard(card);
}
}
// Show Start Card, draw board text
if (startCard != 0) {
x = width * 8;
y = (int)(height * 3.7);
bprintf(hWnd, x, y, BWHITE, "Start Card:");
y += height / 3;
bprintf(hWnd, x, y, BWHITE, "Score: %i.", score);
x += 100;
y = height * 4;
drawCard(hWnd, x, y, startCard, 0, false);
}
return;
}
void StreetsAndAlleys::undo() {
UNDO *thisMove = back;
TABLELOC temp;
int i, j, k, i1, j1, k1, region, region1, card, x, y;
if (won) // don't do anything if the game has been won
return;
if (selected.region != -1) {
temp.columb = selected.columb;
temp.row = selected.row;
temp.depth = selected.depth;
temp.region = selected.region;
selected.region = -1;
placeCard(temp);
}
if (back == NULL)
return;
back = back->previous;
i = thisMove->source.columb;
j = thisMove->source.row;
k = thisMove->source.depth;
region = thisMove->source.region;
i1 = thisMove->dest.columb;
j1 = thisMove->dest.row;
k1 = thisMove->dest.depth;
region1 = thisMove->dest.region;
removeCard(thisMove->dest);
card = table[i1][j1][k1][region1];
table[i][j][k][region] = card;
card--;
placeCard(thisMove->source);
table[i1][j1][k1][region1] = 0;
if (k1 != 0) {
temp.columb = i1;
temp.row = j1;
temp.depth = k1 - 1;
temp.region = region1;
placeCard(temp);
}
if (region1 == 2) {
score--;
x = width * 8;
y = (int)(height * 3.7);
y += height / 3;
bprintf(hWnd, x, y, BWHITE, "Score: %i.", score);
if (card % 13 == 0)
card += 13;
card--;
/*bprintf(hWnd, 0, 0, BWHITE,
"card = %i, startcard r13 = %i, card r13 = %i, i = %i, j = %i",
card, (startCard - 1) % 13, (card + 1) % 13, i1, j1);*/
if ((card + 1) % 13 != (startCard - 1) % 13)
table[i1][j1][0][2] = card + 1;
placeCard(thisMove->dest);
}
delete thisMove;
return;
}
int StreetsAndAlleys::getRegion(int X, int Y) {
// What Board region are we in?
// Based on maximum card locations
if (Y > (int)(3.3 * height)) // Average between 2.75 and 4
return 1;
if (X > (int)(10.3 * width)) // Average between 9.5 and 11
return 2;
return 0; // Main board is everything else
}
LOCATION StreetsAndAlleys::getRegion1(int X, int Y) {
LOCATION card;
const int lheight = (int)(height / 1.3);
int x, y;
card.isValid = false;
card.row = 1;
card.columb = 0;
if (Y < (int)(1.25 * height))
card.row = 0;
if (Y > (int)(2.25 * height))
card.row = 2;
// 1.5 2.8
if (X > (int)(2.15 * width))
card.columb = 1;
//3.5 4.8
if (X > (int)(4.15 * width))
card.columb = 2;
//5.5 6.8
if (X > (int)(6.15 * width))
card.columb = 3;
if (X > (int)(8.15 * width))
card.columb = 4;
//bprintf(hWnd, 0, 0, BWHITE,
// "Click in Region 1 Subregion %i, %i", card.row, card.columb);
x = width / 3;
x += width * 2 * card.columb;
x += width / 2;
y = height * card.row;
y += (int)(height * .75);
card.depth = 0;
if ( isValid(x, y, X, Y, 345) )
card.isValid = true;
// base of row is now determined, now we check for selecting additional cards
if (table[card.columb][card.row][1][0] != 0) {
x = width * 2 / 3;
x += width * 2 * (card.columb);
y -= lheight / 2;
//bprintf(hWnd, x, y, BWHITE, "Processing...");
if (X > x && X < x + width && Y > y && Y < y + lheight) {
card.depth = 1;
card.isValid = true;
//bprintf(hWnd, x, y, BWHITE,
// "Click in Region 1 Subregion %i, %i, depth = 1",
// card.row, card.columb);
}
y += lheight / 2;
}
if (table[card.columb][card.row][2][0] != 0) {
x = width * 2 * (card.columb);
x += (int)(width * 1.5);
if ( isValid(x, y, X, Y, 15) ) {
card.depth = 2;
card.isValid = true;
//bprintf(hWnd, x, y, BWHITE,
// "Click in Region 1 Subregion %i, %i, depth = 2",
// card.row, card.columb);
}
}
return card;
}
LOCATION StreetsAndAlleys::getRegion2(int X, int Y) {
LOCATION card;
const int lheight = (int)(height / 1.3);
int region = 2;
int x, y;
card.isValid = false;
card.row = 0;
if (X < (int)(width * 3.15))
region = 1;
if (X > (int)(width * 5.15))
region = 3;
card.columb = region - 1;
x = width / 3;
x += width * 2 * (region - 1);
x += (int)(width * 1.5);
y = height * 4;
card.depth = 0;
if ( isValid(x, y, X, Y, 345) )
card.isValid = true;
// base of row is now determined, now we check for selecting additional cards
if (table[card.columb][0][1][1] != 0) {
x = width * 2 / 3;
x += width * 2 * (region - 1);
x += (int)(width * 1.5);
x -= width / 2;
y = height * 4;
y -= lheight / 2;
if (X > x && X < x + width && Y > y && Y < y + lheight) {
card.depth = 1;
card.isValid = true;
//bprintf(hWnd, x, y, BWHITE,
// "Click in Region 2 Subregion %i, card height = %i, card width = %i, depth = 1",
// region, lheight, width);
}
}
if (table[card.columb][0][2][1] != 0) {
x = width * 2 * (region - 1);
x += (int)(width * 2.5);
y = height * 4;
if ( isValid(x, y, X, Y, 15) ) {
card.depth = 2;
card.isValid = true;
//bprintf(hWnd, x, y, BWHITE,
// "Click in Region 2 Subregion %i, card height = %i, card width = %i, depth = 2",
// region, lheight, width);
}
}
return card;
}
LOCATION StreetsAndAlleys::getRegion3(int X, int Y) {
LOCATION card;
CARDDIM cardDim = getCardAttributes();
const int cheight = cardDim.Height / 2;
card.depth = 0;
card.isValid = false;
if (X > (int)(width * 10.5) && X < (int)(width * 11.5)) {
card.columb = 0;
//bprintf(hWnd, 0, 0, BWHITE, "card.columb = 1");
if (Y > (int)(height * .75 - cheight) && Y < (int)(height * .75 + cheight)) {
card.row = 0;
card.isValid = true;
//bprintf(hWnd, 0, 15, BWHITE, "card.row = 1");
}
if (Y > (int)(height * 2.25 - cheight) && Y < (int)(height * 2.25 + cheight)) {
card.row = 1;
card.isValid = true;
//bprintf(hWnd, 0, 15, BWHITE, "card.row = 2");
}
}
if (X > (int)(width * 12.5) && X < (int)(width * 13.5)) {
card.columb = 1;
//bprintf(hWnd, 0, 0, BWHITE, "card.columb = 2");
if (Y > (int)(height * .75 - cheight) && Y < (int)(height * .75 + cheight)) {
card.row = 0;
card.isValid = true;
//bprintf(hWnd, 0, 15, BWHITE, "card.row = 1");
}
if (Y > (int)(height * 2.25 - cheight) && Y < (int)(height * 2.25 + cheight)) {
card.row = 1;
card.isValid = true;
//bprintf(hWnd, 0, 15, BWHITE, "card.row = 2");
}
}
return card;
}
bool StreetsAndAlleys::isValid(int x, int y, int X, int Y, int rotation) {
// Determine weather or not Y is within the region bounded by the card
// I know that this works with +/- 15deg -- don't know about anything else
const int lheight = (int)(height / 1.3);
double m, m1, rad, B, B1;
int a, b, c, d;
while (rotation > 360)
rotation-=360;
while (rotation < 1)
rotation += 360;
rad = DegtoRad(rotation);
a = lheight / 2;
b = width / 2;
c = (int)((-cos(rad) * a) + (-sin(rad) * b));
d = (int)((-cos(rad) * b) + (sin(rad) * a));
// Corner 4 of card
y += c;
x += d;
c = (int)((cos(rad) * a) + (sin(rad) * b));
d = (int)((cos(rad) * b) + (-sin(rad) * a));
// corner 3 of card
m = tan(rad);
m1 = -1 / m;
B = (c * 2) - (m * d * 2);
B1 = (c * 2) - (m1 * d * 2);
X -= x;
Y -= y;
// bprintf(hWnd, x, y, BWHITE, "Processing...");
if ( Y > m * X && Y < (m * X) + B && Y > m1 * X && Y < (m1 * X) + B1
&& rotation < 180)
return true;
if ( Y > m * X && Y < (m * X) + B && Y < m1 * X && Y > (m1 * X) + B1
&& rotation > 180)
return true;
return false;
}
void StreetsAndAlleys::placeCard(TABLELOC card) {
// Main Board
int i = card.columb;
int j = card.row;
int k = card.depth;
int x, y, n = 0, value = table[i][j][k][card.region];
bool isSelected;
switch (card.region) {
case 0:
switch (card.depth) {
case 0:
n = -15;
break;
case 1:
n = 0;
break;
case 2:
n = 15;
break;
}
x = width / 3;
x *= k + 1;
x += width * 2 * i;
x += width / 2;
y = height * j;
y += (int)(height * .75);
break;
// Free Board
case 1:
switch (k) {
case 0:
n = -15;
break;
case 1:
n = 0;
break;
case 2:
n = 15;
break;
}
x = width / 3;
x *= k + 1;
x += width * 2 * i;
x += (int)(width * 1.5);
y = height * 4;
break;
// Place Board
case 2:
x = width * 11;
x += width * 2 * i;
y = (int)(height * .75);
y += (int)(height * 1.5 * j);
break;
}
isSelected = (card.columb == selected.columb && card.row == selected.row &&
card.depth == selected.depth && card.region == selected.region);
if (value == 0 && (card.region == 2 || (card.region == 1 && k == 0))) {
value = E; // Print the "E" card so users know where empty areas are
isSelected = false;
}
drawCard(hWnd, x, y, value, n, isSelected);
return;
}
void StreetsAndAlleys::rightUp() {
int i;
TABLELOC maybe;
if (won) // don't do anything if the game has been won
return;
maybe.columb = loc.columb;
maybe.row = loc.row;
maybe.region = loc.region;
for (i = loc.depth; i < 3; i++) {
maybe.depth = i;
placeCard(maybe);
}
return;
}
void StreetsAndAlleys::selectCard(LOCATION card, int region) {
int i = card.columb, j = card.row, k = card.depth;
// Card must exist and be at the top of the stack
if (card.isValid && table[i][j][k][region] != 0 &&
(k + 1 == 3 || table[i][j][k + 1][region] == 0)) {
if (region == 2) // can not select a card in the place pile
return;
selected.columb = i;
selected.row = j;
selected.depth = k;
selected.region = region;
//bprintf(hWnd, 0, 0, BWHITE, "Selecting: %i, %i, %i, %i", i, j, k, region);
placeCard(selected);
}
return;
}
bool StreetsAndAlleys::moveIsValid(LOCATION card, int region) {
int i = card.columb, j = card.row, k = card.depth;
int i1 = selected.columb, j1 = selected.row, k1 = selected.depth;
int cardNum = table[i1][j1][k1][selected.region] - 1;
int series = cardNum % 13, suit = cardNum / 13;
int destNum = table[i][j][k][region] - 1;
int destSeries = destNum % 13, destSuit = destNum / 13;
int isRed = suit % 2, destIsRed = destSuit % 2;
// suit should be 0 - 4 for Spades, Diamonds, Clubs, Hearts
// series should be 0 - 13 for A, 2-9, J, Q, K
int a, b, c; // Some temporary use integers
/*bprintf(hWnd, 0, 0, BWHITE,
"Calc: Source: i = %i, j = %i, k = %i, cardNum = %i, series = %i, suit = %i, isRed = %i",
i1, j1, k1, cardNum, series, suit, isRed);
bprintf(hWnd, 0, 15, BWHITE,
"Dest: i = %i, j = %i, k = %i, cardNum = %i, series = %i, suit = %i, isRed = %i",
i, j, k, destNum, destSeries, destSuit, destIsRed);*/
// First .. some sanity checks
if (k == 2)
return false; // can not place on a full stack
if (k == 0 && region == 0 && destNum == -1)
return false; // can not place on a stack in the main board
// that has been depleted
a = destSeries - 1;
if (a < 0)
a = 12;
b = destSeries + 1;
if (b > 12)
b = 0;
c = (startCard - 1) % 13; // the series of the start card
// this only works if you assume that there are
// no duplicate cards... which there shouldn't be
//bprintf(hWnd, 500, 0, BWHITE, "a = %i, b = %i, c = %i", a, b, c);
switch (region) {
case 0:
if (destIsRed != isRed && series == a)
return true; // Place in decending order, alternating red/black
break;
case 1:
if (k == 0 && destNum == -1)
return true; // Anything can start a new stack in the free zone
if (destIsRed != isRed && (series == a || series == b))
return true; // Place in acending or decending order
// alternating red/black in the place zone
break;
case 2:
if (suit == destSuit && series == b)
return true; // Place in ascending order in same suit for
// placing area
if (destNum == -1 && series == c)
return true; // start a new pile in the place area if the
// selected card is the same in the series as the
// start card
break;
}
return false; // if we have gotten here, it is not a legal move
}
void StreetsAndAlleys::makeMove(LOCATION card, int region) {
int i = selected.columb, j = selected.row, k = selected.depth;
int i1 = card.columb, j1 = card.row, k1 = card.depth;
int cardNum = table[i][j][k][selected.region];
int newNum = table[i1][j1][k1][region];
int x, y;
TABLELOC temp;
UNDO *newMove;
if (back != NULL) {
newMove = back;
back = new UNDO;
back->previous = newMove;
} else {
back = new UNDO;
back->previous = NULL;
}
back->source.columb = i;
back->source.row = j;
back->source.depth = k;
back->source.region = selected.region;
// first erase the origin card
removeCard(selected);
table[i][j][k][selected.region] = 0;
//if (k == 0 && selected.region == 1)
// placeCard(selected);
if (k != 0) {
temp.columb = i;
temp.row = j;
temp.depth = k - 1;
temp.region = selected.region;
placeCard(temp);
}
if (newNum != 0 && region != 2)
k1++;
table[i1][j1][k1][region] = cardNum;
back->dest.columb = i1;
back->dest.row = j1;
back->dest.depth = k1;
back->dest.region = region;
temp.columb = i1;
temp.row = j1;
temp.depth = k1;
temp.region = region;
placeCard(temp);