Question
In C-code: Sample output is below. Create Go Fish game between a user and computer. Current Code: #include #include #include typedef struct card_s { char
In C-code:
Sample output is below.
Create Go Fish game between a user and computer.
Current Code:
#include
#include
#include
typedef struct card_s {
char suit;
int face;
struct card_s *next;
} card;
//// Functions
struct card_s* CreateDeck();
struct card_s* ShuffleCard(struct card_s* deck);
void Deal(struct card_s** deck, struct card_s** hand1, struct card_s** hand2, int num);
void Free(struct card_s* deck);
void Print(struct card_s* deck);
///////////////////////////////// Initial Deck /////////////////////////////////
struct card_s* CreateDeck() {
struct card_s* deck = NULL, *current = NULL, *jumper;
int i, j;
for(j = 1; j
for(i = 0; i
jumper = (struct card_s *)malloc(sizeof(struct card_s));
if(i == 0)
jumper->suit = 'S'; //Spade
else if(i == 1)
jumper->suit = 'D'; //Diamond
else if(i == 2)
jumper->suit = 'H'; //Heart
else
jumper->suit = 'C'; //Club
jumper->face = j;
jumper->next = NULL;
if(current == NULL) {
deck = jumper;
} else {
current->next = jumper;
}
current = jumper;
}
}
return deck;
}
////////////////////////////////////// Shuffle //////////////////////////////////////
struct card_s* ShuffleCard(struct card_s* deck) {
struct card_s* crd;
int n;
int i, j, k;
struct card_s** arr; // Stores as array for shuffling
for(n = 0, crd = deck; crd != NULL; n++, crd = crd->next); //Size of list
arr = (struct card_s**)malloc (n * sizeof(struct card_s*)); //Allocates memory
for(i = 0, crd = deck; crd != NULL; i++, crd = crd->next) { //Places the cards in the array
arr[i] = crd;
}
//Shuffle Loop
for(i = 0; i
for(j = 0; j
k = rand() % n; //Random number for the swap
crd = arr[j];
arr[j] = arr[k];
arr[k] = crd;
}
}
for(i = 0; i
arr[i]->next = arr[i+1];
}
arr[i]->next = NULL; //End of linked list
crd = arr[0]; //Start of new deck
free(arr); //Clears array
return crd;
}
//////////////////////////// Deals two hands and update deck ////////////////////////////
void Deal(struct card_s** deck, struct card_s** hand1, struct card_s** hand2, int n) {
struct card_s *cur, *cur1, *cur2; //Stores current card in 3 lists
int i;
cur1 = cur2 = NULL;
cur = *deck;
for(i = 0; i next) {
if(i % 2 == 0) { //Player 1's turn (even)
if(cur1 == NULL) { //Refers to first element(card) in list
*hand1 = cur;
} else {
cur1->next = cur;
}
cur1 = cur;
}
else { //Computer's turn (odd)
if(cur2 == NULL) { //Refers to first element(card) in list
*hand2 = cur;
}
else {
cur2->next = cur;
}
cur2 = cur;
}
}
cur1->next = cur2->next = NULL; //Ends the lists with NULL
*deck = cur; //Updates the deck with the remaining elements(cards)
}
///////////////////////////// Create memory in Linked List /////////////////////////////
void Free(struct card_s* deck) {
struct card_s * cd;
while(deck != NULL) {
cd = deck;
deck = deck->next;
free(cd);
}
}
////////////////////////////////////// Print Suit ////////////////////////////////////////
void printSuit(char suit) {
switch(suit) {
case 'C': //Club
printf("");
return;
case 'D': //Diamond
printf("");
return;
case 'H': //Heart
printf("");
return;
case 'S': //Spade
printf("");
return;
}
}
////////////////////////////// Print cards from Linked List //////////////////////////////
void Print(struct card_s* deck) {
int i;
for(i = 0; deck != NULL; i++, deck = deck->next) {
if(i % 8 == 0)
printf(" ");
if(deck->face == 1) //A has value 1
printf("A");
else if(deck->face == 11) //J has value 11
printf("J");
else if(deck->face == 12) //Q has value 12
printf("Q");
else if(deck->face == 13) //K has value 13
printf("K");
else
printf("%d", deck->face); //else prints the number 2-10
printSuit(deck->suit); //Prints suit after face is printed
printf("\t");
}
printf(" ");
}
///////////////////////////////////////// MAIN /////////////////////////////////////////
int main() {
char name[50];
printf("Enter your name: ");
scanf("%s", name);
struct card_s *deck, *hand1, *hand2; //Refers to each set: main deck, hand 1 and hand 2
srand(time(NULL)); //Random number
deck = CreateDeck(); //Deck created
//printf("Create 52-card deck ");
//Print(deck); //Prints deck
//fflush(stdout);
deck = ShuffleCard(deck); //Shuffles deck
// printf(" Shuffle Card ");
//Print(deck); //Prints shuffled deck
Deal(&deck, &hand1, &hand2, 7); //Two hands are dealt
printf(" %s's hand has 7 cards ", name);
Print(hand1); //Hand for player 1
printf(" Computer's hand has 7 cards ");
Print(hand2); //Hand for player 2
//printf(" Deck after cards are distributed ");
//Print(deck); //Prints remaining deck after hands
char ans;
printf(" >> Hit C to continue: "); //Continue?
scanf(" %c", &ans);
if ((ans == 'C') || (ans == 'c') ) {
printf(" %s, your turn ", name);
printf(" %s's hand has 7 cards ", name);
Print(hand1); //Hand for player 1
}
printf(" Which card (2 - 10, J, Q, K, A) do you want to ask for? ");
//scanf("");
//Clears memory space
Free(hand1);
Free(hand2);
Free(deck);
return 0;
}
Current Output:
Sample Output:
Enter your name: Wilma Wilma's hand has 7 cards Computer's hand has 7 cards 6 QJ 44 4 104 10 >> Hit C to continue: C Wilma, your turn Wilma's hand has 7 cards Which card (2 10, J,Q, K, A) do you want to ask for? Program ended with exit code: Enter your name: Wilma Wilma's hand has 7 cards Computer's hand has 7 cards 6 QJ 44 4 104 10 >> Hit C to continue: C Wilma, your turn Wilma's hand has 7 cards Which card (2 10, J,Q, K, A) do you want to ask for? Program ended with exit code
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started