我的C卡随机播放功能错误地创建了重复的卡

时间:2018-01-05 22:48:44

标签: c

如何修复学校项目的shuffle功能?问题是,当我打印出卡片时,同一张卡片有重复,我只需要将它们洗牌。

我知道副本是因为rand()函数而且我需要一个循环遍历我的数组才能得到任何我尝试过的循环。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>


typedef struct Hand {
    int hand[13];
    char cardPlayed;
}Hand;

typedef struct Card {
    const char *suit;
    const char *rank;
    int *value;
}Card;

//typedef struct Deck {
    //Card deck[52];
//}Deck;

typedef struct Player {
    char name[15];
    int score;
    int playerNum;
    Card deck[52];
}Player;




void fillDeck(Player players[]);
void createPlayer(int numPlayers, Player * const players);
void displayPlayers(int numPlayers, Player players[]);
void incrementScore(Player *player, int score);
void shuffle(Player players[]);
void deal(Player players[]);

const char * pSuits[] = {"Spades", "Hearts", "Clubs", "Diamonds"};

const char * pRanks[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", 
"Q", "K", "A"};

const int * pValues[] = {2,3,4,5,6,7,8,9,10,11,12,13,14};
//Player players[10];


void main()
{
    //Card deck[52];

    Player players[10];

    int numPlayers;

    //int values[13] = { 2,3,4,5,6,7,8,9,10,11,12,13,14 };
     //const char *rank[13] = { "2", "3", "4", "5", "6", "7", "8", "9", 
"10", "J", "Q", "K", "A" };
    //const char *suit[4] = { 'C', 'S', 'D', 'H' };

    srand(time(NULL));

    do {
        printf("Please enter amount of players between 2 and 10:\n");
        scanf("%d", &numPlayers);
    } while (!(numPlayers >= 2 && numPlayers <= 10));

    createPlayer(numPlayers, players);
    displayPlayers(numPlayers, players);
    fillDeck(numPlayers, players);
    shuffle(numPlayers, players);
    deal(numPlayers, players);


}

void fillDeck(int numPlayers, Player players[])
{
    for(int i = 0; i < numPlayers; i++)
    {
        for (int j = 0; j < 52; j++)
        {
            players[i].deck[j].suit = pSuits[j / 13];
            players[i].deck[j].rank = pRanks[j % 13];
            players[i].deck[j].value = pValues[j % 13];
        }
    }

}//fillDeck

void shuffle(int numPlayers, Player players[])
{
    int cardDup;
    int duplicate[52];

    for(int i = 0; i < numPlayers; i++)
    {
            for (int k = 0; k < 52; k++)
            {
                int j = rand() % 52;
                //swap cards
                Player temp;
                temp.deck[k] = players[i].deck[j];
                players[i].deck[k] = players[i].deck[j];
                players[i].deck[j] = temp.deck[k];
            }//for

    }//for

}//shuffle

void deal(int numPlayers, Player players[])
{
    for(int i = 0; i < numPlayers; i++)
    {
        printf("\nPlayer %d \n\n", players[i].playerNum);

        for (int j = 0; j < 52; j++)
        {
            printf("%s of %s \t %d\n", players[i].deck[j].rank, 
players[i].deck[j].suit, players[i].deck[j].value);
        }
    }
}

//void incrementScore(Player player, int score)
//{
    //player->score += score;
//}

void createPlayer(int numPlayers, Player * const players)
{
    /*do {
        printf("Please enter amount of players between 2 and 10:\n");
        scanf("%d", &numPlayers);
    } while (!(numPlayers >= 2 && numPlayers <= 10));*/

    printf("A game has been created with %d Players.\n", numPlayers);

    for (int i = 0; i < numPlayers; i++) 
    {
        printf("Enter Player %d Name: ", (i + 1));
        scanf("%15s", &players[i].name);
        players[i].score = 0;
        players[i].playerNum = (i + 1);
    }
}//end create_player()

void displayPlayers(int numPlayers, Player players[]) {
    printf("Players:\n\n");
    for (int i = 0; i < numPlayers; i++) 
    {
        printf("Player %d \tName: %s \tScore: %d\n", players[i].playerNum, 
players[i].name, players[i].score);
    }
}

0 个答案:

没有答案