我正尝试向5只不同的手分发5张卡,但是当第一张卡分发给每个手时,接下来的4张卡是第一张卡的副本(与玩家无关)。 AFAIK内部的for循环第一次将连续的卡分配给每个Hand,但似乎不适用于其余4张卡。我的for循环中有什么错误?
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);
答案 0 :(得分:1)
其他人对代码编译和警告发表了评论,因此我将跳过。在该行中:
AddToTail(Deck.Items[h], &Hands[h]);
记下您正在使用的Deck中的哪个索引。每次循环执行时,它都会使用相同的h值。考虑如何更改此设置,以便它不会在卡座中使用相同的索引(您每次都需要新卡,但是对于像这样的相同索引是不可能的。)