Make a crazy eights game in C using the following parameters. I have through ste
ID: 3694330 • Letter: M
Question
Make a crazy eights game in C using the following parameters. I have through step 3 done, but I am stuck on number 4 on.
1) To simulate the deck, and each of the players' hands, your team MUST use a dynamic list of cards with the following type typedef struct card_s { char suit; int face; struct card_s *listp; } card; each card consists of a suit [clubs (),spades (),hearts (), or diamonds ()) a face (1(Ace) – 10, Jacks (J), Queens (Q), and Kings (K))
2) Create a full deck of 52 cards that are in order. In other words, for each of the four suits, the cards should be in order from Ace (1) through King (13). At the demo time, you will be asked to print the deck that you create.
3) You must then shuffle the deck (52 cards), using the following algorithm: (a) For each card in the deck, get a random number in the range of 0 to 50 to be used as the index of the element to swap that card with, i.e. if deck[0] holds the Jack of clubs (J ) and the random number generated was 24, and deck[24] holds the 9 of diamonds (9), then after the first swap, deck[0] would hold the 9 of diamonds (9) and deck[24] would hold the Jack of clubs (J ). You would then proceed to deck[1], find a random index of a card to swap with, and swap those cards, etc. (b) Repeat step (a) at least 100 times. 3 Note: You must seed the random number generator with a call to time() with srand(). [see sec 2.22 Random numbers in your Zyante book] At the demo time, you will be asked to print the deck after the shuffle is done.
4) After shuffling the deck, you must deal the cards by giving one card to the user, followed by one card to the computer, followed by one card to the user, etc. until each player get 8 cards. The player’s hand is represented as a dynamic list of cards. The list is populated with the cards drawn by the player. The dealer’s hand is represented as a dynamic list of cards. The list is populated with the cards drawn by the dealer. Note: a) the card(s) added to each of the player/dealer’s hand (drawn from the deck) must be added to that player/dealer’s linked list correctly and MUST be removed from the deck. b) the cards removed from each of the player/dealer's hand MUST BE deleted from that player/dealer’s linked list correctly. At the demo time, you will be asked to print both computer’s hand and user’s hand
5) Your program should then display one card (called, top card of the discard pile)
6) The game will start with a player/user either (only one option below can be used) a) puts (on the discard pile) any or all of card(s) with the same FACE as the face of top card OR b) puts (on the discard pile) any of card with the same SUIT as the SUIT of top card OR c) puts (on the discard pile) any 8 and then he/she must declare a SUIT to play next OR d) draws from the stockpile until he/she is able to play one of the above (a – c). Draw a maximum of 3 cards from the stockpile, if none of the 3 cards is matched, the player then passes.
7) Then it is a computer’s turn. The computer must put the card(s) on the discard pile as discussed in step 6a – 6d above.
8) The game continues (repeat steps 6 – 8) until one player empties his/her hand OR there is no card left in the stockpile -> the winner is the hand with minimum number of points (add up all the face values of the cards left in each hand).
9) At the end of the game, your code should - announce the winner - ask whether a user wants to play a new game (Q/q to quit). If yes, your program should repeat steps 2 – 9. (Note 2 -3 may be optional depending on how you write your program.
Explanation / Answer
Hope you find the solutions from the following examples and topics
Look at the following card game
What is a linked list?
A linked list is a set of dynamically allocated nodes, arranged in such a way that each node contains one value and one pointer. The pointer always points to the next member of the list. If the pointer is NULL, then it is the last node in the list.
A linked list is held using a local pointer variable which points to the first item of the list. If that pointer is also NULL, then the list is considered to be empty.
Execute Code
Let's define a linked list node:
Execute Code
Notice that we are defining the struct in a recursive manner, which is possible in C. Let's name our node type node_t.
Now we can use the nodes. Let's create a local variable which points to the first item of the list (called head).
Execute Code
We've just created the first variable in the list. We must set the value, and the next item to be empty, if we want to finish populating the list. Notice that we should always check if malloc returned a NULL value or not.
To add a variable to the end of the list, we can just continue advancing to the next pointer:
Execute Code
This can go on and on, but what we should actually do is advance to the last item of the list, until the next variable will be NULL.
Iterating over a list
Let's build a function that prints out all the items of a list. To do this, we need to use a current pointer that will keep track of the node we are currently printing. After printing the value of the node, we set the current pointer to the next node, and print again, until we've reached the end of the list (the next node is NULL).
Execute Code
Adding an item to the end of the list
To iterate over all the members of the linked list, we use a pointer called current. We set it to start from the head and then in each step, we advance the pointer to the next item in the list, until we reach the last item.
Execute Code
The best use cases for linked lists are stacks and queues, which we will now implement:
Adding an item to the beginning of the list (pushing to the list)
To add to the beginning of the list, we will need to do the following:
This will effectively create a new head to the list with a new value, and keep the rest of the list linked to it.
Since we use a function to do this operation, we want to be able to modify the head variable. To do this, we must pass a pointer to the pointer variable (a double pointer) so we will be able to modify the pointer itself.
Example code that illustrate card game
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>
typedef struct card { //Use a typedef struct to define the cards.
char suit;
int face;
struct card *next;
}card;
void shuffle(card **deck)
{
card *pickup, *pickup_2;
int i, number, max = 51, min = 0, temp_face,x;
char temp_suit;
srand((int)time(NULL));
for(x = 0; x <= 100; x++)
{
number = min+(floor)((max-min+1)*((float)rand()/(RAND_MAX+2))); //used to randomize number
pickup=*deck;//pickup points to to contents of deck
for(i=0;i<number;i++)
{
pickup = pickup->next;
}
number = min+(floor)((max-min+1)*((float)rand()/(RAND_MAX+2))); //used to randomize number
pickup_2=*deck;//pickup_2 points to to contents of deck
for(i=0;i<number;i++)
{
pickup_2 = pickup_2->next;
}
temp_face = pickup->face;
temp_suit= pickup->suit;
pickup->face = pickup_2->face;
pickup->suit = pickup_2->suit;
pickup_2->face = temp_face;
pickup_2->suit = temp_suit;
}
}
int count(card* x)//function used to count the number of cards in players hand
{
card* i;
int cnt = 0; //intialy count is equal to 0
for(i = x; i != NULL; i = i->next)
cnt++; //increments for each card in deck as long as not end (null)
return cnt; //send back the count for the hand
}
void print_round(int x, int y, card* k, card* l)
{
printf(" ******* ******* "); //printing borders
printf(" * * * * ");
printf(" * * * * ");
if((x<10)&&(y<10)) //Printing only numerical cards
{
printf(" * %d * * %d * ", x, y);
}
if((x>9)&&(y<10)) //printing face cards for x only
{
switch(x)
{
case 10:
printf(" * T * * %d * ", y);
break;
case 11:
printf(" * J * * %d * ", y);
break;
case 12:
printf(" * Q * * %d * ", y);
break;
case 13:
printf(" * K * * %d * ", y);
break;
case 14:
printf(" * A * * %d * ", y);
break;
}
}
if((x<10)&&(y>9)) //printing face cards for b only
{
switch(y)
{
case 10:
printf(" * %d * * T * ", x);
break;
case 11:
printf(" * %d * * J * ", x);
break;
case 12:
printf(" * %d * * Q * ", x);
break;
case 13:
printf(" * %d * * K * ", x);
break;
case 14:
printf(" * %d * * A * ", x);
break;
}
}
if((x>9)&&(y>9)) //Printing all face cards
{
switch(x)
{
case 10:
printf(" * T * ");
break;
case 11:
printf(" * J * ");
break;
case 12:
printf(" * Q * ");
break;
case 13:
printf(" * K * ");
break;
case 14:
printf(" * A * ");
break;
}
switch(y)
{
case 10:
printf("* T * ");
break;
case 11:
printf("* J * ");
break;
case 12:
printf("* Q * ");
break;
case 13:
printf("* K * ");
break;
case 14:
printf("* A * ");
break;
}
}
printf(" * * * * "); //printing borders
printf(" * * * * ");
printf(" ******* ******* ");
if(x==y) //Printing War
{
printf("War! %d cards %d cards ", count(k), count(l));
printf(" ");
}
else
{
printf("No War %d cards %d cards ", count(k), count(l));
printf(" ");
}
}
int main(void)
{
int w=0, a_value, b_value, i; //used for "for" loop/card #/52; a value and b value hold values of two hand cards
card *deck, *temp, *current=NULL, *a_top, *a_bottom, *b_top, *b_bottom; //used to reprsent top and bottom of hand a and b
card *temp_top, *temp_bottom; //used for holding top card in playing war
char ans='y'; //used to implement loop again
while(ans != 'n')
{
for(w = 0; w < 52; w++) //loop is used to create card values
{
temp=(card *)malloc(sizeof(card)); // creation of memory
temp->face = (w % 13) + 2; //find remainder in order to represent number values for all 52 cards (2-Ace) for each suit
if(w/13 == 0) //use integer divesion to find suits H, S, D, and C with corresponding value 0-3
{
temp->suit = 'H'; //Hearts
}
if(w/13 == 1)
{
temp->suit = 'S'; //Spades
}
if(w/13 == 2)
{
temp->suit = 'D'; //Diamonds
}
if(w/13 == 3)
{
temp->suit = 'C'; //Clubs
}
if (current==NULL)
{
deck=temp; // setting the head of the list
}
else
{
current->next=temp; // else connecting to previous element
}
current=temp; // updating the current element
temp->next=NULL; // setting pointer to null.
}
shuffle(&deck); //shuffle deck
a_top = deck; //point to beginning of card deck linked list "deck"
a_bottom = deck;
deck = deck->next;
b_top = deck; //starts at second card
b_bottom = deck;
deck = deck->next;
while (deck != NULL) //change bottom deck for linked of hands until reach null at end of the deck
{
a_bottom->next = deck;
a_bottom = deck;
deck = deck->next;
b_bottom->next = deck;
b_bottom = deck;
deck = deck->next;
}
a_bottom->next = NULL; //set bottom of hand equal to null at end to close hand
b_bottom->next = NULL; //same as for hand a
while((a_top != NULL) && (b_top != NULL)) //play game until one player runs out of cards
{
a_value = a_top->face; //set value equal to face for top card
b_value = b_top->face;
print_round(a_value, b_value, a_top, b_top);//print current card values
printf("Next round ");
temp_top = a_top; //set the two cards equal to each other in list for cards currently played
temp_bottom = b_top;
a_top = a_top->next; //have a_top now be equal to the next card in the hand
b_top = b_top->next;
temp_top->next = temp_bottom; //points to card in b hand so winner gets both cards
temp_bottom->next=NULL; //have bottom card in list point to null so add at end of a end
while(a_value == b_value) //while loop for case of war
{
for(i = 0; i < 4; i++) //loop is designed to draw number of cards for war
{
if(a_top == NULL)
{
printf("Player B is the winner! ");
//printf("Do you want to play another game of War? (y/n): ");
//scanf("%c", &ans);
return; //return if player has no cards left
}
temp_bottom->next = a_top; //draw additional cards and put together
temp_bottom = a_top;
a_top = a_top->next;
}
a_value = temp_bottom->face; //last card is put face up
for(i = 0; i < 4; i++) //loop is designed to draw number of cards for war; same loop except for b card
{
if(b_top == NULL)
{
printf("Player A is the winner! ");
//printf("Do you want to play another game of War? (y/n): ");
//scanf("%c", &ans);
return; //return if player has no cards left
}
temp_bottom->next = b_top; //draw additional cards and put together
temp_bottom = b_top;
b_top = b_top->next;
}
b_value = temp_bottom->face; //last card is put face up
print_round(a_value, b_value, a_top, b_top);//print current card values
}
temp_bottom->next = NULL; //points to NULL to end temporary set of cards, then moves onto war rules since exited war
if(a_value > b_value)
{
if(a_top == NULL)
{
a_top = temp_top; //a_top becomes equal to temp if last card was played and they won making temp hand
}
else
{
a_bottom->next = temp_top; //bottom of deck now contains winning cards
a_bottom = temp_bottom;
}
}
if(b_value > a_value) //same as a, but add cards to b
{
if(b_top == NULL)
{
b_top = temp_top;
}
else
{
b_bottom->next = temp_top; //bottom of deck now contains winning cards
b_bottom = temp_bottom;
}
}
//Sleep(3000); //slow game down in order to view each hand
}
if((count(a_top)>count(b_top)))
{
printf("Player A is the winner! ");
}
else
{
printf("Player B is the winner! ");
}
printf("Do you want to play another game of War? (y/n): ");
scanf("%c", &ans);
free(temp);
free(next);
}
return(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.