You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

卡牌洗牌功能实现疑问:类型不匹配错误与代码完善求助

Hey there! Let's break down what's going on with your reshuffle function and fix those issues step by step.

Fixing Your Card Shuffling Function: Type Mismatch & Fisher-Yates Implementation

First, let's tackle that card_t vs int type mismatch error and then refine the shuffle logic to work properly with your shoe_t structure.

1. Root Cause of the Type Error

From your code snippet, it looks like you might be accidentally mixing card_t values with integers somewhere (maybe when initializing the arr array or assigning values to it). Since card_t is a custom type (likely a struct or enum representing a playing card), you can't directly assign an int to it or vice versa without explicit conversion (which you probably don't want here).

2. Fixing the Shuffle Logic (Fisher-Yates Algorithm)

Your current code starts with the right idea for Fisher-Yates, but it's missing key steps:

  • You need to first populate your temporary arr with the existing cards from the shoe (right now, arr is uninitialized garbage)
  • After shuffling the temporary array, you need to copy the shuffled cards back into the shoe's card storage

Here's a corrected version of your function, with explanations:

#include <stdlib.h> // For rand()
#include <string.h> // For memcpy() (optional, but useful)

void reshuffle(shoe_t* shoe) {
    if (!shoe || shoe->num_decks <= 0) {
        return; // Guard against invalid input to prevent crashes
    }

    int total_cards = 52 * shoe->num_decks;
    // Use dynamic allocation to avoid stack overflow for large deck counts
    card_t* arr = malloc(total_cards * sizeof(card_t));
    if (!arr) {
        return; // Handle allocation failure gracefully
    }

    // Step 1: Copy existing cards from the shoe into our temporary array
    memcpy(arr, shoe->cards, total_cards * sizeof(card_t)); 
    // If memcpy isn't allowed, use a loop instead:
    // for (int i = 0; i < total_cards; i++) {
    //     arr[i] = shoe->cards[i];
    // }

    printf("SHUFFLING!\n");

    // Step 2: Execute Fisher-Yates Shuffle
    for (int i = 0; i < total_cards - 1; i++) {
        // Generate a random position between i and the end of the array
        int random_position = i + rand() % (total_cards - i);

        // Swap the current card with the random card
        card_t temp = arr[random_position];
        arr[random_position] = arr[i];
        arr[i] = temp;
    }

    // Step 3: Copy the shuffled cards back into the shoe
    memcpy(shoe->cards, arr, total_cards * sizeof(card_t));
    // Or use the loop version again if needed

    free(arr); // Clean up the temporary array
    shoe->next_card = 0; // Reset the shoe's pointer to the first card (adjust if your field name differs)
}

3. Key Notes to Avoid Future Issues

  • Always initialize your arrays: Your original arr was uninitialized, which would lead to undefined behavior. We either copy the shoe's cards into it or initialize each element properly.
  • Seed the random generator: Don't forget to call srand(time(NULL)) once at the start of your program—without this, you'll get the exact same shuffle every time you run the code.
  • Check for valid inputs: Adding guards for NULL shoe pointers or non-positive deck counts prevents crashes from bad data.
  • Dynamic vs static allocation: A static array like card_t arr[52*shoe->num_decks] can cause stack overflow for large deck counts (like 6-8 decks in blackjack), so malloc is safer for variable-sized data.

4. If the Type Mismatch Persists

Double-check your card_t definition and where you're assigning values:

  • If card_t is a struct with integer fields (like rank or suit), make sure you're assigning the entire struct, not just the integer field.
  • If you're reading card values from input, ensure you're converting them into a valid card_t instance before assigning to the array.

内容的提问来源于stack exchange,提问作者Stacy Willis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:13:44