-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCardGames.Model.pas
1597 lines (1411 loc) · 49.6 KB
/
CardGames.Model.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
{******************************************************************************}
{ }
{ CardGames.Model: }
{ Model of Card Games }
{ }
{ Copyright (c) 2024 }
{ Author: Carlo Barazzetta }
{ Contributor: Lorenzo Barazzetta }
{ }
{ https://github.com/carloBarazzetta/CARD_Games_Delphi }
{ }
{******************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{******************************************************************************}
unit CardGames.Model;
interface
uses
System.Generics.Collections
, System.Generics.Defaults
, System.SysUtils
, CardGames.Types
;
type
//Forward declarations
TCardGameElement = class;
//Main Classes of Model
TCardGame = class;
TCardGameRules = class;
TCardGameCard = class;
TCardGamePlayer = class;
TCardGameTeam = class;
TCardGameTable = class;
TCardGameDeck = class;
//Group of Cards, for Move management
TCardGameCardsGroup = class;
TCardGameTableCards = class;
TCardGamePlayerCards = class;
TCardGameDeckCards = class;
// Classes of
TCardGameRulesClass = Class of TCardGameRules;
TCardGameClass = Class of TCardGame;
TCardGameCardClass = Class of TCardGameCard;
TCardGamePlayerClass = Class of TCardGamePlayer;
TCardGameTeamClass = Class of TCardGameTeam;
TCardGameTableClass = Class of TCardGameTable;
/// <summary>A List of Players of type TCardGamePlayer</summary>
TCardGamePlayers = TList<TCardGamePlayer>;
/// <summary>A List of Cards of type TCardGameCard</summary>
TCardGameCards = TList<TCardGameCard>;
/// <summary>A List of Teams of a TCardGame</summary>
TCardGameTeams = TList<TCardGameTeam>;
/// <summary>
/// A generic class of an element of the Model for a Card Game
/// with a Unique identifier
/// </summary>
TCardGameElement = class(TInterfacedObject)
FId: string;
protected
constructor Create;
public
property Id: string read FId write FId;
end;
/// <summary>
/// A base Class of the CardGame Rules that defines
/// the Game Title, the Deck Type and the number of Players
/// </summary>
TCardGameRules = class(TCardGameElement)
private
FGameTitle: TGameTitle;
FDescription: TGameDescription;
FInstructions: TGameInstructions;
FPlayersRotation: TPlayersRotationType;
FPlayersCountType: TPlayersCountType;
FPlayerTypes: TPlayerTypes;
FTeamsCount: Integer;
FDeckType: TCardDeckType;
procedure SetPlayersRotation(const ARotation: TPlayersRotationType);
procedure SetDeckType(const ADeckType: TCardDeckType);
procedure SetPlayersCountType(const AValue: TPlayersCountType);
procedure SetTeamsCount(const AValue: Integer);
function GetPlayersCount: Integer;
procedure SetPlayerTypes(const AValue: TPlayerTypes);
protected
/// <summary>
/// In descendant class you must implement this procedure
/// to initialize base game rules and other specific rules
/// </summary>
procedure InitRules(out AGameTitle: TGameTitle;
out ADeckType: TCardDeckType;
out APlayersCountType: TPlayersCountType); virtual; abstract;
/// <summary>
/// The Calculation of the Score of a Card in the Game Context
/// used to calculate the Score of Handled, or Collected Cards
/// </summary>
function ScoreForCard(const ACard: TCardGameCard): Single; virtual;
/// <summary>
/// The Score of a Card Played by a Player in the Game Context
/// used to calculate the Winner of the Hand
/// </summary>
function ScoreForCardPlayed(const APlayer: TCardGamePlayer;
const ACard: TCardGameCard): Single; virtual;
/// <summary>
/// The Comparison of two Cards of two Players to determinate
/// the Winner Player. Player1 has played before Player2.
/// Returns the WinnerPlayer
/// </summary>
function WinnerPlayerByCard(const APlayer1, APlayer2: TCardGamePlayer;
const ACard1, ACard2: TCardGameCard): TCardGamePlayer; virtual;
/// <summary>The Title of the Card Game</summary>
property GameTitle: TGameTitle read FGameTitle;
/// <summary>The Type of count of Players of the Game</summary>
property PlayersCountType: TPlayersCountType read FPlayersCountType write SetPlayersCountType;
/// <summary>The Types of Players available in the engine (human or IA)</summary>
property PlayersTypes: TPlayerTypes read FPlayerTypes write SetPlayerTypes;
/// <summary>The count of Teams of the Game: if Zero the Game is for individual opponents</summary>
property TeamsCount: Integer read FTeamsCount write SetTeamsCount;
/// <summary>The type of Rotation for a Game with Turns</summary>
property PlayersRotation: TPlayersRotationType read FPlayersRotation write SetPlayersRotation;
/// <summary>The type of the Deck for a Game</summary>
property DeckType: TCardDeckType read FDeckType write SetDeckType;
/// <summary>The Description of the Card Game</summary>
property Description: TGameDescription read FDescription write FDescription;
/// <summary>The Instructions of the Card Game</summary>
property Instructions: TGameInstructions read FInstructions write FInstructions;
public
/// <summary>
/// Base Constructor for a TCardGameRules
/// </summary>
constructor Create;
/// <summary>
/// function to check if an Engine can start a new Match
/// </summary>
function CanStartNewMatch(const AGame: TCardGame): Boolean;
/// <summary>
/// function to check if a Game is Finished
/// </summary>
function GameIsFinished(const AGame: TCardGame): Boolean;
/// <summary>
/// function to check if a Player can Play a Card based on Game Rules
/// </summary>
function CanPlayCard(const APlayer: TCardGamePlayer;
const APlayerCardGroup: TCardGamePlayerCards; const ACard: TCardGameCard): boolean; virtual;
/// <summary>
/// function to check if a Player can Pick a Card based on Game Rules
/// </summary>
function CanPickCard(const APlayer: TCardGamePlayer;
const ADeckCardGroup: TCardGameDeckCards; const ACard: TCardGameCard): boolean; virtual;
/// <summary>
/// function to check if a Card on the Table can be collected by a Player
/// </summary>
function CanCollectCard(const APlayer: TCardGamePlayer;
const ACardGroup: TCardGameTableCards; const ACard: TCardGameCard): boolean; virtual;
/// <summary>The Players count of the Game</summary>
property PlayersCount: Integer read GetPlayersCount;
end;
/// <summary>
/// A Card of a Deck of a Card Game
/// </summary>
TCardGameCard = class(TCardGameElement)
private
//Owner of the Card
FDeck: TCardGameDeck;
FSuit: TCardGameSuit;
FValue: TCardGameValue;
FState: TCardGameState;
FHandlerGroup: TCardGameCardsGroup;
procedure SetState(const AValue: TCardGameState);
function GetScore: Single;
/// <summary>
/// Constructor of the CardGame Card
/// Only a CardGameDeck can create a CardGame Card
/// </summary>
constructor Create(const AOwnerDeck: TCardGameDeck;
const ASuit: TCardGameSuit;
const AValue: TCardGameValue;
const AState: TCardGameState = csFaceDown);
/// <summary>
/// The only way to move a card from a group to another
/// </summary>
procedure MoveTo(const AFromGroup, ATargetGroup: TCardGameCardsGroup);
public
destructor Destroy; override;
procedure FlipCardOnTable;
property Deck: TCardGameDeck read FDeck;
property Suit: TCardGameSuit read FSuit;
property Value: TCardGameValue read FValue;
property State: TCardGameState read FState write SetState default csFaceDown;
property Score: Single read GetScore;
end;
/// <summary>
/// A generic Group of Cards
/// </summary>
TCardGameCardsGroup = class(TCardGameElement)
private
FOwner: TCardGameElement;
//Cards of the Group
FCards: TCardGameCards;
protected
procedure SortAscending;
procedure SortDescending;
/// <summary>
/// Constructor of the CardGame Card Group
/// Only a Class inside this unit can create a CardGame Group
/// </summary>
constructor Create(const AOwner: TCardGameElement);
public
procedure Shuffle;
destructor Destroy; override;
function RandomSelect: TCardGameCard;
property Cards: TCardGameCards read FCards;
end;
/// <summary>
/// The CardGameTableCards, a special Group of Cards owned by the TCardGameTable
/// inherits from TCardGameCardsGroup to manage Moving a Card from
/// a group to another, for example from a Player to the cards
/// visible on the Table
/// </summary>
TCardGameTableCards = class(TCardGameCardsGroup)
private
//Owner of the Group of Cards when is a CardTable
FOwnerTable: TCardGameTable;
function GetOwnerTable: TCardGameTable;
function GetCardGame: TCardGame;
function CalculateScore: Single;
protected
property OwnerTable: TCardGameTable read GetOwnerTable;
property CardGame: TCardGame read GetCardGame;
public
constructor Create(const AOwner: TCardGameTable);
property CardsScore: Single read CalculateScore;
end;
/// <summary>
/// The Table of a Card Game, containing the Cards on it
/// </summary>
TCardGameTable = class(TCardGameElement)
private
/// <summary>Reference to the Card Game</summary>
FCardGame: TCardGame;
/// <summary>
/// References to the Cards present on the Table
/// inherits from TCardGameTableCards to manage Moving a Card from
/// a group to another, for example from the Player to the Table
/// </summary>
FCardsOnTable: TCardGameTableCards;
FCardsDiscarded: TCardGameTableCards;
/// <summary>
/// Constructor of the CardGame Table
/// Only a CardGame can create a CardGame Table
/// </summary>
constructor Create(const AOwner: TCardGame);
protected
property CardGame: TCardGame read FCardGame;
public
destructor Destroy; override;
property CardsOnTable: TCardGameTableCards read FCardsOnTable;
property CardsDiscarded: TCardGameTableCards read FCardsDiscarded;
end;
/// <summary>
/// The CardGameDeckCards, a special Group of Cards owned by the TCardGameDeck
/// inherits from TCardGameCardsGroup to manage Moving a Card from
/// a group to another, for example Picking a Card from a Deck
/// to a Player
/// </summary>
TCardGameDeckCards = class(TCardGameCardsGroup)
private
//Owner of the Group of Cards when is a CardDeck
FOwnerDeck: TCardGameDeck;
function GetOwnerDeck: TCardGameDeck;
protected
property OwnerDeck: TCardGameDeck read GetOwnerDeck;
public
constructor Create(const AOwner: TCardGameDeck);
end;
/// <summary>
/// The Deck, a special Group of Cards owned by the a TCardGame
/// that contains Two series of CardGroup:
/// - FaceDownCards: the "facedown" cards that can be Pickable by a Player
/// - FaceUpCards: the "faceup" cards that can be Pickable by a Player
/// </summary>
TCardGameDeck = class(TCardGameElement)
private
FOwnerCardGame: TCardGame;
FDeckType: TCardDeckType;
FAllCards: TCardGameDeckCards;
FMainCards: TCardGameDeckCards;
FDiscardedCards: TCardGameDeckCards;
procedure GenerateDeck(const AType: TCardDeckType);
function GetCardGame: TCardGame;
procedure CreateNewCard(const ASuit: TCardGameSuit;
const AValue: TCardGameValue);
procedure ClearCards;
protected
property OwnerCardGame: TCardGame read GetCardGame;
public
constructor Create(const AOwner: TCardGame;
const AType: TCardDeckType);
destructor Destroy; override;
function IsEmpty: Boolean;
property MainCards: TCardGameDeckCards read FMainCards;
property DiscardedCards: TCardGameDeckCards read FDiscardedCards;
property DeckType: TCardDeckType read FDeckType;
end;
/// <summary>
/// The PlayerCards, a special Group of Cards owned by a CardPlayer
/// inherits from TCardGameCardsGroup to manage Moving a Card from
/// a group to another, for example from the Deck to the Handled
/// cards of a Player
/// </summary>
TCardGamePlayerCards = class(TCardGameCardsGroup)
private
//Owner of the Group of Cards when is a CardPlayer
FOwnerPlayer: TCardGamePlayer;
function GetOwnerPlayer: TCardGamePlayer;
function GetCardGame: TCardGame;
function CalculateScore: Single;
protected
property OwnerPlayer: TCardGamePlayer read GetOwnerPlayer;
property CardGame: TCardGame read GetCardGame;
public
constructor Create(const AOwner: TCardGamePlayer);
function ContainsCard(const ACard: TCardGameCard): boolean;
property CardsScore: Single read CalculateScore;
end;
/// <summary>
/// A Player present in a Table of a Card Game
/// </summary>
TCardGamePlayer = class(TCardGameElement)
private
//Owner of the Player
FCardGame: TCardGame;
FPlayerType: TPlayerType;
FState: TPlayerState;
FPlayerName: TCardPlayerName;
FHandledCards: TCardGamePlayerCards;
FPlayedCards: TCardGamePlayerCards;
FCollectedCards: TCardGamePlayerCards;
procedure SetPlayerType(const AValue: TPlayerType);
procedure SetCardPlayerName(const AValue: TCardPlayerName);
/// <summary>
/// Constructor of the CardGame Player
/// Only a CardGame can create a CardGame Player
/// </summary>
constructor Create(const ACardGame: TCardGame);
procedure SetPlayerState(const AValue: TPlayerState);
protected
property CardGame: TCardGame read FCardGame;
public
destructor Destroy; override;
/// <summary>State Info for Pickable Cards In Deck by the Player</summary>
function AllPickableCardsCount: Integer;
function IsPickableCard(const ADeckCardsGroup: TCardGameDeckCards;
const ACard: TCardGameCard): Boolean;
function PickableCardsCount(const ADeckCardsGroup: TCardGameDeckCards): Integer;
/// <summary>State Info for Collectible Cards by the Player</summary>
function CollectibleTableCardsCount: Integer;
function CollectibleCardsCount(const ATableCardGroup: TCardGameTableCards) : Integer;
function IsCollectibleCard(const ATableCardsGroup: TCardGameTableCards;
const ACard: TCardGameCard): Boolean;
/// <summary>State Info for Playable Cards by the Player</summary>
function AllPlayableCardsCount: Integer;
function IsPlayableCard(const APlayerCardsGroup: TCardGamePlayerCards;
const ACard: TCardGameCard): Boolean;
function PlayableCardsCount(const APlayerCardsGroup: TCardGamePlayerCards): Integer;
/// <summary>Pick a Card from Deck that moves cards</summary>
function PickCardFromDeck(const AMode: TPickCardMode = pcFromTop): TCardGameCard;
procedure PlayCard(const ACard: TCardGameCard;
const AState: TCardGameState = csFaceUp);
procedure CollectCardFromTable(const ACard: TCardGameCard;
const AState: TCardGameState = csFaceDown);
property PlayerType: TPlayerType read FPlayerType write SetPlayerType;
property State: TPlayerState read FState write SetPlayerState;
property Name: TCardPlayerName read FPlayerName write SetCardPlayerName;
property HandledCards: TCardGamePlayerCards read FHandledCards;
property CollectedCards: TCardGamePlayerCards read FCollectedCards;
property PlayedCards: TCardGamePlayerCards read FPlayedCards;
end;
/// <summary>
/// The Team is a List of Players inside a TCardGame
/// </summary>
TCardGameTeam = class(TCardGameElement)
private
FOwner: TCardGame;
FPlayers: TCardGamePlayers;
public
constructor Create(const AOwner: TCardGame);
destructor Destroy; override;
procedure AddPlayer(const APlayer: TCardGamePlayer);
procedure RemovePlayer(const APlayer: TCardGamePlayer);
property Players: TCardGamePlayers read FPlayers;
end;
/// <summary>
/// The base class of a Card Game with Table, Players, Teams and Cards
/// An instanca of a TCardGame contains the actual state of the Game
/// managed by the CardGameEngine
/// </summary>
TCardGame = class(TCardGameElement)
private
//A reference to GameRules
FGameRulesRef: TCardGameRules;
//Sub-Objects
FCardTable: TCardGameTable;
FPlayers: TCardGamePlayers;
FDeck: TCardGameDeck;
FTeams: TCardGameTeams;
FCurrentDealer: TCardGamePlayer;
FCurrentPlayer: TCardGamePlayer;
procedure SetCardTable(const AValue: TCardGameTable);
function DeckIsEmpty: Boolean;
/// <summary>Pick a Card from the Deck</summary>
function PickCardFromDeck(const AMode: TPickCardMode = pcFromTop): TCardGameCard;
/// <summary>Pick a number of Cards from the Deck</summary>
procedure PickCardsFromDeck(const ANumCards: Integer;
const ACards: TCardGameCardsGroup;
const AMode: TPickCardMode = pcFromTop;
const AState: TCardGameState = csFaceUp);
public
/// <summary>Generic method to Move a Card from a Group to Another</summary>
procedure MoveCard(const AFromGroup, AToGroup: TCardGameCardsGroup;
const ACard: TCardGameCard; const AState: TCardGameState = csFaceUp);
/// <summary>Generic method to Move a Group of Card from a Group to Another</summary>
procedure MoveCards(const AFromGroup, AToGroup: TCardGameCardsGroup;
const ACards: TCardGameCards; const AState: TCardGameState = csFaceUp);
/// <summary>Move a single Card from the Deck to a Player</summary>
function MoveCardFromDeckToPlayer(const APlayerCards: TCardGamePlayerCards;
const AMode: TPickCardMode = pcFromTop;
const AState: TCardGameState = csFaceUp): TCardGameCard;
/// <summary>Move a single Card from the Main Deck to the Table</summary>
function MoveCardFromMainDeckToTable(const ATableGroup: TCardGameCardsGroup;
const AMode: TPickCardMode = pcFromTop;
const AState: TCardGameState = csFaceUp): TCardGameCard;
/// <summary>Move a single Card from the Deck to the Table</summary>
function MoveCardFromDeckToTable(
const AMode: TPickCardMode = pcFromTop;
const AState: TCardGameState = csFaceUp): TCardGameCard;
/// <summary>Move a single Card from a Player to the Table</summary>
procedure MoveCardFromPlayerToTable(const APlayerCards: TCardGamePlayerCards;
const ATableGroup: TCardGameCardsGroup; const ACard: TCardGameCard;
const AState: TCardGameState = csFaceUp);
/// <summary>Move a single Card from a Player to the Table</summary>
procedure MoveCardFromTableToPlayer(const ATableGroup: TCardGameCardsGroup;
const APlayerCards: TCardGamePlayerCards;
const ACard: TCardGameCard);
/// <summary>Move a List of Cards from the Deck to a Player</summary>
procedure MoveCardsFromDeckToPlayer(const ANumCards: Integer;
const APlayerCards: TCardGamePlayerCards;
const AMode: TPickCardMode = pcFromTop);
/// <summary>Move a List of Cards from the Deck to the Table</summary>
procedure MoveCardsFromDeckToTable(const ANumCards: Integer;
const ATableGroup: TCardGameCardsGroup;
const AMode: TPickCardMode = pcFromTop;
const AState: TCardGameState = csFaceUp);
/// <summary>Move a List of Cards from a Player to the Table</summary>
procedure MoveCardsFromPlayerToTable(const APlayerCards: TCardGamePlayerCards;
const ATableGroup: TCardGameCardsGroup; const ACards: TCardGameCards;
const AState: TCardGameState = csFaceUp);
/// <summary>Move a List of Cards from the Table to a Player</summary>
procedure MoveCardsFromTableToPlayer(const ATableGroup: TCardGameCardsGroup;
const APlayerCards: TCardGamePlayerCards; const ACards: TCardGameCards);
/// <summary>
/// Deal a number of Cards present in the Deck to
/// the Players of the Game
/// </summary>
procedure DealCardsToPlayers(const ANumCards: Integer;
const AOneByOne: Boolean = True;
const AMode: TPickCardMode = pcFromTop);
/// <summary>Shuffle the Main Deck cards present in the Deck</summary>
function ShuffleMainDeck: TCardGameDeckCards; virtual;
/// <summary>The Title of the Game</summary>
function GetTitle: TGameTitle;
/// <summary>The short Description of the Game</summary>
function GetDescription: TGameDescription;
/// <summary>The Instructions of the Game</summary>
function GetInstructions: TGameInstructions;
/// <summary>The Calculation of the Score of a Card in the Game Context</summary>
function ScoreForCard(const ACard: TCardGameCard): Single; virtual;
/// <summary>Count Players in AState (default Active Players)</summary>
function GetPlayersCount(const AStates: TPlayerStates = [psActive]) : Integer;
/// <summary>Select a Player Randomly</summary>
function SelectRandomPlayer(
const AStates: TPlayerStates = [psActive]): Boolean;
/// <summary>Select a Player Randomly</summary>
function SelectRandomDealer(
const AStates: TPlayerStates = [psActive]): Boolean;
/// <summary>
/// Select the next Player based on PlayerRotation
/// that have the State required (by default Active Players)
/// </summary>
function SelectNextPlayerByRotation(var APlayer: TCardGamePlayer;
const AStates: TPlayerStates = [psActive]): Boolean;
/// <summary>
/// Select the Player at the Right of the Player
/// that have the State required (by default Active Players)
/// </summary>
function SelectPlayerAtRight(var APlayer: TCardGamePlayer;
const AStates: TPlayerStates = [psActive]): Boolean;
/// <summary>
/// Select the Player at the Left of the Player
/// that have the State required (by default Active Players)
/// </summary>
function SelectPlayerAtLeft(var APlayer: TCardGamePlayer;
const AStates: TPlayerStates = [psActive]): Boolean;
constructor Create(const AOwner: TCardGameElement;
const ACardGameRules: TCardGameRules);
destructor Destroy; override;
/// <summary>Adds a Player to a Game</summary>
function AddPlayer(const AName: TCardPlayerName;
const AType: TPlayerType = ptHuman): TCardGamePlayer;
/// <summary>Clear and Free the Players List</summary>
procedure ClearAndFreePlayers;
/// <summary>Clear and Free the Teams List</summary>
procedure ClearAndFreeTeams;
/// <summary>
/// Select the Dealer for a new Game.
/// The first is selected randomly
/// the next one based on PlayersRotation
/// </summary>
procedure SelectNextDealer(const AStartingFrom: TCardGamePlayer;
const AStates: TPlayerStates = [psActive]);
/// <summary>
/// Select next Player based on PlayersRotation.
/// The first is selected randomly
/// the next one based on PlayersRotation
/// </summary>
procedure SelectNextPlayer(const AStartFrom: TCardGamePlayer;
const AStates: TPlayerStates = [psActive]);
/// <summary>Functions to check if there are cards to play</summary>
function PlaybleCardsByPlayersCount: Integer; virtual;
/// <summary>Functions to check if there are cards to collect</summary>
function CollectibleCardsOnTableCount: Integer; virtual;
/// <summary>Functions to check if there are cards in Deck to Pickup</summary>
function PickableCardsInDeckCount: Integer; virtual;
/// <summary>Calculate the Winner Player of Hand
/// Comparing Score of Played Cards based on GameRules
/// </summary>
function CalcWinnerPlayerOfHand(
const AFirstPlayerOfHand: TCardGamePlayer): TCardGamePlayer;
/// <summary>Rules for the current Game</summary>
property GameRules: TCardGameRules read FGameRulesRef write FGameRulesRef;
property Title: TGameTitle read GetTitle;
property Description: TGameDescription read GetDescription;
property Instructions: TGameInstructions read GetInstructions;
property CardTable: TCardGameTable read FCardTable write SetCardTable;
property Players: TCardGamePlayers read FPlayers;
property Deck: TCardGameDeck read FDeck;
property Teams: TCardGameTeams read FTeams;
property CurrentDealer: TCardGamePlayer read FCurrentDealer;
property CurrentPlayer: TCardGamePlayer read FCurrentPlayer;
end;
implementation
uses
CardGames.Consts
, CardGames.Utils
, CardGames.JSONUtils
;
{ TCardGameElement }
constructor TCardGameElement.Create;
begin
inherited Create;
FId := TGuid.NewGuid.ToString;
end;
{ TCardPlayer }
constructor TCardGamePlayer.Create(const ACardGame: TCardGame);
begin
inherited Create;
FCardGame := ACardGame;
FHandledCards := TCardGamePlayerCards.Create(Self);
FPlayedCards := TCardGamePlayerCards.Create(Self);
FCollectedCards := TCardGamePlayerCards.Create(Self);
FState := psActive;
end;
destructor TCardGamePlayer.Destroy;
begin
FreeAndNil(FHandledCards);
FreeAndNil(FPlayedCards);
FreeAndNil(FCollectedCards);
inherited;
end;
function TCardGamePlayer.PickCardFromDeck(
const AMode: TPickCardMode = pcFromTop): TCardGameCard;
begin
Result := FCardGame.MoveCardFromDeckToPlayer(HandledCards, AMode);
end;
procedure TCardGamePlayer.CollectCardFromTable(const ACard: TCardGameCard;
const AState: TCardGameState);
begin
FCardGame.MoveCard(CollectedCards, FCardGame.CardTable.CardsOnTable, ACard);
ACard.State := AState;
end;
function TCardGamePlayer.CollectibleCardsCount(
const ATableCardGroup: TCardGameTableCards) : Integer;
var
LCard: TCardGameCard;
begin
Result := 0;
for LCard in ATableCardGroup.Cards do
if IsCollectibleCard(ATableCardGroup, LCard) then
Inc(Result);
end;
function TCardGamePlayer.PlayableCardsCount(
const APlayerCardsGroup: TCardGamePlayerCards): Integer;
var
LCard: TCardGameCard;
begin
Result := 0;
for LCard in APlayerCardsGroup.Cards do
if IsPlayableCard(APlayerCardsGroup, LCard) then
Inc(Result);
end;
function TCardGamePlayer.IsPlayableCard(
const APlayerCardsGroup: TCardGamePlayerCards;
const ACard: TCardGameCard): boolean;
begin
Result := CardGame.GameRules.CanPlayCard(Self, APlayerCardsGroup, ACard);
end;
function TCardGamePlayer.IsCollectibleCard(const ATableCardsGroup: TCardGameTableCards;
const ACard: TCardGameCard): Boolean;
begin
Result := CardGame.GameRules.CanCollectCard(Self, ATableCardsGroup, ACard);
end;
function TCardGamePlayer.AllPickableCardsCount: Integer;
begin
Result := PickableCardsCount(CardGame.Deck.MainCards) +
PickableCardsCount(CardGame.Deck.DiscardedCards);
end;
function TCardGamePlayer.AllPlayableCardsCount: Integer;
begin
Result := PlayableCardsCount(HandledCards) +
PlayableCardsCount(CollectedCards) +
PlayableCardsCount(PlayedCards);
end;
function TCardGamePlayer.IsPickableCard(
const ADeckCardsGroup: TCardGameDeckCards;
const ACard: TCardGameCard): Boolean;
begin
Result := CardGame.GameRules.CanPickCard(Self, ADeckCardsGroup, ACard);
end;
function TCardGamePlayer.PickableCardsCount(
const ADeckCardsGroup: TCardGameDeckCards): Integer;
var
LCard: TCardGameCard;
begin
Result := 0;
for LCard in ADeckCardsGroup.Cards do
if IsPickableCard(ADeckCardsGroup, LCard) then
Inc(Result);
end;
function TCardGamePlayer.CollectibleTableCardsCount: Integer;
begin
Result := CollectibleCardsCount(CardGame.CardTable.CardsOnTable);
end;
procedure TCardGamePlayer.PlayCard(const ACard: TCardGameCard;
const AState: TCardGameState = csFaceUp);
begin
FCardGame.MoveCard(HandledCards, PlayedCards, ACard);
ACard.State := AState;
end;
procedure TCardGamePlayer.SetCardPlayerName(const AValue: TCardPlayerName);
begin
FPlayerName := AValue;
end;
procedure TCardGamePlayer.SetPlayerState(const AValue: TPlayerState);
begin
FState := AValue;
end;
procedure TCardGamePlayer.SetPlayerType(const AValue: TPlayerType);
begin
FPlayerType := AValue;
end;
{ TCardGame }
constructor TCardGame.Create(const AOwner: TCardGameElement;
const ACardGameRules: TCardGameRules);
begin
inherited Create;
//a Reference to the Card Game Rules
FGameRulesRef := ACardGameRules;
//Creates CardTable
FCardTable := TCardGameTable.Create(Self);
//Creates Players List for the game
FPlayers := TCardGamePlayers.Create;
//Creates Teams List for the game
FTeams := TCardGameTeams.Create;
//Creates The Deck for the game
FDeck := TCardGameDeck.Create(Self, ACardGameRules.DeckType);
end;
destructor TCardGame.Destroy;
begin
FreeAndNil(FCardTable);
if Assigned(FTeams) then
ClearAndFreeTeams;
if Assigned(FPlayers) then
ClearAndFreePlayers;
FreeAndNil(FDeck);
inherited;
end;
function TCardGame.AddPlayer(const AName: TCardPlayerName;
const AType: TPlayerType = ptHuman): TCardGamePlayer;
begin
Result := TCardGamePlayer.Create(Self);
try
Result.PlayerType := AType;
Result.Name := AName;
FPlayers.Add(Result);
//Check for maximum "active" Player Count
if Players.Count > GameRules.PlayersCount then
Result.State := psSpectator
else
Result.State := psActive;
except
FreeAndNil(Result);
raise;
end;
end;
function TCardGame.CalcWinnerPlayerOfHand(
const AFirstPlayerOfHand: TCardGamePlayer): TCardGamePlayer;
var
LPlayer: TCardGamePlayer;
begin
Result := FCurrentPlayer;
for LPlayer in Players do
begin
Result := GameRules.WinnerPlayerByCard(Result, LPlayer,
Result.PlayedCards.Cards[0], LPlayer.PlayedCards.Cards[0]);
end;
end;
procedure TCardGame.ClearAndFreePlayers;
var
LPlayer: TCardGamePlayer;
begin
for LPlayer in FPlayers do
FreeAndNil(LPlayer);
FreeAndNil(FPlayers);
end;
procedure TCardGame.ClearAndFreeTeams;
var
LTeam: TCardGameTeam;
begin
for LTeam in FTeams do
FreeAndNil(LTeam);
FreeAndNil(FTeams);
end;
function TCardGame.CollectibleCardsOnTableCount: Integer;
var
LPlayer: TCardGamePlayer;
begin
Result := 0;
for LPlayer in Players do
Inc(Result, LPlayer.CollectibleTableCardsCount);
end;
procedure TCardGame.DealCardsToPlayers(const ANumCards: Integer;
const AOneByOne: Boolean; const AMode: TPickCardMode);
var
I: Integer;
LPlayer: TCardGamePlayer;
begin
if AOneByOne then
begin
for I := 1 to ANumCards do
for LPlayer in FPlayers do
MoveCardFromDeckToPlayer(LPlayer.HandledCards, AMode);
end
else
begin
for LPlayer in FPlayers do
for I := 1 to ANumCards do
MoveCardFromDeckToPlayer(LPlayer.HandledCards, AMode);
end;
end;
function TCardGame.DeckIsEmpty: Boolean;
begin
Assert(Assigned(FDeck), DECK_NOT_ASSIGNED);
Result := FDeck.MainCards.Cards.Count = 0;
end;
function TCardGame.GetTitle: TGameTitle;
begin
Assert(Assigned(FGameRulesRef), GAME_RULES_NOT_ASSIGNED);
Result := FGameRulesRef.GameTitle;
end;
function TCardGame.GetDescription: TGameDescription;
begin
Assert(Assigned(FGameRulesRef), DECK_NOT_ASSIGNED);
Result := FGameRulesRef.Description;
end;
function TCardGame.GetInstructions: TGameInstructions;
begin
Assert(Assigned(FGameRulesRef), DECK_NOT_ASSIGNED);
Result := FGameRulesRef.Instructions;
end;
function TCardGame.GetPlayersCount(const AStates: TPlayerStates = [psActive]): Integer;
var
LPlayer: TCardGamePlayer;
begin
Result := 0;
for LPlayer in FPlayers do
if LPlayer.State in AStates then
Inc(Result);
end;
procedure TCardGame.MoveCard(const AFromGroup, AToGroup: TCardGameCardsGroup;
const ACard: TCardGameCard; const AState: TCardGameState = csFaceUp);
begin
Assert(Assigned(ACard), CARD_NOT_ASSIGNED);
ACard.MoveTo(AFromGroup, AToGroup);
end;
function TCardGame.MoveCardFromDeckToPlayer(
const APlayerCards: TCardGamePlayerCards;
const AMode: TPickCardMode = pcFromTop;
const AState: TCardGameState = csFaceUp): TCardGameCard;
begin
Result := PickCardFromDeck(AMode);
MoveCard(FDeck.MainCards, APlayerCards, Result);
end;
function TCardGame.MoveCardFromMainDeckToTable(
const ATableGroup: TCardGameCardsGroup;
const AMode: TPickCardMode = pcFromTop;
const AState: TCardGameState = csFaceUp): TCardGameCard;
begin
Result := PickCardFromDeck(AMode);
MoveCard(FDeck.MainCards, ATableGroup, Result, AState);
end;
function TCardGame.MoveCardFromDeckToTable(
const AMode: TPickCardMode = pcFromTop;
const AState: TCardGameState = csFaceUp): TCardGameCard;
begin
Result := PickCardFromDeck(AMode);
MoveCard(FDeck.MainCards, FCardTable.FCardsOnTable, Result, AState);
end;
procedure TCardGame.MoveCardFromPlayerToTable(
const APlayerCards: TCardGamePlayerCards;
const ATableGroup: TCardGameCardsGroup;
const ACard: TCardGameCard;
const AState: TCardGameState = csFaceUp);
begin
MoveCard(APlayerCards, ATableGroup, ACard, AState);
end;
procedure TCardGame.MoveCardFromTableToPlayer(
const ATableGroup: TCardGameCardsGroup;
const APlayerCards: TCardGamePlayerCards;
const ACard: TCardGameCard);
begin
MoveCard(ATableGroup, APlayerCards, ACard);
end;
procedure TCardGame.MoveCards(const AFromGroup, AToGroup: TCardGameCardsGroup;
const ACards: TCardGameCards;
const AState: TCardGameState = csFaceUp);
var
LCard: TCardGameCard;
begin
for LCard in ACards do
MoveCard(AFromGroup, AToGroup, LCard, AState);
end;
procedure TCardGame.MoveCardsFromDeckToPlayer(const ANumCards: Integer;
const APlayerCards: TCardGamePlayerCards;
const AMode: TPickCardMode = pcFromTop);
begin
PickCardsFromDeck(ANumCards, APlayerCards, AMode);
end;
procedure TCardGame.MoveCardsFromDeckToTable(const ANumCards: Integer;
const ATableGroup: TCardGameCardsGroup;
const AMode: TPickCardMode = pcFromTop;
const AState: TCardGameState = csFaceUp);
begin
PickCardsFromDeck(ANumCards, ATableGroup, AMode, AState);
end;
procedure TCardGame.MoveCardsFromPlayerToTable(
const APlayerCards: TCardGamePlayerCards;
const ATableGroup: TCardGameCardsGroup;
const ACards: TCardGameCards;
const AState: TCardGameState = csFaceUp);
var
LCard: TCardGameCard;
begin
for LCard in ACards do
MoveCard(APlayerCards, ATableGroup, LCard);
end;
procedure TCardGame.MoveCardsFromTableToPlayer(
const ATableGroup: TCardGameCardsGroup;
const APlayerCards: TCardGamePlayerCards;
const ACards: TCardGameCards);
var
LCard: TCardGameCard;