Hi, it's the guy with the card game code again...
main():
Code:
#include <stdlib.h>#include <stdio.h>
#include <time.h>
#include "bjfunctions.h"
int main()
{
srand((unsigned int)time(NULL)); //initializing srand
struct card *deckAr = createDeck(); //creating the struct card deck array
for (int i = 0; i < 100000; i++)
{
shuffleDeck(deckAr);
}
struct card *player1hand = (struct card*)malloc(sizeof(player1hand));
struct card *player2hand = (struct card*)malloc(sizeof(player2hand));
struct card *househand = (struct card*)malloc(sizeof(househand));
int player1NCards = 0, player2NCards = 0, houseNCards = 0;
player1NCards = 2;
deal(player1hand, deckAr, player1NCards, 2);
for (int j = 0; j < player1NCards; j++){
printf("Card %d %d of %d\n\n", j+1, player1hand[j].value, player1hand[j].suit);
}
for (int j = 0; j < player1NCards; j++) {
printf("Card %d %d of %d\n\n", j + 1, deckAr[j].value, deckAr[j].suit);
}
return 0;
}
bjfunctions.c:
Code:
#include "bjfunctions.h"#include <stdlib.h>
#include <stdio.h>
void shuffleDeck(struct card *deck) {
int cardSwitch1, cardSwitch2;
struct card temp;
cardSwitch1 = (rand() % 52);
cardSwitch2 = (rand() % 52);
temp = deck[cardSwitch1];
deck[cardSwitch1] = deck[cardSwitch2];
deck[cardSwitch2] = temp;
}
const char *suitName(const int suitNum)
{
switch (suitNum) {
case 1: return "Spades";
case 2: return "Clubs";
case 3: return "Hearts";
case 4: return "Diamonds";
}
return "(invalid suit)";
}
/*
int dealNCards(struct card playerHandAr[], struct card deckAr[], int N, int nOfCards)
{
for (int i = 0; i < N; i++)
playerHandAr[nOfCards] = deckAr[0];
moveToBack(deckAr);
nOfCards++;
return nOfCards;
}
void moveToBack(struct card deckAr[]) {
struct card temp;
for (int i = 0; i < 52; i++){
temp = deckAr[0];
}
}*/
const char *valueName(const int valueNum)
{
switch (valueNum) {
case 1: return "Ace";
case 2: return "Two";
case 3: return "Three";
case 4: return "Four";
case 5: return "Five";
case 6: return "Six";
case 7: return "Seven";
case 8: return "Eight";
case 9: return "Nine";
case 10: return "Ten";
case 11: return "Jack";
case 12: return "Queen";
case 13: return "King";
}
return "(invalid value)";
}
void *deal(struct card *ptr, struct card *deckArray, int currentNofCards, int nOfCards)
{
ptr = (struct card *)realloc(ptr, sizeof(ptr) * currentNofCards + nOfCards);
for (int i = currentNofCards; i < nOfCards+currentNofCards; i++)
{
ptr[i+currentNofCards] = deckArray[i];
}
}
struct card makeCard(int suit, int value) {
struct card tempCard;
tempCard.suit = suit;
tempCard.value = value;
return tempCard;
}
struct card *createDeck()
{
const size_t count = 52;
struct card *deck;
// Allocate new deck of 'count' cards.
deck = malloc(count * sizeof deck[0]);
/*if (!deck) {
fprintf(stderr, "createDeck(): Out of memory!\n");
exit(EXIT_FAILURE);
}*/
int i = 0;
struct card *deckArrayPtr = deck;
for (int suit = 1; suit < 5; suit++)
{
{
for (int value = 1; value < 14; value++) {
deck[i] = makeCard(suit, value);
i++;
}
}
}
return deck;
}
Output is something like:
Card 1 -33686019 of -842150451
Card 2 -394304501 of -1646174777
Card 1 3 of 1
Card 2 9 of 4
Press any key to continue . . .
any ideas? I've been at this piece of ........ function for hours
Code:
#pragma once
struct card {
int suit;
int value;
};
void shuffleDeck(struct card *deck);
int dealNCards(struct card deckAr[], int N, int nOfCards);
moveToBack(struct card deckAr[]);
struct card *createDeck();
struct card makeCard(int suit, int value);
const char *suitName(const int suitNum);
const char *valueName(const int valueNum);
void deal(struct card *ptr, struct card *deckArray, int currentNofCards, int nOfCards);