private static void DealerPlay(Hand DealerHand, Hand PlayerHand, Deck CardDeck) { Console.WriteLine($"\nDealers Hand: {DealerHand.ToString()} - Value: {DealerHand.BlackJackValue}"); if (DealerHand.BlackJackValue >= PlayerHand.BlackJackValue) { Console.WriteLine("Dealer wins (no additional cards needed)."); } else { while (DealerHand.BlackJackValue < 17 && DealerHand.BlackJackValue < 22) { Card card = CardDeck.Draw(); if (card != null) { // We have a valid card DealerHand.Add(card); Console.WriteLine($"\nDealers Hand: {DealerHand.ToString()} - Value: {DealerHand.BlackJackValue}"); } else { // Out of cards Console.WriteLine("Out of cards???"); throw new Exception("Out of cards."); } } if (DealerHand.BlackJackValue > 21) { Console.WriteLine("Dealer busts. You win!"); } else { if (DealerHand.BlackJackValue == PlayerHand.BlackJackValue) { Console.WriteLine("Tie goes to dealers. Dealer wins.."); } else { if (DealerHand.BlackJackValue > PlayerHand.BlackJackValue) { Console.WriteLine("Dealer wins."); } else { Console.WriteLine("Player wins."); } } } } }
public void Deck_newDrawsOneContains51Cards() { Deck deck = new Deck(); deck.Draw(); Assert.AreEqual(51, deck.CardsInDeck); }
private static void Stats() { int maxRoyalFlush = 10; Console.Write("Royal Flushes to get: "); maxRoyalFlush = Console.ReadLine().ToIntegerOrDefault(-1); Deck deck = new Deck(); deck.CreateDefaultDeck(false); deck.Shuffle(); var cards = deck.Draw(7).ToList(); int tryCount = 0; int highCard = 0; int pair = 0; int twoPair = 0; int threeKind = 0; int fullHouse = 0; int straight = 0; int flush = 0; int straightFlush = 0; int fourKind = 0; int royalFlush = 0; DateTime startTime = DateTime.Now; while (royalFlush < maxRoyalFlush) { tryCount++; //for (int i = 0; i < cards.Count; i++) //{ // Console.Write(cards[i].ToString()); // if (i < cards.Count - 1) // { // Console.Write(','); // } // else // { // Console.WriteLine(); // } //} switch (Poker.GetHand(cards)) { case Poker.Hands.Flush: flush++; break; case Poker.Hands.FourKind: fourKind++; break; case Poker.Hands.FullHouse: fullHouse++; break; case Poker.Hands.Pair: pair++; break; case Poker.Hands.RoyalFlush: royalFlush++; break; case Poker.Hands.Straight: straight++; break; case Poker.Hands.StraightFlush: straightFlush++; break; case Poker.Hands.ThreeKind: threeKind++; break; case Poker.Hands.TwoPair: twoPair++; break; case Poker.Hands.HighCard: highCard++; break; } Console.SetCursorPosition(0, 0); Console.WriteLine(string.Format("Pairs: {0} ({1})\r\n2 Pairs: {2} ({3})\r\nPairs of 3: {4} ({5})\r\nPairs of 4: {6} ({7})\r\nFull Houses: {8} ({9})\r\nFlushes: {10} ({11})\r\nStraights: {12} ({13})\r\nStraight Flushes: {14} ({15})\r\nRoyal Flushes: {16} ({17})\r\nHigh Card: {18} ({19})", pair, ((decimal)pair / (decimal)tryCount).ToString("P9"), twoPair, ((decimal)twoPair / (decimal)tryCount).ToString("P9"), threeKind, ((decimal)threeKind / (decimal)tryCount).ToString("P9"), fourKind, ((decimal)fourKind / (decimal)tryCount).ToString("P9"), fullHouse, ((decimal)fullHouse / (decimal)tryCount).ToString("P9"), flush, ((decimal)flush / (decimal)tryCount).ToString("P9"), straight, ((decimal)straight / (decimal)tryCount).ToString("P9"), straightFlush, ((decimal)straightFlush / (decimal)tryCount).ToString("P9"), royalFlush, ((decimal)royalFlush / (decimal)tryCount).ToString("P9"), highCard, ((decimal)highCard / (decimal)tryCount).ToString("P9"))); deck.Shuffle(); cards = deck.Draw(7).ToList(); } TimeSpan difference = DateTime.Now - startTime; Console.WriteLine(string.Format("Took {0} tries.", tryCount)); Console.WriteLine(string.Format("Took {0} seconds ({1}/second)", difference.TotalSeconds, ((decimal)tryCount / (decimal)difference.TotalSeconds).ToString("F5"))); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); StringBuilder sb = new StringBuilder(); sb.Append(string.Format("Pairs: {0} ({1})\r\n2 Pairs: {2} ({3})\r\nPairs of 3: {4} ({5})\r\nPairs of 4: {6} ({7})\r\nFull Houses: {8} ({9})\r\nFlushes: {10} ({11})\r\nStraights: {12} ({13})\r\nStraight Flushes: {14} ({15})\r\nRoyal Flushes: {16} ({17})\r\nHigh Card: {18} ({19})", pair, ((decimal)pair / (decimal)tryCount).ToString("P9"), twoPair, ((decimal)twoPair / (decimal)tryCount).ToString("P9"), threeKind, ((decimal)threeKind / (decimal)tryCount).ToString("P9"), fourKind, ((decimal)fourKind / (decimal)tryCount).ToString("P9"), fullHouse, ((decimal)fullHouse / (decimal)tryCount).ToString("P9"), flush, ((decimal)flush / (decimal)tryCount).ToString("P9"), straight, ((decimal)straight / (decimal)tryCount).ToString("P9"), straightFlush, ((decimal)straightFlush / (decimal)tryCount).ToString("P9"), royalFlush, ((decimal)royalFlush / (decimal)tryCount).ToString("P9"), highCard, ((decimal)highCard / (decimal)tryCount).ToString("P9"))); sb.Append("\r\n").Append(string.Format("Took {0} tries.", tryCount)).Append("\r\n"); sb.Append(string.Format("Took {0} seconds ({1}/second)", difference.TotalSeconds, ((decimal)tryCount / (decimal)difference.TotalSeconds).ToString("F5"))).Append("\r\n\r\n"); SaveFile(sb.ToString()); }
static void Main(string[] args) { Deck mystack = new Deck(); List <Card> discard = new List <Card>(); List <Card> hand = new List <Card>(); List <Card> hand2 = new List <Card>(); int selection; do { Console.WriteLine("1) Shuffle"); Console.WriteLine("2) Discard the top card in the deck"); Console.WriteLine("3) Draw"); Console.WriteLine("4) Print Deck"); Console.WriteLine("5) Print Discard Pile"); Console.WriteLine("6) Print Hand 1"); Console.WriteLine("7) Print Hand 2"); Console.WriteLine("8) Exit"); Console.WriteLine("What do you want to do?"); selection = int.Parse(Console.ReadLine()); if (selection == 1) { discard.Clear(); mystack.Shuffle(); } if (selection == 2) { discard.Add(mystack.discarded()); } if (selection == 3) { Console.WriteLine("Which hand do you want to put the card in? (1 or 2)"); int handwhich = int.Parse(Console.ReadLine()); if (handwhich == 1) { hand.Add(mystack.Draw()); } if (handwhich == 2) { hand2.Add(mystack.Draw()); } } if (selection == 4) { mystack.PrintDeck(); } if (selection == 5) { Console.WriteLine("The discard pile: "); foreach (Card d in discard) { d.Print(); } } if (selection == 6) { Console.WriteLine("Your hand: "); foreach (Card h in hand) { h.Print(); } } if (selection == 7) { Console.WriteLine("Your hand 2: "); foreach (Card h in hand2) { h.Print(); } } } while (selection != 8); }
public void Draw(Deck deck) { handcards.Add(deck.Draw()); }
// Rules from // https://www.bicyclecards.com/how-to-play/blackjack/ static void Main(string[] args) { Deck deck = new Deck(); deck.shuffle(); // Deal cards Hand playerHand = new Hand(); Hand dealerHand = new Hand(); playerHand.Add(Card: deck.Draw()); dealerHand.Add(Card: deck.Draw()); playerHand.Add(Card: deck.Draw()); dealerHand.Add(Card: deck.Draw()); // Show initial card info (first dealer card face up) Console.WriteLine($"Dealer shows: {dealerHand.Cards[index: 0].ToString()}"); Console.WriteLine($"Initial Draw:\n{playerHand.ToString()} - Value: {playerHand.BlackJackValue}"); // Check for tie (Natural 21 for both player and dealer) if (playerHand.BlackJackValue == 21 && dealerHand.BlackJackValue == 21) { Console.WriteLine($"\nDealers Hand: {dealerHand.ToString()} - Value: {dealerHand.BlackJackValue}"); Console.WriteLine($"Player and dealer both have natural Blackjacks (Stand-off). Tie."); } else { if (playerHand.BlackJackValue == 21) { Console.WriteLine($"\nDealers Hand: {dealerHand.ToString()} - Value: {dealerHand.BlackJackValue}"); Console.WriteLine("Natural Blackjack for player. Player wins!"); } else { if (dealerHand.BlackJackValue == 21) { Console.WriteLine($"\nDealers Hand: {dealerHand.ToString()} - Value: {dealerHand.BlackJackValue}"); Console.WriteLine("Natural Blackjack for dealer. Dealer wins!"); } else { // Loop if player want more cards and isnt bust Console.Write("\n\nPress H for another card (any other key to stay): "); ConsoleKeyInfo key; key = Console.ReadKey(); Console.WriteLine(); while (key.KeyChar.ToString().ToUpper() == "H" && playerHand.BlackJackValue < 22) { Card card = deck.Draw(); if (card != null) { // We have a valid card playerHand.Add(Card: card); Console.WriteLine($"{playerHand.ToString()} - Value: {playerHand.BlackJackValue}"); } else { // This should never happen Console.WriteLine("Out of cards! Game over."); throw new Exception("Out of cards"); } if (playerHand.BlackJackValue < 22) { Console.Write("Press H for another card (any other key to stay): "); key = Console.ReadKey(); Console.WriteLine(); } } if (playerHand.BlackJackValue > 21) { Console.WriteLine("Over 21 bust. You loose."); } else { // Dealer Play Logic here DealerPlay(DealerHand: dealerHand, PlayerHand: playerHand, CardDeck: deck); } } } } Console.WriteLine("\n\nGame over"); Console.ReadKey(); }