trackler 2.0.8.4 → 2.0.8.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -151,7 +151,16 @@
151
151
  {
152
152
  "slug": "book-store",
153
153
  "difficulty": 6
154
- }
154
+ },
155
+ {
156
+ "slug": "poker",
157
+ "difficulty": 7,
158
+ "topics": [
159
+ "Parsing",
160
+ "Sorting",
161
+ "Games"
162
+ ]
163
+ }
155
164
  ],
156
165
  "ignored": [
157
166
  "bin",
@@ -2,13 +2,13 @@ unit uHelloWorld;
2
2
 
3
3
  interface
4
4
 
5
- function Hello(name: string='World'): string;
5
+ function Hello: string;
6
6
 
7
7
  implementation
8
8
 
9
- function Hello(name: string='World'): string;
9
+ function Hello: string;
10
10
  begin
11
- result := 'Hello, ' + name + '!';
11
+ result := 'Hello, World!';
12
12
  end;
13
13
 
14
14
  end.
@@ -36,33 +36,17 @@ type
36
36
  HelloWorldTest = class(TObject)
37
37
  public
38
38
  [Test]
39
- procedure test_no_name;
40
- [Test]
41
- [Ignore('Comment this line to run this test')]
42
- procedure test_sample_name;
43
- [Test]
44
- [Ignore('Comment this line to run this test')]
45
- procedure test_other_sample_name;
39
+ procedure Say_hi;
46
40
  end;
47
41
 
48
42
  implementation
49
43
  uses uHelloWorld;
50
44
 
51
- procedure HelloWorldTest.test_no_name;
45
+ procedure HelloWorldTest.Say_hi;
52
46
  begin
53
47
  assert.AreEqual('Hello, World!', Hello);
54
48
  end;
55
49
 
56
- procedure HelloWorldTest.test_sample_name;
57
- begin
58
- assert.AreEqual('Hello, Alice!',Hello('Alice'));
59
- end;
60
-
61
- procedure HelloWorldTest.test_other_sample_name;
62
- begin
63
- assert.AreEqual('Hello, Bob!', Hello('Bob'));
64
- end;
65
-
66
50
  initialization
67
51
  TDUnitX.RegisterTestFixture(HelloWorldTest);
68
52
  end.
@@ -0,0 +1,60 @@
1
+ program Poker;
2
+
3
+ {$IFNDEF TESTINSIGHT}
4
+ {$APPTYPE CONSOLE}
5
+ {$ENDIF}{$STRONGLINKTYPES ON}
6
+ uses
7
+ System.SysUtils,
8
+ {$IFDEF TESTINSIGHT}
9
+ TestInsight.DUnitX,
10
+ {$ENDIF }
11
+ DUnitX.Loggers.Console,
12
+ DUnitX.Loggers.Xml.NUnit,
13
+ DUnitX.TestFramework,
14
+ uPokerTest in 'uPokerTest.pas',
15
+ uPoker in 'uPoker.pas';
16
+
17
+ var
18
+ runner : ITestRunner;
19
+ results : IRunResults;
20
+ logger : ITestLogger;
21
+ nunitLogger : ITestLogger;
22
+ begin
23
+ {$IFDEF TESTINSIGHT}
24
+ TestInsight.DUnitX.RunRegisteredTests;
25
+ exit;
26
+ {$ENDIF}
27
+ try
28
+ //Check command line options, will exit if invalid
29
+ TDUnitX.CheckCommandLine;
30
+ //Create the test runner
31
+ runner := TDUnitX.CreateRunner;
32
+ //Tell the runner to use RTTI to find Fixtures
33
+ runner.UseRTTI := True;
34
+ //tell the runner how we will log things
35
+ //Log to the console window
36
+ logger := TDUnitXConsoleLogger.Create(true);
37
+ runner.AddLogger(logger);
38
+ //Generate an NUnit compatible XML File
39
+ nunitLogger := TDUnitXXMLNUnitFileLogger.Create(TDUnitX.Options.XMLOutputFile);
40
+ runner.AddLogger(nunitLogger);
41
+ runner.FailsOnNoAsserts := False; //When true, Assertions must be made during tests;
42
+
43
+ //Run tests
44
+ results := runner.Execute;
45
+ if not results.AllPassed then
46
+ System.ExitCode := EXIT_ERRORS;
47
+
48
+ {$IFNDEF CI}
49
+ //We don't want this happening when running under CI.
50
+ if TDUnitX.Options.ExitBehavior = TDUnitXExitBehavior.Pause then
51
+ begin
52
+ System.Write('Done.. press <Enter> key to quit.');
53
+ System.Readln;
54
+ end;
55
+ {$ENDIF}
56
+ except
57
+ on E: Exception do
58
+ System.Writeln(E.ClassName, ': ', E.Message);
59
+ end;
60
+ end.
@@ -0,0 +1,253 @@
1
+ unit uPoker;
2
+
3
+ interface
4
+ uses Generics.Collections;
5
+
6
+ type
7
+
8
+ Poker = class
9
+ private
10
+ type
11
+ Card = record
12
+ Rank,
13
+ Suit: integer;
14
+ end;
15
+ Hand = record
16
+ Input: string;
17
+ Score: integer;
18
+ end;
19
+ class function ParseHand(aHand: string): Hand; static;
20
+ class function ParseCards(aHand: string): TArray<Card>; static;
21
+ class function ParseCard(aCard: string): Card; static;
22
+ class function ParseRank(aCard: string): integer; static;
23
+ class function ParseSuit(aCard: string): integer; static;
24
+ class function ScoreHand(aCards: TArray<Card>): integer; static;
25
+ public
26
+ class function BestHands(aHands: TList<String>): TList<string>; static;
27
+ end;
28
+
29
+ implementation
30
+ uses SysUtils, Math, Generics.Defaults, Spring.Collections;
31
+
32
+ class function Poker.BestHands(aHands: TList<String>): TList<string>;
33
+
34
+ {$region 'BestHands workers'}
35
+ function MaxScore(aList: IList<Hand>): integer;
36
+ var lResult: integer;
37
+ aItem: Hand;
38
+ begin
39
+ lResult := 0;
40
+ for aItem in aList do
41
+ if aItem.Score > lResult then
42
+ lResult := aItem.score;
43
+ result := lResult;
44
+ end;
45
+
46
+ function ScoreHands(aList: TList<string>): IList<Hand>;
47
+ var aItem: string;
48
+ begin
49
+ Result := TCollections.CreateList<Hand>;
50
+ for aItem in aList do
51
+ result.Add(ParseHand(aItem));
52
+ end;
53
+
54
+ function ListOfWinningHands(const aTarget: integer; aList: IList<Hand>): IList<string>;
55
+ var aItem: Hand;
56
+ begin
57
+ result := TCollections.CreateList<string>;
58
+ for aItem in aList do
59
+ if aItem.Score = aTarget then
60
+ result.Add(aItem.Input);
61
+ end;
62
+ {$endregion}
63
+
64
+ var scoredHands: IList<Hand>;
65
+ lmaxScore: integer;
66
+ begin
67
+ scoredHands := ScoreHands(aHands);
68
+ lMaxScore := MaxScore(scoredHands);
69
+
70
+ result := TList<string>.Create;
71
+ result.AddRange(ListOfWinningHands(lMaxScore, scoredHands).ToArray);
72
+ end;
73
+
74
+ class function Poker.ParseHand(aHand: string): Hand;
75
+ begin
76
+ result.Input := aHand;
77
+ result.Score := ScoreHand(ParseCards(aHand));
78
+ end;
79
+
80
+ class function Poker.ParseCards(aHand: string): TArray<Card>;
81
+ var lhand: TArray<string>;
82
+ lCardStr: string;
83
+ lParsedHand: IList<Card>;
84
+ begin
85
+ lParsedHand := TCollections.CreateList<Card>(
86
+ function(const left, right: Card): integer
87
+ begin
88
+ result := TComparer<integer>.Default.Compare(left.Rank, right.Rank);
89
+ end);
90
+
91
+ lhand := aHand.Split([' ']);
92
+ for lCardStr in lhand do
93
+ lParsedHand.Add(ParseCard(lCardStr));
94
+ lParsedHand.Sort;
95
+ lParsedhand.Reverse;
96
+ result := lParsedHand.ToArray;
97
+ end;
98
+
99
+ class function Poker.ParseCard(aCard: string): Card;
100
+ begin
101
+ result.Rank := ParseRank(aCard);
102
+ result.Suit := ParseSuit(aCard);
103
+ end;
104
+
105
+ class function Poker.ParseRank(aCard: string): integer;
106
+ var lRanks: IList<char>;
107
+ lCard: TArray<char>;
108
+ begin
109
+ lCard := aCard.ToCharArray;
110
+ lRanks := TCollections.CreateList<char>;
111
+ lRanks.AddRange(['.','.','2','3','4','5','6','7','8','9','T','J','K','Q','A']);
112
+ result := lRanks.IndexOf(lCard[low(lCard)]);
113
+ end;
114
+
115
+ class function Poker.ParseSuit(aCard: string): integer;
116
+ var lSuits: IList<char>;
117
+ lCard: TArray<char>;
118
+ begin
119
+ lCard := aCard.ToCharArray;
120
+ lSuits := TCollections.CreateList<char>;
121
+ lSuits.AddRange(['.','H','S','D','C']);
122
+ result := lSuits.IndexOf(lCard[high(lCard)]);
123
+ end;
124
+
125
+ class function Poker.ScoreHand(aCards: TArray<Card>): integer;
126
+
127
+ {$region 'ScoreHand Workers'}
128
+ function CardsByRankOccurance: IList<integer>;
129
+ var rank: integer;
130
+ lRankOccurence: TArray<integer>;
131
+ aCard: Card;
132
+ currentOccurenceCount: integer;
133
+ begin
134
+ SetLength(lRankOccurence, 15);
135
+ for aCard in aCards do
136
+ inc(lRankOccurence[aCard.Rank]);
137
+ currentOccurenceCount := 0;
138
+ result := TCollections.CreateList<integer>;
139
+ result.Add(0);
140
+ for rank := High(lRankOccurence) downto Low(lRankOccurence) + 2 do
141
+ begin
142
+ if lRankOccurence[rank] >= currentOccurenceCount then
143
+ begin
144
+ result.Insert(0,rank);
145
+ currentOccurenceCount := lRankOccurence[rank];
146
+ end;
147
+ end;
148
+ end;
149
+
150
+ function CountRanks: IList<integer>;
151
+ var rankCount: integer;
152
+ i: integer;
153
+ begin
154
+ result := TCollections.CreateList<integer>;
155
+ rankCount := 1;
156
+ for i := low(aCards) to high(aCards) - 1 do
157
+ begin
158
+ if aCards[i].Rank = aCards[i+1].Rank then
159
+ inc(rankCount)
160
+ else
161
+ begin
162
+ result.Add(rankCount);
163
+ rankCount := 1;
164
+ end;
165
+ end;
166
+ result.Add(rankCount);
167
+ result.Sort;
168
+ result.Reverse;
169
+ end;
170
+
171
+ procedure SelectRankAndSuits(out Suits, Ranks: IList<integer>);
172
+ var aCard: Card;
173
+ begin
174
+ Suits := TCollections.CreateList<integer>;
175
+ Ranks := TCollections.CreateList<integer>;
176
+
177
+ for aCard in aCards do
178
+ begin
179
+ Suits.Add(aCard.Suit);
180
+ Ranks.Add(aCard.Rank);
181
+ end;
182
+ Suits.Sort;
183
+ Suits.Reverse;
184
+ Ranks.Sort;
185
+ Ranks.Reverse;
186
+ if Ranks.EqualsTo([14,5,4,3,2]) then
187
+ begin //Ace is worth one when hand is straight to five
188
+ Ranks.Remove(14);
189
+ Ranks.Add(1);
190
+ end;
191
+ end;
192
+
193
+ function Distinct(aList: IList<integer>): integer;
194
+ var prevItem: integer;
195
+ ItemCount: integer;
196
+ item: integer;
197
+ begin
198
+ ItemCount := 0;
199
+ prevItem := 0;
200
+ for item in aList do
201
+ if item <> prevItem then
202
+ begin
203
+ inc(ItemCount);
204
+ prevItem := item;
205
+ end;
206
+ result := ItemCount;
207
+ end;
208
+ {$endregion}
209
+
210
+ var listOfCardsByRank: IList<integer>;
211
+ listOfRankCounts: IList<integer>;
212
+ Ranks: IList<integer>;
213
+ Suits: IList<integer>;
214
+ flush: boolean;
215
+ straight: boolean;
216
+ begin
217
+ listOfCardsByRank := CardsByRankOccurance;
218
+ listOfRankCounts := CountRanks;
219
+
220
+ SelectRankAndSuits(Suits, Ranks);
221
+
222
+ flush := Distinct(Suits) = 1;
223
+ straight := (Distinct(Ranks) = 5) and ((Ranks[0] - Ranks[4]) = 4);
224
+
225
+ if straight and flush then
226
+ result := 800 + Ranks.First
227
+ else
228
+ if listOfRankCounts.EqualsTo([4,1]) then // 4 of a kind
229
+ result := 700 + listOfCardsByRank[0]
230
+ else
231
+ if listOfRankCounts.EqualsTo([3,2]) then // full house
232
+ result := 600 + listOfCardsByRank[0]
233
+ else
234
+ if flush then
235
+ result := 500 + Ranks.First
236
+ else
237
+ if straight then
238
+ result := 400 + Ranks.First
239
+ else
240
+ if listOfRankCounts.EqualsTo([3,1,1]) then //3 of a kind
241
+ result := 300 + listOfCardsByRank[0]
242
+ else
243
+ if listOfRankCounts.EqualsTo([2,2,1]) then // 2 pair
244
+ result := 200 + Math.Max(listOfCardsByRank[0], listOfCardsByRank[1])
245
+ else
246
+ if listOfRankCounts.EqualsTo([2,1,1,1]) then // 1 pair
247
+ result := 100 + listOfCardsByRank[0]
248
+ else
249
+ result := Ranks.Max; // high card
250
+
251
+ end;
252
+
253
+ end.
@@ -0,0 +1,582 @@
1
+ unit uPokerTest;
2
+
3
+ interface
4
+ uses
5
+ DUnitX.TestFramework;
6
+
7
+ type
8
+
9
+ [TestFixture]
10
+ PokerTest = class(TObject)
11
+ public
12
+ [Test]
13
+ procedure One_hand;
14
+
15
+ [Test]
16
+ [Ignore]
17
+ procedure Nothing_vs_one_pair;
18
+
19
+ [Test]
20
+ [Ignore]
21
+ procedure Two_pairs;
22
+
23
+ [Test]
24
+ [Ignore]
25
+ procedure One_pair_vs_double_pair;
26
+
27
+ [Test]
28
+ [Ignore]
29
+ procedure Two_double_pairs;
30
+
31
+ [Test]
32
+ [Ignore]
33
+ procedure Double_pair_vs_three;
34
+
35
+ [Test]
36
+ [Ignore]
37
+ procedure Two_threes;
38
+
39
+ [Test]
40
+ [Ignore]
41
+ procedure Three_vs_straight;
42
+
43
+ [Test]
44
+ [Ignore]
45
+ procedure Two_straights;
46
+
47
+ [Test]
48
+ [Ignore]
49
+ procedure Straight_vs_flush;
50
+
51
+ [Test]
52
+ [Ignore]
53
+ procedure Two_flushes;
54
+
55
+ [Test]
56
+ [Ignore]
57
+ procedure Flush_vs_full;
58
+
59
+ [Test]
60
+ [Ignore]
61
+ procedure Two_fulls;
62
+
63
+ [Test]
64
+ [Ignore]
65
+ procedure Full_vs_square;
66
+
67
+ [Test]
68
+ [Ignore]
69
+ procedure Two_squares;
70
+
71
+ [Test]
72
+ [Ignore]
73
+ procedure Square_vs_straight_flush;
74
+
75
+ [Test]
76
+ [Ignore]
77
+ procedure Two_straight_flushes;
78
+
79
+ [Test]
80
+ [Ignore]
81
+ procedure Three_hand_with_tie;
82
+
83
+ [Test]
84
+ [Ignore]
85
+ procedure Straight_to_5_against_a_pair_of_jacks;
86
+ end;
87
+
88
+ implementation
89
+ uses Generics.Collections, uPoker;
90
+
91
+ procedure PokerTest.One_hand;
92
+ const hand: string = '5S 4S 7H 8D JC';
93
+ var expectedHands,
94
+ inputHands: TList<string>;
95
+ ActualHands: TList<string>;
96
+ expectedHandsArray,
97
+ ActualHandsArray: TArray<string>;
98
+ i: integer;
99
+ begin
100
+ inputHands := TList<string>.Create;
101
+ inputHands.AddRange(hand);
102
+ expectedHands := TList<string>.Create;
103
+ expectedHands.AddRange(hand);
104
+
105
+ ActualHands := Poker.BestHands(inputHands);
106
+
107
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
108
+ expectedHandsArray := expectedHands.ToArray;
109
+ ActualHandsArray := ActualHands.ToArray;
110
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
111
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
112
+ end;
113
+
114
+ procedure PokerTest.Nothing_vs_one_pair;
115
+ const nothing: string = '4S 5H 6S 8D JH';
116
+ pairOf4: string = '2S 4H 6S 4D JH';
117
+ var expectedHands,
118
+ inputHands: TList<string>;
119
+ ActualHands: TList<string>;
120
+ expectedHandsArray,
121
+ ActualHandsArray: TArray<string>;
122
+ i: integer;
123
+ begin
124
+ inputHands := TList<string>.Create;
125
+ inputHands.AddRange([nothing, pairOf4]);
126
+ expectedHands := TList<string>.Create;
127
+ expectedHands.AddRange(pairOf4);
128
+
129
+ ActualHands := Poker.BestHands(inputHands);
130
+
131
+ expectedHandsArray := expectedHands.ToArray;
132
+ ActualHandsArray := ActualHands.ToArray;
133
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
134
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
135
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
136
+ end;
137
+
138
+ procedure PokerTest.Two_pairs;
139
+ const pairOf2: string = '4S 2H 6D 2D JD';
140
+ pairOf4: string = '2S 4H 6S 4D JH';
141
+ var expectedHands,
142
+ inputHands: TList<string>;
143
+ ActualHands: TList<string>;
144
+ expectedHandsArray,
145
+ ActualHandsArray: TArray<string>;
146
+ i: integer;
147
+ begin
148
+ inputHands := TList<string>.create;
149
+ inputHands.AddRange([pairOf2, pairOf4]);
150
+ expectedHands := TList<string>.Create;
151
+ expectedHands.AddRange(pairOf4);
152
+
153
+ ActualHands := Poker.BestHands(inputHands);
154
+
155
+ expectedHandsArray := expectedHands.ToArray;
156
+ ActualHandsArray := ActualHands.ToArray;
157
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
158
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
159
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
160
+ end;
161
+
162
+ procedure PokerTest.One_pair_vs_double_pair;
163
+ const pairOf8: string = '2S 8H 6S 8D JH';
164
+ doublePair: string = '4C 5C 4S 8D 5H';
165
+ var expectedHands,
166
+ inputHands: TList<string>;
167
+ ActualHands: TList<string>;
168
+ expectedHandsArray,
169
+ ActualHandsArray: TArray<string>;
170
+ i: integer;
171
+ begin
172
+ inputHands := TList<string>.Create;
173
+ inputHands.AddRange([pairOf8, doublePair]);
174
+
175
+ expectedHands := TList<string>.Create;
176
+ expectedHands.Add(doublePair);
177
+
178
+ ActualHands := Poker.BestHands(inputHands);
179
+
180
+ expectedHandsArray := expectedHands.ToArray;
181
+ ActualHandsArray := ActualHands.ToArray;
182
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
183
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
184
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
185
+ end;
186
+
187
+ procedure PokerTest.Two_double_pairs;
188
+ const doublePair2And8: string = '2D 8H 2S 8D JH';
189
+ doublePair4And5: string = '4S 5C 4C 8D 5H';
190
+ var expectedHands,
191
+ inputHands: TList<string>;
192
+ ActualHands: TList<string>;
193
+ expectedHandsArray,
194
+ ActualHandsArray: TArray<string>;
195
+ i: integer;
196
+ begin
197
+ inputHands := TList<string>.Create;
198
+ inputHands.AddRange([doublePair2And8, doublePair4And5]);
199
+
200
+ expectedHands := TList<string>.Create;
201
+ expectedHands.Add(doublePair2And8);
202
+
203
+ ActualHands := Poker.BestHands(inputHands);
204
+
205
+ expectedHandsArray := expectedHands.ToArray;
206
+ ActualHandsArray := ActualHands.ToArray;
207
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
208
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
209
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
210
+ end;
211
+
212
+ procedure PokerTest.Double_pair_vs_three;
213
+ const doublePair2And8: string = '2D 8H 2S 8D JH';
214
+ threeOf4: string = '4D 5H 4S 8D 4H';
215
+ var expectedHands,
216
+ inputHands: TList<string>;
217
+ ActualHands: TList<string>;
218
+ expectedHandsArray,
219
+ ActualHandsArray: TArray<string>;
220
+ i: integer;
221
+ begin
222
+ inputHands := TList<string>.Create;
223
+ inputHands.AddRange([doublePair2And8, threeOf4]);
224
+
225
+ expectedHands := TList<string>.Create;
226
+ expectedHands.Add(threeOf4);
227
+
228
+ ActualHands := Poker.BestHands(inputHands);
229
+
230
+ expectedHandsArray := expectedHands.ToArray;
231
+ ActualHandsArray := ActualHands.ToArray;
232
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
233
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
234
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
235
+ end;
236
+
237
+ procedure PokerTest.Two_threes;
238
+ const threeOf2: string = '2S 2H 2S 8D JH';
239
+ threeOf1: string = '4S AH AS 8D AH';
240
+ var expectedHands,
241
+ inputHands: TList<string>;
242
+ ActualHands: TList<string>;
243
+ expectedHandsArray,
244
+ ActualHandsArray: TArray<string>;
245
+ i: integer;
246
+ begin
247
+ inputHands := TList<string>.Create;
248
+ inputHands.AddRange([threeOf2, threeOf1]);
249
+
250
+ expectedHands := TList<string>.Create;
251
+ expectedHands.Add(threeOf1);
252
+
253
+ ActualHands := Poker.BestHands(inputHands);
254
+
255
+ expectedHandsArray := expectedHands.ToArray;
256
+ ActualHandsArray := ActualHands.ToArray;
257
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
258
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
259
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
260
+ end;
261
+
262
+ procedure PokerTest.Three_vs_straight;
263
+ const threeOf4: string = '4S 5D 4C 8D 4H';
264
+ straight: string = '3S 4D 2S 6D 5H';
265
+ var expectedHands,
266
+ inputHands: TList<string>;
267
+ ActualHands: TList<string>;
268
+ expectedHandsArray,
269
+ ActualHandsArray: TArray<string>;
270
+ i: integer;
271
+ begin
272
+ inputHands := TList<string>.Create;
273
+ inputHands.AddRange([threeOf4, straight]);
274
+
275
+ expectedHands := TList<string>.Create;
276
+ expectedHands.Add(straight);
277
+
278
+ ActualHands := Poker.BestHands(inputHands);
279
+
280
+ expectedHandsArray := expectedHands.ToArray;
281
+ ActualHandsArray := ActualHands.ToArray;
282
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
283
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
284
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
285
+ end;
286
+
287
+ procedure PokerTest.Two_straights;
288
+ const straightTo8: string = '4S 6H 7S 8D 5H';
289
+ straightTo9: string = '5S 7H 8S 9D 6H';
290
+ straightTo1: string = 'AS QH KS TD JH';
291
+ straightTo5: string = '4S AH 3S 2D 5H';
292
+ var expectedHands,
293
+ inputHands: TList<string>;
294
+ ActualHands: TList<string>;
295
+ expectedHandsArray,
296
+ ActualHandsArray: TArray<string>;
297
+ i: integer;
298
+ begin
299
+ inputHands := TList<string>.Create;
300
+ inputHands.AddRange([straightTo8, straightTo9]);
301
+
302
+ expectedHands := TList<string>.Create;
303
+ expectedHands.Add(straightTo9);
304
+
305
+ ActualHands := Poker.BestHands(inputHands);
306
+
307
+ expectedHandsArray := expectedHands.ToArray;
308
+ ActualHandsArray := ActualHands.ToArray;
309
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
310
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
311
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
312
+
313
+ inputHands.Clear;
314
+ inputHands.AddRange([straightTo1, straightTo5]);
315
+
316
+ expectedHands.Clear;
317
+ expectedHands.Add(straightTo1);
318
+
319
+ ActualHands := Poker.BestHands(inputHands);
320
+
321
+ expectedHandsArray := expectedHands.ToArray;
322
+ ActualHandsArray := ActualHands.ToArray;
323
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
324
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
325
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
326
+ end;
327
+
328
+ procedure PokerTest.Straight_vs_flush;
329
+ const straightTo8: string = '4S 6H 7S 8D 5H';
330
+ flushTo7: string = '2S 4S 5S 6S 7S';
331
+ var expectedHands,
332
+ inputHands: TList<string>;
333
+ ActualHands: TList<string>;
334
+ expectedHandsArray,
335
+ ActualHandsArray: TArray<string>;
336
+ i: integer;
337
+ begin
338
+ inputHands := TList<string>.Create;
339
+ inputHands.AddRange([straightTo8, flushTo7]);
340
+
341
+ expectedHands := TList<string>.Create;
342
+ expectedHands.Add(flushTo7);
343
+
344
+ ActualHands := Poker.BestHands(inputHands);
345
+
346
+ expectedHandsArray := expectedHands.ToArray;
347
+ ActualHandsArray := ActualHands.ToArray;
348
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
349
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
350
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
351
+ end;
352
+
353
+ procedure PokerTest.Two_flushes;
354
+ const flushTo8: string = '3H 6H 7H 8H 5H';
355
+ flushTo7: string = '2S 4S 5S 6S 7S';
356
+ var expectedHands,
357
+ inputHands: TList<string>;
358
+ ActualHands: TList<string>;
359
+ expectedHandsArray,
360
+ ActualHandsArray: TArray<string>;
361
+ i: integer;
362
+ begin
363
+ inputHands := TList<string>.Create;
364
+ inputHands.AddRange([flushTo8, flushTo7]);
365
+
366
+ expectedHands := TList<string>.Create;
367
+ expectedHands.Add(flushTo8);
368
+
369
+ ActualHands := Poker.BestHands(inputHands);
370
+
371
+ expectedHandsArray := expectedHands.ToArray;
372
+ ActualHandsArray := ActualHands.ToArray;
373
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
374
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
375
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
376
+ end;
377
+
378
+ procedure PokerTest.Flush_vs_full;
379
+ const flushTo8: string = '3H 6H 7H 8H 5H';
380
+ full: string = '4S 5H 4S 5D 4H';
381
+ var expectedHands,
382
+ inputHands: TList<string>;
383
+ ActualHands: TList<string>;
384
+ expectedHandsArray,
385
+ ActualHandsArray: TArray<string>;
386
+ i: integer;
387
+ begin
388
+ inputHands := TList<string>.Create;
389
+ inputHands.AddRange([flushTo8, full]);
390
+
391
+ expectedHands := TList<string>.Create;
392
+ expectedHands.Add(full);
393
+
394
+ ActualHands := Poker.BestHands(inputHands);
395
+
396
+ expectedHandsArray := expectedHands.ToArray;
397
+ ActualHandsArray := ActualHands.ToArray;
398
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
399
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
400
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
401
+ end;
402
+
403
+ procedure PokerTest.Two_fulls;
404
+ const fullOf4By9: string = '4H 4S 4D 9S 9D';
405
+ fullOf5By8: string = '5H 5S 5D 8S 8D';
406
+ var expectedHands,
407
+ inputHands: TList<string>;
408
+ ActualHands: TList<string>;
409
+ expectedHandsArray,
410
+ ActualHandsArray: TArray<string>;
411
+ i: integer;
412
+ begin
413
+ inputHands := TList<string>.Create;
414
+ inputHands.AddRange([fullOf4By9, fullOf5By8]);
415
+
416
+ expectedHands := TList<string>.Create;
417
+ expectedHands.Add(fullOf5By8);
418
+
419
+ ActualHands := Poker.BestHands(inputHands);
420
+
421
+ expectedHandsArray := expectedHands.ToArray;
422
+ ActualHandsArray := ActualHands.ToArray;
423
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
424
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
425
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
426
+ end;
427
+
428
+ procedure PokerTest.Full_vs_square;
429
+ const full: string = '4S 5H 4S 5D 4H';
430
+ squareOf3: string = '3S 3H 2S 3D 3H';
431
+ var expectedHands,
432
+ inputHands: TList<string>;
433
+ ActualHands: TList<string>;
434
+ expectedHandsArray,
435
+ ActualHandsArray: TArray<string>;
436
+ i: integer;
437
+ begin
438
+ inputHands := TList<string>.Create;
439
+ inputHands.AddRange([squareOf3,full]);
440
+
441
+ expectedHands := TList<string>.Create;
442
+ expectedHands.Add(squareOf3);
443
+
444
+ ActualHands := Poker.BestHands(inputHands);
445
+
446
+ expectedHandsArray := expectedHands.ToArray;
447
+ ActualHandsArray := ActualHands.ToArray;
448
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
449
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
450
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
451
+ end;
452
+
453
+ procedure PokerTest.Two_squares;
454
+ const squareOf2: string = '2S 2H 2S 8D 2H';
455
+ squareOf5: string = '4S 5H 5S 5D 5H';
456
+ var expectedHands,
457
+ inputHands: TList<string>;
458
+ ActualHands: TList<string>;
459
+ expectedHandsArray,
460
+ ActualHandsArray: TArray<string>;
461
+ i: integer;
462
+ begin
463
+ inputHands := TList<string>.Create;
464
+ inputHands.AddRange([squareOf2, squareOf5]);
465
+
466
+ expectedHands := TList<string>.Create;
467
+ expectedHands.Add(squareOf5);
468
+
469
+ ActualHands := Poker.BestHands(inputHands);
470
+
471
+ expectedHandsArray := expectedHands.ToArray;
472
+ ActualHandsArray := ActualHands.ToArray;
473
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
474
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
475
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
476
+ end;
477
+
478
+ procedure PokerTest.Square_vs_straight_flush;
479
+ const squareOf5: string = '4S 5H 5S 5D 5H';
480
+ straightFlushTo9: string = '5S 7S 8S 9S 6S';
481
+ var expectedHands,
482
+ inputHands: TList<string>;
483
+ ActualHands: TList<string>;
484
+ expectedHandsArray,
485
+ ActualHandsArray: TArray<string>;
486
+ i: integer;
487
+ begin
488
+ inputHands := TList<string>.Create;
489
+ inputHands.AddRange([squareOf5, straightFlushTo9]);
490
+
491
+ expectedHands := TList<string>.Create;
492
+ expectedHands.Add(straightFlushTo9);
493
+
494
+ ActualHands := Poker.BestHands(inputHands);
495
+
496
+ expectedHandsArray := expectedHands.ToArray;
497
+ ActualHandsArray := ActualHands.ToArray;
498
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
499
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
500
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
501
+ end;
502
+
503
+ procedure PokerTest.Two_straight_flushes;
504
+ const straightFlushTo8: string = '4H 6H 7H 8H 5H';
505
+ straightFlushTo9: string = '5S 7S 8S 9S 6S';
506
+ var expectedHands,
507
+ inputHands: TList<string>;
508
+ ActualHands: TList<string>;
509
+ expectedHandsArray,
510
+ ActualHandsArray: TArray<string>;
511
+ i: integer;
512
+ begin
513
+ inputHands := TList<string>.Create;
514
+ inputHands.AddRange([straightFlushTo8, straightFlushTo9]);
515
+
516
+ expectedHands := TList<string>.Create;
517
+ expectedHands.Add(straightFlushTo9);
518
+
519
+ ActualHands := Poker.BestHands(inputHands);
520
+
521
+ expectedHandsArray := expectedHands.ToArray;
522
+ ActualHandsArray := ActualHands.ToArray;
523
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
524
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
525
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
526
+ end;
527
+
528
+ procedure PokerTest.Three_hand_with_tie;
529
+ const spadeStraightTo9: string = '9S 8S 7S 6S 5S';
530
+ diamondStraightTo9: string = '9D 8D 7D 6D 5D';
531
+ threeOf4: string = '4D 4S 4H QS KS';
532
+ var expectedHands,
533
+ inputHands: TList<string>;
534
+ ActualHands: TList<string>;
535
+ expectedHandsArray,
536
+ ActualHandsArray: TArray<string>;
537
+ i: integer;
538
+ begin
539
+ inputHands := TList<string>.Create;
540
+ inputHands.AddRange([spadeStraightTo9, threeOf4, diamondStraightTo9]);
541
+
542
+ expectedHands := TList<string>.Create;
543
+ expectedHands.AddRange([spadeStraightTo9, diamondStraightTo9]);
544
+
545
+ ActualHands := Poker.BestHands(inputHands);
546
+
547
+ expectedHandsArray := expectedHands.ToArray;
548
+ ActualHandsArray := ActualHands.ToArray;
549
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
550
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
551
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
552
+ end;
553
+
554
+ procedure PokerTest.Straight_to_5_against_a_pair_of_jacks;
555
+ const straightTo5: string = '2S 4D 5C 3S AS';
556
+ twoJacks: string = 'JD 8D 7D JC 5D';
557
+ var expectedHands,
558
+ inputHands: TList<string>;
559
+ ActualHands: TList<string>;
560
+ expectedHandsArray,
561
+ ActualHandsArray: TArray<string>;
562
+ i: integer;
563
+ begin
564
+ inputHands := TList<string>.Create;
565
+ inputHands.AddRange([straightTo5, twoJacks]);
566
+
567
+ expectedHands := TList<string>.Create;
568
+ expectedHands.AddRange(straightTo5);
569
+
570
+ ActualHands := Poker.BestHands(inputHands);
571
+
572
+ expectedHandsArray := expectedHands.ToArray;
573
+ ActualHandsArray := ActualHands.ToArray;
574
+ Assert.AreEqual(expectedHands.Count,ActualHands.Count);
575
+ for i := Low(expectedHandsArray) to High(expectedHandsArray) do
576
+ Assert.AreEqual(expectedHandsArray[i], ActualHandsArray[i]);
577
+ end;
578
+
579
+
580
+ initialization
581
+ TDUnitX.RegisterTestFixture(PokerTest);
582
+ end.