Created
March 17, 2018 12:12
-
-
Save lukepothier/36880cceb25219dc24c8151c3dad17ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace Blackjack | |
{ | |
internal class Player | |
{ | |
public string Name { get; set; } | |
public ICollection<Card> Hand { get; set; } | |
} | |
internal enum Card | |
{ | |
None, | |
Ace, | |
Two, | |
Three, | |
Four, | |
Five, | |
Six, | |
Seven, | |
Eight, | |
Nine, | |
Ten, | |
Jack, | |
Queen, | |
King | |
} | |
public class Program | |
{ | |
const int _maximumLegalHandValue = 21; | |
const int _minimumAutoWinHandSize = 5; | |
static Dictionary<Card, int[]> _cardValues | |
=> new Dictionary<Card, int[]> | |
{ | |
{ Card.None, new int[]{ 0 } }, | |
{ Card.Ace, new int[]{ 1, 11 } }, | |
{ Card.Two, new int[]{ 2 } }, | |
{ Card.Three, new int[]{ 3 } }, | |
{ Card.Four, new int[]{ 4 } }, | |
{ Card.Five, new int[]{ 5 } }, | |
{ Card.Six, new int[]{ 6 } }, | |
{ Card.Seven, new int[]{ 7 } }, | |
{ Card.Eight, new int[]{ 8 } }, | |
{ Card.Nine, new int[]{ 9 } }, | |
{ Card.Ten, new int[]{ 10 }}, | |
{ Card.Jack, new int[]{ 10 } }, | |
{ Card.Queen, new int[]{ 10 } }, | |
{ Card.King, new int[]{ 10 } } | |
}; | |
static void Main() | |
{ | |
var dealer = new Player | |
{ | |
Name = "the dealer", | |
Hand = new List<Card> | |
{ | |
Card.Six, | |
Card.Nine | |
} | |
}; | |
var andrew = new Player | |
{ | |
Name = "Andrew", | |
Hand = new List<Card> | |
{ | |
Card.Nine, | |
Card.Six, | |
Card.Jack | |
} | |
}; | |
var billy = new Player | |
{ | |
Name = "Billy", | |
Hand = new List<Card> | |
{ | |
Card.Queen, | |
Card.King | |
} | |
}; | |
var carla = new Player | |
{ | |
Name = "Carla", | |
Hand = new List<Card> | |
{ | |
Card.Two, | |
Card.Nine, | |
Card.King | |
} | |
}; | |
Play(dealer, andrew, billy, carla); | |
Console.Read(); | |
} | |
static void Play(Player dealer, params Player[] players) | |
{ | |
if (dealer == null) | |
throw new ArgumentException("A dealer must be provided."); | |
if (players == null || players.Length == 0) | |
throw new ArgumentException("At least one player must be provided."); | |
foreach (Player player in players) | |
Console.WriteLine(PlayerBeatsDealer(dealer.Hand, player.Hand) | |
? $"{player.Name} beats {dealer.Name}!" | |
: $"{player.Name} loses to {dealer.Name}!"); | |
} | |
static bool PlayerBeatsDealer(ICollection<Card> dealerHand, ICollection<Card> playerHand) | |
{ | |
if (dealerHand == null || dealerHand.Count == 0) | |
throw new ArgumentException("Dealer hand must contain cards."); | |
if (playerHand == null || playerHand.Count == 0) | |
throw new ArgumentException("Player hand must contain cards."); | |
var dealerHandValue = GetBestHandValue(dealerHand); | |
var playerHandValue = GetBestHandValue(playerHand); | |
if (playerHandValue > _maximumLegalHandValue) | |
return false; | |
if (playerHand.Count >= _minimumAutoWinHandSize) | |
return true; | |
return playerHandValue > dealerHandValue; | |
} | |
static int GetBestHandValue(ICollection<Card> cards) | |
{ | |
if (cards == null || cards.Count == 0) | |
return 0; | |
var total = 0; | |
var aceCount = 0; | |
foreach (Card card in cards) | |
{ | |
if (card == Card.Ace) | |
aceCount++; | |
total += _cardValues[card][0]; | |
} | |
if (total >= _maximumLegalHandValue || aceCount == 0) | |
return total; | |
for (var i = 0; i < aceCount; i++) | |
if (total + _cardValues[Card.Ace][1] <= _maximumLegalHandValue) | |
total += _cardValues[Card.Ace][1] - _cardValues[Card.Ace][0]; | |
return total; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment