-
Notifications
You must be signed in to change notification settings - Fork 6
/
PokerPot.cs
93 lines (74 loc) · 2.55 KB
/
PokerPot.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
#region References
using System;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace Server.Engines.TexasHoldem
{
public class PokerPot
{
public Dictionary<PokerPlayer, int> ContributionToPot;
public PokerPot()
{
ContributionToPot = new Dictionary<PokerPlayer, int>();
}
public PokerPot(Dictionary<PokerPlayer, int> contributers)
{
ContributionToPot = new Dictionary<PokerPlayer, int>(contributers);
}
public int GetTotalCurrency()
{
return ContributionToPot.Sum(x => x.Value);
}
public void AddtoPot(int amount, PokerPlayer player)
{
if (ContributionToPot.ContainsKey(player))
{
ContributionToPot[player] += amount;
}
else
{
ContributionToPot.Add(player, amount);
}
}
public PokerPot TrySplitPot()
{
var minamount = ContributionToPot.Min(x => x.Value);
var toreturn = new Dictionary<PokerPlayer, int>();
foreach (var kvp in ContributionToPot.ToArray())
{
if (kvp.Value > minamount && !kvp.Key.HasFolded)
{
toreturn.Add(kvp.Key, kvp.Value-minamount);
ContributionToPot[kvp.Key] = minamount;
}
}
if (toreturn.Count > 0)
return new PokerPot(toreturn);
return null;
}
public void AwardPot(List<PokerPlayer> rankedPlayerList)
{
var elligibleplayers = ElligiblePlayers(rankedPlayerList);
var highesthand = elligibleplayers.Max(x => x.HandRank);
var winners = elligibleplayers.Where(x => x.HandRank == highesthand).ToList();
var totalpot = 0;
var share = 0;
foreach (var kvp in ContributionToPot.Where(x => !winners.Contains(x.Key)).ToList())
{
totalpot += ContributionToPot[kvp.Key];
kvp.Key.AwardCredit(-ContributionToPot[kvp.Key]);
}
share = totalpot / winners.Count;
foreach (var winner in winners)
{
winner.AwardCredit(share);
winner.ReturnCredit(ContributionToPot[winner]);
}
}
public List<PokerPlayer> ElligiblePlayers(List<PokerPlayer> allPlayers)
{
return allPlayers.Where(x => ContributionToPot.ContainsKey(x)).ToList();
}
}
}