Question

In: Computer Science

CSharp Simple BlackJack game Also known as 21. Two players (the user and computer) play against...

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!

Solutions

Expert Solution

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

Related Solutions

Java: Simple 21 Game (Blackjack) In this game, the dealer deals two "cards" to each player,...
Java: Simple 21 Game (Blackjack) In this game, the dealer deals two "cards" to each player, one hidden, so that only the player who gets it knows what it is, and one face up, so that everyone can see it. There are four players: one human player (user) and three computer players. The players take turns requesting cards, trying to get as close to 21 as possible, but not going over 21. A player may pass. Once a player has...
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...
Two players A and B play a game of dice . They roll a pair of...
Two players A and B play a game of dice . They roll a pair of dice alternately . The player who rolls 7 first wins . If A starts then find the probability of B winning the game ?
1. Write a program that plays a simple dice game between the computer and the user....
1. Write a program that plays a simple dice game between the computer and the user. When the program runs, it asks the user to input an even number in the interval [2..12], the number of plays. Display a prompt and read the input into a variable called ‘plays’ using input validation. Your code must ensure that the input always ends up being valid, i.e. even and in [2..12]. 2. A loop then repeats for ‘plays’ iterations. Do the following...
Blackjack, or 21, is a popular casino game that begins with each player and the dealer...
Blackjack, or 21, is a popular casino game that begins with each player and the dealer being dealt two cards. The value of each hand is determined by the point total of the cards in the hand. Face cards and 10s count 10 points, aces can be counted as either 1 or 11 points, and all other cards count at their face value. For instance, the value of a hand consisting of a jack and an 8 is 18; the...
Design and implement a Python program which will allow two players to play the game of...
Design and implement a Python program which will allow two players to play the game of Tic-Tac-Toe in a 4x4 grid! X | O | X | O -------------- O | O | X | O -------------- X | X | O | X -------------- X | X | O | X The rules for this game is the same as the classic, 3x3, game – Each cell can hold one of the following three strings: "X", "O", or "...
Two players play a game where they start with a row of n piles of varied...
Two players play a game where they start with a row of n piles of varied amounts of money. The players take turns and in each turn a player can pocket either the money in the first pile or the last pile in the row of piles that remains. Design an efficient algorithm (using dynamic programming), which on any given sequence of amounts, determines the maximum amount of money that player 1 can win. If n is even, prove that...
write on eclipse java Write a program named lab5 that will play a game of Blackjack...
write on eclipse java Write a program named lab5 that will play a game of Blackjack between the user and the computer. Create a second class named Card with the following: Define the following private instance variables: cardValue (int) & cardSuite (String) Write a constructor with no parameters that will Set cardValue to a random number between 1 and 13 Generate a second random number between 0 and 3 and assign cardSuite a string based on its value (0 –...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play the game of rock, paper, scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen...
Two players A and B play a dice game with a 6-face fair dice. Player A...
Two players A and B play a dice game with a 6-face fair dice. Player A is only allowed to roll the dice once. Player B is allowed to roll the dice maximally twice (that is, Player B can decide whether or not she or he would roll the dice again after seeing the value of the first roll). If the player with the larger final value of the dice wins, what is the maximal probability for Player B to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT