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

Inside and outside type casting, depth description of the inside and outsid...

depth description of the inside and outside typecasting

Program to create a class and store student information, Develop a Student ...

Develop a Student class that has the following header file: #ifndef STUDENT_H #define STUDENT_H #include #include #include using namespace std; class Stu

Define auto pointer?, A: The simplest instance of a smart pointer is auto_p...

A: The simplest instance of a smart pointer is auto_ptr that is included in the standard C++ library. Auto Pointer only takes care of Memory leak & does nothing regarding dangling

Char, how many bytes required to char

how many bytes required to char

Program for single int parameter, Write a function that has a single int pa...

Write a function that has a single int parameter n and returns an int: 0 if n is negative, otherwise 1. You are not allowed to simply check the integer value, and should use the fu

C++, Write a program to find the area under the curve y = f(x) between x = ...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Explain about the variables in c language, Explain about the Variables in c...

Explain about the Variables in c language? The Variable is an identifier that is used to represent some specified kind of information within a designated portion of the program

Constant argument, Define  C o ns t a n t a r g u me n t : ...

Define  C o ns t a n t a r g u me n t : T h e a r gu m e n t c a n b e a c on s t a n t a r gu m e n t .    T h e c o

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