I am having some trouble with my c code, My Assignment is create linked lists in
ID: 1839821 • Letter: I
Question
I am having some trouble with my c code, My Assignment is create linked lists in c code to create a 52 deck of cards and shuffle and deal the cards inbetween two players. I have successfully created a linked list card deck but am having difficulty shuffling. I have attached my code.
Can you tell me what I could fix in my shuff_deck pointer function?
Also if I could get some suggestions on how to distribute the dynamic list of cards between the two players where they get deleted after distributed?
//
// main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct card_s {
char suit;
int face;
struct card_s *listp;
} card;
card* build_deck(void);
void print_deck(card* deck);
void shuff_deck(card* deck);
void deal_cards (card* deck);
int main(void) {
card* deck = NULL;
deck = build_deck();
print_deck(deck);
shuff_deck(deck);
return 0;
}
card* build_deck(void) {
int i = 0, j = 0;
card* head = NULL;
card* temp = NULL;
card* tail = NULL;
printf ("Create 52-card deck");
for (i = 1; i < 14; ++i) {
for (j = 0; j < 4; ++j){
temp = (card*) malloc(sizeof(card));
temp->face = i;
temp->listp=NULL;
if (j == 0){
temp->suit = 'S';
}
else if (j == 1){
temp->suit = 'H';
}
else if (j == 2){
temp->suit = 'C';
}
else {
temp->suit = 'D';
}
if (head == NULL) {
head = temp;
tail = temp;
}
else {
tail->listp = temp;
tail = temp;
}
}
}
return (head);
}
void print_deck(card* deck){
card* temp = NULL;
int count = 0;
for (temp = deck; temp != NULL; temp = temp->listp) {
if (count%4 == 0){
printf (" ");
}
if (temp->face == 1){
temp->face = 'A';
printf("%c of %c ", temp->face, temp->suit);
}
else if (temp->face == 11){
temp->face = 'J';
printf("%c of %c ", temp->face, temp->suit);
}
else if (temp->face == 12){
temp->face = 'Q';
printf("%c of %c ", temp->face, temp->suit);
}
else if (temp->face == 13){
temp->face = 'K';
printf("%c of %c ", temp->face, temp->suit);
}
else {
printf("%d of %c ", temp->face, temp->suit);
}
count++;
}
}
void shuff_deck(card* deck){
card* temp = NULL;
int deck_face[100];
int rad_face[100];
int deck_suit[100];
int rad_suit[100];
int i = 0, r = 0;
temp = deck;
printf(" Shuffled cards");
for (i = 0; i < 100; ++i) {
r = (rand()%52)+1;
temp->face = deck_face[i];
deck_face[i] = rad_face[r];
rad_face[r] = temp->face;
//printf("%d and %d ",deck_face[i], rad_face[r] );
//printf(" %d", temp->face);
}
for (i = 0; i < 100; ++i) {
r = (rand()%52)+1;
temp->suit = deck_suit[i];
deck_suit[i] = rad_suit[r];
rad_suit[r] = temp->suit;
//printf("%d and %d ",deck_suit[i], rad_suit[r] );
//printf(" %d", temp->suit);
}
print_deck(deck);
deal_cards(deck);
}
void deal_cards(card* deck) {
card* temp = NULL;
int i = 0;
for (i = 0; i <= 8; i = i + 2) {
temp = (card*) malloc(sizeof(card));
temp->face = i;
temp->listp=NULL;
printf(" Player/User's hand");
printf(" %d of %c ", temp->face, temp->suit);
}
for (i = 1; i <= 8; i = i + 2) {
temp = (card*) malloc(sizeof(card));
temp->face = i;
temp->listp=NULL;
printf(" Computer/Dealer's hand");
printf(" %d of %c ", temp->face, temp->suit);
}
}
Explanation / Answer
Here are the steps
Here are the settings i used to configure my logging on my server, named EXCH1. I decided to increase my logging from 30 days to 90 days based on my own requirements – you may need to go longer.
Notes:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.