In: Computer Science
CSharp Simple BlackJack game
Also known as 21. Two players (the user and computer) play against each other. They are drawing poker cards in order to •Make the added rank (score) as close as to 21, but without going over 21.•And make the added score larger than the opponent’s.−Each player starts with two random cards. −Then ask the user if one more card until the user is satisfied with his/her total score. If the user’s total score gets 21, announce the user win.−If the user doesn’t want any more cards, the computer starts play. The computer must draw cards until it has a total score that beats the user. If the computer’s total score goes over 21, it loses.
Make this game look nice. Clear the console at effective times so the screen doesn’t get cluttered but the total scores for both the user and the computer are easily seen.−For simplicity, make assumptions: 1) draw with replacement; 2) Ace = 1 point, Jack=Queen=King=10 points.−Again, after one round, your game should ask the user if s/he wants another round. If no, program ends.
I don't even know where to begin. In the lectures we used <Lists> and for / for each commands. Help much needed!
Please find the below code in C# for a game of BLACKJACK:-
I have also included comments in the code for better understanding.
using System;
namespace BlackjackGame
{
class Blackjack
{
static string[] playerCards = new string[11]; //this string is to store the cards that the player has
static string hitOrStay = ""; //wheather the player wants another card or not
static int total = 0, count = 1, dealersTotal = 0; //the dealers total, count and users total
static Random ranCard = new Random(); //randomizer so that the cards are random
static void Main(string[] args)
{
Console.Title = "LET'S PLAY BLACKJACK!";
Start(); //invoke the start function
}
static void Start() //start function
{
dealersTotal = ranCard.Next(15, 22); //dealer getting two cards and summing it up
playerCards[0] = DealCard(); // invoking the deal function/method to get player the cards
playerCards[1] = DealCard(); //player's 2nd card
do
{
Console.WriteLine("Thanks for playing Blackjack! You were dealed " + playerCards[0] + " and " + playerCards[1] + ". \nYour total is " + total + ".\nWould you like to hit or stay?");
hitOrStay = Console.ReadLine().ToLower();
} while (!hitOrStay.Equals("hit") && !hitOrStay.Equals("stay"));
PlayGame();
}
static void PlayGame() //method in which we can hit or stay and also win or loose
{
if (hitOrStay.Equals("hit"))
{
GetAnotherCard();
}
else if (hitOrStay.Equals("stay"))
{
if (total > dealersTotal && total <= 21) //checking if the player won or lost
{
Console.WriteLine("\nCongrats! You won the game! The dealer's total was " + dealersTotal + ".\nplay again? y/n");
PlayAgain();
}
else if (total < dealersTotal) //checking if the player won or lost
{
Console.WriteLine("\nSorry, you lost! The dealer's total was " + dealersTotal + ".\nplay again? y/n");
PlayAgain();
}
}
Console.ReadLine();
}
static string DealCard() //deal function to deal the cards
{ //in the following switch statement we may change the values of the cards as we like
string Card = "";
int card = ranCard.Next(1, 14);
switch (card) //getting the total of the cards in each scenario using switch statement
{
case 1: Card = "Two"; total += 2; //here we check the value of the card against it's value
break;
case 2: Card = "Three"; total += 3;
break;
case 3: Card = "Four"; total += 4;
break;
case 4: Card = "Five"; total += 5;
break;
case 5: Card = "Six"; total += 6;
break;
case 6: Card = "Seven"; total += 7;
break;
case 7: Card = "Eight"; total += 8;
break;
case 8: Card = "Nine"; total += 9;
break;
case 9: Card = "Ten"; total += 10;
break;
case 10: Card = "Jack"; total += 10;
break;
case 11: Card = "Queen"; total += 10;
break;
case 12: Card = "King"; total += 10;
break;
case 13: Card = "Ace"; total += 1; //we assumed the value of ace as 1
break;
default: Card = "2"; total += 2;
break;
}
return Card;
}
static void GetAnotherCard() // when the player wants another card
{
count += 1;
playerCards[count] = DealCard(); //goto deal method
Console.WriteLine("\nYou were dealed a(n) " + playerCards[count] + ".\nYour new total is " + total + ".");
if (total.Equals(21))
{
Console.WriteLine("\nYou got Blackjack! The dealer's total was " + dealersTotal + ".\nWould you like to play again?");
PlayAgain();
}
else if (total > 21)
{
Console.WriteLine("\nYou busted, therefore you lost. Sorry. The dealer's total was " + dealersTotal + ".\nWould you like to play again? y/n");
PlayAgain();
}
else if (total < 21)
{
do
{
Console.WriteLine("\nWould you like to hit or stay?");
hitOrStay = Console.ReadLine().ToLower();
} while (!hitOrStay.Equals("hit") && !hitOrStay.Equals("stay"));
PlayGame(); //goto game method to check if the progress of the player
}
}
static void PlayAgain() //if the player decides to play again or not
{
string playAgain = "";
do
{
playAgain = Console.ReadLine().ToLower();
} while (!playAgain.Equals("y") && !playAgain.Equals("n"));
if (playAgain.Equals("y")) //player decides to keep on playing
{
// y means keep playing and n means quit
Console.WriteLine("\nPress enter to restart the Blackjack game!");
Console.ReadLine();
Console.Clear();
dealersTotal = 0;
count = 1;
total = 0;
Start();
}
else if (playAgain.Equals("n")) //player decides to quit
{
Console.WriteLine("\nPress enter to close Blackjack Game.");
Console.ReadLine();
Environment.Exit(0);
}
}
}
}