In: Computer Science
I need a c# console program that lets a player choose to play poker or black jack against the computer.
Here is the caode for blackjack game..here,there are four codes
blackjack.cs
Using System
using System.Text;
Using System.Collections.generic;
using System.Threading.Tasks;
using System.Linq;
namespace BlackjackGame
{
public class Casino
{
private static string versionCode = "1.0";
public static int MinimumBet { get; } = 10;
public static string GetVersionCode()
{
return versionCode;
}
public static bool IsHandBlackjack(List<Card> hand)
{
if (hand.Count == 2)
{
if (hand[0].Face == Face.Ace && hand[1].Value == 10)
{
return true;
}
else if (hand[1].Face == Face.Ace && hand[0].Value == 10)
{
return true;
}
}
return false;
}
public static void ResetColor()
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.BackgroundColor = ConsoleColor.Black;
}
}
public class Player
{
public int Chips { get; set; } = 500;
public int Bet { get; set; }
public int Wins { get; set; }
public int HandsCompleted { get; set; } = 1;
public List<Card> Hand { get; set; }
public void AddBet(int bet)
{
Bet += bet;
Chips -= bet;
}
public void ClearBet()
{
Bet = 0;
}
public void ReturnBet()
{
Chips += Bet;
ClearBet();
}
public int WinBet(bool blackjack)
{
int chipsWon;
if (blackjack)
{
chipsWon = (int) Math.Floor(Bet * 1.5);
}
else
{
chipsWon = Bet * 2;
}
Chips += chipsWon;
ClearBet();
return chipsWon;
}
public int GetHandValue()
{
int value = 0;
foreach(Card card in Hand)
{
value += card.Value;
}
return value;
}
public void WriteHand()
{
Console.Write("Bet: ");
Console.ForegroundColor = ConsoleColor.black;
Console.Write(Bet + " ");
Casino.ResetColor();
Console.Write("Chips: ");
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(Chips + " ");
Casino.ResetColor();
Console.Write("Wins: ");
Console.ForegroundColor = ConsoleColor.red;
Console.WriteLine(Wins);
Casino.ResetColor();
Console.WriteLine("Round #" + HandsCompleted);
Console.WriteLine();
Console.WriteLine("Your Hand (" + GetHandValue() + "):");
foreach (Card card in Hand)
{
card.WriteDescription();
}
Console.WriteLine();
}
}
public class Dealer
{
public static List<Card> HiddenCards { get; set; } = new List<Card>();
public static List<Card> RevealedCards { get; set; } = new List<Card>();
public static void RevealCard()
{
RevealedCards.Add(HiddenCards[0]);
HiddenCards.RemoveAt(0);
}
public static int GetHandValue()
{
int value=0;
foreach (Card card in RevealedCards)
{
value += card.Value;
}
return value;
}
public static void WriteHand()
{
Console.WriteLine("Dealer's Hand (" + GetHandValue() + "):");
foreach (Card card in RevealedCards)
{
card.WriteDescription();
}
for (int i = 0; i < HiddenCards.Count; i++)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("<hidden>");
Casino.ResetColor();
}
Console.WriteLine();
}
}
}
Using System
using System.Text;
Using System.Collections.generic;
using System.Threading;
namespace BlackJackGaming
{
class program
{
private static Deck deck = new Deck();
private static Player player = new Player();
private enum RoundResult
{
PUSH,
PLAYER_WIN,
DEALER_WIN,
PLAYER_BUST,
PLAYER_BLACKJACK,
SURRENDER,
INVALID_BET
}
// Initialization
static void InitializeHands()
{
deck.Initialize();
player.Hand = deck.DealHand();
Dealer.HiddenCards = deck.DealHand();
Dealer.RevealedCards = new List<Card>();
// Next we have to check if the card contains two aces, make them one hard
if (player.Hand[0].Face == Face.Ace && player.Hand[1].Face == Face.Ace)
{
player.Hand[1].Value = 1;
}
if (Dealer.HiddenCards[0].Face == Face.Ace && Dealer.HiddenCards[1].Face == Face.Ace)
{
Dealer.HiddenCards[1].Value = 1;
}
Dealer.RevealCard();
player.WriteHand();
Dealer.WriteHand();
}
static void StartTheRound()
{
Console.clear();
InitializeHands();
TakeActions();
Dealer.RevealCard();
player.WriteHand();
Dealer.WriteHand();
player.HandsCompleted++;
if (player.Hand.Count == 0)
{
EndRound(RoundResult.SURRENDER);
return;
}
else if (player.GetHandValue() > 21)
{
EndRound(RoundResult.PLAYER_BUST);
return;
}
while (Dealer.GetHandValue() <= 16)
{
Thread.Sleep(1000);
Dealer.RevealedCards.Add(deck.DrawCard());
Console.clear();
player.WriteHand();
Dealer.WriteHand();
}
if (player.GetHandValue() > Dealer.GetHandValue())
{
player.Wins++;
if (Casino.IsHandBlackjack(player.Hand))
{
EndRound(RoundResult.PLAYER_BLACKJACK);
}
else
{
EndRound(RoundResult.PLAYER_WIN);
}
}
else if (Dealer.GetHandValue() > 21)
{
player.Wins++;
EndRound(RoundResult.PLAYER_WIN);
}
else if (Dealer.GetHandValue() > player.GetHandValue())
{
EndRound(RoundResult.DEALER_WIN);
}
else
EndRound(RoundResult.PUSH);
}
static void actions()
{
string action;
do
{
Console.clear();
player.WriteHand();
Dealer.WriteHand();
Console.Write("Enter Action");
Console.ForegroundColor = ConsoleColor.Cyan;
action = Console.ReadLine();
Casino.ResetColor();
switch (action.ToUpper())
{
case "hit":
player.Hand.Add(deck.DrawCard());
break;
case "surrender":
player.Hand.Clear();
break;
case "stand":
break;
case "double":
if (player.Chips <= player.Bet)
{
player.AddBet(player.Chips);
}
else
{
player.AddBet(player.Bet);
}
player.Hand.Add(deck.DrawCard());
break;
default:
Console.WriteLine("Moves are valid");
Console.WriteLine("Hit, Stand, Surrender, Double");
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
break;
}
if (player.GetHandValue() > 21)
{
foreach (Card card in player.Hand)
{
if (card.Value == 11)
{
card.value=1;
break;
}
}
}
}
while (!action.ToUpper().Equals("STAND") && !action.ToUpper().Equals("DOUBLE")&& !action.ToUpper().Equals("SURRENDER") && player.GetHandValue() <= 21);
}
static bool TakeBet()
{
Console.Write("Current Chip Count: ");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(player.Chips);
Casino.ResetColor();
Console.Write("Minimum Bet: ");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Casino.MinimumBet);
Casino.ResetColor();
Console.Write("Enter bet to begin hand " + player.HandsCompleted + ": ");
Console.ForegroundColor = ConsoleColor.Magenta;
string s = Console.ReadLine();
Casino.ResetColor();
if (Int32.TryParse(s, out int bet) && bet >= Casino.MinimumBet && player.Chips >= bet)
{
player.AddBet(bet);
return true;
}
return false;
}
static void EndRound(RoundResult result){
switch (result)
{
case RoundResult.PUSH:
player.ReturnBet();
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("Player and Dealer Push.");
break;
case RoundResult.PLAYER_WIN:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Player Wins " + player.WinBet(false) + " chips");
break;
case RoundResult.PLAYER_BUST:
player.ClearBet();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Player Busts");
break;
case RoundResult.PLAYER_BLACKJACK:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Player Wins " + player.WinBet(true) + " chips with Blackjack.");
break;
case RoundResult.DEALER_WIN:
player.ClearBet();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Dealer Wins.");
break;
case RoundResult.SURRENDER:
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine("Player Surrenders " + (player.Bet / 2) + " chips");
player.Chips += player.Bet / 2;
player.ClearBet();
break;
case RoundResult.INVALID_BET:
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Invalid Bet.");
break;
}
if (player.Chips <= 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine("You ran out of Chips after " + (player.HandsCompleted - 1) + " rounds.");
Console.WriteLine("500 Chips will be added and your statistics have been reset.");
player = new Player();
}
Casino.ResetColor();
Console.WriteLine("Press any key to continue");
Console.ReadKey();
StartRound();
}
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
Casino.ResetColor();
Console.Title = "Blackjack";
Console.WriteLine(" Welcome to Blackjack " + Casino.GetVersionCode());
Console.WriteLine("Press any key to play.");
Console.ReadKey();
StartRound();
}
The above code is casino.cs Contains the rules of the house as well as player and dealer classes
DECK.CS
This code contains the code for the Deck--drawing cards and shuffling
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlackjackGaming
{
public class Deck
{
private List<Card> cards;
public Deck()
{
intialize();
}
public List<Card> GetColdDeck()
{
List<Card> coldDeck = new List<Card>();
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 4; j++)
{
coldDeck.Add(new Card((Suit)j, (Face)i));
}
}
return coldDeck;
}
public List<Card> DealHand()
{
List<Card> hand = new List<Card>();
hand.Add(cards[0]);
hand.Add(cards[1]);
cards.RemoveRange(0, 2);
return hand;
}
public Card DrawCard()
{
Card card = cards[0];
cards.Remove(card);
return card;
}
public void Shuffle()
{
Random rng = new Random();
int n = cards.Count;
while(n > 1)
{
n--;
int k = rng.Next(n + 1);
Card card = cards[k];
cards[k] = cards[n];
cards[n] = card;
}
}
public void Initialize()
{
cards = GetColdDeck();
Shuffle();
}
}
}
CARD.CS-
This code contains the code for the card class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Blackjack.Suit;
using static Blackjack.Face;
namespace BlackJackGame
{
public enum suit
{
Clubs,
Spades,
Diamonds,
Hearts
}
public enum face
{
Ace,
two,
three,
four,
five,
six,
seven,
eight,
nine,
ten,
queen,
king,
jack
}
public class Card
{
public Suit Suit { get; }
public Face Face { get; }
public int Value { get; set; }
public char Symbol { get; }
public Card(Suit suit, Face face)
{
suit=suit;
face=face;
switch(suit)
{
case Clubs:
Symbol = '♣';
break;
case Spades:
Symbol = '♠';
break;
case Diamonds:
Symbol = '♦';
break;
case Hearts:
Symbol = '♥';
break;
}
switch(face)
{
case jack;
case queen;
case king:
value=10;
break;
case Ace:
value=11;
break;
default:
Value = (int)Face + 1;
break;
public void WriteDescription()
{
if (Suit == Suit.Diamonds || Suit == Suit.Hearts)
{
Console.ForegroundColor = ConsoleColor.Red;
}
else
{
Console.ForegroundColor = ConsoleColor.White;
}
if (Face == Ace)
{
if(value==11)
{
Console.WriteLine(Symbol + " Soft " + Face + " of " + Suit);
}
else
{
Console.WriteLine(Symbol + " Hard " + Face + " of " + Suit);
}
}
else
{
Console.WriteLine(Symbol + " " + Face + " of " + Suit);
}
Casino.ResetColor();
}
}
}