c++猜拳小游戏(AI出文)

用AI助手写了一个c++猜拳游戏,可以看看!

#include 
#include 

using namespace std;

int main()
{
    srand(time(NULL)); // 设置随机数种子

    int userChoice, computerChoice;

    cout << "石头-剪刀-布\n";
    cout << "请出拳:\n";
    cout << "1. 石头\n";
    cout << "2. 剪刀\n";
    cout << "3. 布\n";
    cin >> userChoice;

    computerChoice = rand() % 3 + 1; // 生成1-3的随机数

    cout << "你出了 ";

    switch (userChoice) {
        case 1:
            cout << "石头\n";
            break;
        case 2:
            cout << "剪刀\n";
            break;
        case 3:
            cout << "布\n";
            break;
        default:
            cout << "无效选择\n";
            return 0;
    }

    cout << "电脑出了 ";

    switch (computerChoice) {
        case 1:
            cout << "石头\n";
            break;
        case 2:
            cout << "剪刀\n";
            break;
        case 3:
            cout << "布\n";
            break;
    }

    // 判断胜负
    if (userChoice == computerChoice) {
        cout << "平局\n";
    } else if ((userChoice == 1 && computerChoice == 2) ||
               (userChoice == 2 && computerChoice == 3) ||
               (userChoice == 3 && computerChoice == 1)) {
        cout << "你赢了\n";
    } else {
        cout << "电脑赢了\n";
    }

    return 0;
}

你可能感兴趣的:(c++,游戏)