Advertisement
ja72

R_Charp_Console_Jack

Mar 11th, 2024 (edited)
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.62 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace ConsoleJackApp
  5. {
  6.     using static Program;
  7.  
  8.     static class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             new JackGame();
  13.             AddSeparator();
  14.             AskForKey("Press any key to close the window.");
  15.         }
  16.  
  17.         public static void Header()
  18.         {
  19.             Console.ForegroundColor = ConsoleColor.Black;
  20.             Console.BackgroundColor = ConsoleColor.Cyan;
  21.             Console.WriteLine("Welcome to TERMINAL JACK!");
  22.             Console.ForegroundColor = ConsoleColor.Gray;
  23.             Console.BackgroundColor = ConsoleColor.Black;
  24.         }
  25.         public static void AddSeparator()
  26.         {
  27.             Console.ForegroundColor = ConsoleColor.White;
  28.             Console.WriteLine("--------------------");
  29.             Console.ForegroundColor = ConsoleColor.Gray;
  30.         }
  31.         public static ConsoleKeyInfo AskForKey(string message)
  32.         {
  33.             Console.WriteLine(message);
  34.             return Console.ReadKey();
  35.         }
  36.         public static string AskChoice(string message)
  37.         {
  38.             Console.ForegroundColor = ConsoleColor.White;
  39.             Console.WriteLine(message);
  40.             Console.ForegroundColor = ConsoleColor.Gray;
  41.             return Console.ReadLine().ToLower();
  42.         }
  43.         public static void ShowMessage(params string[] lines)
  44.             => ShowMessage(ConsoleColor.Gray, lines);
  45.  
  46.         public static void ShowMessage(ConsoleColor color, params string[] lines)
  47.         {
  48.             Console.ForegroundColor = color;
  49.             foreach (var item in lines)
  50.             {
  51.                 if (item!=null)
  52.                 {
  53.                     Console.WriteLine(item);
  54.                 }
  55.                 else
  56.                 {
  57.                     Console.WriteLine();
  58.                 }
  59.                 Console.ForegroundColor = ConsoleColor.Gray;
  60.             }
  61.         }
  62.  
  63.         public static void Delay(int delay = 1500)
  64.         {
  65.             Thread.Sleep(delay);
  66.         }
  67.  
  68.     }
  69.  
  70.     public class JackGame
  71.     {
  72.         const int maxDealerHands = 21;
  73.         const int maxPlayerHands = 5;
  74.         static readonly Random rng = new Random();
  75.  
  76.         bool startFinish = false;
  77.         int handTotal = 0;
  78.         int playerCard = 0;
  79.         readonly int[] dealer = new int[maxDealerHands];
  80.         readonly int[] hand = new int[maxPlayerHands];
  81.  
  82.         public JackGame()
  83.         {
  84.             Header();
  85.             AddSeparator();
  86.             AskForKey("Press any key to continue.");
  87.             ShowMessage(null, "You will now be dealt two cards.");
  88.             AddSeparator();
  89.             Hit();
  90.             Hit();
  91.             startFinish = true;
  92.             GetPlayerChoice();
  93.         }
  94.  
  95.         void Call()
  96.         {
  97.             int dealerTotal = 0;
  98.             ShowMessage(ConsoleColor.Yellow,
  99.                 $"Your total is {handTotal}",
  100.                 "The dealer will now draw cards.");
  101.             Delay();
  102.             for (int dealCard = 0; dealerTotal <= handTotal; dealCard++)
  103.             {
  104.                 dealer[dealCard] = rng.Next(1, 14);
  105.                 if (dealer[dealCard]>10)
  106.                 {
  107.                     dealer[dealCard] = 10;
  108.                 }
  109.                 dealerTotal += dealer[dealCard];
  110.                 Delay();
  111.                 ShowMessage($"Dealer card is {dealer[dealCard]}.");
  112.             }
  113.             if (dealerTotal > maxDealerHands)
  114.             {
  115.                 Delay();
  116.                 AddSeparator();
  117.                 ShowMessage(ConsoleColor.Green,
  118.                     "YOU WIN!",
  119.                     "Dealer busts!");
  120.             }
  121.             else
  122.             {
  123.                 Delay();
  124.                 AddSeparator();
  125.                 ShowMessage(ConsoleColor.Red,
  126.                     "GAME OVER!",
  127.                     "Dealer is closer.");
  128.  
  129.             }
  130.         }
  131.  
  132.         void GetPlayerChoice()
  133.         {
  134.             bool chosen = false;
  135.             while (!chosen)
  136.             {
  137.                 string choice = AskChoice("HIT or CALL?");
  138.                 if (choice == "hit")
  139.                 {
  140.                     chosen = true;
  141.                     Hit();
  142.                 }
  143.                 else if (choice == "call")
  144.                 {
  145.                     chosen = false;
  146.                     Call();
  147.                 }
  148.                 else
  149.                 {
  150.                     ShowMessage("Try Again.");
  151.                 }
  152.             }
  153.         }
  154.  
  155.         void Hit()
  156.         {
  157.             hand[playerCard] = rng.Next(1, 14);
  158.             if (hand[playerCard] > 10)
  159.             {
  160.                 hand[playerCard] = 10;
  161.             }
  162.             handTotal += hand[playerCard];
  163.             ShowMessage($"Your card is {hand[playerCard]}.");
  164.             if (playerCard == maxPlayerHands-1 && handTotal <= maxDealerHands)
  165.             {
  166.                 AddSeparator();
  167.                 ShowMessage(ConsoleColor.Green, "YOU WIN!",
  168.                 "You managed to draw five cards without busting.");
  169.             }
  170.             else if (handTotal > maxDealerHands)
  171.             {
  172.                 AddSeparator();
  173.                 ShowMessage(ConsoleColor.Red, "GAME OVER.",
  174.                 "You busted.");
  175.             }
  176.             else if (handTotal == maxDealerHands)
  177.             {
  178.                 AddSeparator();
  179.                 ShowMessage(ConsoleColor.Cyan, "YOU WIN!",
  180.                 "Your card total is 21!");
  181.             }
  182.             else if (startFinish)
  183.             {
  184.                 GetPlayerChoice();
  185.             }
  186.             playerCard++;
  187.         }
  188.     }
  189. }
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement