Question

In: Computer Science

I need a c# console program that lets a player choose to play poker or black...

I need a c# console program that lets a player choose to play poker or black jack against the computer.

Solutions

Expert Solution

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();
}
}
}


Related Solutions

C++ Please For this assignment, you will write a program that lets the user play against...
C++ Please For this assignment, you will write a program that lets the user play against the computer in a variation of the popular blackjack car game. In this variation of the game, two-six sided dice are used instead of cards. The dice are rolled, and the player tries to beat the computer's hidden total without going over 21. Here are some suggestions for the game's design: Each round of the game is performed as an iteration of a loop...
C# I need working code please Write a console application that accepts the following JSON as...
C# I need working code please Write a console application that accepts the following JSON as input: {"menu": { "header": "SVG Viewer", "items": [ {"id": "Open"}, {"id": "OpenNew", "label": "Open New"}, null, {"id": "ZoomIn", "label": "Zoom In"}, {"id": "ZoomOut", "label": "Zoom Out"}, {"id": "OriginalView", "label": "Original View"}, null, {"id": "Quality"}, {"id": "Pause"}, {"id": "Mute"}, null, {"id": "Find", "label": "Find..."}, {"id": "FindAgain", "label": "Find Again"}, {"id": "Copy"}, {"id": "CopyAgain", "label": "Copy Again"}, {"id": "CopySVG", "label": "Copy SVG"}, {"id": "ViewSVG", "label": "View...
Write a Java program that lets the user play a game where they must guess a...
Write a Java program that lets the user play a game where they must guess a number between zero and one hundred (0-100). The program should generate a random number between 0 and 100 and then the user will try to guess the number. The program should help the user guess the number (see details below). The program must allow the user five attempts to guess the number. Further Instruction: (a) Declare a variable diff and assign to it the...
I have this matlab program, and need to turn it into a C++ program. Can anyone...
I have this matlab program, and need to turn it into a C++ program. Can anyone help me with this? % Prompt the user for the values x and y x = input ('Enter the x coefficient: '); y = input ('Enter the y coefficient: '); % Calculate the function f(x,y) based upon % the signs of x and y. if x >= 0    if y >= 0        fun = x + y;    else        fun = x + y^2;    end...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack,...
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack, which is played between the user and an automated dealer as follows. The dealer shuffles a standard deck of 52 cards, draws two cards, and gives them to the user. The user can then choose to request another card from the dealer, adding it to their hand. The user can continue to request cards or choose to stop at any time. After each time...
PLease use c++ Write a checkbook balancing program. The program will read in, from the console,...
PLease use c++ Write a checkbook balancing program. The program will read in, from the console, the following for all checks that were not cashed as of the last time you balanced your checkbook: the number of each check (int), the amount of the check (double), and whether or not it has been cashed (1 or 0, boolean in the array). Use an array with the class as the type. The class should be a class for a check. There...
How to write a C++ program that lets the user enter a string and checks if...
How to write a C++ program that lets the user enter a string and checks if it is an accepted polynomial. Accepted polynomials need to have one term per degree, no parentheses, spaces ignored.
This problem needs to be solved with source code. I need a C++ program that will...
This problem needs to be solved with source code. I need a C++ program that will help me solve this question. I need it in C++, please. Writing with comments so it maybe cleared. 1.2. We received the following ciphertext which was encoded with a shift cipher: xultpaajcxitltlxaarpjhtiwtgxktghidhipxciwtvgtpilpit ghlxiwiwtxgqadds. 1. Perform an attack against the cipher based on a letter frequency count: How many letters do you have to identify through a frequency count to recover the key? What is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT