Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
725 lines (623 loc) · 21 KB
/
Copy pathProgram.cs
File metadata and controls
725 lines (623 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
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
using System;
using System.Collections.Generic;
using System.Text;
using Combinatorics.Collections;
using System.Linq;
namespace Task1
{
public class Card
{
private Rank _rank;
public Rank Rank
{
get { return _rank; }
set { _rank = value; }
}
private Suit _suit;
public Suit Suit
{
get { return _suit; }
set { _suit = value; }
}
public Card(Rank r, Suit s)
{
this._rank = r;
this._suit = s;
}
public override string ToString()
{
return RanksToString(Rank) + SuitToString(Suit).ToString();
}
public static char SuitToString(Suit s)
{
switch (s)
{
case Suit.ValueH:
return 'h';
case Suit.ValueD:
return 'd';
case Suit.ValueC:
return 'c';
case Suit.ValueS:
return 's';
default:
return 'g';
}
}
public static char RanksToString(Rank s)
{
switch (s)
{
case Rank.Value2:
return '2';
case Rank.Value3:
return '3';
case Rank.Value4:
return '4';
case Rank.Value5:
return '5';
case Rank.Value6:
return '6';
case Rank.Value7:
return '7';
case Rank.Value8:
return '8';
case Rank.Value9:
return '9';
case Rank.ValueT:
return 'T';
case Rank.ValueJ:
return 'J';
case Rank.ValueQ:
return 'Q';
case Rank.ValueK:
return 'K';
case Rank.ValueA:
return 'A';
default:
return 'g';
}
}
}
public enum Rank
{
Value2,
Value3,
Value4,
Value5,
Value6,
Value7,
Value8,
Value9,
ValueT,
ValueJ,
ValueQ,
ValueK,
ValueA
}
public enum Suit
{
ValueH,
ValueD,
ValueC,
ValueS,
}
public enum Combination
{
HighCard,
Pair,
TwoPair,
ThreeOfAKind,
Straight,
Flush,
FullHouse,
FourOfAKind,
StraightFLush
}
static class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter a string:");
while (true)
{
string enter = Console.ReadLine();
CheckEnter(enter);
Console.WriteLine(enter);
}
}
private static string CheckEnter(string checkedString)
{
var tokens = checkedString.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in tokens)
{
Console.Write("'{0}' ", s);
Console.WriteLine();
}
switch (tokens[0])
{
case "texas-holdem":
List<Card> board = ParseCards(tokens[1]);
List<List<Card>> hands = new List<List<Card>>();
for (int i = 2; i < tokens.Length; i++)
{
List<Card> newCards = ParseCards(tokens[i]);
hands.Add(newCards);
}
foreach (var hand in hands)
{
FindHandValue(hand, board);
}
break;
case "omaha-holdem":
break;
case "five-cards-draw":
break;
default:
Console.WriteLine("You entered the wrong gametype, please re-enter");
break;
}
return checkedString;
}
private static List<Card> ParseCards(string hand)
{
List<Card> handList = new List<Card>();
for (int i = 0; i < hand.Length; i += 2)
{
Card newCard = new Card(ReturnRankEnum(hand[i]), ReturnSuitEnum(hand[i + 1]));
handList.Add(newCard);
}
return handList;
}
private static Combination FindHandValue(List<Card> hand, List<Card> board)
{
List<Card> availableCards = new List<Card>();
availableCards.AddRange(hand);
availableCards.AddRange(board);
Combinations<Card> variants = new Combinations<Card>(availableCards, 5);
List<Combination> combinations = new List<Combination>();
foreach (IList<Card> variant in variants)
{
Dictionary<Rank, int> rankGroups = GroupRanks(variant);
Dictionary<Suit, int> suitGroups = GroupSuits(variant);
Combination comb = FindCombination(variant, rankGroups, suitGroups);
combinations.Add(FindCombination(variant, rankGroups, suitGroups));
Console.WriteLine(String.Join("; ", variant));
Console.WriteLine(comb);
}
<<<<<<< Updated upstream
Console.WriteLine("Combination " + combinations.Max().ToString());
return combinations.Max();
=======
combinations.Sort(new CombinationComparer());
Console.WriteLine("Combination: " + combinations.Last() );
return combinations.Last();
>>>>>>> Stashed changes
}
private static Combination FindCombination(IList<Card> cards, Dictionary<Rank, int> ranks, Dictionary<Suit, int> suits)
{
if (IsStraightFlush(cards, ranks, suits)) { return Combination.StraightFLush; }
else if (IsFourOfAKind(cards, ranks)) { return Combination.FourOfAKind; }
else if (IsFullHouse(cards, ranks, suits)) { return Combination.FullHouse; }
else if (IsFlush(cards, suits)) { return Combination.Flush; }
else if (IsStraight(cards, ranks)) { return Combination.Straight; }
else if (IsThreeOfAKind(cards, ranks)) { return Combination.ThreeOfAKind;}
else if (IsTwoPairs(cards, ranks)) { return Combination.TwoPair; }
else if (IsPair(cards, ranks)) { return Combination.Pair; }
else return Combination.HighCard;
}
static Dictionary<Rank, int> GroupRanks(IList<Card> cards)
{
Dictionary<Rank, int> entries = new Dictionary<Rank, int>();
foreach (Card card in cards)
{
if (entries.ContainsKey(card.Rank))
{
entries[card.Rank] += 1;
}
else
{
entries.Add(card.Rank, 1);
}
}
return entries;
}
static Dictionary<Suit, int> GroupSuits(IList<Card> cards)
{
Dictionary<Suit, int> entries = new Dictionary<Suit, int>();
foreach (Card card in cards)
{
if (entries.ContainsKey(card.Suit))
{
entries[card.Suit] += 1;
}
else
{
entries.Add(card.Suit, 1);
}
}
return entries;
}
static Rank ReturnRankEnum(char rank)
{
Rank returnRank = Rank.Value2;
char[] charArray = { '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' };
int indexOfRank = Array.IndexOf(charArray, rank);
foreach (int i in Enum.GetValues(typeof(Rank)))
{
if (indexOfRank == i)
{
returnRank = (Rank)i;
}
}
return returnRank;
}
static Suit ReturnSuitEnum(char suit)
{
Suit returnSuit = Suit.ValueC;
switch (suit)
{
case 'h':
returnSuit = Suit.ValueH;
break;
case 'd':
returnSuit = Suit.ValueD;
break;
case 'c':
returnSuit = Suit.ValueC;
break;
case 's':
returnSuit = Suit.ValueS;
break;
default:
break;
}
return returnSuit;
}
static bool IsStraight(IList<Card> combCards, Dictionary<Rank, int> ranks)
{
if (ranks.Count == 5)
{
List<Rank> sortedRanks = ranks.Keys.ToList();
sortedRanks.Sort();
for (int count = 0; count < (sortedRanks.Count - 1); count++)
{
var element1 = sortedRanks[count];
var element2 = sortedRanks[count + 1];
if (element2 != element1 + 1)
{
return false;
}
}
return true;
}
return false;
}
static bool IsFlush(IList<Card> combCards, Dictionary<Suit, int> suits)
{
if(suits.ContainsValue(5))
{
// Console.WriteLine($"{{{combCards[0].Suit} {combCards[1].Suit} {combCards[2].Suit} {combCards[3].Suit} {combCards[4].Suit}}}");
return true;
}
return false;
}
static bool IsStraightFlush(IList<Card> combCards, Dictionary<Rank, int> ranks, Dictionary<Suit, int> suits)
{
if (IsStraight(combCards, ranks) && IsFlush(combCards, suits) )
{
// Console.WriteLine($"{{{combCards[0].Rank} {combCards[1].Rank} {combCards[2].Rank} {combCards[3].Rank} {combCards[4].Rank}}}");
return true;
}
else
return false;
}
static bool IsFourOfAKind(IList<Card> combCards, Dictionary<Rank, int> ranks)
{
if (ranks.ContainsValue(4))
{
// Console.WriteLine($"{{{combCards[0].Rank} {combCards[1].Rank} {combCards[2].Rank} {combCards[3].Rank} {combCards[4].Rank}}}");
return true;
}
return false;
}
static bool IsFullHouse(IList<Card> combCards, Dictionary<Rank, int> ranks, Dictionary<Suit, int> suits)
{
if (ranks.Count == 2 && (ranks.ContainsValue(2) && ranks.ContainsValue(3)))
{
// Console.WriteLine($"{{{combCards[0].Rank} {combCards[1].Rank} {combCards[2].Rank} {combCards[3].Rank} {combCards[4].Rank}}}");
return true;
}
return false;
}
<<<<<<< Updated upstream
static bool IsThreeOfAKind(IList<Card> combCards, Dictionary<Rank, int> ranks)
{
if (ranks.ContainsValue(3))
=======
static Pair MakePair(IList<Card> combCards, Dictionary<Rank, int> ranks)
{
Rank highCard = Rank.Value2; // Use unassigment variable
Rank kicker = Rank.Value2;
List<Rank> potentialKickers = ranks.Keys.ToList();
foreach (KeyValuePair<Rank, int> entry in ranks)
{
if (entry.Value == 2)
{ highCard = entry.Key;}
}
potentialKickers.Remove(highCard);
potentialKickers.Sort();
kicker = potentialKickers.Max();
return new Pair(highCard, kicker);
}
static HighCard MakeHighCard(IList<Card> combCards, Dictionary<Rank, int> ranks)
{
Rank highCard = ranks.Keys.Max();
return new HighCard(highCard);
}
public interface ICombination : IComparable
{
public Combination marker { get; }
}
public class StraightFlush : ICombination, IComparable
{
public Rank HighCard { get; }
public Combination marker { get; }
public StraightFlush(Rank r)
{
this.marker = Combination.StraightFLush;
this.HighCard = r;
}
public int CompareTo(object obj)
{
StraightFlush compObj = (StraightFlush)obj;
if (this.HighCard > compObj.HighCard)
return 1;
if (this.HighCard < compObj.HighCard)
return -1;
else
return 0;
}
public override string ToString()
{
return "STRAIGHT FLUSH: RANK -> " + this.HighCard;
}
}
public class FullHouse: ICombination, IComparable
{
public Rank TwoCard { get; }
public Rank ThreeCard { get; }
public Combination marker { get; }
public FullHouse(Rank th, Rank tw)
{
this.ThreeCard = th;
this.TwoCard = tw;
this.marker = Combination.FullHouse;
}
public int CompareTo(object obj)
{
FullHouse compObj = (FullHouse)obj;
if (this.ThreeCard > compObj.ThreeCard && this.TwoCard > compObj.TwoCard)
return 1;
if (this.ThreeCard < compObj.ThreeCard && this.TwoCard < compObj.TwoCard)
return -1;
else
return 0;
}
public override string ToString()
{
return "FULL HOUSE: THREE CARD -> " + this.ThreeCard + "TWO CARD ->" + this.TwoCard;
}
}
public class Straight : ICombination, IComparable
{
public Rank Rank { get; }
public Combination marker { get; }
public Straight(Rank r)
{
this.Rank = r;
this.marker = Combination.Straight;
}
public int CompareTo(object obj)
{
Straight compObj = (Straight)obj;
if (this.Rank > compObj.Rank)
return 1;
if (this.Rank < compObj.Rank)
return -1;
else
return 0;
}
public override string ToString()
{
return "STRAIGHT: RANK -> " + this.Rank;
}
}
public class Flush : ICombination, IComparable
{
public Rank Rank { get; }
public Combination marker { get; }
public Flush(Rank r)
{
this.Rank = r;
this.marker = Combination.Flush;
}
public int CompareTo(object obj)
{
Flush compObj = (Flush)obj;
if (this.Rank > compObj.Rank)
return 1;
if (this.Rank < compObj.Rank)
return -1;
else
return 0;
}
public override string ToString()
{
return "FLUSH: RANK -> " + this.Rank;
}
}
public class FourOfAKind : ICombination, IComparable
{
public Rank HighCard { get; }
public Rank Kicker { get; }
public Combination marker { get; }
public FourOfAKind(Rank r, Rank k)
{
this.HighCard = r;
this.Kicker = k;
this.marker = Combination.FourOfAKind;
}
public int CompareTo(object obj)
{
FourOfAKind compObj = (FourOfAKind)obj;
if (this.HighCard > compObj.HighCard)
return 1;
if (this.HighCard < compObj.HighCard)
return -1;
else
return 0;
}
public override string ToString()
{
return "FOUR OF A KIND: RANK -> " + this.HighCard + " KICKER -> " + this.Kicker;
}
}
public class ThreeOfAKind : ICombination, IComparable
{
public Rank HighCard { get; }
public Rank Kicker { get; }
public Combination marker { get; }
public ThreeOfAKind(Rank r, Rank k)
{
this.HighCard = r;
this.Kicker = k;
this.marker = Combination.ThreeOfAKind;
}
public int CompareTo(object obj)
{
return 0;
}
public override string ToString()
{
return "THREE OF A KIND: RANK -> " + this.HighCard + " KICKER -> " + this.Kicker;
}
}
public class TwoPairs : ICombination, IComparable
{
public Rank HighPair { get; }
public Rank LowPair { get; }
public Rank Kicker { get; }
public Combination marker { get; }
public TwoPairs(Rank hp, Rank lp, Rank k)
{
this.HighPair = hp;
this.LowPair = lp;
this.Kicker = k;
this.marker = Combination.TwoPair;
}
public int CompareTo(object obj)
{
return 0;
}
public override string ToString()
>>>>>>> Stashed changes
{
// Console.WriteLine($"{{{combCards[0].Rank} {combCards[1].Rank} {combCards[2].Rank} {combCards[3].Rank} {combCards[4].Rank}}}");
return true;
}
return false;
}
static bool IsTwoPairs(IList<Card> combCards, Dictionary<Rank, int> ranks)
{
<<<<<<< Updated upstream
if (ranks.Count == 3 && ranks.ContainsValue(2))
{
// Console.WriteLine($"{{{combCards[0].Rank} {combCards[1].Rank} {combCards[2].Rank} {combCards[3].Rank} {combCards[4].Rank}}}");
return true;
=======
public Rank pairRank { get; }
public Combination marker { get; }
public Rank Kicker { get; }
public Pair(Rank pr, Rank k)
{
this.pairRank = pr;
this.Kicker = k;
this.marker = Combination.Pair;
}
public int CompareTo(object obj)
{
Pair other = (Pair)obj;
if (this.pairRank < other.pairRank) return -1;
if (this.pairRank > other.pairRank) return 1;
if (this.Kicker < other.Kicker) return -1;
if (this.Kicker > other.Kicker) return 1;
return 0;
}
public override string ToString()
{
return "PAIR: PAIR RANK -> " + this.pairRank + " KICKER -> " + this.Kicker;
>>>>>>> Stashed changes
}
return false;
}
static bool IsPair(IList<Card> combCards, Dictionary<Rank, int> ranks)
{
<<<<<<< Updated upstream
if (ranks.ContainsValue(2))
=======
public Rank Kicker { get; }
public Combination marker { get; }
public HighCard(Rank r)
{
this.Kicker = r;
this.marker = Combination.HighCard;
}
public int CompareTo(object obj)
{
HighCard compObj = (HighCard)obj;
if (this.Kicker > compObj.Kicker)
return 1;
if (this.Kicker < compObj.Kicker)
return -1;
else
return 0;
}
public override string ToString()
>>>>>>> Stashed changes
{
// Console.WriteLine($"{{{combCards[0].Rank} {combCards[1].Rank} {combCards[2].Rank} {combCards[3].Rank} {combCards[4].Rank}}}");
return true;
}
return false;
}
<<<<<<< Updated upstream
/* public static char RankToString(Rank r)
{
switch (r)
{
case Suit.ValueH:
return 'h';
case Suit.ValueD:
return 'd';
case Suit.ValueC:
return 'c';
case Suit.ValueS:
return 's';
default:
return 'g';
=======
public class CombinationComparer : IComparer<ICombination>
{
public int Compare(ICombination x, ICombination y)
{
if (x.marker > y.marker)
return 1;
if (x.marker < y.marker)
return -1;
return x.CompareTo(y);
>>>>>>> Stashed changes
}
} */
}
}