-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHandMethods.cs
116 lines (97 loc) · 5.34 KB
/
HandMethods.cs
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
using System;
using System.Collections.Generic;
namespace PokerConsoleApp
{
public partial class Hand
{
public static void Build21Hands(List<Card> hole, List<Card> board, ref List<Hand> retList)
{
// validate arguments
if (retList == null || hole == null || board == null || hole.Count != 2 || board.Count != 5)
{
throw new ArgumentException($"{nameof(hole)} or {nameof(board)} were bad arguments to {nameof(Build21Hands)}");
}
// Find individual players' best hand out of all possible
// combos of hole, flop, turn, and river cards
// hands with both hole cards and 3 of the board cards
retList.Add(new Hand(new List<Card> { hole[0], hole[1], board[0], board[1], board[2] }));
retList.Add(new Hand(new List<Card> { hole[0], hole[1], board[0], board[2], board[3] }));
retList.Add(new Hand(new List<Card> { hole[0], hole[1], board[0], board[2], board[4] }));
retList.Add(new Hand(new List<Card> { hole[0], hole[1], board[0], board[1], board[3] }));
retList.Add(new Hand(new List<Card> { hole[0], hole[1], board[0], board[1], board[4] }));
retList.Add(new Hand(new List<Card> { hole[0], hole[1], board[0], board[3], board[4] }));
retList.Add(new Hand(new List<Card> { hole[0], hole[1], board[1], board[2], board[3] }));
retList.Add(new Hand(new List<Card> { hole[0], hole[1], board[1], board[2], board[4] }));
retList.Add(new Hand(new List<Card> { hole[0], hole[1], board[1], board[3], board[4] }));
retList.Add(new Hand(new List<Card> { hole[0], hole[1], board[2], board[3], board[4] }));
// hands with hole card #1 and 4 board cards
retList.Add(new Hand(new List<Card> { hole[0], board[0], board[1], board[2], board[3] }));
retList.Add(new Hand(new List<Card> { hole[0], board[0], board[1], board[2], board[4] }));
retList.Add(new Hand(new List<Card> { hole[0], board[0], board[1], board[3], board[4] }));
retList.Add(new Hand(new List<Card> { hole[0], board[0], board[2], board[3], board[4] }));
retList.Add(new Hand(new List<Card> { hole[0], board[1], board[2], board[3], board[4] }));
// hands with hole card #2 and 4 board cards
retList.Add(new Hand(new List<Card> { hole[1], board[0], board[1], board[2], board[3] }));
retList.Add(new Hand(new List<Card> { hole[1], board[0], board[1], board[2], board[4] }));
retList.Add(new Hand(new List<Card> { hole[1], board[0], board[1], board[3], board[4] }));
retList.Add(new Hand(new List<Card> { hole[1], board[0], board[2], board[3], board[4] }));
retList.Add(new Hand(new List<Card> { hole[1], board[1], board[2], board[3], board[4] }));
// hand made up of all the board cards
retList.Add(new Hand(new List<Card> { board[0], board[1], board[2], board[3], board[4] }));
}
public static List<int> FindBestHand(List<Hand> inputHands)
{
/*********************************************************
* INPUT: List of Hands to compare
* OUTPUT: List<int> of the winnings hand indices
* NOTES: If two hands tie, they are both returned.
*********************************************************/
if (inputHands == null || inputHands.Count < 1)
throw new ArgumentException($"{nameof(inputHands)} passed to {nameof(FindBestHand)} was invalid.");
Hand[] lst_hand_copy = inputHands.ToArray();
List<int> winnerIndices = new List<int> { };
List<Hand> winningHands = new List<Hand> { };
List<int> losingHands = new List<int> { };
for (int i = 0; i < lst_hand_copy.Length; i++)
{
for (int j = i + 1; j < lst_hand_copy.Length; j++)
{
// Continue if i OR j are one of losing indices
if (lst_hand_copy[i] == null)
{
break;
}
if (lst_hand_copy[j] == null)
{
continue;
}
// assign ranks if they need us to
if (lst_hand_copy[i].Rank == default)
lst_hand_copy[i].AssignRankAndName();
if (lst_hand_copy[j].Rank == default)
lst_hand_copy[j].AssignRankAndName();
int _compareResult = lst_hand_copy[i].CompareTo(lst_hand_copy[j]);
if (_compareResult == 1)
{
// i loses
lst_hand_copy[i] = null;
}
else if (_compareResult == -1)
{
// j loses
lst_hand_copy[j] = null;
}
}
}
// Only non-null value in lst_hand_copy should be tied winners or a single winner
for (int i = 0; i < lst_hand_copy.Length; i++)
{
if (lst_hand_copy[i] != null)
{
winnerIndices.Add(i);
}
}
return winnerIndices;
}
}
}