using System.Collections.Generic;
using System.Threading.Tasks;
public static void Main()
Task.Run(async () => await game.Start()).GetAwaiter().GetResult();
public static class CardGameConstants
public const int MaxCards = 5;
public const int BlackJack = 21;
public const int MinCardValue = 1;
public const int MaxCardValue = 11;
public const int DealerThinkTime = 1000;
private Random rnd = new Random();
private List<int> playerCards = new List<int>();
private int playerTotal = 0;
public async Task Start()
Console.WriteLine("Welcome to TERMINAL-JACK! - Press any key to continue");
Console.WriteLine("-------------------------");
Console.WriteLine("Press any key to close the window.");
private void DealInitialCards()
playerCards.Add(rnd.Next(CardGameConstants.MinCardValue, CardGameConstants.MaxCardValue));
playerCards.Add(rnd.Next(CardGameConstants.MinCardValue, CardGameConstants.MaxCardValue));
Console.WriteLine($"Your cards are {playerCards[0]} and {playerCards[1]}");
Console.WriteLine("-------------------------");
playerTotal = playerCards[0] + playerCards[1];
private async Task PlayerTurn()
bool isPlayerTurnOver = false;
while (!isPlayerTurnOver && playerCards.Count < CardGameConstants.MaxCards)
Console.WriteLine("HIT or CALL?");
string choice = Console.ReadLine()?.ToLower() ?? "";
if (Enum.TryParse(choice, true, out action))
int card = rnd.Next(CardGameConstants.MinCardValue, CardGameConstants.MaxCardValue);
Console.WriteLine($"You got a {card}");
if (playerTotal > CardGameConstants.BlackJack)
Console.WriteLine("-------------------------");
Console.WriteLine("GAME OVER; You've busted!");
else if (playerTotal == CardGameConstants.BlackJack)
Console.WriteLine("-------------------------");
Console.WriteLine("YOU WIN! Blackjack!");
Console.WriteLine("Invalid action. Please choose HIT or CALL.");
Console.WriteLine("Invalid input. Please type 'HIT' or 'CALL'.");
private async Task DealerTurn()
if (playerTotal <= CardGameConstants.BlackJack)
var dealer = new Dealer();
int dealerTotal = await dealer.Play(rnd, playerTotal);
Console.WriteLine($"The Dealer's total is {dealerTotal}");
Console.WriteLine("-------------------------");
if (dealerTotal > CardGameConstants.BlackJack)
Console.WriteLine("YOU WIN! Dealer busts!");
else if (dealerTotal > playerTotal)
Console.WriteLine("GAME OVER. Dealer is closer.");
Console.WriteLine("YOU WIN! You are closer to 21.");
public async Task<int> Play(Random rnd, int playerTotal)
int dealerTotal = rnd.Next(CardGameConstants.MinCardValue, CardGameConstants.MaxCardValue) + rnd.Next(CardGameConstants.MinCardValue, CardGameConstants.MaxCardValue);
while (dealerTotal <= playerTotal && dealerTotal < CardGameConstants.BlackJack)
await Task.Delay(CardGameConstants.DealerThinkTime);
dealerTotal += rnd.Next(CardGameConstants.MinCardValue, CardGameConstants.MaxCardValue);