Repository navigation
Expand file tree
/
Copy pathHandsDefinitions.cs
More file actions
340 lines (247 loc) · 7.57 KB
/
Copy pathHandsDefinitions.cs
File metadata and controls
340 lines (247 loc) · 7.57 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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class HandsDefinitions : MonoBehaviour
{
public static HandsDefinitions Definitions;
void Start ()
{
Definitions = this;
}
public List<Hand> CalculateHand (List<Cards> cards)
{
if(cards.Count != 7) return null;
List<Hand> hands = new List<Hand>();
//single moves
hands.Add(BestHigh(new List<Cards>(cards)));
hands.Add(BestPair(new List<Cards>(cards)));
hands.Add(BestDoublePair(new List<Cards>(cards)));
hands.Add(BestSet(new List<Cards>(cards)));
hands.Add(BestFull(new List<Cards>(cards)));
hands.Add(BestFour(new List<Cards>(cards)));
//composed moves
Hand str = BestStraight(new List<Cards>(cards));
Hand flu = BestFlush(new List<Cards>(cards));
hands.Add(str);
hands.Add(flu);
if(str != null && flu != null)
{
//atleast its a straight flush
hands.Add(new Hand(HandType.StraightFlush, str.GetSecondaryRanks(0)));
//but if the second rank is also an ace, ITS A ROYAL FLUSH
if(str.GetSecondaryRanks(0) == 14)
hands.Add(new Hand(HandType.RoyalFlush, str.GetSecondaryRanks(0)));
}
return hands;
}
public Player GetBestEqualityHand (Player first, Player second)
{
//find out which hand is the best, if they have the same hand
int iR = 0;
while(iR < 5)
{
if(first.bestHand.GetSecondaryRanks(iR) > second.bestHand.GetSecondaryRanks(iR)) return first;
else if(first.bestHand.GetSecondaryRanks(iR) < second.bestHand.GetSecondaryRanks(iR)) return second;
iR++;
}
return null;
}
Hand BestHigh (List<Cards> cards)
{
List<Cards> FiveOutOfSeven = new List<Cards>();
cards.Sort();
for(int i = 0; i < 5; i++) FiveOutOfSeven.Add(cards[i]);
return new Hand(HandType.High, FiveOutOfSeven[0].Rank, FiveOutOfSeven[1].Rank, FiveOutOfSeven[2].Rank,FiveOutOfSeven[3].Rank, FiveOutOfSeven[4].Rank);
}
Hand BestPair (List<Cards> cards)
{
List<Cards> FiveOutOfSeven = new List<Cards>();
List<Cards> Pairs = new List<Cards>();
cards.Sort();
//we set the count to 1 since we're searching for pairs
int count = 1;
int tempCount, temp = 0;
for (int i = 0; i < cards.Count; i++)
{
temp = cards[i].Rank;
tempCount = 0;
for (int j = 0; j < cards.Count; j++) if (temp == cards[j].Rank) tempCount++;
if (tempCount > count) Pairs.Add(cards[i]);
}
if(Pairs.Count > 1)
{
Pairs.Sort();
//here we set our five best cards
for(int z = 0; z < 2; z++){ FiveOutOfSeven.Add(Pairs[z]); cards.Remove(Pairs[z]); }
cards.Sort();
for(int w = 0; w < 3; w++) FiveOutOfSeven.Add(cards[w]);
return new Hand(HandType.Pair, FiveOutOfSeven[0].Rank, FiveOutOfSeven[2].Rank, FiveOutOfSeven[3].Rank, FiveOutOfSeven[4].Rank);
}
return null;
}
Hand BestDoublePair (List<Cards> cards)
{
List<Cards> FiveOutOfSeven = new List<Cards>();
List<Cards> Pairs = new List<Cards>();
cards.Sort();
int count = 1;
int tempCount, temp = 0;
for (int i = 0; i < cards.Count; i++)
{
temp = cards[i].Rank;
tempCount = 0;
for (int j = 0; j < cards.Count; j++) if (temp == cards[j].Rank) tempCount++;
if (tempCount > count) Pairs.Add(cards[i]);
}
if(Pairs.Count > 3)
{
Pairs.Sort();
//here we set our five best cards
for(int z = 0; z < 4; z++) { FiveOutOfSeven.Add(Pairs[z]); cards.Remove(Pairs[z]); }
cards.Sort();
FiveOutOfSeven.Add(cards[0]);
return new Hand(HandType.TwoPair, FiveOutOfSeven[0].Rank, FiveOutOfSeven[2].Rank, FiveOutOfSeven[4].Rank);
}
return null;
}
Hand BestSet (List<Cards> cards)
{
List<Cards> FiveOutOfSeven = new List<Cards>();
List<Cards> Pairs = new List<Cards>();
cards.Sort();
int count = 2;
int tempCount, temp = 0;
for (int i = 0; i < cards.Count; i++)
{
temp = cards[i].Rank;
tempCount = 0;
for (int j = 0; j < cards.Count; j++) if (temp == cards[j].Rank) tempCount++;
if (tempCount > count) Pairs.Add(cards[i]);
}
if(Pairs.Count > 2)
{
Pairs.Sort();
//here we set our five best cards
for(int z = 0; z < 3; z++) { FiveOutOfSeven.Add(Pairs[z]); cards.Remove(Pairs[z]); }
cards.Sort();
FiveOutOfSeven.Add(cards[0]);
FiveOutOfSeven.Add(cards[1]);
return new Hand(HandType.Set, FiveOutOfSeven[0].Rank, FiveOutOfSeven[3].Rank, FiveOutOfSeven[4].Rank);
}
return null;
}
Hand BestStraight (List<Cards> cards)
{
cards.Sort();
int mPossible = cards.Count - 5;
for(int i = 0; i < mPossible; i++)
{
bool broken = false;
for(int j = 0; j < 5; j++)
{
if((cards[i].Rank - j) == cards[i + j].Rank) continue;
else broken = true;
}
if(!broken) return new Hand (HandType.Straight, cards[i].Rank);
}
return null;
}
Hand BestFlush (List<Cards> cards)
{
List<Cards> Flush = new List<Cards>();
cards.Sort();
int count = 4, tempCount = 0;
Type temp = Type.Carreau;
for (int i = 0; i < cards.Count; i++)
{
temp = cards[i].type;
tempCount = 0;
for (int j = 0; j < cards.Count; j++) if (temp == cards[j].type) tempCount++;
if (tempCount > count) Flush.Add(cards[i]);
}
if(Flush.Count > 4)
{
Flush.Sort();
return new Hand(HandType.Flush, Flush[0].Rank);
}
return null;
}
Hand BestFull (List<Cards> cards)
{
List<Cards> Sets = new List<Cards>();
List<Cards> Pairs = new List<Cards>();
List<Cards> Full = new List<Cards>();
cards.Sort();
int tempSet, tempCountSet = 0;
for (int i = 0; i < cards.Count; i++)
{
tempSet = cards[i].Rank;
tempCountSet = 0;
for (int j = 0; j < cards.Count; j++) if (tempSet == cards[j].Rank) tempCountSet++;
if (tempCountSet > 2 && Sets.Count < 3) Sets.Add(cards[i]);
}
foreach(Cards s in Sets) cards.Remove(s);
int tempPair, tempCountPair = 0;
for (int x = 0; x < cards.Count; x++)
{
tempPair = cards[x].Rank;
tempCountPair = 0;
for (int y = 0; y < cards.Count; y++) if (tempPair == cards[y].Rank) tempCountPair++;
if (tempCountPair > 1 && Pairs.Count < 2) Pairs.Add(cards[x]);
}
if(Sets.Count > 2) foreach(Cards S in Sets) Full.Add(S);
if(Pairs.Count > 1) foreach(Cards P in Pairs) Full.Add(P);
//Debug.Log(Sets.Count + " : " + Pairs.Count);
if(Full.Count > 4) return new Hand(HandType.Full, Sets[0].Rank, Pairs[0].Rank);
return null;
}
Hand BestFour (List<Cards> cards)
{
List<Cards> Four = new List<Cards>();
cards.Sort();
int temp, tempCount = 0;
for (int i = 0; i < cards.Count; i++)
{
temp = cards[i].Rank;
tempCount = 0;
for (int j = 0; j < cards.Count; j++) if (temp == cards[j].Rank) tempCount++;
if (tempCount > 3) Four.Add(cards[i]);
}
if(Four.Count > 3)
{
for(int z = 0; z < 4; z++){ cards.Remove(Four[z]); }
cards.Sort();
return new Hand(HandType.Four, Four[0].Rank, cards[0].Rank);
}
return null;
}
}
[System.Serializable]
public class Hand
{
public HandType type = HandType.High;
public List<int> ranksInOrder = new List<int>();
public Hand(HandType _type, int _rank, int OptionnalRankOne = -1, int OptionnalRankTwo = -1, int OptionnalRankThree = -1, int OptionnalRankFour = -1)
{
type = _type;
ranksInOrder.Add(_rank);
ranksInOrder.Add(OptionnalRankOne);
ranksInOrder.Add(OptionnalRankTwo);
ranksInOrder.Add(OptionnalRankThree);
ranksInOrder.Add(OptionnalRankFour);
}
public string typeToString ()
{
return type.ToString();
}
public int GetRank ()
{
return (int)type;
}
public int GetSecondaryRanks (int x)
{
return ranksInOrder[x];
}
}
[System.Serializable]
public enum HandType { High = 1, Pair = 2, TwoPair = 3, Set = 4, Straight = 5, Flush = 6, Full = 7, Four = 8, StraightFlush = 9, RoyalFlush = 10 }