Convert this C++ code over to C #include <stdio.h> #include <stdlib.h> #include
ID: 3715812 • Letter: C
Question
Convert this C++ code over to C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//using namespace std;
//Player structure
struct player
{
int hand[52];
int how_many;
};
//Dealer structure
struct dealer
{
int deck_of_cards[52];
int cards_used[52];
int remaining_cards;
player dealercards;
};
//Shuffle function
void shuffle(player &player1,player &player2,dealer &deck)
{
player1.how_many=0;
player2.how_many=0;
deck.dealercards.how_many=0;
for(int i=0; i<52; i++)
{
deck.cards_used[i]=0;
deck.deck_of_cards[i]=i;
}
deck.remaining_cards=52;
return;
}
//Initial deal function
void initial_deal(player &player1,player &player2,dealer &deck)
{
int card;
for(int i=0; i<2; i++) //Doing twice so that each player gets 2 cards
{
//For player 1
do
card=rand()%52; //Picking a random card
while(deck.cards_used[card]!=0); //Checking if card is available
//doing the necessary updations
player1.hand[player1.how_many]=card;
player1.how_many++;
deck.cards_used[card]=1;
deck.remaining_cards--;
//For player 2
do
card=rand()%52;
while(deck.cards_used[card]!=0);
player2.hand[player1.how_many]=card;
player2.how_many++;
deck.cards_used[card]=1;
deck.remaining_cards--;
//For dealer
do
card=rand()%52;
while(deck.cards_used[card]!=0);
deck.dealercards.hand[player1.how_many]=card;
deck.dealercards.how_many++;
deck.cards_used[card]=1;
deck.remaining_cards--;
}
return;
}
void hit(player &player1,dealer &deck)
{
int card;
//Generating a random card until its available
do
card=rand()%52;
while(deck.cards_used[card]!=0);
//Doing the necessary updations
player1.hand[player1.how_many]=card;
player1.how_many++;
deck.cards_used[card]=1;
deck.remaining_cards--;
return;
}
//This function finds the value of the parameter card. The value of parameter ace defines its value i.e. 1 or 11
int lookup(int card,int ace)
{
card%=13;
switch(card)
{
case 0:
return ace;
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
return card+1;
break;
case 10:
case 11:
case 12:
return 10;
break;
}
}
//Checks for blackjack
bool blackjack(player player1)
{
int sum=0;
for(int i=0; i<player1.how_many; i++)
sum+=lookup(player1.hand[i],1);
if(sum==21)
return true;
sum=0;
for(int i=0; i<player1.how_many; i++)
sum+=lookup(player1.hand[i],11);
if(sum==21)
return true;
else
return false;
}
//Checks if the player is busted
bool bust(player player1)
{
int sum=0;
for(int i=0; i<player1.how_many; i++)
sum+=lookup(player1.hand[i],1);
if(sum>21)
return true;
else
return false;
}
//Displays the cards of a player
void display_hand(player player1)
{
for(int i=0; i<player1.how_many; i++)
{
int card=player1.hand[i];
if(card>-1&&card<13)
cout<<"C";
else if(card>12&&card<26)
cout<<"D";
else if(card>25&&card<39)
cout<<"H";
else
cout<<"S";
switch(card)
{
case 0:
case 13:
case 26:
case 39:
cout<<"A ";
break;
case 10:
case 23:
case 36:
case 49:
cout<<"J ";
break;
case 11:
case 24:
case 37:
case 50:
cout<<"Q ";
break;
case 12:
case 25:
case 38:
case 51:
cout<<"K ";
break;
default:
cout<<(card%13)+1<<" ";
}
}
cout<<" ";
return;
}
int main()
{
srand(time(0)); //Randomizing time seed
player player1,player2;
dealer deck;
char res;
int choice,flag;
do
{
cout<<" ";
shuffle(player1,player2,deck); //Shuffling the deck
initial_deal(player1,player2,deck); //Giving the initial cards
if(blackjack(player1))
{
cout<<"Player 1 won the game! Cards:";
display_hand(player1);
}
else if(blackjack(player2))
{
cout<<"Player 2 won the game! Cards:";
display_hand(player2);
}
else if(blackjack(deck.dealercards))
{
cout<<"Dealer won the game! Cards:";
display_hand(deck.dealercards);
}
else //If no one won the game
{
flag=0;
//For player 1
do
{
cout<<"Player 1's cards: ";
display_hand(player1);
cout<<"1.HIT 2.STAY Enter choice: ";
cin>>choice;
if(choice==1) //If player chooses HIT
{
hit(player1,deck);
if(blackjack(player1))
{
cout<<"Player 1 won the game! ";
display_hand(player1);
flag=1; //Means player won
}
else if(bust(player1))
{
cout<<"Player 1 is busted! ";
display_hand(player1);
choice=2; //To exit the loop
}
}
}
while(choice!=2&&flag!=1);
if(flag==1)
break;
//For player 2
do
{
cout<<"Player 2's cards: ";
display_hand(player2);
cout<<"1.HIT 2.STAY Enter choice: ";
cin>>choice;
if(choice==1)
{
hit(player2,deck);
if(blackjack(player2))
{
cout<<"Player 2 won the game! ";
display_hand(player2);
flag=1;
}
else if(bust(player2))
{
cout<<"Player 2 is busted! ";
display_hand(player2);
choice=2;
}
}
}
while(choice!=2&&flag!=1);
if(flag==1)
break;
//For Dealer
do
{
cout<<"Dealer's cards: ";
display_hand(deck.dealercards);
cout<<"1.HIT 2.STAY Enter choice: ";
cin>>choice;
if(choice==1)
{
hit(deck.dealercards,deck);
if(blackjack(deck.dealercards))
{
cout<<"Dealer won the game! ";
display_hand(deck.dealercards);
flag=1;
}
else if(bust(deck.dealercards))
{
cout<<"Dealer is busted! ";
display_hand(deck.dealercards);
choice=2;
}
}
}
while(choice!=2&&flag!=1);
}
if(flag==0) //If no one won the game, this finds the player with the highest hand
{
int sum1,sum2,sum3;
if(bust(player1))
sum1=0;
else
{
sum1=0;
for(int i=0; i<player1.how_many; i++)
sum1+=lookup(player1.hand[i],1);
}
if(bust(player2))
sum2=0;
else
{
sum2=0;
for(int i=0; i<player2.how_many; i++)
sum1+=lookup(player2.hand[i],1);
}
if(bust(deck.dealercards))
sum3=0;
else
{
sum3=0;
for(int i=0; i<deck.dealercards.how_many; i++)
sum3+=lookup(deck.dealercards.hand[i],1);
}
if(sum1>sum2&&sum1>sum3)
cout<<"Player1 won the game ";
else if(sum1<sum2&&sum2>sum3)
cout<<"Player2 won the game ";
else
cout<<"Dealer won the game ";
}
cout<<"Do you want to play again?(y/n): ";
cin>>res;
}
while(res=='y'||res=='Y');
return 0;
}
Explanation / Answer
Please find the equivalent C code below.
CODE
=====================
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
//Player structure
typedef struct player {
int hand[52];
int how_many;
} player;
//Dealer structure
typedef struct dealer {
int deck_of_cards[52];
int cards_used[52];
int remaining_cards;
player dealercards;
}dealer;
//Shuffle function
void shuffle(player player1, player player2, dealer deck)
{
player1.how_many=0;
player2.how_many=0;
deck.dealercards.how_many=0;
for(int i=0; i<52; i++)
{
deck.cards_used[i]=0;
deck.deck_of_cards[i]=i;
}
deck.remaining_cards=52;
return;
}
//Initial deal function
void initial_deal(player player1, player player2, dealer deck)
{
int card;
for(int i=0; i<2; i++) //Doing twice so that each player gets 2 cards
{
//For player 1
do
card=rand()%52; //Picking a random card
while(deck.cards_used[card]!=0); //Checking if card is available
//doing the necessary updations
player1.hand[player1.how_many]=card;
player1.how_many++;
deck.cards_used[card]=1;
deck.remaining_cards--;
//For player 2
do
card=rand()%52;
while(deck.cards_used[card]!=0);
player2.hand[player1.how_many]=card;
player2.how_many++;
deck.cards_used[card]=1;
deck.remaining_cards--;
//For dealer
do
card=rand()%52;
while(deck.cards_used[card]!=0);
deck.dealercards.hand[player1.how_many]=card;
deck.dealercards.how_many++;
deck.cards_used[card]=1;
deck.remaining_cards--;
}
return;
}
void hit(player player1, dealer deck)
{
int card;
//Generating a random card until its available
do
card=rand()%52;
while(deck.cards_used[card]!=0);
//Doing the necessary updations
player1.hand[player1.how_many]=card;
player1.how_many++;
deck.cards_used[card]=1;
deck.remaining_cards--;
return;
}
//This function finds the value of the parameter card. The value of parameter ace defines its value i.e. 1 or 11
int lookup(int card,int ace)
{
card%=13;
switch(card)
{
case 0:
return ace;
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
return card+1;
break;
case 10:
case 11:
case 12:
return 10;
break;
}
}
//Checks for blackjack
bool blackjack(player player1)
{
int sum=0;
for(int i=0; i<player1.how_many; i++)
sum+=lookup(player1.hand[i],1);
if(sum==21)
return true;
sum=0;
for(int i=0; i<player1.how_many; i++)
sum+=lookup(player1.hand[i],11);
if(sum==21)
return true;
else
return false;
}
//Checks if the player is busted
bool bust(player player1)
{
int sum=0;
for(int i=0; i<player1.how_many; i++)
sum+=lookup(player1.hand[i],1);
if(sum>21)
return true;
else
return false;
}
//Displays the cards of a player
void display_hand(player player1)
{
for(int i=0; i<player1.how_many; i++)
{
int card=player1.hand[i];
if(card>-1&&card<13)
printf("C");
else if(card>12&&card<26)
printf("D");
else if(card>25&&card<39)
printf("H");
else
printf("S");
switch(card)
{
case 0:
case 13:
case 26:
case 39:
printf("A");
break;
case 10:
case 23:
case 36:
case 49:
printf("J");
break;
case 11:
case 24:
case 37:
case 50:
printf("Q");
break;
case 12:
case 25:
case 38:
case 51:
printf("K");
break;
default:
printf("%d ", (card%13)+1);
}
}
printf(" ");
return;
}
int main()
{
srand(time(0)); //Randomizing time seed
player player1,player2;
dealer deck;
char res;
int choice,flag;
do
{
printf(" ");
shuffle(player1,player2,deck); //Shuffling the deck
initial_deal(player1,player2,deck); //Giving the initial cards
if(blackjack(player1))
{
printf("Player 1 won the game! Cards: ");
display_hand(player1);
}
else if(blackjack(player2))
{
printf("Player 2 won the game! Cards: ");
display_hand(player2);
}
else if(blackjack(deck.dealercards))
{
printf("Dealer won the game! Cards: ");
display_hand(deck.dealercards);
}
else //If no one won the game
{
flag=0;
//For player 1
do
{
printf("Player 1's cards: ");
display_hand(player1);
printf("1.HIT 2.STAY Enter choice: ");
scanf("%d", &choice);
if(choice==1) //If player chooses HIT
{
hit(player1,deck);
if(blackjack(player1))
{
printf("Player 1 won the game! ");
display_hand(player1);
flag=1; //Means player won
}
else if(bust(player1))
{
printf("Player 1 is busted! ");
display_hand(player1);
choice=2; //To exit the loop
}
}
}
while(choice!=2&&flag!=1);
if(flag==1)
break;
//For player 2
do
{
printf("Player 2's cards: ");
display_hand(player2);
printf("1.HIT 2.STAY Enter choice: ");
scanf("%d", &choice);
if(choice==1)
{
hit(player2,deck);
if(blackjack(player2))
{
printf("Player 2 won the game! ");
display_hand(player2);
flag=1;
}
else if(bust(player2))
{
printf("Player 2 is busted! ");
display_hand(player2);
choice=2;
}
}
}
while(choice!=2&&flag!=1);
if(flag==1)
break;
//For Dealer
do
{
printf("Dealer's cards: ");
display_hand(deck.dealercards);
printf("1.HIT 2.STAY Enter choice: ");
scanf("%d", &choice);
if(choice==1)
{
hit(deck.dealercards,deck);
if(blackjack(deck.dealercards))
{
printf("Dealer won the game! ");
display_hand(deck.dealercards);
flag=1;
}
else if(bust(deck.dealercards))
{
printf("Dealer is busted! ");
display_hand(deck.dealercards);
choice=2;
}
}
}
while(choice!=2&&flag!=1);
}
if(flag==0) //If no one won the game, this finds the player with the highest hand
{
int sum1,sum2,sum3;
if(bust(player1))
sum1=0;
else
{
sum1=0;
for(int i=0; i<player1.how_many; i++)
sum1+=lookup(player1.hand[i],1);
}
if(bust(player2))
sum2=0;
else
{
sum2=0;
for(int i=0; i<player2.how_many; i++)
sum1+=lookup(player2.hand[i],1);
}
if(bust(deck.dealercards))
sum3=0;
else
{
sum3=0;
for(int i=0; i<deck.dealercards.how_many; i++)
sum3+=lookup(deck.dealercards.hand[i],1);
}
if(sum1>sum2&&sum1>sum3)
printf("Player1 won the game ");
else if(sum1<sum2&&sum2>sum3)
printf("Player2 won the game ");
else
printf("Dealer won the game ");
}
printf("Do you want to play again?(y/n): ");
scanf("%c", &res);
}
while(res=='y'||res=='Y');
return 0;
}