#include <stdio.h> #include <stdlib.h> #include <process.h> #include <time.h> #d
ID: 3633729 • Letter: #
Question
#include <stdio.h>#include <stdlib.h>
#include <process.h>
#include <time.h>
#define DECK_SIZE 53
#define SPADES 6
#define HEARTS 3
#define DIAMONDS 4
#define CLUBS 5
#define PEANUTS 10
void rules ();
void mainMenu();
void initializeDeck(int[]);
int RandomNumber (int,int);
void Shuffle(int[]);
void swap (int[],int,int);
void displayCard(int);
int scoreCard(int);
int dealer (int[]);
void checkDeck(int[]);
void gamePlay();
int main()
{
mainMenu();
}
void mainMenu() // Function for asking player if they want to continue
{
int choice;
system("cls");
printf(" Enter 1 to play Blackjack.");
printf(" Enter 2 to See the Rules.");
printf(" Enter 3 to Exit Game.");
printf(" Choice: ");
scanf("%d", &choice); // Prompts user for choice
if((choice<1) || (choice>3)) // If invalid choice entered
{
printf(" Incorrect Choice. Please enter 1, 2 or 3 ");
scanf("%d", &choice);
}
switch(choice) // Switch case for different choices
{
case 1: // Case to begin game
system("cls");
gamePlay();
break;
case 2: // Case to see rules
system("cls");
rules();
break;
case 3: // Case to exit game
printf(" Have a nice day! ");
system("pause");
exit(0);
break;
default:
printf(" Invalid Input");
} // End switch case
} // End if loop
void rules() //Prints Rules of Blackjack
{
char choice1;
int choice2;
printf(" RULES OF BLACKJACK");
printf(" ---------------------------------");
printf(" I.");
printf(" Each card has a value.");
printf(" Ace has a value of one and number cards 2 to 10 hold a value of their number.");
printf(" J, Q, and K cards hold a value of 10.");
printf(" The goal of this game is to reach a card value colose or equal to 21. ");
printf(" II.");
printf(" After the dealing of the first two cards, you must decide whether to HIT or STAY.");
printf(" Staying will keep you safe, hitting will add a card.");
printf(" If your total goes over 21, you LOSE!.");
printf(" You start the game with a pot of ten peanuts. Each time you win, you get a peanut from the dealer (computer).");
printf(" But if you lose, the dealer gets your peanut.");
printf(" The game goes on until either you or the dealer lose all of your peanuts, but you can leave any time you want.");
printf(" Please enter B to go back to the main menu. ");
scanf(" %c",&choice1);
while((choice1!='B') && (choice1!='b')) // If invalid choice entered
{
printf(" ");
printf("Incorrect Choice. Please Enter B to go back. ");
scanf("%c",&choice1);
}
if((choice1 == 'b') || (choice1 == 'B')) // . Prints menu.
{
system("cls");
mainMenu();
} // End if loop
} // End function
void initializeDeck(int deck[])
{
int i=0;
for (i=0; i<52; i++)
{
deck[i]= i+1;
}
deck[52]=52;
}
int RandomNumber (int upper, int lower)
{
lower = 0;
upper = 51;
srand((unsigned) time == NULL);
return rand()%(upper-lower+1)+lower;
}
void Shuffle(int deck[])
{
int rand=0, i=51;
for(i=51;i>=0;i--)
{
rand=RandomNumber(i,0);
swap(deck[DECK_SIZE],deck[i], deck[rand]);
}
}
void swap (int deck[], int i, int j)
{
int temp;
temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
void displayCard(int number)
{
int rank;
int suit;
suit = number/13;
rank = number%13;
if (suit == 0)
{
printf("%c ",SPADES);
if (rank >=1 && rank <= 10)
{
printf("%i", rank);
}
else if (rank == 11)
{
printf ("J");
}
else if (rank == 12)
{
printf ("Q");
}
else if (rank == 13 )
{
printf ("K");
}
}
if(suit==1)
{
printf("%c",HEARTS);
if (rank >=1 && rank <= 10)
{
printf("%i", rank);
}
else if (rank == 11)
{
printf ("J");
}
else if (rank == 12)
{
printf ("Q");
}
else if (rank == 13 )
{
printf ("K");
}
}
if(suit==2)
{
printf("%c",DIAMONDS);
if (rank >=1 && rank <= 10)
{
printf("%i", rank);
}
else if (rank == 11)
{
printf ("J");
}
else if (rank == 12)
{
printf ("Q");
}
else if (rank == 13 )
{
printf ("K");
}
}
if(suit==3)
{
printf("%c",CLUBS);
if (rank >=1 && rank <= 10)
{
printf("%i", rank);
}
else if (rank == 11)
{
printf ("J");
}
else if (rank == 12)
{
printf ("Q");
}
else if (rank == 13 )
{
printf ("K");
}
}
}
int scoreCard(int number)
{
int value;
if ((number%13>=1) && (number%13<=9))
{
value=number%13;
}
else
{
value=10;
}
return value;
}
int dealer (int deck [])
{
int card=0;
int cardsLeft= deck[52];
int dealerTotal=0, value=0;
while(dealerTotal <=15)
{
card = deck[cardsLeft-1];
displayCard(card);
value= scoreCard(card);
checkDeck(deck[DECK_SIZE]);
cardsLeft=deck[52];
dealerTotal=dealerTotal+value;
}
return dealerTotal;
}
void checkDeck(int deck[])
{
deck[52]=deck[52]-1;
if (deck[52]==0)
{
Shuffle(deck[DECK_SIZE]);
}
}
void gamePlay()
{
printf("WELCOME TO MASIH AND JUAN'S BLACKJACK ! ");
int deck[DECK_SIZE];
int playerTotal=0, i=0;
int value=0, card=0, cardsLeft;
int p_player=PEANUTS;
int p_dealer=PEANUTS;
int dealer_total=0;
char choice, choice1;
initializeDeck(deck[DECK_SIZE]);
Shuffle(deck[DECK_SIZE]);
cardsLeft=deck[52];
printf("So the game begins! ");
do
{
printf("Your turn: ");
printf("Your first two cards are: ");
for (i=0; i<2; i++)
{
card = deck [cardsLeft-1];
displayCard(card);
value= scoreCard(card);
checkDeck(deck[DECK_SIZE]);
cardsLeft=deck[52];
playerTotal=playerTotal+value;
}
printf ("Your total is: %i ", playerTotal);
printf(" Would You Like to Hit or Stay?");
printf("Please press S to stay or H to hit ");
scanf("%c", &choice);
while((choice!='H') && (choice!='h') && (choice!='S') && (choice!='s')) // If invalid choice entered
{
printf(" ");
printf("Please Enter H to Hit or S to Stay. ");
scanf("%c",&choice);
}
if((choice=='H') || (choice=='h'))
{
card = deck [cardsLeft-1];
value= scoreCard(card);
checkDeck(deck[DECK_SIZE]);
cardsLeft=deck[52];
printf("Your next card is: ");
displayCard(card);
playerTotal=playerTotal+value;
printf(" Your total is: %i",playerTotal);
while (playerTotal<21)
{
printf(" Would You Like to Hit or Stay?");
printf("Please enter S to stay or H to hit ");
scanf("%c", &choice);
while((choice!='H') && (choice!='h') && (choice!='S') && (choice!='s')) // If invalid choice entered
{
printf(" ");
printf("Please Enter H to Hit or S to Stay. ");
scanf("%c",&choice);
}
if((choice=='H') || (choice=='h'))
{
card = deck [cardsLeft-1];
value= scoreCard(card);
checkDeck(deck[DECK_SIZE]);
cardsLeft=deck[52];
printf("Your next card is: ");
displayCard(card);
playerTotal=playerTotal+value;
printf(" Your total is: %i",playerTotal);
}
if((choice=='S') || (choice=='s'))
{
break;
}
if (playerTotal==21)
{
printf(" CONGRATULATIONS !!! You won this round! ");
p_player = p_player+1;
p_dealer = p_dealer-1;
}
if (playerTotal>21)
{
printf(" Too bad...You lose this round :( ");
p_player = p_player-1;
p_dealer = p_dealer+1;
}
if (playerTotal<21)
{
printf(" Now it's the dealer's turn.");
dealer_total=dealer (deck [DECK_SIZE]);
printf("Dealer's total for this hand is: %i", dealer_Total);
if (dealer_total==21)
{
printf(" The dealer has won this round. ");
p_player = p_player-1;
p_dealer = p_dealer+1;
}
if (dealer_total>21)
{
printf(" CONGRATULATIONS !!! You won this round! ");
p_player = p_player+1;
p_dealer = p_dealer-1;
}
if (dealer_total<21)
{
if (dealer_total<playerTotal)
{
printf(" CONGRATULATIONS !!! You won this round! ");
p_player = p_player+1;
p_dealer = p_dealer-1;
}
else
{
printf(" The dealer has won this round. ");
p_player = p_player-1;
p_dealer = p_dealer+1;
}
}
}
}
}
else
{
printf(" Now it's the dealer's turn. ");
dealer_total=dealer (deck [DECK_SIZE]);
printf("Dealer's total for this hand is: %i", dealer_Total);
if (dealer_total==21)
{
printf(" The dealer has won this round. ");
p_player = p_player-1;
p_dealer = p_dealer+1;
}
if (dealer_total>21)
{
printf(" CONGRATULATIONS !!! You won this round! ");
p_player = p_player+1;
p_dealer = p_dealer-1;
}
if (dealer_total<21)
{
if (dealer_total<playerTotal)
{
printf(" CONGRATULATIONS !!! You won this round! ");
p_player = p_player+1;
p_dealer = p_dealer-1;
}
else
{
printf(" The dealer has won this round. ");
p_player = p_player-1;
p_dealer = p_dealer+1;
}
}
}
printf(" You have %i peanuts left. ", p_player);
printf("The dealer has %i peanuts left.", p_dealer);
printf(" Would You Like To Play Again?");
printf(" Please Enter Y for Yes or N for No ");
scanf(" %c",&choice1);
while((choice1!='Y') && (choice1!='y') && (choice1!='N') && (choice1!='n')) // If invalid choice entered
{
printf(" ");
printf("Incorrect Choice. Please Enter Y for Yes or N for No. ");
scanf("%c",&choice1);
}
} while ((choice1=='y' || choice1 == 'Y') && (p_player >0 && p_player<20));
if (p_dealer==0)
{
printf(" The dealer is bankrupt, you win the game!!! :) ");
}
else if(p_player==0)
{
printf(" You have no peanuts left. Better luck next time. ");
}
if((choice1 == 'N') || (choice1 == 'n')) // If no, exit program
{
printf(" BYE!!!! ");
system("pause");
exit(0);
}
}