Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C code This is what I have so far: #include #include #include typedef struct card_s { char suit; int face; struct card_s *next; } card;

C code

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedThis is what I have so far:

#include

#include

#include

typedef struct card_s {

char suit;

int face;

struct card_s *next;

} card;

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; /* We will store 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 { //Player 2'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 witht the remaining elements(cards)

}

///////////////////////////// Create memory from 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() {

printf(" ");

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(" Player's hand has 7 cards ");

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

//Clears memory space

Free(hand1);

Free(hand2);

Free(deck);

return 0;

}

Output should look somewhat like:

image text in transcribed

Information: Go Fish (https:// is a card game that can be played by two or more players. The game is played by each player takes turn asking for a card with a specific rank or face from another player. The goal is to form a book (a set of 4 cards with the same face or rank). The winner is the player who has the most number of books at the end of the game. a) For two players, each player starts with 7 cards. The rest of the cards are put in the center so that each player can take a card when needed Let's call two players: Wilbur (a user) and PC (a computer). After each player gets 7 cards on his/her hand, the players take turn asking for the card. Information: Go Fish (https:// is a card game that can be played by two or more players. The game is played by each player takes turn asking for a card with a specific rank or face from another player. The goal is to form a book (a set of 4 cards with the same face or rank). The winner is the player who has the most number of books at the end of the game. a) For two players, each player starts with 7 cards. The rest of the cards are put in the center so that each player can take a card when needed Let's call two players: Wilbur (a user) and PC (a computer). After each player gets 7 cards on his/her hand, the players take turn asking for the card

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

Data Analysis Using SQL And Excel

Authors: Gordon S Linoff

2nd Edition

111902143X, 9781119021438

More Books

Students also viewed these Databases questions

Question

5. Why is the Civil Rights Act of 1991 such a significant law?

Answered: 1 week ago

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago