Blackjack Program This project will simulate a blackjack program. It will put on
ID: 3653979 • Letter: B
Question
Blackjack Program
This project will simulate a blackjack program. It will put one player against the computer. The player will be able to wage money on the game. The game will continue until the player does not want to play anymore or he/she runs out of money.
This program will start with a screen that says
Explanation / Answer
// BlackjackFMJ // BlackjackFMJ.c //F. Marvin //Work Cited //----------- //Vladislav Shulman, Blackjack //http://cboard.cprogramming.com/c-programming/114023-simple-blackjack-program.html // //---------- //C includes //---------- #include #include #include #include #include #define spade 06 #define club 05 #define diamond 04 #define heart 03 #define RESULTS "Blackjack.txt" //---------------- //Global Variables //---------------- int dealerCardOne, dealersHand, dealerHiddenCard, dealerTotal, gameBet, gameCash = 500, gameLoss, gameWon, nextCard, nextSuite, playerTotal = 0, randomCard; //------------------- //Function Prototypes //------------------- void askOver(), askTitle(), cashTest(), dealerBot(), dealerFirstCard(), fileResults(), gameRules(), gamePlay(), gameStay(); int clubCard(), diamondCard(), gameBetting(), heartCard(), randCard(), spadeCard(); /************************************* DESCRIPTION- main D00 Generate random seed for rand(); D01 Call askTitle to bring up startup screen; D02 System pause; Return; **************************************/ int main(void) { /*L00*/ srand((unsigned) time(NULL)); /*L01*/ askTitle(); /*L02*/ system("pause"); return(0); }//main /************************************* DESCRIPTION- askOver D00 char choiceOne; D01 Display ask to play again; D02 Obtain choiceOne; D03 While((choiceOne!='Y') && (choiceOne!='y') && (choiceOne!='N') && (choiceOne!='n')) { Dispay incorrect choice; Obtain choiceOne; }//while D04 If (choiceOne == yes) { System clear; Call gamePlay; }//If D05 Else If (choiceOne == no) { Call fileResults; Dispay closing; System pause; exit; }//Else if Return; **************************************/ void askOver() { /*L00*/ char choiceOne; /*L01*/ printf("Would you like to play again (please enter Y for Yes or N for No)?"); /*L02*/ scanf_s(" %c", &choiceOne); /*L03*/ while((choiceOne!='Y') && (choiceOne!='y') && (choiceOne!='N') && (choiceOne!='n')) { printf(" Incorrect choice. Please enter Y for Yes or N for No:"); scanf_s("%c",&choiceOne); }//while /*L04*/ if((choiceOne == 'Y') || (choiceOne == 'y')) { system("cls"); gamePlay(); }//if /*L05*/ else if((choiceOne == 'N') || (choiceOne == 'n')) { fileResults(); printf(" Exiting Marvin's Blackjack "); printf("--------------------------- "); system("pause"); exit(0); }//else if return; }//askOver /************************************* DESCRIPTION- askTitle D00 char choiceOne; int choiceTwo; D01 Display opening; D02 Obtain choiceOne; D03 While((choiceOne!='Y') && (choiceOne!='y') && (choiceOne!='N') && (choiceOne!='n')) { Dispay incorrect choice; Obtain choiceOne; }//while D04 If (choiceOne == yes) { System clear; Display game choices; Obtain choiceTwo; D05 If((choiceTwo3) { Dispay incorrect choice; Obtain choiceTwo; }//If D06 Switch (Choicetwo) { Case 1: System clear; Call gamePlay; Break; Case 2: System clear; Call GameRules; Break; Case 3: Display exiting game; System pause; System exit; Break; Default: Display Invalid input; }//Switch }//If D07 Else If((choiceOne == 'N') || (choiceOne == 'n')) { Display exiting game; System pause; System exit; }//Else If Return; **************************************/ void askTitle() { /*L00*/ char choiceOne; int choiceTwo; /*L01*/ printf("Entering Marvin's Blackjack "); printf("--------------------------- "); printf("|%c Are you ready? %c| ", spade, spade); printf("--------------------------- "); printf(" (Y/N) "); /*L02*/ scanf_s(" %c",&choiceOne); /*L03*/ while((choiceOne!='Y') && (choiceOne!='y') && (choiceOne!='N') && (choiceOne!='n')) { printf(" "); printf("Incorrect choice. Please enter Y for Yes or N for No. "); scanf_s("%c",&choiceOne); } /*L04*/ if((choiceOne == 'Y') || (choiceOne == 'y')) { system("cls"); printf("Enter 1 to begin the greatest game ever played. "); printf("Enter 2 to see a complete listing of rules. "); printf("Enter 3 to Exit Game. " ); printf("Choice: "); scanf_s(" %d", &choiceTwo); /*L05*/ if((choiceTwo3)) { printf("Incorrect choice. Please enter 1, 2 or 3"); scanf_s(" %d", &choiceTwo); } /*L06*/ switch(choiceTwo) { case 1: system("cls"); gamePlay(); break; case 2: system("cls"); gameRules(); break; case 3: printf("Your day could have been perfect. "); printf("Have an almost perfect day! "); printf("Exiting Marvin's Blackjack "); printf("--------------------------- "); system("pause"); exit(0); break; default: printf("Invalid Input "); }//switch }//if /*L07*/ else if((choiceOne == 'N') || (choiceOne == 'n')) { printf("Your day could have been perfect. "); printf("Have an almost perfect day! "); printf("Exiting Marvin's Blackjack "); printf("--------------------------- "); system("pause"); exit(0); }//else if return; }//askTitle /************************************* DESCRIPTION- cashTest D00 If (gameCash is less than or equal to zero) { Display bankrupt and game over; Set gameCash = 500; Call askOver; }//If **************************************/ void cashTest() { /*L00*/ if (gameCash dealerTotal) { printf("The dealer flips his last card over and... "); printf("The dealer has a total of %d. ", dealerTotal); gameWon = gameWon + 1; gameCash = gameCash + gameBet; printf("You Win! "); printf("You have %d wins and %d losses. ", gameWon, gameLoss); dealerTotal = 0; askOver(); }//if /*L03*/ if(playerTotal == dealerTotal) { printf("The dealer flips his last card over and... "); printf("The dealer has a total of %d. ", dealerTotal); gameWon = gameWon + 0; gameCash = gameCash + 0; printf("The round ends in a push! "); printf("You have %d wins and %d losses. ", gameWon, gameLoss); dealerTotal = 0; askOver(); }//if /*L04*/ if((playerTotal 21) { printf("The dealer flips his last card over and... "); printf("The dealer has a total of %d and busts. ", dealerTotal); gameWon = gameWon + 1; gameCash = gameCash + gameBet; printf("The dealer went over. You Win! "); printf("You have %d wins and %d losses. ", gameWon, gameLoss); dealerTotal = 0; askOver(); }//if }//if /*L06*/ else { gameStay(); }//else }//gameStay /************************************* DESCRIPTION- clubCard D00 Set nextCard to rand()%13 + 1; D01 If(nextCard is less than or equal to 9) { Display number card; }//If D02 If(nextCard is equal to 10) { Display jack; }//If D03 If(nextCard is equal to 11) { Display ace; D04 If (playerTotal greater than 11) { Set nextCard to 1; }//If D05 Else { Set nextCard to 11; }//Else }//If D06 If(nextCard is equal to 12) { Display queen; Set nextCard to 10; }//if D07 If(nextCard is equal to 13) { Display king; Set nextCard to 10; }//If Return nextCard; **************************************/ int clubCard() { /*L00*/ nextCard = rand()%13 + 1; /*L01*/ if(nextCard 11) { nextCard = 1; }//if /*L05*/ else { nextCard = 11; }//else }//if /*L06*/ if(nextCard == 12) { printf(" %c%c%c%c%c%c%c ", 218,196,196,196,196, 196,191); printf("%c%c %c ", 179, club, 179); printf("%c Q %c ", 179, 179); printf("%c %c%c ", 179, club, 179); printf("%c%c%c%c%c%c%c ", 192,196,196,196,196, 196,217); nextCard = 10; }//if /*L07*/if(nextCard == 13) { printf(" %c%c%c%c%c%c%c ", 218,196,196,196,196, 196,191); printf("%c%c %c ", 179, club, 179); printf("%c K %c ", 179, 179); printf("%c %c%c ", 179, club, 179); printf("%c%c%c%c%c%c%c ", 192,196,196,196,196, 196,217); nextCard = 10; }//if return nextCard; }//clubCard /************************************* DESCRIPTION- diamondCard D00 Set nextCard to rand()%13 + 1; D01 If(nextCard is less than or equal to 9) { Display number card; }//If D02 If(nextCard is equal to 10) { Display jack; }//If D03 If(nextCard is equal to 11) { Display ace; D04 If (playerTotal greater than 11) { Set nextCard to 1; }//If D05 Else { Set nextCard to 11; }//Else }//If D06 If(nextCard is equal to 12) { Display queen; Set nextCard to 10; }//if D07 If(nextCard is equal to 13) { Display king; Set nextCard to 10; }//If Return nextCard; **************************************/ int diamondCard() { /*L00*/ nextCard = rand()%13 + 1; /*L01*/ if(nextCard 11) { nextCard = 1; }//if /*L05*/ else { nextCard = 11; }//else }//if /*L06*/ if(nextCard == 12) { printf(" %c%c%c%c%c%c%c ", 218,196,196,196,196, 196,191); printf("%c%c %c ", 179, diamond, 179); printf("%c Q %c ", 179, 179); printf("%c %c%c ", 179, diamond, 179); printf("%c%c%c%c%c%c%c ", 192,196,196,196,196, 196,217); nextCard = 10; }//if /*L07*/ if(nextCard == 13) { printf(" %c%c%c%c%c%c%c ", 218,196,196,196,196, 196,191); printf("%c%c %c ", 179, diamond, 179); printf("%c K %c ", 179, 179); printf("%c %c%c ", 179, diamond, 179); printf("%c%c%c%c%c%c%c ", 192,196,196,196,196, 196,217); nextCard = 10; }//if return nextCard; }//diamondCard /************************************* DESCRIPTION- gameBetting D00 Display enter bet; D01 Obtain bet; D02 If (gameBet > gameCash) { Display error; Display enter bet; Obtain bet; Return gameBet; }//If D03 Else { Return gameBet; }//Else **************************************/ int gameBetting() { /*L00*/ printf("Enter bet: $"); /*L01*/ scanf_s(" %d", &gameBet); /*L02*/ if (gameBet > gameCash) { printf("You cannot bet more money than you have. "); printf("Enter Bet: "); scanf_s(" %d", &gameBet); return gameBet; }//if /*L03*/ else { return gameBet; }//else }//gameBetting /************************************* DESCRIPTION- heartCard D00 Set nextCard to rand()%13 + 1; D01 If(nextCard is less than or equal to 9) { Display number card; }//If D02 If(nextCard is equal to 10) { Display jack; }//If D03 If(nextCard is equal to 11) { Display ace; D04 If (playerTotal greater than 11) { Set nextCard to 1; }//If D05 Else { Set nextCard to 11; }//Else }//If D06 If(nextCard is equal to 12) { Display queen; Set nextCard to 10; }//if D07 If(nextCard is equal to 13) { Display king; Set nextCard to 10; }//If Return nextCard; **************************************/ int heartCard() { /*L00*/nextCard = rand()%13 + 1; /*L01*/ if(nextCard 11) { nextCard = 1; }//if /*L05*/ else { nextCard = 11; }//else }//if /*L06*/ if(nextCard == 12) { printf(" %c%c%c%c%c%c%c ", 218,196,196,196,196, 196,191); printf("%c%c %c ", 179, heart, 179); printf("%c Q %c ", 179, 179); printf("%c %c%c ", 179, heart, 179); printf("%c%c%c%c%c%c%c ", 192,196,196,196,196, 196,217); nextCard = 10; }//if /*L07*/ if(nextCard == 13) { printf(" %c%c%c%c%c%c%c ", 218,196,196,196,196, 196,191); printf("%c%c %c ", 179, heart, 179); printf("%c K %c ", 179, 179); printf("%c %c%c ", 179, heart, 179); printf("%c%c%c%c%c%c%c ", 192,196,196,196,196, 196,217); nextCard = 10; }//if return nextCard; }//heartCard /************************************* DESCRIPTION- randCard D00 Set randomCard to rand()%4 + 1; D01 If(randomCard equals 1) { Call clubCard; Set nextSuite to nextCard; }//If D02 If(randomCard equals 2) { Call diamondCard; Set nextSuite to nextCard; }//If D03 If(randomCard equals 3) { Call heartCard; Set nextSuite to nextCard; }//If D04 If(randomCard == 4) { Call spadeCard; Set nextSuite to nextCard; }//If Return nextSuite; **************************************/ int randCard() { /*L00*/ randomCard = rand()%4 + 1; /*L01*/ if(randomCard == 1) { clubCard(); nextSuite = nextCard; }//if /*L02*/ if(randomCard == 2) { diamondCard(); nextSuite = nextCard; }//if /*L03*/ if(randomCard == 3) { heartCard(); nextSuite = nextCard; }//if /*L04*/ if(randomCard == 4) { spadeCard(); nextSuite = nextCard; }//if return nextSuite; }//randCard /************************************* DESCRIPTION- spadeCard D00 Set nextCard to rand()%13 + 1; D01 If(nextCard is less than or equal to 9) { Display number card; }//If D02 If(nextCard is equal to 10) { Display jack; }//If D03 If(nextCard is equal to 11) { Display ace; D04 If (playerTotal greater than 11) { Set nextCard to 1; }//If D05 Else { Set nextCard to 11; }//Else }//If D06 If(nextCard is equal to 12) { Display queen; Set nextCard to 10; }//if D07 If(nextCard is equal to 13) { Display king; Set nextCard to 10; }//If Return nextCard; **************************************/ int spadeCard() { /*L00*/ nextCard = rand()%13 + 1; /*L01*/ if(nextCard 11) { nextCard = 1; }//if /*L05*/ else { nextCard = 11; }//else }//if /*L06*/ if(nextCard == 12) { printf(" %c%c%c%c%c%c%c ", 218,196,196,196,196, 196,191); printf("%c%c %c ", 179, spade, 179); printf("%c Q %c ", 179, 179); printf("%c %c%c ", 179, spade, 179); printf("%c%c%c%c%c%c%c ", 192,196,196,196,196, 196,217); nextCard = 10; }//if /*L07*/ if(nextCard == 13) { printf(" %c%c%c%c%c%c%c ", 218,196,196,196,196, 196,191); printf("%c%c %c ", 179, spade, 179); printf("%c K %c ", 179, 179); printf("%c %c%c ", 179, spade, 179); printf("%c%c%c%c%c%c%c ", 192,196,196,196,196, 196,217); nextCard = 10; }//if return nextCard; }//spadeCard //BlackjackFMJ.cRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.