program generates cards at random, C/C++ Programming

Assignment Help:

#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*//


Related Discussions:- program generates cards at random

C programming, Byteland county is very famous for luminous jewels. Luminous...

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Summations, How do you write the code for summations

How do you write the code for summations

Parking garage, A parking garage charges a $2.00 minimum fee to park for up...

A parking garage charges a $2.00 minimum fee to park for up to three hours and an additional $0.50 per hour for each hour or part thereof over three hours. The maximum charge for a

C program to convert number to binary, Write a program in C that you will n...

Write a program in C that you will name "divide.exe", to divide one 32-bit twos-complement binary number by another, giving the quotient and the remainder, using the subtract-shift

Minimum shelves, write a program to find the minimum number of shelves

write a program to find the minimum number of shelves

I want im messenger, I would like to get you to collaborate in my project. ...

I would like to get you to collaborate in my project. We want a C programmer to Develop applications for Windows, the first application may be an Instant messenger encrypted in a w

Explain variables, Variables Information stored in a variable can alter...

Variables Information stored in a variable can alter in the course of the program. The type used in the definition explains the kind of information the symbol can store. Variab

Explain the defination and declaration of union, Explain the Defination and...

Explain the Defination and Declaration of Union? Generally in terms the composition of a union may be defined as be as union tag { member 1; member 2; member m; }; Wh

Explain about the string constants in c language, Explain about the String ...

Explain about the String Constants in c language? A collection of characters included within a pair of double quotes is treated as string constant. The character may be numbers

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd