Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask C/C++ Expert


Home >> C/C++

#include

#include

#include

#include

#include

//*Variables Used in Programs*//

int k;

int l;

int d;

int won;

int loss;

int cash = 500;

int bet;

int random_card;

int player_total=0;

int dealer_total;

*//Functions Used in Program*//

int randcard();

int betting();

void asktitle();

void rules();

void play();

void dealer();

void stay();

void cash_test();

void ask_over();

void fileresults();

//*Main Function*//

int main(void)

{

            asktitle();

            printf("\n");

            system ("pause");

            return(0);

} //* End Program*//

void asktitle() //* Function for asking player if they want to continue*//

{

            char choice1;

            int choice2;

            printf("\n Are you ready to play?\n");

            printf("\n (Y/N)\n ");

            scanf("\n%c",&choice1);

            while((choice1!='Y') && (choice1!='y') && (choice1!='N') && (choice1!='n')) //* If invalid choice entered*//

            {                                                                          

                        printf("\n");

                        printf("Invalid Choice. Please Enter Y for Yes or N for No.\n");

                        scanf("%c",&choice1);

            }

           if((choice1 == 'Y') || (choice1 == 'y')) //* If yes, continue.*//

            {

            system("cls");

            printf("\nEnter 1 to Start the Game of BlackJack.");

                            printf("\nEnter 2 to Read the Rules.");

                            printf("\nEnter 3 to Exit Game.");

                            printf("\nChoice: ");

                            scanf("%d", &choice2); //* Prompts user for choice*//

                            if((choice2<1) || (choice2>3)) //* If invalid choice entered*//

                            {

                printf("\nInvalid Choice. Please enter 1, 2 or 3\n");

                scanf("%d", &choice2);

            }

            switch(choice2) //* Switch case for different choices*//

            {  

                case 1: //* Case to start game*//

                   system("cls");

                   play();

                   break;

                case 2: //* Case to see rules*//

                   system("cls");

                   rules();

                   break;

                case 3: //* Case to exit game*//

                   printf("\nPlay again soon!\n\n");

                   exit(0);

                   break;

                default:

                   printf("\nInvalid Input");

            } //* End switch case*//

            } //* End if loop*//

            else if((choice1 == 'N') || (choice1 == 'n')) //* If no, exit program*//

            {

        printf("\nPlay again soon!\n\n");

        exit(0);

            }

            return;

} //* End function*//

void rules() //*Prints "Rules of Blackjack" list*//

{

     char choice1;

     int choice2;

     printf("\n           RULES of BLACKJACK");

     printf("\n          ---------------------------");

     printf("\nI.");

     printf("\n     This program generates cards at random.");

     printf("\nII.");

     printf("\n     Each card has a value.");

     printf("\n     Number cards 1 to 10 hold a value of their number.");

     printf("\n     J, Q, and K cards hold a value of 10.");

     printf("\n     Ace cards hold a value of 11");

     printf("\n     The goal of this game is to reach a card value total of 21.\n");

     printf("\nIII.");

     printf("\n     After the dealing of the first two cards, YOU must decide whether to HIT or STAY.");

     printf("\n     Staying will keep you safe, hitting will add a card.");

     printf("\n     Because you are competing against the dealer, you must beat his hand.");

     printf("\n     If your total goes over 21, you will LOSE!.");

     printf("\n     If you lose,  you can always play again.\n");

     printf("\n     YOUR RESULTS ARE RECORDED AND FOUND IN SAME FOLDER AS PROGRAM\n");

     printf("\nWould you like to go the previous screen?");

     printf("\n                  (Y/N)\n                    ");

     scanf("\n%c",&choice1);

     while((choice1!='Y') && (choice1!='y') && (choice1!='N') && (choice1!='n')) //* If invalid choice entered*//

        {

                printf("\n");

                printf("Incorrect Choice. Please Enter Y for Yes or N for No.\n");

                scanf("%c",&choice1);

        }

        if((choice1 == 'Y') || (choice1 == 'y')) //* If yes, continue. Prints menu.*//

        {

            system("cls");

            asktitle();

        } //* End if loop*//

int randcard() //*Generates random card*//

{

     srand((unsigned) time(NULL)); //Generates random seed for rand()

function*//

     random_card = rand()%4+1;

     if(random_card==1)

     {  

         clubcard();

         l=k;

     }

    if(random_card==2)

     {

         diamondcard();

         l=k;

     }

     if(random_card==3)

     {

         heartcard();

         l=k;

     }

     if(random_card==4)

     {

         spadecard();

         l=k;

     }   

     return l;

} //* End Function*//  

void play() //*Plays game*//

{

     int p=0; //* holds value of player_total*//

     int i=1; //* counter for asking user to hold or stay (aka game turns)*// 

     char choice3;

     cash = cash;

     cash_test();

     printf("\nCash: $%d\n",cash); //*Prints amount of cash user has*//

     randcard(); //*Generates random card*//

     player_total = p + l; //*Computes player total*//

     p = player_total;

     printf("\nYour Total is %d\n", p); //*Prints player total*//

     dealer(); //*Computes and prints dealer total*//

     betting(); //*Prompts user to enter bet amount*//

     while(i<=21) //*While loop used to keep asking user to hit or stay at most twenty-one times because there is a chance user can generate twenty-one consecutive 1's*//

     {

         if(p==21) //*If user total is 21, win*//

         {

             printf("\nCongratulations! You Won!\n");

             won = won+1;

             cash = cash+bet;

             printf("\nYou have %d Wins and %d Losses. Awesome!\n", won, loss);

             dealer_total=0;

             askover();

         }

         if(p>21) //*If player total is over 21, loss*//

         {

             printf("\nYou Bust!\n");

             loss = loss+1;

             cash = cash - bet;

             printf("\nYou have %d Wins and %d Losses. Awesome!\n", won, loss);

             dealer_total=0;

             askover();

         }

         if(p<=21) //*If player total is less than 21, ask to hit or stay*//

         {

             printf("\n\nWould You Like to Hit or Stay?");

                 scanf("%c", &choice3);

             while((choice3!='H') && (choice3!='h') && (choice3!='S') && (choice3!='s')) //* If invalid choice entered*//

                 {

                 printf("\n");

                         printf("Please Enter H to Hit or S to Stay.\n");

                         scanf("%c",&choice3);

                 }

                 if((choice3=='H') || (choice3=='h')) //* If Hit, continues*//

                 {

                 randcard();

                 player_total = p + l;

                 p = player_total;

                 printf("\nYour Total is %d\n", p);

                 dealer();

                  if(dealer_total==21) //*If dealer total is 21, loss*//

                  {

                      printf("\nDealer Has the Better Hand. You Lose.\n");

                      loss = loss+1;

                      cash = cash - bet;

                      printf("\nYou have %d Wins and %d Losses.\n", won, loss);

                      dealer_total=0;

                      askover();

                  }

                 if(dealer_total>21) //*If dealer total is over 21, win*//

                  {

                      printf("\nDealer Went Over!. You Win!\n");

                      won = won+1;

                              cash = cash+bet;

                      printf("\nYou have %d Wins and %d Losses. Awesome!\n", won, loss);

                      dealer_total=0;

                      askover();

                  }

             }

             if((choice3=='S') || (choice3=='s')) //* If Stay, does not continue*//

             {

                printf("\nYou Have Chosen to Stay at %d.\n", player_total);

                stay();

             }

          }

             i++; //*While player total and dealer total are less than 21, re-do while loop*//

     } //* End While Loop*//

} //* End Function*//

 

void dealer() //Function to play for dealer AI

{

     int z;

 

     if(dealer_total<17)

     {

      srand((unsigned) time(NULL) + 1); //*Generates random seed for rand() function*//

      z=rand()%13+1;

      if(z<=10) //*If random number generated is 10 or less, keep that value*//

      {

         d=z;

      }

      if(z>11) //*If random number generated is more than 11, change value to 10*//

      {

         d=10;

      }

      if(z==11) //*If random number is 11(Ace), change value to 11 or 1 depending on dealer total*//

      {

         if(dealer_total<=10)

         {

             d=11;

         }

         else

         {

             d=1;

         }

      }

     dealer_total = dealer_total + d;

     }

     printf("\nThe Dealer Has a Total of %d", dealer_total); //*Prints dealer total*//

} //* End Function*//

void stay() //*Function for when user selects 'Stay'*//

{

     dealer(); //*If stay selected, dealer continues going*//

     if(dealer_total>=17)

     {

      if(player_total>=dealer_total) //*If player's total is more than dealer's total, win*//

      {

         printf("\nCongratulations! You Win!\n");

         won = won+1;

         cash = cash+bet;

         printf("\nYou have %d Wins and %d Losses. Awesome!\n", won, loss);

         dealer_total=0;

         askover();

      }

      if(player_total

      {

         printf("\nDealer Has the Better Hand. You Lose.\n");

         loss = loss+1;

         cash = cash - bet;

         printf("\nYou have %d Wins and %d Losses.\n", won, loss);

         dealer_total=0;

         askover();

      }

      if(dealer_total>21) //*If dealer's total is more than 21, win*//

      {

         printf("\nDealer Bust! You Win!\n");

         won = won+1;

         cash = cash+bet;

         printf("\nYou have %d Wins and %d Losses. Awesome!\n", won, loss);

         dealer_total=0;

         askover();

      }

     }

     else

     {

         stay();

     }

} //* End Function*//

void cash_test() //*Test for if user has cash remaining*//

{

     if (cash <= 0) //*Once user has zero remaining cash, game ends and prompts user to play again*//

     {

                        printf("You Are Bankrupt. Game Over");

                        cash = 500;

        askover();

     }

} //* End Function*//

 

int betting() //*Asks user amount to bet*

{

 printf("\n\nEnter Dollar Amount Bet: $");

 scanf("%d", &bet);

 if (bet > cash) //*If player tries to bet more money than player has*//

 {

                        printf("\nYou cannot bet more money than you have.");

                        printf("\nEnter Bet: ");

        scanf("%d", &bet);

        return bet;

 }

 else return bet;

} //* End Function*//

void askover() //* Function for asking player if they want to play again*//

{

            char choice1;

             printf("\nWould You Like To Play Again?");

     printf("\nPlease Enter Y for Yes or N for No\n");

     scanf("\n%c",&choice1);

           while((choice1!='Y') && (choice1!='y') && (choice1!='N') && (choice1!='n')) //* If invalid choice entered*//

            {                                                                          

                        printf("\n");

                        printf("Invalid Choice. Please Enter Y for Yes or N for No.\n");

                        scanf("%c",&choice1);

            }

            if((choice1 == 'Y') || (choice1 == 'y')) //* If yes, continue.*//

            {

            system("cls");

            play();

            }

            else if((choice1 == 'N') || (choice1 == 'n')) //* If no, exit program*//

            {

        fileresults();

        printf("\nPlay again soon!\n\n");

        exit(0);

            }

            return;

} //* End function*//

void fileresults() //*Prints results into Blackjack.txt file in program directory*//

{

    FILE *fpresults; //*File pointer is fpresults*//

    fpresults = fopen(RESULTS, "w"); //*Creates file and writes into it*//

    if(fpresults == NULL) //* what to do if file missing from directory*//

    {

               printf("\nError: File Missing\n");

               system("pause");

               exit(1);

    }

    else

    {    

     fprintf(fpresults,"\n\t RESULTS");

     fprintf(fpresults,"\n\t---------\n");

     fprintf(fpresults,"\nYou Have Won %d Times\n", won);

     fprintf(fpresults,"\nYou Have Lost %d Times\n", loss);

    }

     fclose(fpresults);

     return;

} //* End Function*//

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M9525022

Have any Question?


Related Questions in C/C++

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As