C语言开发Craps游戏遇问题:点数获胜判定逻辑失效
Fixing the Win Condition in Your C Craps Game
Hey there! Let’s work through that frustrating issue where your Craps game isn’t triggering a win when the player matches their point. I’ve seen this exact problem a bunch of times—usually it boils down to a small oversight in variable persistence or loop logic. Let’s break it down:
Common Culprits to Check
- Point variable scope: If you’re defining your
pointvariable inside the loop that handles re-rolling, it’ll reset every time you roll, so the program can’t remember the original point to compare against. Make sure it’s declared outside that loop. - Accidental assignment instead of comparison: Double-check that you’re using
==(equality check) instead of=(assignment) in your win condition. This is a super easy mistake to make in C! - Missing loop for re-rolls: If you’re not looping to keep rolling until the player hits their point or a 7, the program might only check once and move on without triggering the win.
Example Fixed Code Snippet
Here’s a cleaned-up section of code that handles the point phase correctly, including the name input you mentioned:
#include <stdio.h> #include <stdlib.h> #include <time.h> // Helper function to roll two dice and return the sum int rollDice() { return (rand() % 6 + 1) + (rand() % 6 + 1); } int main() { char playerName[50]; int diceSum, point; // Get player name printf("Enter your name here: "); scanf("%s", playerName); // Seed random number generator srand((unsigned int)time(NULL)); // First roll diceSum = rollDice(); printf("First roll: %d\n", diceSum); switch(diceSum) { // Immediate win cases case 7: case 11: printf("Congrats %s, you win on the first roll!\n", playerName); break; // Immediate loss cases case 2: case 3: case 12: printf("Sorry %s, you crapped out—you lose.\n", playerName); break; // Enter point phase default: point = diceSum; printf("%s, your point is %d. Keep rolling until you hit %d or 7!\n", playerName, point, point); // Loop until win or loss while(1) { diceSum = rollDice(); printf("You rolled: %d\n", diceSum); // Check for win first if(diceSum == point) { printf("Yes! %s, you matched your point—YOU WIN!\n", playerName); break; // Exit loop, game over } // Check for loss else if(diceSum == 7) { printf("Oh no %s, you rolled a 7—you lose.\n", playerName); break; // Exit loop, game over } // If neither, keep rolling else { printf("Keep going...\n"); } } break; } return 0; }
Key Fixes in This Code
- Persistent point variable:
pointis declared outside the re-roll loop, so it holds the original value through every iteration. - Clear loop structure: The
while(1)loop keeps rolling until either the win or loss condition is met, ensuring no rolls are missed. - Explicit condition checks: Uses
==for comparisons, and clearly separates win/loss logic so there’s no ambiguity.
Go through your code and verify these points—chances are you’ll find a small tweak that gets your win condition working perfectly.
内容的提问来源于stack exchange,提问作者nickb
相关产品推荐
相关产品推荐

