-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandHelper.cs
More file actions
92 lines (80 loc) · 2.74 KB
/
HandHelper.cs
File metadata and controls
92 lines (80 loc) · 2.74 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
namespace BlackjackSandbox
{
/// <summary>
/// Helper methods for evaluating hands
/// </summary>
public static class HandHelper
{
static readonly Dictionary<CardName, int> s_cardValues = new Dictionary<CardName, int>()
{
{ CardName.Ace, 11 },
{ CardName.Two, 2 },
{ CardName.Three, 3 },
{ CardName.Four, 4 },
{ CardName.Five, 5 },
{ CardName.Six, 6 },
{ CardName.Seven, 7 },
{ CardName.Eight, 8 },
{ CardName.Nine, 9 },
{ CardName.Ten, 10 },
{ CardName.Jack, 10 },
{ CardName.Queen, 10 },
{ CardName.King, 10 },
};
/// <summary>
/// Gets the value of a card
/// </summary>
/// <param name="c">The card value to check</param>
/// <returns>The value of the specified card</returns>
public static int GetCardValue(CardName c)
{
return s_cardValues[c];
}
/// <summary>
/// Gets the value of a hand
/// </summary>
/// <param name="hand">The hand to evaluate</param>
/// <returns>The value of the specified hand</returns>
public static int GetHandValue(ReadOnlyCollection<Card> hand)
{
bool soft;
return GetHandValue(hand, out soft);
}
/// <summary>
/// Gets the value of a hand and whether it is soft or not
/// </summary>
/// <param name="hand">The hand to evaluate</param>
/// <param name="soft">[OUT] Is the hand's value soft?</param>
/// <returns>The value of the specified hand</returns>
public static int GetHandValue(ReadOnlyCollection<Card> hand, out bool soft)
{
int total = 0;
foreach (Card c in hand)
{
total += s_cardValues[c.Value];
}
int aceCount = hand.Count(c => c.Value == CardName.Ace);
while (total > 21 && aceCount > 0)
{
total -= 10;
aceCount--;
}
soft = aceCount > 0;
return total;
}
/// <summary>
/// Checks if a hand is blackjack or not
/// </summary>
/// <param name="hand">The hand to evaluate</param>
/// <returns>Whether the hand is blackjack or not</returns>
public static bool IsBlackjack(Hand hand)
{
return !hand.IsSplit && hand.Cards.Count == 2 && hand.Cards.Count(c => c.Value == CardName.Ace) > 0 && GetHandValue(hand.Cards.AsReadOnly()) == 21;
}
}
}