-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFREECELL.PAS
More file actions
643 lines (596 loc) · 15.4 KB
/
FREECELL.PAS
File metadata and controls
643 lines (596 loc) · 15.4 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
{ @author: Sylvain Maltais (support@gladir.com)
@created: 2025
@website(https://www.gladir.com/7iles)
@abstract(Target: Turbo Pascal 7, Free Pascal 3.2)
@description: FreeCell - Jeu de solitaire classique
L'objectif est de déplacer toutes les cartes vers les 4 piles de fondation
}
Program FreeCell;
Uses {$IFDEF FPC}
PtcGraph,PtcCrt,PtcMouse
{$ELSE}
DOS,Graph,Crt
{$ENDIF};
Const
SCREEN_WIDTH = 640;
SCREEN_HEIGHT = 480;
CARD_WIDTH = 50;
CARD_HEIGHT = 70;
DECK_SIZE = 52;
{ Dimensions du plateau }
FREECELLS = 4; { Cellules libres }
FOUNDATIONS = 4; { Piles de fondation }
COLUMNS = 8; { Colonnes du tableau }
MAX_CARDS_IN_COLUMN = 20;
{ Couleurs des cartes }
HEARTS = 0;
DIAMONDS = 1;
CLUBS = 2;
SPADES = 3;
{ Valeurs des cartes }
ACE = 1;
JACK = 11;
QUEEN = 12;
KING = 13;
{ tats du jeu }
GAME_PLAYING = 0;
GAME_WON = 1;
{ Zones de clic }
ZONE_FREECELL = 0;
ZONE_FOUNDATION = 1;
ZONE_COLUMN = 2;
Type
TCard=Record
Suit:Integer; { Couleur: 0=Coeur, 1=Pique, 2=Carreau, 3=trefle }
Value:Integer; { Valeur: 1-13 }
Visible:Boolean; { Toujours visible dans FreeCell }
End;
TColumn = Record
Cards: Array[0..19] of TCard;
Count: Integer;
End;
TGameState=Record
Deck:Array[0..51] of TCard;
FreeCells: Array[0..3] of TCard;
FreeCellsUsed: Array[0..3] of Boolean;
Foundations: Array[0..3] of TCard;
FoundationCounts: Array[0..3] of Integer;
Columns: Array[0..7] of TColumn;
SelectedCard: TCard;
SelectedZone: Integer;
SelectedIndex: Integer;
HasSelection: Boolean;
GameState: Integer;
MovesCount: Integer;
End;
Var
Game: TGameState;
NeedRedraw: Boolean;
{$IFNDEF FPC}
Function MouseDriverFound: Boolean;
Var
Regs: Registers;
Begin
Regs.AX := 0;
Intr($33, Regs);
MouseDriverFound := Regs.AX = $FFFF;
End;
Procedure ShowMouse;
Var
Regs: Registers;
Begin
Regs.AX := $0001;
Intr($33, Regs);
End;
Procedure HideMouse;
Var
Regs: Registers;
Begin
Regs.AX := $0002;
Intr($33, Regs);
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;
{$ELSE}
Function MouseDriverFound: Boolean;
Begin
MouseDriverFound := True;
End;
Procedure ShowMouse;
Begin
{ Pas d'implémentation pour Free Pascal }
End;
Procedure HideMouse;
Begin
{ Pas d'implémentation pour Free Pascal }
End;
{$ENDIF}
Procedure InitScreen;
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
WriteLn('Erreur graphique : ', GraphErrorMsg(ErrCode));
Halt(1);
End;
End;
Function GetCardName(Card:TCard):String;Begin
Case Card.Value of
ACE: GetCardName := 'A';
2..10: GetCardName := Chr(Ord('0') + Card.Value);
JACK: GetCardName := 'J';
QUEEN: GetCardName := 'Q';
KING: GetCardName := 'K';
Else GetCardName := '?';
End;
End;
Function GetSuitSymbol(Suit:Integer):String;Begin
Case Suit of
HEARTS: GetSuitSymbol := #3;
DIAMONDS: GetSuitSymbol := #4;
CLUBS: GetSuitSymbol := #5;
SPADES: GetSuitSymbol := #6;
Else GetSuitSymbol := '?';
End;
End;
Function GetSuitColor(Suit:Integer):Integer;Begin
Case Suit of
HEARTS, DIAMONDS: GetSuitColor := Red;
CLUBS, SPADES: GetSuitColor := Black;
Else GetSuitColor := White;
End;
End;
Function IsRedSuit(Suit:Integer):Boolean;Begin
IsRedSuit:=(Suit = HEARTS) Or (Suit = DIAMONDS);
End;
Procedure InitDeck;
Var
I,Suit,Value:Integer;
Begin
I := 0;
For Suit := 0 to 3 Do Begin
For Value := 1 to 13 Do Begin
Game.Deck[I].Suit := Suit;
Game.Deck[I].Value := Value;
Game.Deck[I].Visible := True;
Inc(I);
End;
End;
End;
Procedure ShuffleDeck;
Var
I,J:Integer;
TempCard:TCard;
Begin
For I:=0 to DECK_SIZE - 1 Do Begin
J := Random(DECK_SIZE);
TempCard := Game.Deck[I];
Game.Deck[I] := Game.Deck[J];
Game.Deck[J] := TempCard;
End;
End;
Procedure InitGame;
Var
I,J,CardIndex:Integer;
Begin
{ Initialiser les variables de jeu }
Game.GameState := GAME_PLAYING;
Game.HasSelection := False;
Game.MovesCount := 0;
{ Vider les cellules libres }
For I := 0 to FREECELLS - 1 Do Begin
Game.FreeCellsUsed[I] := False;
End;
{ Vider les fondations }
For I:=0 to FOUNDATIONS - 1 Do Begin
Game.FoundationCounts[I] := 0;
End;
{ Vider les colonnes }
For I := 0 to COLUMNS - 1 Do Begin
Game.Columns[I].Count := 0;
End;
{ Distribuer les cartes dans les colonnes }
InitDeck;
ShuffleDeck;
CardIndex := 0;
For I := 0 to COLUMNS - 1 Do Begin
{ Les 4 premiŠres colonnes ont 7 cartes, les 4 suivantes ont 6 cartes }
If I < 4 Then Begin
For J := 0 to 6 Do Begin
Game.Columns[I].Cards[J] := Game.Deck[CardIndex];
Inc(CardIndex);
End;
Game.Columns[I].Count := 7;
End
Else
Begin
For J:=0 to 5 Do Begin
Game.Columns[I].Cards[J] := Game.Deck[CardIndex];
Inc(CardIndex);
End;
Game.Columns[I].Count := 6;
End;
End;
NeedRedraw := True;
End;
Procedure DrawCard(X, Y: Integer; Card: TCard; Highlighted: Boolean);
Var
CardName: String;
SuitSymbol: String;
SuitColor: Integer;
Begin
{ Fond de la carte }
If Highlighted Then SetColor(Yellow)
Else SetColor(White);
SetFillStyle(SolidFill, White);
Bar(X, Y, X + CARD_WIDTH, Y + CARD_HEIGHT);
{ Bordure }
If Highlighted Then SetColor(Yellow)
Else SetColor(Black);
Rectangle(X, Y, X + CARD_WIDTH, Y+CARD_HEIGHT);
If Card.Value > 0 Then Begin
{ Afficher la carte }
CardName := GetCardName(Card);
SuitSymbol := GetSuitSymbol(Card.Suit);
SuitColor := GetSuitColor(Card.Suit);
{ Valeur en haut à gauche }
SetColor(SuitColor);
SetTextStyle(DefaultFont, HorizDir, 1);
OutTextXY(X + 3, Y + 3, CardName);
OutTextXY(X + 3, Y + 13, SuitSymbol);
{ Valeur en bas à droite (inversée) }
OutTextXY(X + CARD_WIDTH - 15, Y + CARD_HEIGHT - 25, CardName);
OutTextXY(X + CARD_WIDTH - 15, Y + CARD_HEIGHT - 15, SuitSymbol);
{ Symbole au centre }
SetTextStyle(DefaultFont, HorizDir, 2);
OutTextXY(X + CARD_WIDTH Div 2 - 8, Y + CARD_HEIGHT Div 2 - 8, SuitSymbol);
End;
End;
Procedure DrawEmptySlot(X,Y:Integer; Highlighted: Boolean);Begin
If Highlighted Then SetColor(Yellow)
Else SetColor(DarkGray);
SetFillStyle(SolidFill, DarkGray);
Bar(X, Y, X + CARD_WIDTH, Y + CARD_HEIGHT);
If Highlighted Then SetColor(Yellow)
Else SetColor(Black);
Rectangle(X, Y, X + CARD_WIDTH, Y + CARD_HEIGHT);
End;
Procedure DrawFreeCells;
Var
I,X:Integer;
IsHighlighted:Boolean;
Begin
SetColor(White);
SetTextStyle(DefaultFont, HorizDir, 1);
OutTextXY(10, 10, 'Cellules libres:');
For I := 0 to FREECELLS - 1 Do Begin
X := 10 + I * (CARD_WIDTH + 5);
IsHighlighted := Game.HasSelection And (Game.SelectedZone = ZONE_FREECELL) And (Game.SelectedIndex = I);
If Game.FreeCellsUsed[I]Then DrawCard(X, 30, Game.FreeCells[I], IsHighlighted)
Else DrawEmptySlot(X, 30, IsHighlighted);
End;
End;
Procedure DrawFoundations;
Var
I, X: Integer;
IsHighlighted: Boolean;
TopCard: TCard;
Begin
SetColor(White);
SetTextStyle(DefaultFont, HorizDir, 1);
OutTextXY(350, 10, 'Fondations:');
For I := 0 to FOUNDATIONS - 1 Do Begin
X := 350 + I * (CARD_WIDTH + 5);
IsHighlighted := Game.HasSelection And (Game.SelectedZone = ZONE_FOUNDATION) And (Game.SelectedIndex = I);
If Game.FoundationCounts[I] > 0 Then Begin
TopCard := Game.Foundations[I];
DrawCard(X, 30, TopCard, IsHighlighted);
End
Else
Begin
DrawEmptySlot(X, 30, IsHighlighted);
End;
End;
End;
Procedure DrawColumns;
Var
I, J, X, Y: Integer;
IsHighlighted: Boolean;
Begin
SetColor(White);
SetTextStyle(DefaultFont, HorizDir, 1);
OutTextXY(10, 120, 'Colonnes:');
For I := 0 to COLUMNS - 1 Do Begin
X := 10 + I * (CARD_WIDTH + 5);
If Game.Columns[I].Count = 0 Then Begin
{ Colonne vide }
IsHighlighted := Game.HasSelection And (Game.SelectedZone = ZONE_COLUMN) And (Game.SelectedIndex = I);
DrawEmptySlot(X, 140, IsHighlighted);
End
Else
Begin
{ Dessiner les cartes de la colonne }
For J := 0 to Game.Columns[I].Count - 1 Do Begin
Y := 140 + J * 20;
IsHighlighted := Game.HasSelection And (Game.SelectedZone = ZONE_COLUMN) And
(Game.SelectedIndex = I) And (J = Game.Columns[I].Count - 1);
DrawCard(X, Y, Game.Columns[I].Cards[J], IsHighlighted);
End;
End;
End;
End;
Procedure DrawGameInfo;Begin
SetColor(White);
SetTextStyle(DefaultFont, HorizDir, 1);
OutTextXY(10, 450, 'Mouvements: ' + Chr(Ord('0') + (Game.MovesCount Mod 10)));
If Game.GameState = GAME_WON Then Begin
SetColor(Green);
OutTextXY(200, 450, 'FELICITATIONS! Vous avez gagne!');
End;
{ Instructions }
SetColor(LightGray);
OutTextXY(400, 400, 'Instructions:');
OutTextXY(400, 415, 'Clic - Selectionner/Deplacer');
OutTextXY(400, 430, 'R - Nouvelle partie');
OutTextXY(400, 445, 'ESC - Quitter');
End;
Procedure DrawBackground;Begin
SetColor(Green);
SetFillStyle(SolidFill, Green);
Bar(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
{ Titre }
SetColor(White);
SetTextStyle(DefaultFont, HorizDir, 3);
OutTextXY(150, 5, 'FREECELL');
End;
Procedure DrawScreen;Begin
DrawBackground;
DrawFreeCells;
DrawFoundations;
DrawColumns;
DrawGameInfo;
End;
Function CanMoveToFoundation(Card:TCard;FoundationIndex:Integer):Boolean;Begin
CanMoveToFoundation := False;
{ V‚rifier si la fondation correspond … la couleur }
If FoundationIndex <> Card.Suit Then Exit;
{ V‚rifier si c'est la prochaine carte dans la s‚quence }
If Game.FoundationCounts[FoundationIndex] = 0 Then Begin
{ PremiŠre carte doit ˆtre un As }
CanMoveToFoundation := (Card.Value = ACE);
End
Else
Begin
{ Doit ˆtre la carte suivante }
CanMoveToFoundation := (Card.Value = Game.Foundations[FoundationIndex].Value + 1);
End;
End;
Function CanMoveToColumn(Card:TCard;ColumnIndex:Integer):Boolean;
Var
TopCard:TCard;
Begin
CanMoveToColumn := False;
If Game.Columns[ColumnIndex].Count = 0 Then Begin
{ Colonne vide - toute carte peut ˆtre plac‚e }
CanMoveToColumn := True;
End
Else
Begin
{ Doit ˆtre une carte de valeur inf‚rieure et de couleur oppos‚e }
TopCard := Game.Columns[ColumnIndex].Cards[Game.Columns[ColumnIndex].Count - 1];
If (Card.Value = TopCard.Value - 1) And (IsRedSuit(Card.Suit) <> IsRedSuit(TopCard.Suit)) Then
CanMoveToColumn := True;
End;
End;
Function GetClickZone(X,Y:Integer;Var Zone,Index:Integer):Boolean;
Var
I, CardX, CardY: Integer;
Begin
GetClickZone := False;
{ V‚rifier les cellules libres }
For I := 0 to FREECELLS - 1 Do Begin
CardX := 10 + I * (CARD_WIDTH + 5);
If (X >= CardX) And (X <= CardX + CARD_WIDTH) And (Y >= 30) And (Y <= 30 + CARD_HEIGHT) Then Begin
Zone := ZONE_FREECELL;
Index := I;
GetClickZone := True;
Exit;
End;
End;
{ V‚rifier les fondations }
For I := 0 to FOUNDATIONS - 1 Do Begin
CardX := 350 + I * (CARD_WIDTH + 5);
If (X >= CardX) And (X <= CardX + CARD_WIDTH) And (Y >= 30) And (Y <= 30 + CARD_HEIGHT) Then Begin
Zone := ZONE_FOUNDATION;
Index := I;
GetClickZone := True;
Exit;
End;
End;
{ V‚rifier les colonnes }
For I := 0 to COLUMNS - 1 Do Begin
CardX := 10 + I * (CARD_WIDTH + 5);
If (X >= CardX) And (X <= CardX + CARD_WIDTH) And (Y >= 140) Then Begin
Zone := ZONE_COLUMN;
Index := I;
GetClickZone := True;
Exit;
End;
End;
End;
Procedure MoveCardToFoundation(Card: TCard; FoundationIndex: Integer);Begin
Game.Foundations[FoundationIndex] := Card;
Inc(Game.FoundationCounts[FoundationIndex]);
Inc(Game.MovesCount);
End;
Procedure MoveCardToColumn(Card: TCard; ColumnIndex: Integer);Begin
Game.Columns[ColumnIndex].Cards[Game.Columns[ColumnIndex].Count] := Card;
Inc(Game.Columns[ColumnIndex].Count);
Inc(Game.MovesCount);
End;
Procedure MoveCardToFreeCell(Card: TCard; FreeCellIndex: Integer);Begin
Game.FreeCells[FreeCellIndex] := Card;
Game.FreeCellsUsed[FreeCellIndex] := True;
Inc(Game.MovesCount);
End;
Procedure RemoveCardFromColumn(ColumnIndex: Integer);Begin
If Game.Columns[ColumnIndex].Count > 0 Then Dec(Game.Columns[ColumnIndex].Count);
End;
Procedure RemoveCardFromFreeCell(FreeCellIndex: Integer);Begin
Game.FreeCellsUsed[FreeCellIndex] := False;
End;
Procedure HandleCardSelection(Zone,Index:Integer);
Var
Card:TCard;
Begin
If Not Game.HasSelection Then Begin
{ S‚lectionner une carte }
Case Zone of
ZONE_FREECELL:Begin
If Game.FreeCellsUsed[Index] Then Begin
Game.SelectedCard := Game.FreeCells[Index];
Game.SelectedZone := Zone;
Game.SelectedIndex := Index;
Game.HasSelection := True;
End;
End;
ZONE_COLUMN: Begin
If Game.Columns[Index].Count > 0 Then Begin
Game.SelectedCard := Game.Columns[Index].Cards[Game.Columns[Index].Count - 1];
Game.SelectedZone := Zone;
Game.SelectedIndex := Index;
Game.HasSelection := True;
End;
End;
End;
End
Else
Begin
{ D‚placer la carte s‚lectionn‚e }
Case Zone of
ZONE_FREECELL:Begin
If Not Game.FreeCellsUsed[Index] Then Begin
{ D‚placer vers cellule libre }
MoveCardToFreeCell(Game.SelectedCard,Index);
{ Retirer la carte de sa position originale }
Case Game.SelectedZone of
ZONE_FREECELL: RemoveCardFromFreeCell(Game.SelectedIndex);
ZONE_COLUMN: RemoveCardFromColumn(Game.SelectedIndex);
End;
Game.HasSelection := False;
End;
End;
ZONE_FOUNDATION:Begin
If CanMoveToFoundation(Game.SelectedCard, Index) Then Begin
{ D‚placer vers fondation }
MoveCardToFoundation(Game.SelectedCard, Index);
{ Retirer la carte de sa position originale }
Case Game.SelectedZone of
ZONE_FREECELL: RemoveCardFromFreeCell(Game.SelectedIndex);
ZONE_COLUMN: RemoveCardFromColumn(Game.SelectedIndex);
End;
Game.HasSelection := False;
End;
End;
ZONE_COLUMN:Begin
If CanMoveToColumn(Game.SelectedCard,Index)Then Begin
{ D‚placer vers colonne }
MoveCardToColumn(Game.SelectedCard, Index);
{ Retirer la carte de sa position originale }
Case Game.SelectedZone of
ZONE_FREECELL: RemoveCardFromFreeCell(Game.SelectedIndex);
ZONE_COLUMN: RemoveCardFromColumn(Game.SelectedIndex);
End;
Game.HasSelection := False;
End;
End;
End;
End;
NeedRedraw := True;
End;
Procedure HandleMouseClick(X,Y:Integer);
Var
Zone, Index: Integer;
Begin
If GetClickZone(X, Y, Zone, Index) Then Begin
HandleCardSelection(Zone, Index);
End
Else
Begin
{ Clic dans le vide - annuler la sélection }
Game.HasSelection := False;
NeedRedraw := True;
End;
End;
Procedure CheckGameWon;
Var
I: Integer;
Begin
For I := 0 to FOUNDATIONS - 1 Do Begin
If Game.FoundationCounts[I] <> 13 Then Exit;
End;
Game.GameState := GAME_WON;
Game.HasSelection := False;
NeedRedraw := True;
End;
Procedure HandleInput;
Var
Key: Char;
MouseX, MouseY, MouseButton: LongInt;
Begin
{ Clavier }
If KeyPressed Then Begin
Key := ReadKey;
Case Key of
'r', 'R': Begin
InitGame;
End;
#27: Begin
CloseGraph;
Halt;
End;
End;
End;
{ Souris }
If MouseDriverFound Then Begin
GetMouseState(MouseX, MouseY, MouseButton);
If MouseButton = 1 Then Begin
HandleMouseClick(MouseX, MouseY);
While MouseButton = 1 Do GetMouseState(MouseX, MouseY, MouseButton);
End;
End;
End;
BEGIN
Randomize;
InitScreen;
If MouseDriverFound Then ShowMouse;
InitGame;
{ Boucle principale }
Repeat
If NeedRedraw Then Begin
DrawScreen;
NeedRedraw := False;
End;
HandleInput;
If Game.GameState = GAME_PLAYING Then CheckGameWon;
Delay(50);
Until False;
END.