1 solutions

  • -5
    @ 2025-9-7 13:02:48

    #include #include #include #include using namespace std;

    void Start(); void GetResults(int secretNumber, int maxrand, int& remainingLives);

    void Start() { int difficulty; int maxrand = 6; int lives = 5;

    cout << "请选择难度吧:\n"; 
    cout << "1 简单: (0-50)\n";
    cout << "2 正常: (0-100)\n";
    cout << "3 困难: (0-300)\n";
    cout << "4 魔鬼: (0-500)\n";
    cout << "不玩了输入其他的数\n";
    
    cin >> difficulty;
    
    if (cin.fail()) {
        cout << "输入无效,游戏结束\n";
        return;
    }
    
    switch (difficulty) {
        case 1:
            maxrand = 50;  
            break;
        case 2:
            maxrand = 100;
            break;
        case 3:
            maxrand = 300;
            break;
        case 4:
            maxrand = 500;
            break;
        default:
            cout << "游戏结束,谢谢游玩!\n";
            return;
    }
    
    srand((unsigned)time(NULL)); 
    int secretNumber = rand() % (maxrand + 1); 
    
    cout << "你有 " << lives << " 次机会来猜数字!\n\n";
    GetResults(secretNumber, maxrand, lives);
    

    }

    void GetResults(int secretNumber, int maxrand, int& remainingLives) { while (remainingLives > 0) { cout << "剩余生命: " << remainingLives << "\n"; cout << "输入一个数吧 (0-" << maxrand << "): ";

        int guess;
        cin >> guess;
        
        if (cin.fail()) {
            cout << "请输入有效的数字!\n";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            continue;
        }
        
        if (guess > maxrand || guess < 0) {
            cout << "超出了范围 (0-" << maxrand << "),再试试\n\n";
            continue;
        }
        
        if (guess == secretNumber) {
            cout << "?? 你赢了,太棒了!猜对了!\n\n";
            break;
        } else if (guess > secretNumber) {
            cout << "太大了,再试试\n\n";
        } else {
            cout << "太小了,再试试\n\n";
        }
        
        remainingLives--;
    }
    
    if (remainingLives == 0) {
        cout << "?? 游戏结束!正确答案是: " << secretNumber << "\n\n";
    }
    
    cout << "想再玩一次吗?(y/n): ";
    char playAgain;
    cin >> playAgain;
    
    if (playAgain == 'y' || playAgain == 'Y') {
        Start();
    } else {
        cout << "谢谢游玩!\n";
    }
    

    }

    int main() { cout << "** 原版的猜数游戏 \n"; cout << "* 作者: 我是小鹤老师 ***\n"; cout << "这个游戏的目标是猜一个数字.\n"; cout << "会告诉你数字是太大了还是太小了\n"; cout << "与要寻找的秘数相比太小了.\n\n";

    Start();
    
    return 0;
    

    }

    Information

    ID
    337
    Time
    1000ms
    Memory
    64MiB
    Difficulty
    1
    Tags
    # Submissions
    195
    Accepted
    136
    Uploaded By