-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPOKER.PAS
More file actions
571 lines (535 loc) · 13.6 KB
/
POKER.PAS
File metadata and controls
571 lines (535 loc) · 13.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
{ @author: Sylvain Maltais ([email protected])
@created: 2025
@website(https://www.gladir.com/7iles)
@abstract(Target: Turbo Pascal 7, Free Pascal 3.2)
@description: Jeu de Poker Draw a 5 cartes contre l'ordinateur
}
Program Poker;
Uses
{$IFDEF FPC}
Windows, Crt, PtcCrt, PtcGraph, PtcMouse
{$ELSE}
DOS, Crt, Graph
{$ENDIF};
Const
SCREEN_WIDTH = 640;
SCREEN_HEIGHT = 480;
CARD_WIDTH = 60;
CARD_HEIGHT = 80;
CARD_SPACING = 70;
PLAYER_CARD_Y = 300;
COMPUTER_CARD_Y = 50;
CARDS_START_X = 50;
MAX_CARDS = 52;
HAND_SIZE = 5;
{ Types de mains (du plus faible au plus fort) }
HIGH_CARD = 0;
ONE_PAIR = 1;
TWO_PAIR = 2;
THREE_KIND = 3;
STRAIGHT = 4;
FLUSH = 5;
FULL_HOUSE = 6;
FOUR_KIND = 7;
STRAIGHT_FLUSH = 8;
ROYAL_FLUSH = 9;
Type
TSuit = (Hearts, Diamonds, Clubs, Spades);
TRank = (Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace);
TCard = Record
suit: TSuit;
rank: TRank;
visible: Boolean;
End;
TDeck = Record
cards: Array[1..MAX_CARDS] of TCard;
count: Integer;
End;
TPlayerHand = Record
cards: Array[1..HAND_SIZE] of TCard;
selected: Array[1..HAND_SIZE] of Boolean;
count: Integer;
End;
TGameState = Record
deck: TDeck;
playerHand: TPlayerHand;
computerHand: TPlayerHand;
playerMoney: Integer;
currentBet: Integer;
pot: Integer;
gamePhase: Integer; { 0=deal, 1=draw, 2=showdown }
playerHandType: Integer;
computerHandType: Integer;
playerHandName: String;
computerHandName: String;
gameOver: Boolean;
winner: Integer; { 0=tie, 1=player, 2=computer }
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 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) + 2;
End;
Function IsRed(suit:TSuit):Boolean;Begin
IsRed:=(suit=Hearts)or(suit=Diamonds);
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);
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 DrawCardFromDeck(Var card:TCard);Begin
card:=Game.deck.cards[Game.deck.count];
Dec(Game.deck.count);
End;
Procedure InitializeGame;
Var
i:Integer;
Begin
Game.playerMoney:=1000;
Game.currentBet:=50;
Game.pot:=0;
Game.gamePhase:=0;
Game.gameOver:=False;
Game.winner:=0;
{ Initialiser les mains }
For i:=1 to HAND_SIZE do Begin
Game.playerHand.selected[i] := False;
End;
Game.playerHand.count := 0;
Game.computerHand.count := 0;
InitializeDeck;
needRedraw:=True;
End;
Procedure DrawCard(x,y:Integer;card:TCard;faceUp,selected:Boolean);
Var
suitChar:Char;
rankStr:String;
color:Integer;
Begin
{ Fond de la carte }
If selected Then SetColor(14) { Jaune }
Else SetColor(15); { Blanc }
SetFillStyle(1, GetColor);
Bar(x, y, x + CARD_WIDTH, y + CARD_HEIGHT);
{ Bordure }
SetColor(0); { Noir }
Rectangle(x, y, x + CARD_WIDTH, y + CARD_HEIGHT);
If faceUp Then Begin
{ Déterminer la couleur }
If IsRed(card.suit)Then color:=4 { Rouge }
Else color:=0; { Noir }
{ 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
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';
Ace: rankStr := 'A';
End;
{ Dessiner le rang et la suite }
SetColor(color);
SetTextStyle(0,0,1);
OutTextXY(x+3,y+3,rankStr);
OutTextXY(x+3,y+15,suitChar);
{ Symbole central }
If card.rank>=Jack Then Begin
SetTextStyle(0, 0, 2);
OutTextXY(x + CARD_WIDTH div 2 - 8, y + CARD_HEIGHT div 2 - 8, rankStr);
End
Else
Begin
SetTextStyle(0, 0, 3);
OutTextXY(x + CARD_WIDTH div 2 - 8, y + CARD_HEIGHT div 2 - 8, suitChar);
End;
End
Else
Begin
{ Dos de carte }
SetColor(1); { Bleu }
SetFillStyle(1, 1);
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 DrawPlayerHand;
Var
i,x:Integer;
Begin
For i:=1 to Game.playerHand.count do Begin
x:=CARDS_START_X+(i-1)*CARD_SPACING;
DrawCard(x,PLAYER_CARD_Y,Game.playerHand.cards[i],True,Game.playerHand.selected[i]);
End;
End;
Procedure DrawComputerHand;
Var
i,x:Integer;
showCards:Boolean;
Begin
showCards:=(Game.gamePhase=2); { Montrer les cartes au showdown }
For i:=1 to Game.computerHand.count do Begin
x:=CARDS_START_X + (i - 1) * CARD_SPACING;
DrawCard(x, COMPUTER_CARD_Y, Game.computerHand.cards[i], showCards, False);
End;
End;
Function EvaluateHand(Var hand:TPlayerHand):Integer;
Var
i:Integer;
ranks:Array[1..13] of Integer;
suits:Array[1..4] of Integer;
isFlush,isStraight:Boolean;
pairs,threes,fours:Integer;
Begin
{ Initialiser les compteurs }
For i:=1 to 13 do ranks[i] := 0;
For i:=1 to 4 do suits[i] := 0;
{ Compter les rangs et les couleurs }
For i:=1 to hand.count do Begin
Inc(ranks[Ord(hand.cards[i].rank) + 1]);
Inc(suits[Ord(hand.cards[i].suit) + 1]);
End;
{ Vérifier flush }
isFlush:=False;
For i:=1 to 4 do If suits[i]=5 Then isFlush:=True;
{ Vérifier straight }
isStraight := False;
For i:=1 to 9 do Begin
If (ranks[i]=1)and(ranks[i+1]=1)and(ranks[i+2] = 1)and(ranks[i+3] = 1)and(ranks[i+4]=1)Then isStraight:=True;
End;
{ Vérifier A-2-3-4-5 (wheel) }
If(ranks[1] = 1)and(ranks[2]=1)and(ranks[3]=1)and(ranks[4]=1)and(ranks[13]=1)Then isStraight:=True;
{ Compter les paires, brelans, carrés }
pairs:=0;
threes:=0;
fours:=0;
For i:=1 to 13 do Begin
If ranks[i] = 2 Then Inc(pairs);
If ranks[i] = 3 Then Inc(threes);
If ranks[i] = 4 Then Inc(fours);
End;
{ Déterminer le type de main }
If isFlush and isStraight Then Begin
{ Vérifier quinte flush royale }
If(ranks[10]=1)and(ranks[11]=1)and(ranks[12]=1)and(ranks[13]=1)and(ranks[1]=1)Then EvaluateHand:=ROYAL_FLUSH
Else EvaluateHand:=STRAIGHT_FLUSH;
End
Else
If fours=1 Then EvaluateHand:=FOUR_KIND Else
If(threes=1)and(pairs=1)Then EvaluateHand:=FULL_HOUSE Else
If isFlush Then EvaluateHand:=FLUSH Else
If isStraight Then EvaluateHand:=STRAIGHT Else
If threes=1 Then EvaluateHand:=THREE_KIND Else
If pairs=2 Then EvaluateHand:=TWO_PAIR Else
If pairs=1 Then EvaluateHand:=ONE_PAIR
Else EvaluateHand:=HIGH_CARD;
End;
Function GetHandName(handType:Integer):String;Begin
Case handType of
HIGH_CARD: GetHandName := 'Carte haute';
ONE_PAIR: GetHandName := 'Paire';
TWO_PAIR: GetHandName := 'Double paire';
THREE_KIND: GetHandName := 'Brelan';
STRAIGHT: GetHandName := 'Quinte';
FLUSH: GetHandName := 'Couleur';
FULL_HOUSE: GetHandName := 'Full';
FOUR_KIND: GetHandName := 'Carre';
STRAIGHT_FLUSH: GetHandName := 'Quinte flush';
ROYAL_FLUSH: GetHandName := 'Quinte flush royale';
Else GetHandName := 'Inconnu';
End;
End;
Procedure DealInitialCards;
Var
i: Integer;
Begin
{ Distribuer 5 cartes à chaque joueur }
For i := 1 to HAND_SIZE do Begin
DrawCardFromDeck(Game.playerHand.cards[i]);
Game.playerHand.count := i;
End;
For i:=1 to HAND_SIZE do Begin
DrawCardFromDeck(Game.computerHand.cards[i]);
Game.computerHand.count := i;
End;
Game.gamePhase := 1; { Phase de draw }
needRedraw := True;
End;
Procedure ComputerDraw;
Var
i,j,cardsToReplace:Integer;
handType:Integer;
Begin
{ IA simple pour l'ordinateur }
handType := EvaluateHand(Game.computerHand);
{ Stratégie basique : garder les bonnes cartes }
If handType >= ONE_PAIR Then cardsToReplace:=Random(2) { Garder la paire ou mieux }
Else cardsToReplace:=3+Random(2); { Remplacer 3-4 cartes }
{ Remplacer les cartes aléatoirement }
For i:=1 to cardsToReplace do Begin
j:=Random(HAND_SIZE) + 1;
If Game.deck.count>0 Then DrawCardFromDeck(Game.computerHand.cards[j]);
End;
End;
Procedure DrawPhase;
Var
i,replacedCards: Integer;
Begin
{ Remplacer les cartes sélectionnées du joueur }
replacedCards := 0;
For i:=1 to HAND_SIZE do Begin
If Game.playerHand.selected[i]Then Begin
If Game.deck.count>0 Then Begin
DrawCardFromDeck(Game.playerHand.cards[i]);
Inc(replacedCards);
End;
Game.playerHand.selected[i] := False;
End;
End;
{ L'ordinateur fait son draw }
ComputerDraw;
{ Passer au showdown }
Game.gamePhase := 2;
needRedraw := True;
End;
Procedure Showdown;Begin
{ Évaluer les mains }
Game.playerHandType:=EvaluateHand(Game.playerHand);
Game.computerHandType:=EvaluateHand(Game.computerHand);
Game.playerHandName:=GetHandName(Game.playerHandType);
Game.computerHandName:=GetHandName(Game.computerHandType);
{ Déterminer le gagnant }
If Game.playerHandType>Game.computerHandType Then Begin
Game.winner:=1; { Joueur gagne }
Inc(Game.playerMoney, Game.pot);
End
Else
If Game.computerHandType > Game.playerHandType Then Begin
Game.winner:=2; { Ordinateur gagne }
End
Else
Begin
Game.winner := 0; { Match nulle }
Inc(Game.playerMoney, Game.currentBet); { Remboursement }
End;
needRedraw := True;
End;
Procedure DrawUI;
Var
s:String;
Begin
{ Fond vert }
SetColor(2);
SetFillStyle(1, 2);
Bar(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
{ Titre }
SetColor(15);
SetTextStyle(0, 0, 2);
OutTextXY(SCREEN_WIDTH div 2 - 50, 10, 'POKER DRAW');
{ Informations du joueur }
SetTextStyle(0, 0, 1);
Str(Game.playerMoney, s);
OutTextXY(10, SCREEN_HEIGHT - 80, 'Argent: $' + s);
Str(Game.currentBet, s);
OutTextXY(10, SCREEN_HEIGHT - 65, 'Mise: $' + s);
Str(Game.pot, s);
OutTextXY(10, SCREEN_HEIGHT - 50, 'Pot: $' + s);
{ Instructions selon la phase }
Case Game.gamePhase of
0:OutTextXY(10, SCREEN_HEIGHT - 35, 'Appuyez ESPACE pour distribuer');
1:Begin
OutTextXY(10, SCREEN_HEIGHT - 35, 'Cliquez sur les cartes a remplacer');
OutTextXY(10, SCREEN_HEIGHT - 20, 'Appuyez ENTREE pour echanger');
End;
2:Begin
OutTextXY(10, SCREEN_HEIGHT - 35, 'Votre main: ' + Game.playerHandName);
OutTextXY(10, SCREEN_HEIGHT - 20, 'Ordinateur: ' + Game.computerHandName);
Case Game.winner of
0: OutTextXY(300, SCREEN_HEIGHT - 35, 'EGALITE!');
1: OutTextXY(300, SCREEN_HEIGHT - 35, 'VOUS GAGNEZ!');
2: OutTextXY(300, SCREEN_HEIGHT - 35, 'ORDINATEUR GAGNE!');
End;
OutTextXY(300, SCREEN_HEIGHT - 20, 'Appuyez N pour nouvelle partie');
End;
End;
{ Légende générale }
OutTextXY(450, SCREEN_HEIGHT - 50, 'ESC: Quitter');
End;
Function GetCardAtPosition(x,y:Integer):Integer;
Var
i,cardX:Integer;
Begin
GetCardAtPosition := 0;
{ Vérifier si on clique sur une carte du joueur pendant la phase draw }
If(Game.gamePhase=1)and(y >= PLAYER_CARD_Y)and(y <= PLAYER_CARD_Y + CARD_HEIGHT)Then Begin
For i:=1 to Game.playerHand.count do Begin
cardX:=CARDS_START_X+(i-1)*CARD_SPACING;
If (x>=cardX)and(x <= cardX+CARD_WIDTH) Then Begin
GetCardAtPosition := i;
Exit;
End;
End;
End;
End;
Procedure HandleMouseClick(x,y:Integer);
Var
cardIndex:Integer;
Begin
cardIndex:=GetCardAtPosition(x, y);
If cardIndex>0 Then Begin
{ Toggle selection de la carte }
Game.playerHand.selected[cardIndex] := not Game.playerHand.selected[cardIndex];
needRedraw := True;
End;
End;
Procedure HandleInput;
Var
key:Char;
mouseX,mouseY,mouseButton:LongInt;
Begin
{ Gérer la souris }
GetMouseState(mouseX, mouseY, mouseButton);
If (mouseButton and 1)<>0 Then Begin
{ Attendre le relâchement }
repeat
GetMouseState(mouseX, mouseY, mouseButton);
Delay(10);
Until (mouseButton and 1) = 0;
HandleMouseClick(mouseX, mouseY);
End;
{ Gérer le clavier }
If KeyPressed Then Begin
key:=ReadKey;
Case key of
' ': If Game.gamePhase = 0 Then Begin
{ Nouvelle donne }
Game.pot := Game.currentBet * 2;
Dec(Game.playerMoney, Game.currentBet);
DealInitialCards;
End;
#13: If Game.gamePhase = 1 Then Begin
{ Draw phase }
DrawPhase;
Showdown;
End;
'n', 'N': If Game.gamePhase = 2 Then Begin
{ Nouvelle partie }
If Game.playerMoney >= Game.currentBet Then Begin
InitializeGame;
End
Else
Begin
{ Game over - pas assez d'argent }
Game.gameOver := True;
End;
End;
#27: Game.gameOver := True; { ESC pour quitter }
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);
SetLineStyle(0, 0, 1);
End
Else
Begin
WriteLn('Erreur graphique : ', GraphErrorMsg(ErrCode));
Halt;
End;
End;
Procedure GameLoop;Begin
InitializeGraphics;
InitializeGame;
Repeat
If needRedraw Then Begin
DrawUI;
DrawComputerHand;
DrawPlayerHand;
needRedraw:=False;
End;
HandleInput;
Delay(50);
Until Game.gameOver;
CloseGraph;
End;
BEGIN
Randomize;
GameLoop;
END.