C Programming Arrays and Linked Lists
I have a deck of 52 cards in a linked list and I want to shuffle the cards so I can place them in each player's hand. However, I'm finding it very hard to randomize nodes in the list to create this shuffle function. I am trying to now put the list nodes addresses into an array and then randomize that and share the cards from the array which would be initially pointing to nodes in the linked list. I can't seem to get the code to work. Please help.
Arrays and Linked Lists Pt2
I'm doing a card game which requires me to create a deck of 52 cards, shuffle the deck then share the cards to 4 players (13 cards in each player's hand). From my understanding, I wanted to use a link list so when the cards are shuffled and given to each player then the deck would be empty. Also, when a player plays a card from his hand, that card should not be there. Initially, I was thinking that if I use the array to store the cards, I would not be able to remove it from the array, but just set that index in the array to NULL. With linked lists, I would be able to remove a node when it is gone instead of just leaving an empty node lying around. However, it is challenging to use a linked list to do this. This is where I'm stuck. To use arrays or linked lists
Deck Array Printing Problems
I've created a deck array and I'm running into a problem when I want to print my cards with their suit and face.
Here is the code:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define DECK_SIZE 52
#define NSUITS 4
#define NFACES 13
#define NVALUES 13
/*card structure definition*/
typedef struct Card{
int face; /* define face */
char suit; /* define suit */
}Card, *CardPtr; /* end structure card */
/*Face array*/
char *faces[] = {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
/*function prototypes*/
CardPtr createDeck();
void printCard(CardPtr deck, int x);
int main(){
createDeck();
return 0;
}
CardPtr createDeck(){
CardPtr deckArray[DECK_SIZE]; /*creates deck*/
int i,j; /*counters*/
int k=1;
for (j = 0; j < 4; j++){
for (i = 1; i < 14; i++){
if (j == 0) {
CardPtr newCard = (CardPtr)malloc(sizeof(Card)); /*allocates space to newly created card*/
newCard->suit = 'S'; /*Spades*/
newCard->face = i;
deckArray[k-1]= newCard;
k++;
}
if (j == 1) {
CardPtr newCard = (CardPtr)malloc(sizeof(Card)); /*allocates space to newly created card*/
newCard->suit = 'H'; /*Hearts*/
newCard->face = i;
deckArray[k-1]= newCard;
k++;
}
if (j == 2) {
CardPtr newCard = (CardPtr)malloc(sizeof(Card)); /*allocates space to newly created card*/
newCard->suit = 'D'; /*Diamonds*/
newCard->face = i;
deckArray[k-1]= newCard;
k++;
}
if (j == 3) {
CardPtr newCard = (CardPtr)malloc(sizeof(Card)); /*allocates space to newly created card*/
newCard->suit = 'S'; /*Clubs*/
newCard->face = i;
deckArray[k-1]= newCard;
k++;
}
}
}
int l = 0;
for(l=0;l<52;l++){
printCard(deckArray, l);
}
return 0;
}
/*prints card to screen */
void printCard(CardPtr deckArray, int i){
char *suit;
switch (deckArray[i].suit) {
case 'D':
suit = "Diamonds";
break;
case 'C':
suit = "Clubs";
break;
case 'H':
suit = "Hearts";
break;
case 'S':
suit = "Spades";
break;
}
printf("%s of %s\n", faces[i], suit);
return;
}
I can easily do a for loop to print all the cards by incrementing the index of the array.
However, I would like if my printCard function could be something separate so I can use it in other parts of the program. I might be confusing the arrays for the printCard function.
Kindly guide me on the right track.