不能随机卡.其中一些是重复的 [英] Can't randomize cards. Some of them are duplicates

查看:57
本文介绍了不能随机卡.其中一些是重复的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C 新手,我正在尝试随机化 16 张卡片.在较长的评论中,我写了一些我不太清楚的东西......顺便说一下,这是代码:

I'm new to C, I am trying to randomize 16 cards. In the longer comments I wrote something that I don't have much clear... Btw this is the code:

#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 行,我不明白为什么我应该只使用一个 *,因为如果我只使用一个,它应该比较地址,而不是值.如果我放 ** 它不会工作艰难(无限期加载)所以我只留下了一个 *,但我不知道为什么.我认为问题出在检查器内部的某个地方(也许是这个 ** 的东西).

For example in line 38 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 tough (loads indefinitely) so I left only one *, but I don't know why. I think that the problem is somewhere inside checker (maybe this ** thing).

随机输出之一,如您所见,有重复项.c

One of the randomized outputs, as you can see, there are duplicates.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

推荐答案

您应该首先使用数组中没有重复的每张卡片创建套牌:

You should first create the deck with every card without duplicates in the array:

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*/

}

然后使用 Fisher-Yates shuffle 算法对数组...

Then use the Fisher-Yates shuffle algorithm to shuffle the array...

这篇关于不能随机卡.其中一些是重复的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆