Advertisement
prampec

C# Blackjack

Sep 28th, 2019
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5.  
  6. namespace Peldak
  7. {
  8.     public class Blackjack
  9.     {
  10.         public struct Card
  11.         {
  12.             public string name;
  13.             public int value;
  14.  
  15.             public Card(string name, int value)
  16.             {
  17.                 this.name = name;
  18.                 this.value = value;
  19.             }
  20.         }
  21.         Card[] cards = new Card[] {
  22.             new Card("2", 2),
  23.             new Card("3", 3),
  24.             new Card("4", 4),
  25.             new Card("5", 5),
  26.             new Card("6", 6),
  27.             new Card("7", 7),
  28.             new Card("8", 8),
  29.             new Card("9", 9),
  30.             new Card("10", 10),
  31.             new Card("J", 10),
  32.             new Card("Q", 10),
  33.             new Card("K", 10),
  34.             new Card("A", 11)
  35.         };
  36.         Random rnd = new Random();
  37.         List<Card> cardsInHand = new List<Card>();
  38.         List<Card> cardsInDealerHand = new List<Card>();
  39.  
  40.         public void run()
  41.         {
  42.             deal();
  43.             printHand("Cards in dealer hand:", cardsInDealerHand);
  44.             while (true)
  45.             {
  46.                 int valueInHand = printHand("Cards in hand:", cardsInHand);
  47.                 if (valueInHand > 21)
  48.                 {
  49.                     Console.WriteLine("GAME OVER - You lost!");
  50.                     return;
  51.                 }
  52.                 bool needMoreCards = askPlayer();
  53.                 if (needMoreCards)
  54.                 {
  55.                     addCard(cardsInHand);
  56.                 }
  57.                 else
  58.                 {
  59.                     printHand("Cards in dealer hand:", cardsInDealerHand);
  60.                     while (true)
  61.                     {
  62.                         Thread.Sleep(1500);
  63.                         addCard(cardsInDealerHand);
  64.                         int valueInDealerHand = printHand("Cards in dealer hand:", cardsInDealerHand);
  65.                         if (valueInDealerHand > 21)
  66.                         {
  67.                             Console.WriteLine("GAME OVER - You WON!");
  68.                             return;
  69.                         }
  70.                         if (valueInDealerHand >= valueInHand)
  71.                         {
  72.                             Console.WriteLine("GAME OVER - You lost!");
  73.                             return;
  74.                         }
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.  
  80.         void deal()
  81.         {
  82.             addCard(cardsInHand);
  83.             addCard(cardsInHand);
  84.  
  85.             addCard(cardsInDealerHand);
  86.         }
  87.         void addCard(List<Card> hand)
  88.         {
  89.             int i = rnd.Next(cards.Length);
  90.             Card c = cards[i];
  91.             hand.Add(c);
  92.         }
  93.         int printHand(string message, List<Card> hand)
  94.         {
  95.             Console.Write(message);
  96.             int sum = 0;
  97.             for (int i = 0; i < hand.Count; i++)
  98.             {
  99.                 Console.Write(" " + hand[i].name);
  100.                 sum += hand[i].value;
  101.             }
  102.             if ((sum == 22) && (hand.Count == 2))
  103.             {
  104.                 sum = 21;
  105.             }
  106.             Console.WriteLine(" (" + sum + ")");
  107.             return sum;
  108.         }
  109.  
  110.         bool askPlayer()
  111.         {
  112.             Console.Write("Need more cards? (Y/N) ");
  113.             string answer = Console.ReadLine();
  114.             if ((answer == "Y") || (answer == "y"))
  115.             {
  116.                 return true;
  117.             }
  118.             return false;
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement