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
| unit Unit2;
interface
uses System.Classes, System.Generics.Collections;
type
TCarteColor = (ccCoeur, ccCarreaux, ccTrefle, ccPiques);
TCarteType = (ctAutre, ctNumber, ctFigure, ctAtout);
TCarteFigure = (cfValet, cfCavalier, cfDame, cfRoi);
TCarte = class(TObject)
strict private
FName: string;
FCarteType: TCarteType;
public
constructor Create(const AName: string; const ACarteType: TCarteType);
function Clone(): TCarte; virtual;
property Name: string read FName;
property CarteType: TCarteType read FCarteType;
end;
TCarteWithCouleur = class(TCarte)
public
const
COLORS: array[TCarteColor] of string = ('Coeur', 'Carreaux', 'Trefle', 'Piques');
strict private
FNumber: string;
FCarteColor: TCarteColor;
public
constructor Create(const ANumber: string; const ACarteColor: TCarteColor; const ACarteType: TCarteType = ctNumber);
function Clone(): TCarte; override;
property Number: string read FNumber;
property CarteColor: TCarteColor read FCarteColor;
end;
TCarteWithFigure = class(TCarteWithCouleur)
public
const
FIGURES: array[TCarteFigure] of string = ('Valet', 'Cavalier', 'Dame', 'Roi');
VALET = 11;
CAVALIER = 12;
DAME = 13;
ROI = 14;
strict private
FCarteFigure: TCarteFigure;
public
constructor Create(const ACarteFigure: TCarteFigure; const ACarteColor: TCarteColor);
function Clone(): TCarte; override;
property CarteFigure: TCarteFigure read FCarteFigure;
end;
TCartes = class(System.Generics.Collections.TObjectList<TCarte>)
private
function GetCarte(Index: Integer): TCarte;
public
constructor Create(AOwnsCartes: Boolean = True);
function Clone(): TCartes;
procedure ToStrings(AStrings: TStrings; DoClear: Boolean = True);
procedure ToFlatStrings(AStrings: TStrings; DoClear: Boolean = True);
property Cartes[Index: Integer]: TCarte read GetCarte; default;
end;
TDecks = class(System.Generics.Collections.TObjectList<TCartes>)
public
constructor Create(AOwnsCartes: Boolean = True);
procedure ToStrings(AStrings: TStrings; DoClear: Boolean = True);
procedure ToFlatStrings(AStrings: TStrings; DoClear: Boolean = True);
end;
TTarot = class(TObject)
public
const
NUMBERS: array[1..10] of string = ('As', 'Deux', 'Trois', 'Quatre', 'Cinq', 'Six', 'Sept', 'Huit', 'Neuf', 'Dix');
strict private
FAvailableCartes: TCartes;
FAvailableAtouts: TCartes;
FAvailableCouleurs: array[TCarteColor] of TCartes;
procedure FillAvailableCartes();
private
function GetAvailableCouleurs(Index: TCarteColor): TCartes;
public
constructor Create();
destructor Destroy(); override;
property AvailableCartes: TCartes read FAvailableCartes;
property AvailableAtouts: TCartes read FAvailableAtouts;
property AvailableCouleurs[Index: TCarteColor]: TCartes read GetAvailableCouleurs;
end;
implementation
uses System.SysUtils;
{ TCarte }
constructor TCarte.Create(const AName: string; const ACarteType: TCarteType);
begin
inherited Create();
FName := AName;
FCarteType := ACarteType;
end;
function TCarte.Clone(): TCarte;
begin
Result := TCarte.Create(FName, FCarteType);
end;
{ TCarteWithCouleur }
constructor TCarteWithCouleur.Create(const ANumber: string; const ACarteColor: TCarteColor; const ACarteType: TCarteType = ctNumber);
begin
inherited Create(Format('%s de %s', [ANumber, COLORS[ACarteColor]]), ACarteType);
FNumber := ANumber;
FCarteColor := ACarteColor;
end;
function TCarteWithCouleur.Clone(): TCarte;
begin
Result := TCarteWithCouleur.Create(FNumber, FCarteColor);
end;
{ TCarteWithFigure }
constructor TCarteWithFigure.Create(const ACarteFigure: TCarteFigure; const ACarteColor: TCarteColor);
begin
inherited Create(FIGURES[ACarteFigure], ACarteColor, ctFigure);
FCarteFigure := ACarteFigure;
end;
function TCarteWithFigure.Clone(): TCarte;
begin
Result := TCarteWithFigure.Create(FCarteFigure, CarteColor);
end;
{ TCartes }
constructor TCartes.Create(AOwnsCartes: Boolean = True);
begin
inherited Create(AOwnsCartes);
end;
function TCartes.GetCarte(Index: Integer): TCarte;
begin
Result := Items[Index - 1];
end;
procedure TCartes.ToStrings(AStrings: TStrings; DoClear: Boolean = True);
var
C: TCarte;
begin
AStrings.BeginUpdate();
try
if DoClear then
AStrings.Clear();
for C in Self do
AStrings.Add(C.Name);
finally
AStrings.EndUpdate();
end;
end;
procedure TCartes.ToFlatStrings(AStrings: TStrings; DoClear: Boolean = True);
var
C: TCarte;
I: Integer;
S: string;
begin
AStrings.BeginUpdate();
try
if DoClear then
AStrings.Clear();
if Count > 1 then
begin
S := Items[0].Name;
for I := 1 to Count - 1 do
S := S + ' + ' + Items[I].Name;
end;
AStrings.Add(S);
finally
AStrings.EndUpdate();
end;
end;
function TCartes.Clone(): TCartes;
var
C: TCarte;
begin
Result := TCartes.Create(True);
for C in Self do
Result.Add(C.Clone());
end;
{ TDecks }
constructor TDecks.Create(AOwnsCartes: Boolean = True);
begin
inherited Create(AOwnsCartes);
end;
procedure TDecks.ToStrings(AStrings: TStrings; DoClear: Boolean = True);
var
C: TCartes;
begin
AStrings.BeginUpdate();
try
if DoClear then
AStrings.Clear();
for C in Self do
begin
C.ToStrings(AStrings, False);
AStrings.Add('');
end;
finally
AStrings.EndUpdate();
end;
end;
procedure TDecks.ToFlatStrings(AStrings: TStrings; DoClear: Boolean = True);
var
C: TCartes;
begin
AStrings.BeginUpdate();
try
if DoClear then
AStrings.Clear();
for C in Self do
C.ToFlatStrings(AStrings, False);
finally
AStrings.EndUpdate();
end;
end;
{ TTarot }
constructor TTarot.Create();
var
C: TCarteColor;
begin
inherited Create();
FAvailableCartes := TCartes.Create(True);
FAvailableAtouts := TCartes.Create(False);
for C := Low(C) to High(C) do
FAvailableCouleurs[C] := TCartes.Create(False);
FillAvailableCartes();
end;
destructor TTarot.Destroy();
var
C: TCarteColor;
begin
for C := Low(C) to High(C) do
FreeAndNil(FAvailableCouleurs[C]);
FreeAndNil(FAvailableAtouts);
FreeAndNil(FAvailableCartes);
inherited Destroy();
end;
procedure TTarot.FillAvailableCartes();
var
I: Integer;
C: TCarteColor;
F: TCarteFigure;
Carte: TCarte;
begin
for C := Low(C) to High(C) do
begin
for I := 1 to 10 do
FAvailableCartes.Add(TCarteWithCouleur.Create(NUMBERS[I], C));
for F := Low(F) to High(F) do
FAvailableCartes.Add(TCarteWithFigure.Create(F, C));
end;
for I := 1 to 21 do
FAvailableCartes.Add(TCarte.Create(Format('%d d''Atout', [I]), ctAtout));
FAvailableCartes.Add(TCarte.Create('L''excuse', ctAutre));
for Carte in FAvailableCartes do
begin
if Carte is TCarteWithCouleur then
FAvailableCouleurs[TCarteWithCouleur(Carte).CarteColor].Add(Carte)
else if Carte.CarteType = ctAtout then
FAvailableAtouts.Add(Carte);
end;
end;
function TTarot.GetAvailableCouleurs(Index: TCarteColor): TCartes;
begin
Result := FAvailableCouleurs[Index];
end;
end. |
Partager