Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace ConsoleJackApp
- {
- using static Program;
- static class Program
- {
- static void Main(string[] args)
- {
- new JackGame();
- AddSeparator();
- AskForKey("Press any key to close the window.");
- }
- public static void Header()
- {
- Console.ForegroundColor = ConsoleColor.Black;
- Console.BackgroundColor = ConsoleColor.Cyan;
- Console.WriteLine("Welcome to TERMINAL JACK!");
- Console.ForegroundColor = ConsoleColor.Gray;
- Console.BackgroundColor = ConsoleColor.Black;
- }
- public static void AddSeparator()
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine("--------------------");
- Console.ForegroundColor = ConsoleColor.Gray;
- }
- public static ConsoleKeyInfo AskForKey(string message)
- {
- Console.WriteLine(message);
- return Console.ReadKey();
- }
- public static string AskChoice(string message)
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine(message);
- Console.ForegroundColor = ConsoleColor.Gray;
- return Console.ReadLine().ToLower();
- }
- public static void ShowMessage(params string[] lines)
- => ShowMessage(ConsoleColor.Gray, lines);
- public static void ShowMessage(ConsoleColor color, params string[] lines)
- {
- Console.ForegroundColor = color;
- foreach (var item in lines)
- {
- if (item!=null)
- {
- Console.WriteLine(item);
- }
- else
- {
- Console.WriteLine();
- }
- Console.ForegroundColor = ConsoleColor.Gray;
- }
- }
- public static void Delay(int delay = 1500)
- {
- Thread.Sleep(delay);
- }
- }
- public class JackGame
- {
- const int maxDealerHands = 21;
- const int maxPlayerHands = 5;
- static readonly Random rng = new Random();
- bool startFinish = false;
- int handTotal = 0;
- int playerCard = 0;
- readonly int[] dealer = new int[maxDealerHands];
- readonly int[] hand = new int[maxPlayerHands];
- public JackGame()
- {
- Header();
- AddSeparator();
- AskForKey("Press any key to continue.");
- ShowMessage(null, "You will now be dealt two cards.");
- AddSeparator();
- Hit();
- Hit();
- startFinish = true;
- GetPlayerChoice();
- }
- void Call()
- {
- int dealerTotal = 0;
- ShowMessage(ConsoleColor.Yellow,
- $"Your total is {handTotal}",
- "The dealer will now draw cards.");
- Delay();
- for (int dealCard = 0; dealerTotal <= handTotal; dealCard++)
- {
- dealer[dealCard] = rng.Next(1, 14);
- if (dealer[dealCard]>10)
- {
- dealer[dealCard] = 10;
- }
- dealerTotal += dealer[dealCard];
- Delay();
- ShowMessage($"Dealer card is {dealer[dealCard]}.");
- }
- if (dealerTotal > maxDealerHands)
- {
- Delay();
- AddSeparator();
- ShowMessage(ConsoleColor.Green,
- "YOU WIN!",
- "Dealer busts!");
- }
- else
- {
- Delay();
- AddSeparator();
- ShowMessage(ConsoleColor.Red,
- "GAME OVER!",
- "Dealer is closer.");
- }
- }
- void GetPlayerChoice()
- {
- bool chosen = false;
- while (!chosen)
- {
- string choice = AskChoice("HIT or CALL?");
- if (choice == "hit")
- {
- chosen = true;
- Hit();
- }
- else if (choice == "call")
- {
- chosen = false;
- Call();
- }
- else
- {
- ShowMessage("Try Again.");
- }
- }
- }
- void Hit()
- {
- hand[playerCard] = rng.Next(1, 14);
- if (hand[playerCard] > 10)
- {
- hand[playerCard] = 10;
- }
- handTotal += hand[playerCard];
- ShowMessage($"Your card is {hand[playerCard]}.");
- if (playerCard == maxPlayerHands-1 && handTotal <= maxDealerHands)
- {
- AddSeparator();
- ShowMessage(ConsoleColor.Green, "YOU WIN!",
- "You managed to draw five cards without busting.");
- }
- else if (handTotal > maxDealerHands)
- {
- AddSeparator();
- ShowMessage(ConsoleColor.Red, "GAME OVER.",
- "You busted.");
- }
- else if (handTotal == maxDealerHands)
- {
- AddSeparator();
- ShowMessage(ConsoleColor.Cyan, "YOU WIN!",
- "Your card total is 21!");
- }
- else if (startFinish)
- {
- GetPlayerChoice();
- }
- playerCard++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement