-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSOLITAIR.PAS
More file actions
1128 lines (1052 loc) · 32.6 KB
/
SOLITAIR.PAS
File metadata and controls
1128 lines (1052 loc) · 32.6 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
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
{ @author: Sylvain Maltais (support@gladir.com)
@created: 2025
@website(https://www.gladir.com/7iles)
@abstract(Target: Turbo Pascal 7, Free Pascal 3.2)
@description: Jeu de Solitaire Klondike avec controles clavier complets
}
Program Solitaire;
Uses
{$IFDEF FPC}
Windows, Crt, PtcCrt, PtcGraph, PtcMouse
{$ELSE}
DOS,Crt, Graph
{$ENDIF};
Const
SCREEN_WIDTH=640;
SCREEN_HEIGHT=480;
CARD_WIDTH=50;
CARD_HEIGHT=70;
CARD_SPACING=60;
CARD_OVERLAP=20;
DECK_X=50;
DECK_Y=50;
WASTE_X=120;
WASTE_Y=50;
FOUNDATION_START_X = 300;
FOUNDATION_Y = 50;
TABLEAU_START_X = 50;
TABLEAU_Y = 150;
MAX_CARDS = 52;
MAX_TABLEAU_CARDS = 25;
Type
TSuit=(Hearts,Diamonds,Clubs,Spades);
TRank=(Ace,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King);
TCard=Record
suit:TSuit;
rank:TRank;
faceUp:Boolean;
visible:Boolean;
End;
TCardStack=Record
cards:Array[1..MAX_CARDS] of TCard;
count:Integer;
End;
TTableauColumn=Record
cards:Array[1..MAX_TABLEAU_CARDS] of TCard;
count:Integer;
End;
TGameState=Record
deck:TCardStack;
waste:TCardStack;
foundations:Array[1..4] of TCardStack;
tableau:Array[1..7] of TTableauColumn;
score:Integer;
moves:Integer;
gameWon:Boolean;
End;
TSelection=Record
sourceType:Integer; { 0=aucun, 1=defausse, 2=fondation, 3=tableau }
sourceIndex:Integer;
cardIndex:Integer;
active:Boolean;
End;
TCursor=Record
areaType:Integer; { 0=paquet, 1=defausse, 2=fondation, 3=tableau }
areaIndex:Integer; { Index dans la zone (1-4 pour fondation, 1-7 pour tableau) }
cardIndex:Integer; { Index de la carte dans la colonne }
End;
Var
Game:TGameState;
Selection:TSelection;
Cursor:TCursor;
NeedRedraw:Boolean;
{$IFNDEF FPC}
Function MouseDriverFound:Boolean;
Var
Regs:Registers;
Begin
Regs.AX:=0;
Intr($33,Regs);
MouseDriverFound:=Regs.AX=$FFFF;
End;
Procedure GetMouseState(Var X,Y,Button:LongInt);
Var
Regs:Registers;
Begin
Regs.AX:=$0003;
Intr($33,Regs);
Button:=Regs.BX;
X:=Regs.CX;
Y:=Regs.DX;
End;
Function GetMouseButton:Word;
Var
X,Y,Button:LongInt;
Begin
GetMouseState(X,Y,Button);
GetMouseButton:=Button;
End;
{$ENDIF}
Function GetCardValue(rank:TRank):Integer;Begin
GetCardValue:=Ord(rank)+1;
End;
Function IsRed(suit:TSuit):Boolean;Begin
IsRed:=(suit=Hearts)or(suit=Diamonds);
End;
Function IsBlack(suit:TSuit):Boolean;Begin
IsBlack:=(suit=Clubs)or(suit=Spades);
End;
Function CanPlaceOnFoundation(card:TCard;foundationIndex:Integer):Boolean;Begin
With Game.foundations[foundationIndex] do Begin
If count = 0 then
CanPlaceOnFoundation:=(card.rank = Ace)
Else
CanPlaceOnFoundation:=(card.suit = cards[count].suit) and(GetCardValue(card.rank) = GetCardValue(cards[count].rank) + 1);
End;
End;
Function CanPlaceOnTableau(card:TCard;tableauIndex:Integer):Boolean;Begin
With Game.tableau[tableauIndex] do Begin
If count = 0 then
CanPlaceOnTableau := (card.rank = King)
Else
CanPlaceOnTableau := (IsRed(card.suit) <> IsRed(cards[count].suit)) and
(GetCardValue(card.rank) = GetCardValue(cards[count].rank) - 1);
End;
End;
Function GetSequenceStart(columnIndex,cardIndex:Integer):Integer;
Var
i:Integer;
Begin
GetSequenceStart := cardIndex;
{ Chercher le début de la séquence déplaçable }
For i := cardIndex - 1 downto 1 do Begin
{ Arrêter si la carte n'est pas face visible }
If Not Game.tableau[columnIndex].cards[i].faceUp Then Break;
{ Arrêter si la séquence n'est pas valide }
If (GetCardValue(Game.tableau[columnIndex].cards[i].rank) <>
GetCardValue(Game.tableau[columnIndex].cards[i + 1].rank) + 1) or
(IsRed(Game.tableau[columnIndex].cards[i].suit) =
IsRed(Game.tableau[columnIndex].cards[i + 1].suit))Then Break;
GetSequenceStart := i;
End;
End;
Function CanMoveSequence(sourceIndex,startCard:Integer):Boolean;
Var
i:Integer;
Begin
CanMoveSequence := True;
{ Vérifier que toutes les cartes de la séquence sont face visible }
For i:=startCard to Game.tableau[sourceIndex].count do Begin
If Not Game.tableau[sourceIndex].cards[i].faceUp Then Begin
CanMoveSequence:=False;
Exit;
End;
End;
{ Vérifier que la séquence est bien ordonnée (décroissante et alternée) }
For i:=startCard to Game.tableau[sourceIndex].count - 1 do Begin
If (GetCardValue(Game.tableau[sourceIndex].cards[i].rank) <>
GetCardValue(Game.tableau[sourceIndex].cards[i + 1].rank) + 1) or
(IsRed(Game.tableau[sourceIndex].cards[i].suit) =
IsRed(Game.tableau[sourceIndex].cards[i + 1].suit))Then Begin
CanMoveSequence := False;
Exit;
End;
End;
End;
Procedure InitializeGraphics;
Var
Driver,Mode:Integer;
ErrCode:Integer;
Begin
{$IFDEF FPC}
Driver := VGA;
Mode := VGAHi;
{$ELSE}
Driver := Detect;
Mode := VGAHi;
{$ENDIF}
InitGraph(Driver, Mode, '');
ErrCode:=GraphResult;
If ErrCode = grOk Then Begin
SetColor(15); { White }
SetLineStyle(0, 0, 1);
End
Else
Begin
WriteLn('Erreur graphique : ', GraphErrorMsg(ErrCode));
Halt;
End;
End;
Procedure InitializeDeck;
Var
i,j,k:Integer;
temp:TCard;
Begin
{ Créer le deck de 52 cartes }
k := 1;
For i := 0 to 3 do For j := 0 to 12 do Begin
With Game.deck.cards[k] do Begin
suit := TSuit(i);
rank := TRank(j);
faceUp := False;
visible := True;
End;
Inc(k);
End;
Game.deck.count:=52;
{ Mélanger le deck }
For i := 1 to 52 do Begin
j:=Random(52)+1;
temp := Game.deck.cards[i];
Game.deck.cards[i] := Game.deck.cards[j];
Game.deck.cards[j] := temp;
End;
End;
Procedure InitializeGame;
Var
i,j,cardIndex:Integer;
Begin
{ Initialiser les structures }
Game.waste.count:=0;
For i:=1 to 4 do Game.foundations[i].count := 0;
For i:=1 to 7 do Game.tableau[i].count := 0;
Game.score:=0;
Game.moves:=0;
Game.gameWon:=False;
Selection.active:=False;
Selection.sourceType := 0;
{ Initialiser le curseur }
Cursor.areaType := 0;
Cursor.areaIndex := 1;
Cursor.cardIndex := 1;
{ Initialiser le deck }
InitializeDeck;
{ Distribuer les cartes sur le tableau }
cardIndex := 1;
For i := 1 to 7 do For j := 1 to i do Begin
With Game.tableau[i] do Begin
Inc(count);
cards[count]:=Game.deck.cards[cardIndex];
cards[count].faceUp := (j = i); { Seule la dernière carte est face visible }
Inc(cardIndex);
End;
End;
{ Ajuster le deck }
For i:=cardIndex to 52 do Game.deck.cards[i - cardIndex + 1] := Game.deck.cards[i];
Game.deck.count := 52 - cardIndex + 1;
End;
Procedure DrawCard(x,y:Integer;card:TCard;selected:Boolean);
Var
suitChar:Char;
rankStr:String;
color:Integer;
Begin
{ Fond de la carte }
If selected Then SetColor(14) { Yellow }
Else SetColor(15); { White }
SetFillStyle(1, GetColor); { SolidFill }
Bar(x, y, x + CARD_WIDTH, y + CARD_HEIGHT);
{ Bordure }
SetColor(0); { Black }
Rectangle(x,y,x+CARD_WIDTH,y+CARD_HEIGHT);
If card.faceUp Then Begin
{ Déterminer la couleur }
If IsRed(card.suit)Then color:=4 { Red }
Else color:=0; { Black }
{ Symbole de la suite }
Case card.suit of
Hearts:suitChar:=#3;
Diamonds:suitChar:=#4;
Clubs:suitChar:=#5;
Spades:suitChar:=#6;
End;
{ Rang de la carte }
Case card.rank of
Ace:rankStr := 'A';
Two:rankStr := '2';
Three:rankStr := '3';
Four:rankStr := '4';
Five:rankStr := '5';
Six:rankStr := '6';
Seven:rankStr := '7';
Eight:rankStr := '8';
Nine:rankStr := '9';
Ten:rankStr := '10';
Jack:rankStr := 'J';
Queen:rankStr := 'Q';
King:rankStr := 'K';
End;
{ Dessiner le rang et la suite }
SetColor(color);
SetTextStyle(0, 0, 1); { DefaultFont, HorizDir, size 1 }
OutTextXY(x + 3, y + 3, rankStr);
OutTextXY(x + 3, y + 15, suitChar);
{ Symbole central pour les figures }
If card.rank>=Jack Then Begin
SetTextStyle(0, 0, 2); { DefaultFont, HorizDir, size 2 }
OutTextXY(x + CARD_WIDTH div 2 - 5, y + CARD_HEIGHT div 2 - 5, rankStr);
End
Else
Begin
SetTextStyle(0, 0, 3); { DefaultFont, HorizDir, size 3 }
OutTextXY(x + CARD_WIDTH div 2 - 8, y + CARD_HEIGHT div 2 - 8, suitChar);
End;
End
Else
Begin
{ Dos de carte }
SetColor(1); { Blue }
SetFillStyle(1, 1); { SolidFill, Blue }
Bar(x + 3, y + 3, x + CARD_WIDTH - 3, y + CARD_HEIGHT - 3);
SetColor(15); { White }
Rectangle(x + 6, y + 6, x + CARD_WIDTH - 6, y + CARD_HEIGHT - 6);
End;
End;
Procedure DrawEmptySlot(x,y:Integer);Begin
SetColor(8); { DarkGray }
SetFillStyle(1, 8); { SolidFill, DarkGray }
Bar(x, y, x + CARD_WIDTH, y + CARD_HEIGHT);
SetColor(7); { LightGray }
Rectangle(x, y, x + CARD_WIDTH, y + CARD_HEIGHT);
End;
Procedure GetCursorPosition(Var x,y:Integer);Begin
Case Cursor.areaType of
0:Begin { Paquet }
x:=DECK_X;
y:=DECK_Y;
End;
1:Begin { Defausse }
x:=WASTE_X;
y:=WASTE_Y;
End;
2:Begin { Fondation }
x:=FOUNDATION_START_X+(Cursor.areaIndex - 1)*CARD_SPACING;
y:=FOUNDATION_Y;
End;
3:Begin { Tableau }
x:=TABLEAU_START_X + (Cursor.areaIndex - 1)*CARD_SPACING;
If Game.tableau[Cursor.areaIndex].count>0 Then y:=TABLEAU_Y + (Cursor.cardIndex - 1) * CARD_OVERLAP
Else y:=TABLEAU_Y;
End;
End;
End;
Procedure DrawCursor;
Var
x,y:Integer;
Begin
GetCursorPosition(x, y);
{ Dessiner le curseur autour de la zone sélectionnée }
SetColor(11); { Cyan }
SetLineStyle(0, 0, 3);
Rectangle(x - 2, y - 2, x + CARD_WIDTH + 2, y + CARD_HEIGHT + 2);
SetLineStyle(0, 0, 1);
End;
Procedure DrawDeck;Begin
If Game.deck.count > 0 then
DrawCard(DECK_X, DECK_Y, Game.deck.cards[Game.deck.count], False)
Else
DrawEmptySlot(DECK_X, DECK_Y);
End;
Procedure DrawWaste;Begin
If Game.waste.count > 0 then
DrawCard(WASTE_X, WASTE_Y, Game.waste.cards[Game.waste.count],Selection.active and (Selection.sourceType = 1))
Else
DrawEmptySlot(WASTE_X, WASTE_Y);
End;
Procedure DrawFoundations;
Var
i:Integer;
Begin
For i:=1 to 4 do Begin
If Game.foundations[i].count>0 Then
DrawCard(FOUNDATION_START_X + (i - 1) * CARD_SPACING, FOUNDATION_Y,
Game.foundations[i].cards[Game.foundations[i].count],
Selection.active and (Selection.sourceType = 2) and (Selection.sourceIndex = i))
Else
DrawEmptySlot(FOUNDATION_START_X + (i - 1) * CARD_SPACING, FOUNDATION_Y);
End;
End;
Procedure DrawTableau;
Var
i,j:Integer;
x,y:Integer;
selected:Boolean;
Begin
For i:=1 to 7 do Begin
x := TABLEAU_START_X + (i - 1) * CARD_SPACING;
If Game.tableau[i].count=0 Then DrawEmptySlot(x, TABLEAU_Y) Else
For j := 1 to Game.tableau[i].count do Begin
y := TABLEAU_Y + (j - 1) * CARD_OVERLAP;
{ Mettre en surbrillance toute la séquence si elle est sélectionnée }
selected := Selection.active and (Selection.sourceType = 3) and
(Selection.sourceIndex = i) and (j >= Selection.cardIndex);
DrawCard(x, y, Game.tableau[i].cards[j], selected);
End;
End;
End;
Procedure DrawHUD;
Var
s:String;
Begin
{ Pointage }
SetColor(15); { Blanc }
SetTextStyle(0, 0, 2); { DefaultFont, HorizDir, size 2 }
Str(Game.score, s);
OutTextXY(10, 10, 'Points : ' + s);
{ D�placements }
Str(Game.moves, s);
OutTextXY(200, 10, 'Coups : ' + s);
{ Instructions }
SetTextStyle(0, 0, 1); { DefaultFont, HorizDir, size 1 }
OutTextXY(10, SCREEN_HEIGHT - 125, 'Fleches: Deplacer curseur');
OutTextXY(10, SCREEN_HEIGHT - 110, 'ESPACE: Selectionner/Deplacer');
OutTextXY(10, SCREEN_HEIGHT - 95, 'SOURIS: Cliquer pour jouer');
OutTextXY(10, SCREEN_HEIGHT - 80, 'D: Retourner paquet');
OutTextXY(10, SCREEN_HEIGHT - 65, 'F: Retourner carte tableau');
OutTextXY(10, SCREEN_HEIGHT - 50, 'A: Deplacement auto fondations');
OutTextXY(10, SCREEN_HEIGHT - 35, 'R: Recommencer, ESC: Quitter');
OutTextXY(10, SCREEN_HEIGHT - 20, 'Sequences auto-selectionnees');
End;
Procedure DrawWinMessage;Begin
SetColor(14); { Yellow }
SetFillStyle(1, 14); { SolidFill, Yellow }
Bar(SCREEN_WIDTH div 2 - 100, SCREEN_HEIGHT div 2 - 30,SCREEN_WIDTH div 2 + 100, SCREEN_HEIGHT div 2 + 30);
SetColor(4); { Red }
Rectangle(SCREEN_WIDTH div 2 - 100, SCREEN_HEIGHT div 2 - 30,SCREEN_WIDTH div 2 + 100, SCREEN_HEIGHT div 2 + 30);
SetColor(0); { Black }
SetTextStyle(0, 0, 2); { DefaultFont, HorizDir, size 2 }
OutTextXY(SCREEN_WIDTH div 2 - 80, SCREEN_HEIGHT div 2 - 15, 'VICTOIRE !');
SetTextStyle(0, 0, 1); { DefaultFont, HorizDir, size 1 }
OutTextXY(SCREEN_WIDTH div 2 - 80, SCREEN_HEIGHT div 2 + 5, 'Appuyez R pour rejouer');
End;
Procedure FlipDeckCard;Begin
If Game.deck.count>0 Then Begin
{ Déplacer la carte du deck vers la waste }
Inc(Game.waste.count);
Game.waste.cards[Game.waste.count] := Game.deck.cards[Game.deck.count];
Game.waste.cards[Game.waste.count].faceUp := True;
Dec(Game.deck.count);
End
Else
If Game.waste.count>0 Then Begin
{ Remettre toutes les cartes de la waste dans le deck }
While Game.waste.count>0 do Begin
Inc(Game.deck.count);
Game.deck.cards[Game.deck.count] := Game.waste.cards[Game.waste.count];
Game.deck.cards[Game.deck.count].faceUp := False;
Dec(Game.waste.count);
End;
End;
NeedRedraw:=True;
End;
Procedure MoveCard(sourceType, sourceIndex, cardIndex, destType, destIndex: Integer);
Var
card:TCard;
i:Integer;
Begin
{ Prendre la carte source }
Case sourceType of
1:card:=Game.waste.cards[Game.waste.count];
2:card:=Game.foundations[sourceIndex].cards[Game.foundations[sourceIndex].count];
3:card:=Game.tableau[sourceIndex].cards[cardIndex];
End;
{ Placer la carte à destination }
Case destType of
2: Begin { Foundation }
Inc(Game.foundations[destIndex].count);
Game.foundations[destIndex].cards[Game.foundations[destIndex].count]:=card;
Inc(Game.score, 10);
End;
3:Begin { Tableau }
Inc(Game.tableau[destIndex].count);
Game.tableau[destIndex].cards[Game.tableau[destIndex].count] := card;
If sourceType = 2 then Dec(Game.score, 15); { Pénalité pour retirer de foundation }
End;
End;
{ Supprimer la carte source }
Case sourceType of
1:Dec(Game.waste.count);
2:Dec(Game.foundations[sourceIndex].count);
3:Begin
{ Supprimer la carte du tableau }
For i := cardIndex to Game.tableau[sourceIndex].count-1 do
Game.tableau[sourceIndex].cards[i] := Game.tableau[sourceIndex].cards[i + 1];
Dec(Game.tableau[sourceIndex].count);
{ Retourner la carte qui devient visible si elle existe et est face cachée }
If (Game.tableau[sourceIndex].count > 0)and(cardIndex <= Game.tableau[sourceIndex].count) and
(not Game.tableau[sourceIndex].cards[cardIndex].faceUp)Then
Game.tableau[sourceIndex].cards[cardIndex].faceUp := True
Else If (Game.tableau[sourceIndex].count > 0) and
(cardIndex > Game.tableau[sourceIndex].count) and
(not Game.tableau[sourceIndex].cards[Game.tableau[sourceIndex].count].faceUp) then
Game.tableau[sourceIndex].cards[Game.tableau[sourceIndex].count].faceUp := True;
End;
End;
Inc(Game.moves);
NeedRedraw := True;
End;
Procedure MoveSequence(sourceIndex, startCard, destIndex: Integer);
Var
i, sequenceLength: Integer;
tempCards: array[1..MAX_TABLEAU_CARDS] of TCard;
Begin
{ Calculer la longueur de la séquence }
sequenceLength := Game.tableau[sourceIndex].count - startCard + 1;
{ Copier la séquence dans un tableau temporaire }
For i := 1 to sequenceLength do
tempCards[i] := Game.tableau[sourceIndex].cards[startCard + i - 1];
{ Supprimer la séquence de la source }
Game.tableau[sourceIndex].count := startCard - 1;
{ Retourner la carte qui devient visible si elle existe et est face cachée }
If (Game.tableau[sourceIndex].count > 0) and
(not Game.tableau[sourceIndex].cards[Game.tableau[sourceIndex].count].faceUp) then
Game.tableau[sourceIndex].cards[Game.tableau[sourceIndex].count].faceUp := True;
{ Ajouter la séquence à la destination }
For i := 1 to sequenceLength do
Begin
Inc(Game.tableau[destIndex].count);
Game.tableau[destIndex].cards[Game.tableau[destIndex].count] := tempCards[i];
End;
Inc(Game.moves);
NeedRedraw := True;
End;
Procedure MoveCursor(direction: Integer);
Begin
Case direction of
1: Begin { Gauche }
Case Cursor.areaType of
1: Cursor.areaType := 0; { Waste -> Deck }
2: Begin { Foundation }
If Cursor.areaIndex > 1 then
Dec(Cursor.areaIndex)
Else
Begin
Cursor.areaType := 1; { -> Waste }
Cursor.areaIndex := 1;
End;
End;
3: Begin { Tableau }
If Cursor.areaIndex > 1 then
Begin
Dec(Cursor.areaIndex);
If Game.tableau[Cursor.areaIndex].count > 0 then
Cursor.cardIndex := Game.tableau[Cursor.areaIndex].count
Else
Cursor.cardIndex := 1;
End
Else
Begin
Cursor.areaType := 2; { -> Foundation }
Cursor.areaIndex := 4;
End;
End;
End;
End;
2: Begin { Droite }
Case Cursor.areaType of
0: Begin { Deck -> Waste }
Cursor.areaType := 1;
Cursor.areaIndex := 1;
End;
1: Begin { Waste -> Foundation }
Cursor.areaType := 2;
Cursor.areaIndex := 1;
End;
2: Begin { Foundation }
If Cursor.areaIndex < 4 then
Inc(Cursor.areaIndex)
Else
Begin
Cursor.areaType := 3; { -> Tableau }
Cursor.areaIndex := 1;
If Game.tableau[1].count > 0 then
Cursor.cardIndex := Game.tableau[1].count
Else
Cursor.cardIndex := 1;
End;
End;
3: Begin { Tableau }
If Cursor.areaIndex < 7 then
Begin
Inc(Cursor.areaIndex);
If Game.tableau[Cursor.areaIndex].count > 0 then
Cursor.cardIndex := Game.tableau[Cursor.areaIndex].count
Else
Cursor.cardIndex := 1;
End;
End;
End;
End;
3: Begin { Haut }
Case Cursor.areaType of
3: Begin { Tableau }
If Cursor.cardIndex > 1 then
Begin
Dec(Cursor.cardIndex);
{ Permettre de naviguer sur toutes les cartes, même cachées }
End;
End;
Else
Begin
Cursor.areaType := 2; { -> Foundation }
If Cursor.areaType = 0 then
Cursor.areaIndex := 1
Else If Cursor.areaType = 1 then
Cursor.areaIndex := 1
Else
Cursor.areaIndex := 1;
End;
End;
End;
4: Begin { Bas }
Case Cursor.areaType of
0, 1, 2: Begin { Deck/Waste/Foundation -> Tableau }
Cursor.areaType := 3;
Case Cursor.areaType of
0: Cursor.areaIndex := 1;
1: Cursor.areaIndex := 1;
2: Cursor.areaIndex := Cursor.areaIndex;
End;
If Game.tableau[Cursor.areaIndex].count > 0 then
Cursor.cardIndex := Game.tableau[Cursor.areaIndex].count
Else
Cursor.cardIndex := 1;
End;
3: Begin { Tableau }
If (Cursor.cardIndex < Game.tableau[Cursor.areaIndex].count) then
Inc(Cursor.cardIndex);
End;
End;
End;
End;
NeedRedraw := True;
End;
Procedure FlipTableauCard;
Var
columnIndex: Integer;
Begin
{ Vérifier si on est sur le tableau }
If Cursor.areaType = 3 then
Begin
columnIndex := Cursor.areaIndex;
{ Vérifier s'il y a des cartes dans cette colonne }
If Game.tableau[columnIndex].count > 0 then
Begin
{ Si on est sur une carte cachée, la retourner }
If (Cursor.cardIndex <= Game.tableau[columnIndex].count) and
(not Game.tableau[columnIndex].cards[Cursor.cardIndex].faceUp) then
Begin
Game.tableau[columnIndex].cards[Cursor.cardIndex].faceUp := True;
NeedRedraw := True;
Inc(Game.score, 5); { Bonus pour retourner une carte }
End
{ Sinon, retourner la dernière carte si elle est cachée }
Else If not Game.tableau[columnIndex].cards[Game.tableau[columnIndex].count].faceUp then
Begin
Game.tableau[columnIndex].cards[Game.tableau[columnIndex].count].faceUp := True;
NeedRedraw := True;
Inc(Game.score, 5); { Bonus pour retourner une carte }
End;
End;
End;
End;
Procedure AutoMoveToFoundation;
Var
i, j: Integer;
moved: Boolean;
Begin
moved := False;
{ Essayer de déplacer depuis la waste }
If Game.waste.count > 0 then
Begin
For i := 1 to 4 do
Begin
If CanPlaceOnFoundation(Game.waste.cards[Game.waste.count], i) then
Begin
MoveCard(1, 0, Game.waste.count, 2, i);
moved := True;
Break;
End;
End;
End;
{ Essayer de déplacer depuis le tableau }
If not moved then
Begin
For i := 1 to 7 do
Begin
If Game.tableau[i].count > 0 then
Begin
For j := 1 to 4 do
Begin
If CanPlaceOnFoundation(Game.tableau[i].cards[Game.tableau[i].count], j) then
Begin
MoveCard(3, i, Game.tableau[i].count, 2, j);
moved := True;
Break;
End;
End;
End;
If moved then Break;
End;
End;
If moved then NeedRedraw := True;
End;
Procedure HandleKeyboardInput;
Var
areaType, areaIndex, cardIndex: Integer;
canMove: Boolean;
Begin
{ Déterminer la position actuelle du curseur }
areaType := Cursor.areaType;
areaIndex := Cursor.areaIndex;
cardIndex := Cursor.cardIndex;
If not Selection.active then
Begin
{ Sélectionner une carte }
Case areaType of
0: FlipDeckCard; { Deck }
1: If Game.waste.count > 0 then { Waste }
Begin
Selection.active := True;
Selection.sourceType := 1;
Selection.sourceIndex := 0;
Selection.cardIndex := Game.waste.count;
NeedRedraw := True;
End;
2: If Game.foundations[areaIndex].count > 0 then { Foundation }
Begin
Selection.active := True;
Selection.sourceType := 2;
Selection.sourceIndex := areaIndex;
Selection.cardIndex := Game.foundations[areaIndex].count;
NeedRedraw := True;
End;
3: If (Game.tableau[areaIndex].count > 0) and { Tableau }
(cardIndex <= Game.tableau[areaIndex].count) and
(Game.tableau[areaIndex].cards[cardIndex].faceUp) then
Begin
Selection.active := True;
Selection.sourceType := 3;
Selection.sourceIndex := areaIndex;
{ Sélectionner le début de la séquence déplaçable }
Selection.cardIndex := GetSequenceStart(areaIndex, cardIndex);
NeedRedraw := True;
End
Else If (Game.tableau[areaIndex].count > 0) and
(cardIndex <= Game.tableau[areaIndex].count) and
(not Game.tableau[areaIndex].cards[cardIndex].faceUp) then
Begin
{ Si la carte est cachée, la retourner automatiquement }
FlipTableauCard;
End;
End;
End
Else
Begin
{ Tenter de déplacer la carte sélectionnée }
canMove := False;
Case areaType of
2: { Foundation }
If Selection.sourceType = 1 then
canMove := CanPlaceOnFoundation(Game.waste.cards[Game.waste.count], areaIndex)
Else If Selection.sourceType = 3 then
canMove := CanPlaceOnFoundation(Game.tableau[Selection.sourceIndex].cards[Selection.cardIndex], areaIndex);
3: { Tableau }
If Selection.sourceType = 1 then
canMove := CanPlaceOnTableau(Game.waste.cards[Game.waste.count], areaIndex)
Else If Selection.sourceType = 2 then
canMove := CanPlaceOnTableau(Game.foundations[Selection.sourceIndex].cards[Selection.cardIndex], areaIndex)
Else If Selection.sourceType = 3 then
Begin
{ Vérifier si on peut placer la carte sélectionnée }
canMove := CanPlaceOnTableau(Game.tableau[Selection.sourceIndex].cards[Selection.cardIndex], areaIndex);
{ Vérifier si on peut déplacer toute la séquence }
If canMove and CanMoveSequence(Selection.sourceIndex, Selection.cardIndex) then
Begin
{ Déplacer la séquence complète }
MoveSequence(Selection.sourceIndex, Selection.cardIndex, areaIndex);
canMove := False; { Pour éviter le MoveCard normal }
End;
End;
End;
If canMove then
MoveCard(Selection.sourceType, Selection.sourceIndex, Selection.cardIndex, areaType, areaIndex);
Selection.active := False;
NeedRedraw := True;
End;
End;
Function CheckWin:Boolean;
Var
i:Integer;
Begin
CheckWin:=True;
For i:=1 to 4 do If Game.foundations[i].count<>13 Then Begin
CheckWin:=False;
Exit;
End;
End;
Function GetCardAtPosition(x, y: Integer; var areaType, areaIndex, cardIndex: Integer): Boolean;
Var
i, j: Integer;
Begin
GetCardAtPosition := False;
{ Vérifier le deck }
If (x >= DECK_X) and (x <= DECK_X + CARD_WIDTH) and
(y >= DECK_Y) and (y <= DECK_Y + CARD_HEIGHT) then
Begin
areaType := 0;
areaIndex := 1;
cardIndex := 1;
GetCardAtPosition := True;
Exit;
End;
{ Vérifier la défausse }
If (x >= WASTE_X) and (x <= WASTE_X + CARD_WIDTH) and
(y >= WASTE_Y) and (y <= WASTE_Y + CARD_HEIGHT) then
Begin
areaType := 1;
areaIndex := 1;
cardIndex := 1;
GetCardAtPosition := True;
Exit;
End;
{ Vérifier les fondations }
For i := 1 to 4 do
Begin
If (x >= FOUNDATION_START_X + (i - 1) * CARD_SPACING) and
(x <= FOUNDATION_START_X + (i - 1) * CARD_SPACING + CARD_WIDTH) and
(y >= FOUNDATION_Y) and (y <= FOUNDATION_Y + CARD_HEIGHT) then
Begin
areaType := 2;
areaIndex := i;
cardIndex := 1;
GetCardAtPosition := True;
Exit;
End;
End;
{ Vérifier le tableau }
For i := 1 to 7 do
Begin
If (x >= TABLEAU_START_X + (i - 1) * CARD_SPACING) and
(x <= TABLEAU_START_X + (i - 1) * CARD_SPACING + CARD_WIDTH) then
Begin
{ Déterminer quelle carte dans la colonne }
If Game.tableau[i].count = 0 then
Begin
If (y >= TABLEAU_Y) and (y <= TABLEAU_Y + CARD_HEIGHT) then
Begin
areaType := 3;
areaIndex := i;
cardIndex := 1;
GetCardAtPosition := True;
Exit;
End;
End
Else
Begin
{ Vérifier chaque carte de la colonne (de bas en haut) }
For j := Game.tableau[i].count downto 1 do
Begin
If (y >= TABLEAU_Y + (j - 1) * CARD_OVERLAP) and
(y <= TABLEAU_Y + (j - 1) * CARD_OVERLAP + CARD_HEIGHT) then
Begin
areaType := 3;
areaIndex := i;
cardIndex := j;
GetCardAtPosition := True;
Exit;
End;
End;
End;
End;
End;
End;
Procedure HandleMouseInput;
Var
mouseX, mouseY, mouseButton: LongInt;
areaType, areaIndex, cardIndex: Integer;
canMove: Boolean;
Begin
Begin
{ Obtenir l'état actuel de la souris }
GetMouseState(mouseX, mouseY, mouseButton);
{ Vérifier si le bouton gauche vient d'être pressé }
If (mouseButton and 1) <> 0 then
Begin
{ Attendre que le bouton soit relâché pour éviter les clics multiples }
repeat
GetMouseState(mouseX, mouseY, mouseButton);
Delay(10);
until (mouseButton and 1) = 0;
{ Déterminer quelle carte a été cliquée }
If GetCardAtPosition(mouseX, mouseY, areaType, areaIndex, cardIndex) then
Begin
{ Mettre à jour la position du curseur }
Cursor.areaType := areaType;
Cursor.areaIndex := areaIndex;
Cursor.cardIndex := cardIndex;
If not Selection.active then
Begin
{ Sélectionner une carte }
Case areaType of
0: FlipDeckCard; { Deck }
1: If Game.waste.count > 0 then { Waste }
Begin
Selection.active := True;
Selection.sourceType := 1;
Selection.sourceIndex := 0;
Selection.cardIndex := Game.waste.count;
NeedRedraw := True;
End;
2: If Game.foundations[areaIndex].count > 0 then { Foundation }
Begin
Selection.active := True;
Selection.sourceType := 2;
Selection.sourceIndex := areaIndex;
Selection.cardIndex := Game.foundations[areaIndex].count;
NeedRedraw := True;
End;
3: If (Game.tableau[areaIndex].count > 0) and { Tableau }
(cardIndex <= Game.tableau[areaIndex].count) and
(Game.tableau[areaIndex].cards[cardIndex].faceUp) then
Begin
Selection.active := True;
Selection.sourceType := 3;
Selection.sourceIndex := areaIndex;
{ Sélectionner le début de la séquence déplaçable }
Selection.cardIndex := GetSequenceStart(areaIndex, cardIndex);
NeedRedraw := True;
End
Else If (Game.tableau[areaIndex].count > 0) and
(cardIndex <= Game.tableau[areaIndex].count) and