Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 #include #include #include

//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

//Checks if the player is busted int bust(struct player player1) { int sum=0; for(int i=0; i21) return 1; else return 0; }

//Displays the cards of a player void display_hand(struct player player1) { for(int i=0; i-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; isum2&&sum1>sum3) printf("Player1 won the game "); else if(sum1sum3) 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; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions

Question

Ty e2y Evaluate the integral dy

Answered: 1 week ago