This C code doesnt seem to be working right, I keep getting segfaults, can someo
ID: 3716464 • Letter: T
Question
This C code doesnt seem to be working right, I keep getting segfaults, can someone help me, I think it may be in how I'm accessing pointers.
#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(struct player player1, struct player player2, struct 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(struct player player1, struct player player2, struct 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
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(struct player player1, struct dealer deck)
{
int card;
//Generating a random card until its available
do
card=rand()%52;
while(deck.cards_used[card]!=0);
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
int blackjack(struct player player1)
{
int sum=0;
for(int i=0; i<player1.how_many; i++)
sum+=lookup(player1.hand[i],1);
if(sum==21)
return 1;
sum=0;
for(int i=0; i<player1.how_many; i++)
sum+=lookup(player1.hand[i],11);
if(sum==21)
return 1;
else
return 0;
}
//Checks if the player is busted
int bust(struct player player1)
{
int sum=0;
for(int i=0; i<player1.how_many; i++)
sum+=lookup(player1.hand[i],1);
if(sum>21)
return 1;
else
return 0;
}
//Displays the cards of a player
void display_hand(struct 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
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;
}
Explanation / Answer
#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(struct player player1, struct player player2, struct dealer deck)
{
player1.how_many=0;
player2.how_many=0;
deck.dealercards.how_many=0;
int i;
for(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(struct player player1, struct player player2, struct dealer deck)
{
int card,i;
for(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
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(struct player player1, struct dealer deck)
{
int card;
//Generating a random card until its available
do
card=rand()%52;
while(deck.cards_used[card]!=0);
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
int blackjack(struct player player1)
{
int sum=0,i;
printf("Hello1 ");
for(i=0; i<player1.how_many; i++)
sum+=lookup(player1.hand[i],1);
if(sum==21)
return 1;
sum=0;
for(i=0; i<player1.how_many; i++)
sum+=lookup(player1.hand[i],11);
if(sum==21)
return 1;
else
return 0;
}
//Checks if the player is busted
int bust(struct player player1)
{
int sum=0,i;
for(i=0; i<player1.how_many; i++)
sum+=lookup(player1.hand[i],1);
if(sum>21)
return 1;
else
return 0;
}
//Displays the cards of a player
void display_hand(struct player player1)
{
int i;
for(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
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))//run time error caused here
{
//check...logic.//for deck.dealercards
printf("Dealer won the game! Cards: ");
display_hand(deck.dealercards);
}
else //If no one won the game
{
printf("hello2 ");
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;
int i;
for(i=0; i<player1.how_many; i++)
sum1+=lookup(player1.hand[i],1);
}
if(bust(player2))
sum2=0;
else
{
sum2=0;
int i;
for( i=0; i<player2.how_many; i++)
sum1+=lookup(player2.hand[i],1);
}
if(bust(deck.dealercards))
sum3=0;
else
{
sum3=0;
int i;
for(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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.