I'm trying to deal 5 cards to 4 different hands, however while the first card is distributed to each Hand, the next 4 cards are the copy of the first card (respective to the player). AFAIK the inner for loop distributes successive cards to each Hand the first time, but doesn't seem to work for the rest of the 4 cards. What is the error in my for loop?
main.c
#include "functions.h"
#include <stdlib.h>
#include <stdio.h>
#include <crtdbg.h>
int main(int argc, char ** argv)
{
const int SuitSize = 13; //const int of 13 faces for the double for loop
int i,j,k; //iterating variables
Vector Deck; //create a vector Deck (with size, capacity, and ptr to card array)
VectorInit(&Deck, 52); //initialize deck with 10, will grow later
Card cardArray[52]; //create a Card type array of 52, fill with unshuffled Deck of Cards in for loop(s) below
//outer loop is for Suits
for (k = Clubs; k <= Spades; k++)
{
//inner for loop is for face
for (j = Deuce; j <= Ace; j++)
{
cardArray[j - Deuce + k * SuitSize].suit = k; //j-Deuce is 2 - face pos plus k * 13 k
cardArray[j - Deuce + k * SuitSize].face = j;
}
}
//copy cardArray into Deck using AddToTail for the correct order
for (i = 0; i < 52; ++i)
{
AddToTail(cardArray[i], &Deck);
Deck.Items[i];
}
//array of vectors, each with ptr to an array of cards
Vector Hands[4];
for (int v = 0; v < 4; ++v)
{
VectorInit(&Hands[v], 5);
}
for (int c = 0; c < 5; ++c)
{
for (int h = 0; h < 4; ++h)
{
AddToTail(Deck.Items[h], &Hands[h]);
}
}
system("pause");
}
functions.c
#include "functions.h"
void VectorInit(Vector * vect, int capacity)
{
vect->size = 0; //initialize the size to 0 (no elements)
vect->capacity = capacity; //initialize capacity to 10, can grow later
vect->Items = malloc(sizeof(Card) * capacity);
}
void Grow(Vector * vect)
{
int i;
if (vect->capacity < 0) //if the capacity ever reaches 0 or below, reset it to 10
vect->capacity = 10;
else
vect->capacity *= 2; // 'grow' the capacity by doubling it
Card *newStore;
newStore = (Card*)malloc(vect->Items, (vect->capacity * sizeof(Card)));
vect->Items = newStore;
free(vect->Items);
}
void Add(Card card, int pos, Vector * vect)
{
int i;
if (vect->size == vect->capacity) //if the num of elements = capacity, the array is full - Grow it
Grow(vect);
//for (i = vect->size; i > pos; --i) //copy everything over by 1 to make an empty spot at pos
//{
// vect->Items[i] = vect->Items[i - 1];
//}
vect->Items[pos] = card; //add a provided index and value for insertion in Items array
++(vect->size);
}
void AddToTail(Card value, Vector * vect)
{
Add(value, vect->size, vect);
}
functions.h
#pragma once
typedef enum {Clubs,Diamonds,Hearts,Spades} Suits;
typedef enum{Deuce = 2,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King,Ace} Face;
typedef struct card
{
Suits suit;
Face face;
} Card;
typedef struct vector
{
Card * Items; //pointer to array of cards
int size; //current num of elements
int capacity; //max num of elements
}Vector;
void VectorInit(Vector * vect, int capacity);
void Grow(Vector * vect);
void Add(Card card, int pos, Vector * vect);
void AddToTail(Card value, Vector * vect);