c - 嵌套 For 循环发牌

标签 c vector struct enums nested-loops

我试图将 5 张牌发给 4 个不同的手牌,但是当第一张牌分发到每手牌时,接下来的 4 张牌是第一张牌的副本(相对于玩家)。 AFAIK 内部 for 循环第一次将连续的牌分发给每手牌,但似乎不适用于其余 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");

}

函数.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);
}

函数.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);

最佳答案

其他人已经评论了代码编译和警告,所以我将跳过它。行中:

AddToTail(Deck.Items[h], &Hands[h]);

记下您正在使用的 Deck 的索引。每次执行循环时,它都会使用相同的 h 值。考虑如何更改此设置,以便它不会在牌组中使用相同的索引(您每次都需要新卡,但使用像这样的相同索引是不可能的)。

关于c - 嵌套 For 循环发牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53789022/

相关文章:

c - C 中的字符串操作,<optimized out> 错误

c - 带符号的递归调用函数

c++ - vector 删除迭代器

c++ - 使用指针 vector 的问题(附代码)C++(以及对其使用便利性的怀疑)

arrays - 在结构数组中设置特定的变量

c - 我尝试添加链接列表时遇到问题,但调试器说无法访问临时内存

c - Berkeley DB SIGBUS 错误

c++ - 将 std::stack 复制到 std::vector

matlab - 在结构体元胞数组中查找值

c - c 中结构位值的问题