卡牌洗牌功能实现疑问:类型不匹配错误与代码完善求助
Hey there! Let's break down what's going on with your reshuffle function and fix those issues step by step.
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
arrwith the existing cards from theshoe(right now,arris 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
arrwas 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
shoepointers 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), somallocis 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_tis a struct with integer fields (likerankorsuit), 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_tinstance before assigning to the array.
内容的提问来源于stack exchange,提问作者Stacy Willis

