Haha, right, code might help you. So I have made a temporary fix that keeps everything printing right, but it seems unnecessary. Here is the code for creating the deck of cards.
Code:
#include "deck.h"
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
void intializeDeck()
{
struct Card* newCard = NULL;
int deckOf52 = 1; // keeps track of how mand cards are in the deck and gives deckValue its value.
int x = 0; // gCard array value.
int y = 2; // used to give numCard face value.
int z = 1; // used to give card numarical value.
while (deckOf52 < 53)
{
if(deckOf52 < 14)
{
gCard[ x ].suit = 'h';
}
if(deckOf52 > 13 && deckOf52 < 27)
{
gCard[ x ].suit = 'd';
}
if(deckOf52 > 26 && deckOf52 < 40)
{
gCard[ x ].suit = 'c';
}
if(deckOf52 > 39)
{
gCard[ x ].suit = 's';
}
if( z == 1 )
{
gCard[ x ].nonNumCard = 'A';
}
else
{
if( z == 11 )
{
gCard[ x ].nonNumCard = 'J';
}
else
{
if( z == 12 )
{
gCard[ x ].nonNumCard = 'Q';
}
else
{
if( z == 13 )
{
gCard[ x ].nonNumCard = 'K';
}
else
{
gCard[ x ].numCard = y;
}
}
}
}
gCard[ x].cardValue = z;
z += 1;
if( z == 14 )
z = 1;
gCard[ x ].deckValue = deckOf52;
y += 1;
if(gCard[ x ].nonNumCard == 'A' || gCard[ x ].nonNumCard == 'J' || gCard[ x ].nonNumCard == 'Q' || gCard[ x ].nonNumCard == 'K')
{
y = 2;
}
if( y == 11 )
{
y = 2;
}
deckOf52 += 1;
x += 1;
newCard = realloc(gCard , 1 + sizeof(struct Card));
gCard = newCard;
}
}
and how I'm printing them now with the fix I mentioned
Code:
#include "deck.h"
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
struct Card* gCard = NULL;
int main ()
{
int x = 0;
gCard = malloc(sizeof(struct Card));
printf("Black Jack!\n\n");
while(x < 52)
{
intializeDeck(); // function used to be called before this loop when I was having the problem
if(gCard[ x ].nonNumCard == 'A' || gCard[ x ].nonNumCard == 'J' || gCard[ x ].nonNumCard == 'Q' || gCard[ x ].nonNumCard == 'K')
printf("%c of %c deckValue = %d cardValue = %d\n\n" , gCard[ x ].nonNumCard , gCard[ x ].suit , gCard[ x ].deckValue , gCard[ x ].cardValue);
else
printf("%d of %c deckValue = %d cardValue = %d\n\n" , gCard[ x ].numCard , gCard[ x ].suit , gCard[ x ].deckValue , gCard[ x ].cardValue);
x += 1;
}
return 0;
}
OH, and here is the data struct declaration.
Code:
struct Card
{
int cardValue; // Numerical value of card, such as Jack = 11, A = 1 or 11, Queen = 12, etc.
int deckValue; // deckValue = the numerical value of the card in the deck of 1 - 52, starting from "Ah" being "1", "2h" being "2", etc.
char suit;
char nonNumCard;
int numCard;
};
struct Card* gCard;