-
Notifications
You must be signed in to change notification settings - Fork 0
/
newcards.pas
5388 lines (4236 loc) · 161 KB
/
newcards.pas
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
UNIT newcards;
// See end of program for author/licence, etc.
// Lazarus requires object Free Pascal mode.
// By default, "string" type is ANSI string. (I think Lazarus puts this.)
{$mode objfpc}{$H+}
INTERFACE
Uses
About,
Classes,
Controls,
Dialogs,
ExtCtrls, // TImage
Forms,
Graphics,
StdCtrls, Buttons, Menus, ComCtrls,
SysUtils,
heartsdebug;
Type
// FORM TYPES ...
// TfrmMain
TfrmMain = class(TForm)
pnlHearts : TPanel;
imgHeartsNewGame : TImage;
imgNewGame : TImage; // + sign to start new game
imgNewGameHighlight : TImage;
imgHeartsReplayHand : TImage;
imgReplay : TImage; // Circular arrow to replay hand
imgReplayHighlight : TImage;
imgHeartsHint : TImage;
imgHint : TImage; // Lightbulb to ask for hint
imgHintHighlight : TImage;
imgHeartsReveal : TImage;
imgRevealOpen : TImage; // Open/closed eyes to reveal/hide cards
imgRevealOpenHighlight: TImage;
imgRevealClosed : TImage;
imgRevealClosedHighlight: TImage;
imgHeartsPlayer : TImage; // Switch between human and robot player
imgHuman : TImage;
imgHumanHighlight : TImage;
imgRobot : TImage;
imgRobotHighlight : TImage;
tbarSpeed : TTrackBar; // How fast to play/remove the cards
imgAbout : TImage;
imgAboutNormal : TImage; // Question mark to get program info
imgAboutHighlight : TImage;
imgHeartsQuit : TImage;
imgQuit : TImage; // Green running person to exit program
imgQuitHighlight : TImage;
butDebug : TButton; // Button to show debug form
timClearTrick : TTimer;
timClearPass : TTimer;
imgWest : TImage; // The characters
imgNorth : TImage;
imgEast : TImage;
imgSouth : TImage;
imgSouthHuman : TImage; // Use tanuki image when human playing
imgSouthRobot : TImage; // Use robot image when robot playing
imgHeart : TImage;
imgWestHeart : TImage; // Spot for their winning heart
imgNorthHeart : TImage;
imgEastheart : TImage;
imgSouthHeart : TImage;
imgPass : TImage; // Where to pass cards this hand
imgPassleft : TImage;
imgPassright : TImage;
imgPassacross : TImage;
imgContinue : TImage; // Big image so can click anywhere
Card01 : TImage; // The user's cards
Card02 : TImage;
Card03 : TImage;
Card04 : TImage;
Card05 : TImage;
Card06 : TImage;
Card07 : TImage;
Card08 : TImage;
Card09 : TImage;
Card10 : TImage;
Card11 : TImage;
Card12 : TImage;
Card13 : TImage;
imgTag : TImage; // The little red hint arrowhead
labWestGamesWon : TLabel; // Number of games won by each
labNorthGamesWon : TLabel;
labEastGamesWon : TLabel;
labSouthGamesWon : TLabel;
imgWestsmall : TImage; // Character images in the score
imgNorthsmall : TImage;
imgEastsmall : TImage;
imgSouthsmall : TImage;
imgSouthsmallhuman : TImage; // Use tanuki image when human playing
imgSouthsmallrobot : TImage; // Use robot image when robot playing
labWestGamePoints : TLabel;
labNorthGamePoints : TLabel;
labEastGamePoints : TLabel; // Scores
labSouthGamePoints : TLabel;
labWestHandPoints : TLabel;
labNorthHandPoints : TLabel;
labEastHandPoints : TLabel;
labSouthHandPoints : TLabel;
procedure butDebugClick(Sender: TObject);
procedure Card01Click(Sender: TObject);
procedure Card02Click(Sender: TObject);
procedure Card03Click(Sender: TObject);
procedure Card04Click(Sender: TObject);
procedure Card05Click(Sender: TObject);
procedure Card06Click(Sender: TObject);
procedure Card07Click(Sender: TObject);
procedure Card08Click(Sender: TObject);
procedure Card09Click(Sender: TObject);
procedure Card10Click(Sender: TObject);
procedure Card11Click(Sender: TObject);
procedure Card12Click(Sender: TObject);
procedure Card13Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure imgAboutClick(Sender: TObject);
procedure imgAboutMouseEnter(Sender: TObject);
procedure imgAboutMouseLeave(Sender: TObject);
procedure imgContinueClick(Sender: TObject);
procedure imgHeartsHintClick(Sender: TObject);
procedure imgHeartsHintMouseEnter(Sender: TObject);
procedure imgHeartsHintMouseLeave(Sender: TObject);
procedure imgHeartsNewGameClick(Sender: TObject);
procedure imgHeartsNewGameMouseEnter(Sender: TObject);
procedure imgHeartsNewGameMouseLeave(Sender: TObject);
procedure imgHeartsPlayerClick(Sender: TObject);
procedure imgHeartsPlayerMouseEnter(Sender: TObject);
procedure imgHeartsPlayerMouseLeave(Sender: TObject);
procedure imgHeartsQuitClick(Sender: TObject);
procedure imgHeartsQuitMouseEnter(Sender: TObject);
procedure imgHeartsQuitMouseLeave(Sender: TObject);
procedure imgHeartsReplayHandClick(Sender: TObject);
procedure imgHeartsReplayHandMouseEnter(Sender: TObject);
procedure imgHeartsReplayHandMouseLeave(Sender: TObject);
procedure imgHeartsRevealClick(Sender: TObject);
procedure imgHeartsRevealMouseEnter(Sender: TObject);
procedure imgHeartsRevealMouseLeave(Sender: TObject);
procedure imgPassClick(Sender: TObject);
procedure tbarSpeedChange(Sender: TObject);
procedure timClearPassTimer(Sender: TObject);
procedure timClearTrickTimer(Sender: TObject);
private
public
end;
// ENUMERATION TYPES ...
// "card" is an enumerated type.
// Just like a "char" etc.
// So a variable of type "card" is one value from this list, not a set.
// Note that we use "c2" etc. and not "2c". Why? Because enumeration
// entries must conform to identifier naming.
// Note too that they are in the order clubs, diamonds, spades, hearts.
// This is the order they will be shown in hands, and contrasts colours.
card = (c2, c3, c4, c5, c6, c7, c8, c9, c10, cj, cq, ck, ca,
d2, d3, d4, d5, d6, d7, d8, d9, d10, dj, dq, dk, da,
s2, s3, s4, s5, s6, s7, s8, s9, s10, sj, sq, sk, sa,
h2, h3, h4, h5, h6, h7, h8, h9, h10, hj, hq, hk, ha);
// Note that these are just suit names.
// The actual suits of cards are in the Const section below, with "s".
suit = (club, diamond, spade, heart);
// What the human user can be doing
// Players in the game
// The possible pass directions
gamemode = (passing, accepting, playing);
player = (west, north, east, south);
passway = (toleft, toright, across, hold);
// SET TYPES ...
// So a variable of type "cardset" is a set containing members of type "card".
cardset = set of card;
Const
cardnames : array [card] of shortstring
= ('2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', 'TC', 'JC', 'QC', 'KC', 'AC',
'2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', 'TD', 'JD', 'QD', 'KD', 'AD',
'2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', 'TS', 'JS', 'QS', 'KS', 'AS',
'2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', 'TH', 'JH', 'QH', 'KH', 'AH');
CARDWIDTH : smallint = 72;
CARDHEIGHT : smallint = 96;
deck : cardset = [c2..ha];
clubs : cardset = [c2..ca];
diamonds : cardset = [d2..da];
spades : cardset = [s2..sa];
hearts : cardset = [h2..ha];
twoofclubs : card = c2;
queenofspades : card = sq;
kingofspades : card = sk;
aceofspades : card = sa;
playername : array [player] of shortstring
= ('west', 'north', 'east', 'south');
Var
// These are global variables.
// Rather than pass game state stuff back and forth through arguments,
// it's a lot simpler to just say these values are always available.
// Note that the structure of game elements goes:
// game the game until a winner is declared
// hand however many hands (deals) it takes
// trick the 13 tricks in each hand
// play the 4 plays in each trick
frmMain : TfrmMain;
backimage : TImage;
cardimage : array [card] of TImage;
cardplayed : boolean;
controlbroken : Boolean;
dealer : player;
debug : Boolean = false;
gameinprogress : Boolean = False;
gamepoints : array [player] of smallint; {small int because scores can go negative}
gamewon : Boolean;
gameswon : array [player] of smallint;
handcards : array [player] of cardset;
handimage : array [player, 1..13] of TImage;
handnumber : smallint;
handover : Boolean;
handpoints : array [player] of smallint;
heartsbroken : boolean;
humanimage : array [1..13] of TImage; {Could probably use handimage for this}
humanplayer : player = south;
humanstate : gamemode;
humantimerson : boolean;
imagenum : smallint; { The 1-13 card01 image number}
loghand : cardset; { all cards played during this hand}
logplayer : array [player] of cardset; { cards played by each player in hand}
logsuit : array [suit] of cardset; { cards of each suit played in hand}
passcards : array [player] of cardset;
passdirection : passway;
passhintcards : cardset;
passmade : boolean;
playcard : card;
playnumber : smallint;
playplayer : player;
playlegal : cardset;
playsuit : suit;
playerdanger : array [player] of smallint;
playerstanding : array [player] of smallint;
playeroutofsuit : array [player,suit] of boolean;
projectedtrickwinner : player;
receivecards : array [player] of cardset;
replayinprogress : boolean = false;
reveal : boolean = false;
robotdelay : smallint;
robotplay : boolean = false;
savecurrenthand : array [player] of cardset;
savecurrentpass : array [player] of cardset;
savecurrentdirection : passway;
savecurrentgamepoints : array [player] of smallint;
savecurrentgamepointstext : array [player] of shortstring;
savecurrenthandpointstext : array [player] of shortstring;
saveprevioushand : array [player] of cardset;
savepreviouspass : array [player] of cardset;
savepreviousdirection : passway;
savepreviousgamepoints : array [player] of smallint;
savepreviousgamepointstext : array [player] of shortstring;
saveprevioushandpointstext : array [player] of shortstring;
tagcards : cardset;
tagimage : array [1..13] of TImage;
trickcard : array [1..4] of card;
trickimage : array [player] of TImage;
tricknumber : smallint;
trickover : Boolean;
trickplayer : array [1..4] of player;
trickpoints : array [player] of smallint;
trickset : cardset; {like trickcard, but as a set of cards played so far}
tricksetsuitcards : cardset; {subset of trickset, just cards of the led suit}
tricksuit : cardset; {all the cards in the suit of the card led}
trickwinner : player;
winners : set of player; {can be more than 1}
IMPLEMENTATION
// We declare all our procedures/functions "forward".
// It allows us to use them in any order without worrying whether they have
// been defined lexically first. And provides a nice reference list.
// Set up
procedure setuppanelimages; forward;
procedure loadcardimagesfromresources; forward;
procedure setuphandimages; forward;
procedure setuptagimages; forward;
procedure setuptrickimages; forward;
procedure setupotherimages; forward;
procedure setuptiming; forward;
// Ersatz form procedure
procedure processcardclick; forward;
// Game flow
procedure startgame; forward;
procedure finishgame; forward;
procedure starthand; forward;
procedure finishhand; forward;
procedure starttrick; forward;
procedure finishtrick; forward;
// Game help
// ... dealing
procedure choosedealer; forward;
procedure dealcards; forward;
// ... passing
procedure select3cardstopass; forward;
procedure selectforthisplayer (p : player); forward;
procedure pass3cards; forward;
// ... playing
function playerwith2clubs : player; forward;
procedure getlegalcards; forward;
procedure selectcardtoplay; forward;
procedure chooseplayingcard; forward;
procedure maketheplay; forward;
// ... winning
procedure checkforwinner; forward;
// Strategy
procedure basicplay; forward;
procedure basicleading; forward;
procedure basicnotleadingnotfollowingsuit; forward;
procedure basicfollowhearts; forward;
procedure basicfollowspades; forward;
procedure basicfollowother; forward;
procedure getcontrolplay; forward;
procedure stopcontrolplay; forward;
procedure calculateplayerstandings; forward;
procedure calculateplayerdangers; forward;
function oktogivepoints (xcard: card) : boolean; forward;
procedure checkforcontrol; forward;
function canprojecttrickwinner (xcard: card) : boolean; forward;
// Card display
procedure resetcardpositions; forward;
procedure showeveryhand; forward;
procedure showhand (p : player); forward;
procedure removecard (c : card; p: player); forward;
procedure popcard; forward;
procedure sinkcard; forward;
procedure tagcard; forward;
procedure untagcard; forward;
procedure bughands; forward;
procedure bugit (s : string); forward;
// Score display
procedure clearscores; forward;
procedure savepreviousscores; forward;
procedure savecurrentscores; forward;
procedure restorescoresfromcurrent; forward;
procedure restorescoresfromprevious; forward;
procedure resetcurrenttoprevious; forward;
// Utility
function countcards (xcards : cardset) : smallint; forward;
function cards_to_cardnames (xcards : cardset) : shortstring; forward;
function cardname_to_card (s : shortstring) : card; forward;
function card_to_cardpos (x : card; xcards : cardset) : smallint; forward;
function cardpos_to_card (n : smallint; xcards : cardset) : card; forward;
function card_to_image (x : card; xcards : cardset) : smallint; forward;
function image_to_card (imagenum : smallint; p : player) : card; forward;
function pickarandomcard (xcards : cardset) : card; forward;
function cardsuit (x: card) : suit; forward;
function cardrank (x : card) : smallint; forward;
function averagerank (xcards : cardset) : single; forward;
function highestofsuit (xcards: cardset; suitset: cardset) : card; forward;
function highestofall (xcards: cardset) : card; forward;
function lowestofsuit (xcards: cardset; suitset: cardset) : card; forward;
function lowestofall (xcards: cardset) : card; forward;
function cardtoavoidtrick : card; forward;
// Include resource newcards.lfm, the form layout file
// * doesn't mean all, it means substitute unit name, i.e. newcards
{$R *.lfm}
//###################################################################
// SET UP PROCEDURES
//###################################################################
//===================================================================
// Load the form
// The last thing it does is start the first game.
//===================================================================
procedure TfrmMain.FormCreate(Sender: TObject);
begin
randomize;
setuppanelimages;
loadcardimagesfromresources;
setuphandimages;
setuptagimages;
setuptrickimages;
setupotherimages;
setuptiming;
startgame;
end;
//===================================================================
// The panel images.
//===================================================================
procedure setuppanelimages;
begin
frmMain.imgHeartsNewGame.Picture := frmMain.imgNewGame.Picture;
frmMain.imgHeartsReplayHand.Picture := frmMain.imgReplay.Picture;
frmMain.imgHeartsHint.Picture := frmMain.imgHint.Picture;
frmMain.imgHeartsReveal.Picture := frmMain.imgRevealClosed.Picture;
frmMain.imgHeartsPlayer.Picture := frmMain.imgHuman.Picture;
frmMain.imgAbout.Picture := frmMain.imgAboutNormal.Picture;
frmMain.imgHeartsQuit.Picture := frmMain.imgQuit.Picture;
end;
//===================================================================
// Load card images from resources.
// This is the preferred to loading from files as it allows the app
// to be distributed as a self-contained executable with no other
// files needed.
//===================================================================
procedure loadcardimagesfromresources;
var
c : card;
i : smallint = 0;
n : shortstring;
pic : TPicture;
resname : shortstring;
begin
pic := TPicture.Create;
// The card back
backimage := TImage.Create(frmMain);
TImage(backimage).Transparent := True;
TImage(backimage).Parent := frmMain;
TImage(backimage).Width := CARDWIDTH;
TImage(backimage).Height := CARDHEIGHT;
TImage(backimage).Left := 1;
TImage(backimage).Top := 500;
TImage(backimage).Visible := false;
pic.LoadFromResourceName (HInstance, 'BACKLOON');
TImage(backimage).Picture.Assign(pic);
// The card fronts
for c in card do begin
i := i + 1;
cardimage[c] := TImage.Create(frmMain);
TImage(cardimage[c]).Transparent := True;
TImage(cardimage[c]).Parent := frmMain;
TImage(cardimage[c]).Width := CARDWIDTH;
TImage(cardimage[c]).Height := CARDHEIGHT;
TImage(cardimage[c]).Left := 15*i;
TImage(cardimage[c]).Top := 500;
TImage(cardimage[c]).Visible := false;
n := inttostr(i);
if length(n) = 1 then begin
n := '0' + n;
end;
resname := 'C' + n;
pic.LoadFromResourceName (HInstance, resname);
TImage(cardimage[c]).Picture.Assign(pic);
end;
pic.free;
end;
//===================================================================
// Set up images to display each player's hand.
//===================================================================
procedure setuphandimages;
var
i : smallint;
p : player;
begin
for p in player do begin
case p of
west, north, east : begin
for i := 1 to 13 do begin
handimage[p,i] := TImage.Create(frmMain);
TImage(handimage[p,i]).Transparent := True;
TImage(handimage[p,i]).Parent := frmMain;
TImage(handimage[p,i]).Width := CARDWIDTH;
TImage(handimage[p,i]).Height := CARDHEIGHT;
TImage(handimage[p,i]).Visible := false;
end;
end;
south : begin
humanimage[1] := frmMain.Card01;
humanimage[2] := frmMain.Card02;
humanimage[3] := frmMain.Card03;
humanimage[4] := frmMain.Card04;
humanimage[5] := frmMain.Card05;
humanimage[6] := frmMain.Card06;
humanimage[7] := frmMain.Card07;
humanimage[8] := frmMain.Card08;
humanimage[9] := frmMain.Card09;
humanimage[10] := frmMain.Card10;
humanimage[11] := frmMain.Card11;
humanimage[12] := frmMain.Card12;
humanimage[13] := frmMain.Card13;
end;
end; {end case}
end; {end for}
// Note that we reset card positions here. Although they will be
// reset by the start game procedure anyway, we do it here so that
// they have the correct values in time for the setuptrickimages
// routine, which is based on positions relative to the cards.
resetcardpositions;
end;
//===================================================================
// Set up tag images - the little markers when a hint is requested.
//===================================================================
procedure setuptagimages;
var
i : smallint;
begin
for i := 1 to 13 do begin
TImage(tagimage[i]) := TImage.Create(frmMain);
TImage(tagimage[i]).Transparent := True;
TImage(tagimage[i]).Parent := frmMain;
TImage(tagimage[i]).Width := 16;
TImage(tagimage[i]).Height := 16;
TImage(tagimage[i]).Top := humanimage[i].Top + CARDHEIGHT + 1;
TImage(tagimage[i]).Left := humanimage[i].Left + 2;
TImage(tagimage[i]).Picture := frmMain.imgTag.Picture;
TImage(tagimage[i]).Visible := false;
end;
end;
//===================================================================
// Set up images to display each card of the trick.
//
// Rather than give them absolute pixels, we center between card hands.
// The formula for midpoint of the center area looks a little weird,
// but it does reduce to that.
//
// Then we position the trick cards around the midpoint.
//===================================================================
procedure setuptrickimages;
var
midpointleft : smallint;
midpointtop : smallint;
p : player;
posleft : smallint;
postop : smallint;
begin
midpointtop := (handimage[north,1].Top + handimage[north,1].Height + humanimage[1].Top ) div 2;
midpointleft := (handimage[west,1].Left + handimage[west,1].Width + handimage[east,1].Left) div 2;
for p in player do begin
case p of
south: begin
posleft := midpointleft - (humanimage[1].width div 2);
postop := midpointtop + 2;
end;
west: begin
posleft := (midpointleft - handimage[west,1].width) - 4;
postop := midpointtop - (handimage[west,1].Height div 2);
end;
north: begin
posleft := midpointleft - (handimage[north,1].width div 2);
postop := (midpointtop - handimage[north,1].Height) - 2;
end;
east: begin
posleft := midpointleft + 4;
postop := midpointtop - (handimage[east,1].height div 2);
end;
end;
trickimage[p] := TImage.Create(frmMain);
TImage(trickimage[p]).Transparent := True;
TImage(trickimage[p]).Parent := frmMain;
TImage(trickimage[p]).Width := CARDWIDTH;
TImage(trickimage[p]).Height := CARDHEIGHT;
TImage(trickimage[p]).Left:= posleft;
TImage(trickimage[p]).Top:= postop;
TImage(trickimage[p]).visible := false;
end;
end;
//===================================================================
// Set up pass image, heart images, and the overlay for continuing.
// The overlay would be in the way on the form if we had it at its
// proper size and position during design. That's why we move and
// size it now.
//===================================================================
procedure setupotherimages;
var
p : player;
begin
frmMain.imgPass.Visible := False;
frmMain.imgSouthHeart.Picture := frmMain.imgHeart.Picture;
frmMain.imgWestHeart.Picture := frmMain.imgHeart.Picture;
frmMain.imgNorthHeart.Picture := frmMain.imgHeart.Picture;
frmMain.imgEastHeart.Picture := frmMain.imgHeart.Picture;
frmMain.imgContinue.Top := frmMain.imgNorth.Top - 5;
frmMain.imgContinue.Left := 1;
frmMain.imgContinue.Width := frmMain.Width - 2;
frmMain.imgContinue.Height := frmMain.Height - frmMain.imgContinue.Top;
frmMain.imgContinue.Visible := False;
frmMain.imgContinue.Sendtoback;
frmMain.labSouthGamesWon.Caption := '';
frmMain.labWestGamesWon.Caption := '';
frmMain.labNorthGamesWon.Caption := '';
frmMain.labEastGamesWon.Caption := '';
for p in player do begin
gameswon[p] := 0;
end;
end;
//===================================================================
// Set up the initial timing for:
// - receiving a pass
// - clearing away the trick
// - robot play
//
// We set the slider close to the right. The slider is an indication of
// speed. Moving it to the right means increasing the speed of play.
// We have to convert that to a time delay. That's the inverse, so
// we subtract the slider position. We want the delay to range from
// 0 to 3 seconds. Each tick represents 1/10 of a second (100 msec).
//
// When the user is playing, they have a choice of waiting for the
// program to receive the pass and clear the trick away. Or they can
// click anywhere on the green table to have that done immediately.
//
// When we start the program, they may not know they can click to
// make that happen, so we put the timers on. They can then adjust the
// timing to their liking. If they slide it all the way to the left,
// the timers will not be used. (Hopefully they will then discover
// that clicking moves the game along.) We want to have this setting
// where the timers are completely off, so they can take as long as
// they like to look at things before moving on.
//
// When the robot is playing, the timers will be used, and the slider
// will control the pace of the game - not only receiving the pass
// and clearing away the trick, but also when making the pass so any
// one watching can see what cards the robot chooses to pass.
//
// When the robot starts, we will choose a slightly shorter delay
// because this initial delay is geared toward the user playing.
//===================================================================
procedure setuptiming;
var
playdelay : smallint;
begin
frmmain.tbarspeed.min := 0;
frmmain.tbarspeed.max := 30;
frmmain.tbarspeed.position := 25;
playdelay := 100 * ((frmmain.tbarspeed.max +1) - frmmain.tbarSpeed.position);
humantimerson := true;
frmmain.timClearTrick.interval := playdelay;
frmmain.timClearPass.interval := playdelay;
robotdelay := playdelay;
end;
//###################################################################
// PROCEDURES FOR TfrmMain
//
// User interactive procedures.
//###################################################################
//===================================================================
// New game - click (start game).
//===================================================================
procedure TfrmMain.imgHeartsNewGameClick(Sender: TObject);
begin
startgame;
end;
//===================================================================
// - enter (highlight it).
//===================================================================
procedure TfrmMain.imgHeartsNewGameMouseEnter(Sender: TObject);
begin
imgHeartsNewGame.Picture := frmMain.imgNewGameHighlight.Picture;
end;
//===================================================================
// - leave (dull it).
//===================================================================
procedure TfrmMain.imgHeartsNewGameMouseLeave(Sender: TObject);
begin
imgHeartsNewGame.Picture := frmMain.imgNewGame.Picture;
end;
//===================================================================
// Replay - click (replay hand).
//
// This is just so the human player can try a different approach and
// see how it might come out differently. We don't attempt to have
// the computer players reproduce what they did before. They will
// pass the same cards, but they may not play the same card as before
// even if everything is the same as it was up to that point.
//
// If they are in the middle of some hand (ie they have passed cards
// and/or played cards), then clicking replay will restart this hand.
//
// If they have not yet passed and/or played a card in the current
// hand, then clicking replay will restart the previous hand.
//
// Note that they can only go back one hand, not to the hand before
// that and so on back to the beginning of the game. That is, they
// cannot restart the current hand, and then, before passing or
// playing, try to replay the hand before that.
//
// Note too that the "previous" information is only useful for that
// very brief period when they are starting a new hand but have not
// yet passed (or played a card in the case of a hold). It's there
// because we don't know if they are going to ask for a repeat or
// not, of course. We have to assume they won't, so we have to save
// the score at the end of the hand just played, etc. and that wipes
// out the "current" info which was recording the scores etc. as they
// stood at the beginning of the previous hand (which was the current
// hand until they just started this new hand). But if they do repeat
// the now previous hand before getting this one officially under way
// we want to have that hand's info still around, which is what the
// "previous" data elements do.
//===================================================================
procedure TfrmMain.imgHeartsReplayHandClick(Sender: TObject);
var
gameswontext : shortstring;
i : smallint;
p : player;
s : suit;
situation : char;
begin
// Which situation is it?
situation := 'X';
if ((handnumber = 1) and (passmade = false)) then
situation := 'A'
else
if gameinprogress = false then
situation := 'B'
else
if (passmade = true) or (cardplayed = true) then
situation := 'C'
else
if (replayinprogress = False)
and ((passmade = false) or
((passdirection = hold) and (cardplayed = false))) then
situation := 'D';
// Process the situation.
case situation of
// Situation A
//
// It is the first hand, and they haven't passed yet.
// There is no previous hand to go to, and restarting this one
// would just bring them to the same point they are already at.
// So do nothing.
'A' : begin
bugit ('Situation A - ignore');
exit;
end;
// Situation B
//
// The game was over. They want to replay the last hand.
// Since we hadn't started a new hand (because the game was over),
// we restore from the current situation, not the previous, and we
// don't have to decrement the handnumber.
// Make sure the heart winner symbol(s) from previous game are invisible
// and decrement their number of games won.
'B' : begin
bugit ('Situation B - replay last hand of game');
for p in player do begin
handcards[p] := savecurrenthand[p];
gamepoints[p] := savecurrentgamepoints[p];
end;
restorescoresfromcurrent;
passdirection := savecurrentdirection;
if passdirection <> hold then begin
for p in player do begin
passcards[p] := savecurrentpass[p];
end;
end;
for p in winners do begin
gameswon[p] := gameswon[p] - 1;
case gameswon[p] of
0 : gameswontext := '';
else gameswontext :=inttostr (gameswon[p]);
end;
case p of
south : begin
frmMain.imgSouthHeart.Visible := False;
frmMain.labSouthGamesWon.Caption := gameswontext;
end;
west : begin
frmMain.imgWestHeart.Visible := False;
frmMain.labWestGamesWon.Caption := gameswontext;
end;
north : begin
frmMain.imgNorthHeart.Visible := False;
frmMain.labNorthGamesWon.Caption := gameswontext;
end;
east : begin
frmMain.imgEastHeart.Visible := False;
frmMain.labEastGamesWon.Caption := gameswontext;
end;
end;
end;
gameinprogress := true;
gamewon := false;
end;
// Situation C
//
// They have already passed (or played a card in the case of a hold).
// Can only restart this hand.
// No need to change the displayed score or handnumber.
'C' : begin
bugit ('Situation C - replay this hand');
for p in player do begin
handcards[p] := savecurrenthand[p];
end;
passdirection := savecurrentdirection;
if passdirection <> hold then begin
for p in player do begin
passcards[p] := savecurrentpass[p];
end;
end;
end;
// Situation D
//
// They haven't passed yet, or, in the case of a hold, played a card.
// They want to go back to the previous hand.
// But if they are already replaying this hand, they can't. We only
// save the cards of one previous hand, and this is it.
'D' : begin
bugit ('Situation D - replay previous hand');
for p in player do begin
handcards[p] := saveprevioushand[p];
savecurrenthand[p] := saveprevioushand[p];
gamepoints[p] := savepreviousgamepoints[p];
savecurrentgamepoints[p] := savepreviousgamepoints[p];
end;
restorescoresfromprevious;
resetcurrenttoprevious;
passdirection := savepreviousdirection;
savecurrentdirection := savepreviousdirection;