This is the second pass on this.
pass one
A single player versus a dealer. It does not perform bets - it only track wins and losses and has no splitting. Other than that I tried to get it all in.
I take it most people know Blackjack. Number are the number, face cards are 10, and ace is 1 or 11. Player has no rules about hitting (take a card). Dealer has to hit to 17. Closest to 21 without going over wins. Dealer wins a tie.
class Program
{
static void Main(string[] args)
{
BlackJack blackJack = new BlackJack();
blackJack.PlayGame();
}
public class Card
{
public enum CardRank { Two = 0, Three = 1, Four = 2, Five = 3, Six = 4, Seven = 5, Eight = 6, Nine = 7, Ten = 8, Jack = 9, Queen = 10, King = 11, Ace = 12 }
public enum CardSuit { Spade = 0, Club = 1, Heart = 2, Diamond = 3 }
private int i;
public CardRank Rank { get { return (CardRank)(i % 13); } }
public CardSuit Suit { get { return (CardSuit)(i / 13); } }
public int Value
{
get
{
int value;
if (i % 13 == 12)
{
value = 11;
}
else
{
value = i % 13 + 2;
if (value > 10)
{
value = 10;
}
}
return value;
}
}
public override string ToString() { return $"{Rank} {Suit}"; }
public Card(int I)
{
i = I;
}
}
public abstract class Seat
{
public List<Card> Cards { get; private set; } = new List<Card>();
public bool Busted { get { return Total > 21; } }
public bool HasBlackJack { get { return (Total == 21 && Cards.Count == 2); } }
public int Total
{
get
{
int total = 0;
int aceCount = 0;
foreach (Card c in Cards)
{
total += c.Value;
if (c.Rank == Card.CardRank.Ace)
{
aceCount++;
}
}
for (int i = 0; i < aceCount && total > 21; i++)
{
total -= 10;
}
return total;
}
}
public abstract bool CanHit { get; }
public virtual string DisplayCards
{
get
{
string cards = string.Join(", ", Cards);
return $"{cards} {Total}";
}
}
public void HitMe(Card C)
{
Cards.Add(C);
}
}
public class Dealer : Seat
{
public override bool CanHit { get { return Total <= 17; } }
public override string DisplayCards
{
get
{
if(Cards.Count == 2 & !HasBlackJack)
{
return $"{Cards[0].ToString()} {Cards[0].Value}";
}
else
{
return base.DisplayCards;
}
}
}
}
public class Player : Seat
{
public override bool CanHit { get { return Total < 21; } }
public int Wins { get; set; } = 0;
public int Loss { get; set; } = 0;
public string WinLoss { get { return $"Win {Wins} Loss {Loss}"; } }
}
public class BlackJack
{
private Random rand = new Random();
private List<Card> Deck;
private int deckPos = -1;
private Dealer dealer = new Dealer();
private Player player = new Player();
private void Shuffle()
{
deckPos = -1;
int swap;
Card temp;
for (int i = Deck.Count - 1; i > 0; i--)
{
swap = rand.Next(i + 1); //.net rand is not inclusive
if (swap != i)
{
temp = Deck[i];
Deck[i] = Deck[swap];
Deck[swap] = temp;
}
}
}
private Card Pop()
{
deckPos++;
return (Deck[deckPos]);
}
private Card Peek()
{
return (Deck[deckPos + 1]);
}
private void PlayHand()
{
if (deckPos > 30)
{
Shuffle();
}
dealer.Cards.Clear();
player.Cards.Clear();
Console.WriteLine();
Console.WriteLine();
Card card = Pop();
dealer.HitMe(card);
dealer.HitMe(Pop());
Console.WriteLine("Dealer");
Console.WriteLine(dealer.DisplayCards);
if (dealer.HasBlackJack)
{
Console.WriteLine("Dealer has blackjack");
player.Loss++;
}
else
{
Console.WriteLine("Player H to hit S to Stay");
player.HitMe(Pop());
player.HitMe(Pop());
Console.WriteLine(player.DisplayCards);
if (!player.HasBlackJack)
{
while (true)
{
string p = Console.ReadLine();
if (p == "H" || p == "h")
{
if (player.CanHit)
{
player.HitMe(Pop());
Console.WriteLine(player.DisplayCards);
if (player.Busted || player.Total == 21)
{
break;
}
}
else
{
break;
}
}
else if (p == "S" || p == "s")
{
break;
}
}
}
if (player.Busted)
{
Console.WriteLine("Player busted out dealer and wins");
player.Loss++;
}
else if (player.HasBlackJack)
{
player.Wins++;
Console.WriteLine("Player has Blackjack and wins");
}
else
{
Console.WriteLine("Dealer");
while (dealer.CanHit)
{
dealer.HitMe(Pop());
Console.WriteLine(dealer.DisplayCards);
if (dealer.Busted)
{
break;
}
}
if (dealer.Busted)
{
Console.WriteLine("Dealer busted out and player wins");
player.Wins++;
}
else if (dealer.Total >= player.Total)
{
Console.WriteLine("Dealer wins");
player.Loss++;
}
else
{
Console.WriteLine("Player wins");
player.Wins++;
}
}
}
Console.WriteLine(player.WinLoss);
}
public void PlayGame()
{
while (true)
{
Console.WriteLine("C to continue Q to quit");
string p = Console.ReadLine();
if (p == "C" || p == "c")
{
PlayHand();
}
else if (p == "Q" || p == "q")
{
Console.WriteLine("Bye");
break;
}
}
}
public BlackJack()
{
Deck = new List<Card>();
//deck will always have 52 cards
//card constuctor counts on that
for (int i = 0; i < 52; i++)
{
Card card = new Card(i);
Console.WriteLine(card);
Deck.Add(card);
}
Shuffle();
Console.WriteLine();
}
}
}