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

C语言Craps游戏逻辑问题求助:自动跳过用户输入提示

Craps游戏程序异常问题修复求助

我是自学编程一年多的新手,C语言学习时长不足一年,正在使用K.N.King的《C语言现代方法》教材。按照书中任务要求从零编写Craps游戏,程序能运行但不符合预期。

问题表现

部分代码不可达,提示按'R'的语句会自动执行,程序不等待用户输入就继续运行,仅显示打印语句后直接执行后续代码。

开发环境

Mac OS(MacBook M2),用Vim编写代码,终端使用lldb调试器。

我已经用lldb调试了一周,还是没找到问题所在,希望能得到修复指导。以下是我的程序代码:

#include <stdio.h>
#include <stdbool.h> 
#include <stdlib.h> // For rand()

/*
2025-02-19

RULES

Come-Out Roll:
You roll two dice.
 - If the sum is 7 or 11, you win right away.
 - If the sum is 2, 3, or 12 (called “craps”), you lose immediately.
 - If the sum is 4, 5, 6, 8, 9, or 10, that number becomes your point.

Point Phase:
Once a point is set, you keep rolling the dice.
 - If you roll the point number again before rolling a 7, you win.
 - If you roll a 7 before the point, you lose.

*/

int roll_dice(void);
bool play_game(void);

int main() {
    char user_choice;
    int roll = 0;
    // Game loop
    printf("Play a game? (Y/N): ");
    scanf("%c", &user_choice);
    while (user_choice == 'y' || user_choice == 'Y') {
        // Game logic
        play_game();
    }
    return 0;
}

int roll_dice(void) {
    // Generate 2 random numbers 1-6
    int dice1 = (rand() % 6 + 1);
    int dice2 = (rand() % 6 + 1);
    // Sum the numbers
    return dice1 + dice2;
}

bool play_game(void) {
    bool result = false;
    int point = 0;
    // One dice roll
    int roll_result = roll_dice();
    int roll = 1;

    if (roll == 1)
    {
        switch (roll_result) {
            // Immediate win
            case 7: {
                        result = true;
                        point = 0;
                        break;
                    };
            case 11: {
                        result = true;
                        point = 0;
                        break;
                     };
                     // Immediate lose
            case 2: {
                        result = false;
                        break;
                    };
            case 3: {
                        result = false;
                        break;
                    };
            case 12: {
                        result = false;
                        break;
                     };
                     // Point
            case 4: {
                        roll = 2;
                        point = 4;
                        result = true;
                        break;
                    };
            case 5: {
                        roll = 2;
                        point = 5;
                        result = true;
                        break;
                    };
            case 6: {
                        roll = 2;
                        point = 6;
                        result = true;
                        break;
                    };
            case 8: {
                        roll = 2;
                        point = 8;
                        result = true;
                        break;
                    };
            case 9: {
                        roll = 2;
                        point = 9;
                        result = true;
                        break;
                    };
            case 10: {
                        roll = 2;
                        point = 10;
                        result = true;
                        break;
                     };
        }
    }
    if (result == false) {
        printf("Roll 1: %d\n", roll_result);
        printf("You lose!\n");
        return result;
    }
    else if (result == true && roll == 1 && point == 0) {
        printf("Roll 1: %d\n", roll_result);
        printf("You win!\n");
        return result;
    }
    else {
        // Roll 2
        if (roll == 2 && point > 0) {
            printf("Point: %d\n", roll_result);
            char roll_2 = ' ';
            printf("Press 'R' to roll again.\n");
            scanf("%c", &roll_2);

            while (roll_2 == 'R' || roll_2 == 'r') {
                printf("Press 'R' to roll again.(inside while)");
                scanf("%c", &roll_2);
            }

            // Print result and a win/lose message depending on the result
            switch (roll_dice()) {
                case 7: {
                    result = false;
                    printf("Roll 2: %d\n", roll_dice());
                    printf("You lose!\n");
                    return result;
                }
                default: {
                    result = true;
                    printf("Roll 2: %d\n", roll_dice());
                    printf("You win!\n");
                    return result;
                }
            }
        }
    }

    return result;
}

内容的提问来源于stack exchange,提问作者Hexe Decima

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 02:24:55