代码之家  ›  专栏  ›  技术社区  ›  user16218933

我正试图洗牌,但它又一次展示了我的牌

c
  •  0
  • user16218933  · 技术社区  · 4 年前

    我有一个全套卡片的程序。我想随机地显示它,但每次我显示它时,它都是非缓冲地显示。我的洗牌方法似乎不起作用。我怎样才能修复它,使我的牌组每次都被洗牌。

    #include <stdio.h>
    #include <time.h>
    
    char* suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
    char* face[13] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
        "Ten", "Jack", "Queen", "King"};
    char* deck[4][13];
    
    void display(){
        for (int i = 0; i < 4; ++i)
            for (int j = 0; j < 13; ++j)
                printf("%s of %s\n", face[j], suit[i]);
    }
    
    void shuffleDeck() {
        int shuffler[52];
    
        for (int i = 0; i < 52; ++i) {
            shuffler[i] = i;
        }
    
        //Randomiser
        for (int i = 0; i < 52; ++i) {
            int ind = i+(rand()%(52-i));
    
            int deckI = ind/13;
            int deckJ = ind%13;
    
            int temp = shuffler[i];
            shuffler[i] = shuffler[ind];
            shuffler[ind] = temp;
    
            int temp2 = deck[i/13][i%13];
            deck[i/13][i%13] = deck[deckI][deckJ];
            deck[deckI][deckJ] = temp2;
        }
    }
    
    int main() {
        srand(time(NULL));
        shuffleDeck();
        display();
    }
    
    1 回复  |  直到 4 年前
        1
  •  1
  •   Jonathan Leffler    4 年前

    正如我在评论中指出的:

    • 你的编译器不是在尖叫吗 int temp2 = deck[i/13][i%13]; 你在哪里 char *deck[4][13]; ? 应该是!

    • 你的 display() 函数根本不显示甲板;它显示 face 和 suit 尚未洗牌的数组。

    • 可能是类似于 for (int i = 0; i < 52; i++) printf("%s of %s\n", face[deck[i%13]], suit[deck[i/13]]); ? 你可能需要重新定义 int deck[4][13]; 太多了,或者你需要两个 char * 中每个条目的值 deck ,一件衣服,一件脸。你需要更仔细地考虑你的数据结构。我也不确定洗牌器阵列是否能提供任何帮助。

    即时编写的代码是错误的;它需要使用 deck[i]%13 和 deck[i]/13 (而不是 deck[i%13] 和 deck[i/13] ).这个 甲板 也最好表示为52个整数的数组,而不是2D数组。虽然可以使用2D阵列,但必须对下面的代码进行一些中等规模的大手术。

    这似乎奏效了。它使用的是你的洗牌代码,如果不是费舍尔·耶茨洗牌的话,它就接近于费舍尔·耶茨洗牌。它有一个有偏的随机数发生器。考虑到 rand() ,它几乎无法产生52张牌组中所有可能的洗牌。注意我的 comment 有 %13 和 /13 在错误的地方。因为只有一个源文件,所以除了 main() 是静态的,所有变量也是静态的。这个 面对 和 适合 数组也是常量。

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    static const char * const suit[4] = { "Hearts", "Diamonds", "Clubs", "Spades" };
    static const char * const face[13] =
    {
        "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
        "Ten", "Jack", "Queen", "King"
    };
    
    static int deck[52];
    
    static void displayDeck(const char *tag)
    {
        printf("%s:\n", tag);
        for (int i = 0; i < 52; i++)
            printf("%s of %s\n", face[deck[i] % 13], suit[deck[i] / 13]);
    }
    
    static void shuffleDeck(void)
    {
        for (int i = 0; i < 52; ++i)
        {
            /* Biassed random number generation */
            int ind = i + (rand() % (52 - i));
            int temp = deck[i];
            deck[i] = deck[ind];
            deck[ind] = temp;
        }
    }
    
    static void initDeck(void)
    {
        for (int i = 0; i < 52; i++)
            deck[i] = i;
    }
    
    int main(void)
    {
        srand(time(NULL));
        initDeck();
        displayDeck("Before shuffle");
        shuffleDeck();
        displayDeck("After shuffle");
    }
    

    样本输出:

    Before shuffle:
    Ace of Hearts
    Two of Hearts
    Three of Hearts
    Four of Hearts
    Five of Hearts
    Six of Hearts
    Seven of Hearts
    Eight of Hearts
    Nine of Hearts
    Ten of Hearts
    Jack of Hearts
    Queen of Hearts
    King of Hearts
    Ace of Diamonds
    Two of Diamonds
    Three of Diamonds
    Four of Diamonds
    Five of Diamonds
    Six of Diamonds
    Seven of Diamonds
    Eight of Diamonds
    Nine of Diamonds
    Ten of Diamonds
    Jack of Diamonds
    Queen of Diamonds
    King of Diamonds
    Ace of Clubs
    Two of Clubs
    Three of Clubs
    Four of Clubs
    Five of Clubs
    Six of Clubs
    Seven of Clubs
    Eight of Clubs
    Nine of Clubs
    Ten of Clubs
    Jack of Clubs
    Queen of Clubs
    King of Clubs
    Ace of Spades
    Two of Spades
    Three of Spades
    Four of Spades
    Five of Spades
    Six of Spades
    Seven of Spades
    Eight of Spades
    Nine of Spades
    Ten of Spades
    Jack of Spades
    Queen of Spades
    King of Spades
    After shuffle:
    Six of Spades
    Six of Diamonds
    Three of Diamonds
    Queen of Hearts
    Ace of Spades
    Four of Spades
    Two of Clubs
    Six of Clubs
    Jack of Diamonds
    Five of Clubs
    Two of Diamonds
    Seven of Spades
    Nine of Spades
    Five of Hearts
    Three of Clubs
    Jack of Clubs
    Five of Diamonds
    Eight of Hearts
    Two of Hearts
    Five of Spades
    King of Hearts
    Queen of Clubs
    Nine of Diamonds
    Four of Diamonds
    Seven of Clubs
    King of Clubs
    Two of Spades
    Four of Hearts
    Eight of Clubs
    Seven of Diamonds
    King of Diamonds
    Ten of Spades
    Six of Hearts
    Seven of Hearts
    Ten of Clubs
    Ace of Clubs
    Eight of Diamonds
    Ace of Diamonds
    Four of Clubs
    Queen of Spades
    Three of Hearts
    King of Spades
    Eight of Spades
    Nine of Hearts
    Nine of Clubs
    Three of Spades
    Jack of Hearts
    Queen of Diamonds
    Ten of Hearts
    Ace of Hearts
    Ten of Diamonds
    Jack of Spades
    

    我还使用了诊断版本的 displayDeck() :

    static void displayDeck(const char *tag)
    {
        printf("%s:\n", tag);
        for (int i = 0; i < 52; i++)
            printf("%2d [%2d] %s of %s\n", i, deck[i], face[deck[i] % 13], suit[deck[i] / 13]);
    }
    

    这帮助我发现了代码中间版本中的错误。它说明了如何确保打印有助于诊断问题。