Вложено для петли для раздачи карт - PullRequest
0 голосов
/ 15 декабря 2018

Я пытаюсь раздать 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");

}

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);

1 Ответ

0 голосов
/ 15 декабря 2018

Другие люди прокомментировали компиляцию кода и предупреждения, поэтому я пропущу это.В строке:

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

Запишите, какой индекс в колоду вы используете.Каждый раз, когда цикл выполняется, он использует одни и те же значения для h.Подумайте, как это изменить, чтобы он не использовал один и тот же индекс в колоде (вам нужны новые карты каждый раз, но это невозможно с таким же индексом, как этот).

...