不能随机的卡。有些牌是重复的

问题描述 投票:0回答:2

我是新手,我是想随机16张卡。在较长的评论中,我写了一些我不太清楚的东西... ... 顺便说一下,这是代码。

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

#define FACES 4
#define SUITS 4

void shuffle(char *wFace[], char *wSuit[], char *wMixed[][20]);     // prototype
size_t checker(char *wwMixed[][20], size_t current);                // prototype

int main(){
    char *face[] = {"Jack", "Queen", "King", "Ace"};
    char *suit[] = {"Hearts", "Spades", "Diamonds", "Clubs"};
    char *mixed_cards[FACES*SUITS][20] = {0};           // initialize the final array

    shuffle(face,suit,mixed_cards);         // send to shuffle the pointer array

    for (size_t k=0; k<FACES*SUITS;++k){        // at the end
        printf("%s\n", *(mixed_cards+k));       // it prints the results
    }
}

void shuffle(char *wFace[], char *wSuit[], char *wMixed[][20]){

    srand(time(NULL));

    for (size_t j = 0; j<(FACES*SUITS); ++j){       // for every card
        do {
        size_t face = rand() % FACES;           // choose randomly
        size_t suit = rand() % SUITS;           // choose randomly
        sprintf(*(wMixed+j), "%s of %s", *(wFace+face), *(wSuit+suit));     // copy to *(wMixed+j) so in mixed_card[j][0] matrix
        } while (checker(wMixed, j));           // it does the cycle until checker function says that the string is unique
    }
}

size_t checker(char *wwMixed[][20], size_t current){
    for (size_t i=0; i<FACES*SUITS;++i){
        if ( (*(wwMixed+current) == *(wwMixed+i)) && (current!=i) ) {       // I don't get why I should use ONLY ONE *, since if I use only one it should be comparing ADDRESSES, NOT VALUES. if I put ** it doesn't work though, but I don't know why.
            return 1;           // the string is already in use, so it has to continue to randomize. it returns 1 and the while cycle continues so new rand string is created 
        }
    }
    return 0;                   // otherwise, if this for cycle doesn't find any duplicate, it returns 0 to shuffle function and while stops so j is increased (in the other for)
}

比如说在第38行,我不明白为什么我应该只用一张牌 *因为如果我只使用一个,它应该是比较地址,而不是值。** 它不工作的强硬(负载无限期),所以我只留下一个。*但我不知道为什么,我认为问题出在checker内部的某个地方(可能是这个)。** 的东西)。)

其中一个随机化的输出,你可以看到,有重复的.c。

Jack of Diamonds
Queen of Clubs
Ace of Hearts
Jack of Clubs
Ace of Diamonds
King of Diamonds
Ace of Spades
Jack of Spades
King of Spades
Jack of Clubs
Jack of Spades
King of Spades
King of Clubs
Queen of Hearts
King of Spades
King of Spades
c arrays pointers random shuffle
2个回答
0
投票

写作 (*(wwMixed+current) == *(wwMixed+i)) 基本上和写作是一样的。

(wwMixed[current] == wwMixed[i])

这就是为什么你只需要一个 *.

由于 wwMixed[i] 包含一个字符串,而你想比较字符串,我建议你使用 strcmp():

if ( (strcmp(*(wwMixed+current),*(wwMixed+i)) == 0) && (current!=i) )
    return 1;


0
投票

你应该首先创建一副牌,牌阵中没有重复的牌。

void shuffle(const char *wFace[], const char *wSuit[], char wMixed[][20]){
    srand(time(NULL));
    for (size_t i=0; i<FACES; ++i){
        for (size_t j=0; j<SUITS; ++j){
            sprintf(wMixed[i*SUITS+j], "%s of %s\n", *(wFace+i), *(wSuit+j));  // save the card string
            printf("%s", wMixed[i*SUITS+j]);   // print card string
        }
    }

    /*Add Fisher-Yates shuffle here*/

}

然后用 费舍尔-耶茨洗牌 阵列洗牌的算法... ...

© www.soinside.com 2019 - 2024. All rights reserved.