I need helping fixing my code, I can\'t find out what the problem is. This is my
ID: 3688847 • Letter: I
Question
I need helping fixing my code, I can't find out what the problem is. This is my code:
#include<stdio.h>
#include<stdlib.h>
char SPADE = '@';
char CLUB = '#';
char HEART = '$';
char DIAMOND = '%';
typedef struct card_s {
char suit;
int face;
struct card_s *listp;
} card;
void print(card* cards) {
if (cards == NULL) {
printf("");
}
else {
card* temp = cards;
printf(" ");
}
int i = 1;
while (temp->listp != NULL) {
if (temp->face == 1)
{
printf("%c A ", temp->suit);
}
else if (temp->face == 11)
{
printf("%c J ", temp->suit);
}
else if (temp->face == 12)
{
printf("%c Q ", temp->suit);
}
else if (temp->face == 13)
{
printf("%c K ", temp->suit);
}
else
{
printf("%c %d ", temp->suit, temp->face);
}
if (i % 4 == 0)printf(" ");
temp = temp->listp;
i++;
}
}
struct card_s* insert(struct card_s* head, char suit, int face) {
if (head == NULL) {
head = (struct card_s*)malloc(sizeof(struct card_s));
head->suit = suit;
head->face = face;
head->listp = NULL;
return head;
}
struct card_s *temp = head;
while (temp->listp != NULL) {
temp = temp->listp;
}
struct card_s* add = (struct card_s*)malloc(sizeof(struct card_s));
add->suit = suit;
add->face = face;
add->listp = NULL;
temp->listp = add;
return head;
}
card* populate(card* cards) {
int i = 0;
for (i = 1; i <= 13; i++) {
cards = insert(cards, CLUB, i);
}
for (i = 1; i <= 13; i++) {
cards = insert(cards, SPADE, i);
}
for (i = 1; i <= 13; i++) {
cards = insert(cards, HEART, i);
}
for (i = 1; i <= 13; i++) {
cards = insert(cards, DIAMOND, i);
}
}
card* swap(int f, int s, card* cards) {
card* temp1 = cards;
card* temp2 = cards;
int i;
for (i = 0; i < f; i++) {
temp1 = temp1->listp;
}
for (i = 0; i<s; i++) {
temp2 = temp2->listp;
}
char suit = temp2->suit;
int face = temp2->face;
temp2->suit = temp1->suit;
temp2->face = temp1->face;
temp1->face = face;
temp1->suit = suit;
return cards;
}
card* shuffle(card* cards) {
int i = 0;
for (i = 0; i < 50; i++) {
int ran = rand() % 51;
cards = swap(i, ran, cards);
}
return cards;
}
card* delete(card* cards) {
if (cards == NULL)
return NULL;
card* temp = cards;
cards = cards->listp;
free(temp);
return cards;
}
void fillList(card* cards) {
struct card_s* player1 = NULL;
struct card_s* player2 = NULL;
int i;
for (i = 0; i<9; i++)
{
card* play1 = cards;
printf("%d ",play1->face);
player1 = insert(player1, cards->suit, cards->face);
cards = delete(cards);
card* play2 = cards;
player2 = insert(player2, cards->suit, cards->face);
cards = delete(cards);
if(i==0)
continue;
}
print(player1);
print(player2);
}
int main(void) {
card* mainList = NULL;
check= insert(check,SPADE,3);
mainList = populate(mainList);
print(mainList);
mainList = shuffle(mainList);
printf(" -----------------------------");
print(mainList);
printf(" -----------------------------");
fillList(mainList);
return;
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
char SPADE = '@';
char CLUB = '#';
char HEART = '$';
char DIAMOND = '%';
typedef struct card_s {
char suit;
int face;
struct card_s *listp;
} card;
void print(card* cards) {
card* temp = cards;
if (cards == NULL) {
printf(" ");
}
else {
printf(" ");
}
int i = 1;
while (temp->listp != NULL) {
if (temp->face == 1)
{
printf("%c A ", temp->suit);
}
else if (temp->face == 11)
{
printf("%c J ", temp->suit);
}
else if (temp->face == 12)
{
printf("%c Q ", temp->suit);
}
else if (temp->face == 13)
{
printf("%c K ", temp->suit);
}
else
{
printf("%c %d ", temp->suit, temp->face);
}
if (i % 4 == 0)printf(" ");
temp = temp->listp;
i++;
}
}
struct card_s* insert(struct card_s* head, char suit, int face) {
if (head == NULL) {
head = (struct card_s*)malloc(sizeof(struct card_s));
head->suit = suit;
head->face = face;
head->listp = NULL;
return head;
}
struct card_s *temp = head;
while (temp->listp != NULL) {
temp = temp->listp;
}
struct card_s* add = (struct card_s*)malloc(sizeof(struct card_s));
add->suit = suit;
add->face = face;
add->listp = NULL;
temp->listp = add;
return head;
}
card* populate(card* cards) {
int i = 0;
for (i = 1; i <= 13; i++) {
cards = insert(cards, CLUB, i);
}
for (i = 1; i <= 13; i++) {
cards = insert(cards, SPADE, i);
}
for (i = 1; i <= 13; i++) {
cards = insert(cards, HEART, i);
}
for (i = 1; i <= 13; i++) {
cards = insert(cards, DIAMOND, i);
}
}
card* swap(int f, int s, card* cards) {
card* temp1 = cards;
card* temp2 = cards;
int i;
for (i = 0; i < f; i++) {
temp1 = temp1->listp;
}
for (i = 0; i<s; i++) {
temp2 = temp2->listp;
}
char suit = temp2->suit;
int face = temp2->face;
temp2->suit = temp1->suit;
temp2->face = temp1->face;
temp1->face = face;
temp1->suit = suit;
return cards;
}
card* shuffle(card* cards) {
int i = 0;
for (i = 0; i < 50; i++) {
int ran = rand() % 51;
cards = swap(i, ran, cards);
}
return cards;
}
card* delete(card* cards) {
if (cards == NULL)
return NULL;
card* temp = cards;
cards = cards->listp;
free(temp);
return cards;
}
void fillList(card* cards) {
struct card_s* player1 = NULL;
struct card_s* player2 = NULL;
int i;
for (i = 0; i<9; i++)
{
card* play1 = cards;
printf("%d ",play1->face);
player1 = insert(player1, cards->suit, cards->face);
cards = delete(cards);
card* play2 = cards;
player2 = insert(player2, cards->suit, cards->face);
cards = delete(cards);
if(i==0)
continue;
}
print(player1);
print(player2);
}
int main(void) {
card* mainList = NULL;
struct card_s* check;
insert(check,SPADE,3);
mainList = populate(mainList);
print(mainList);
mainList = shuffle(mainList);
printf(" -----------------------------");
print(mainList);
printf(" -----------------------------");
fillList(mainList);
return;
}