石头剪刀布

 今天我们是第四讲了,上节课我们做了一个计算大挑战的游戏,没想到今天会做石头剪刀布的游戏吧!


一、导库

工欲善其事,必先利其器。我们需要导入上节课的库,来吧!

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
    
    return 0;
)

二、输入我方的出拳

哎哟,这等于 0!还要不要玩了?赶紧出拳吧!

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    int choice, computerChoice, result, scorea, scoreb;

    srand(time(0));  // 设置随机数种子

    cout << "欢迎来到石头剪刀布游戏!" << endl;
    for (int i = 1; i <= 10; i++){
        cout << "1. 石头 ";
        cout << "2. 剪刀 ";
        cout << "3. 布" << endl;
        cout << "你想出:";
        cin >> choice;
    }
    return 0;
}

三、电脑出结果

我输入了,电脑出不出了?

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    int choice;
    int computerChoice;
    int result;
    int scorea, scoreb;

    srand(time(0));  // 设置随机数种子

    cout << "欢迎来到石头剪刀布游戏!" << endl;
    for (int i = 1; i <= 10; i++){
        cout << "1. 石头 ";
        cout << "2. 剪刀 ";
        cout << "3. 布" << endl;
        cout << "你想出:";
        cin >> choice;
        computerChoice = rand() % 3 + 1;  // 随机生成电脑的选择

        cout << "电脑出";
        switch (computerChoice) {
            case 1:
                cout << "石头" << endl;
                break;
            case 2:
                cout << "剪刀" << endl;
                break;
            case 3:
                cout << "布" << endl;
                break;
            default:
                break;
        }
    }
    return 0;
}

四、出结果

结果呢?结果呢?我们快编掉!

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    int choice;
    int computerChoice;
    int result;
    int scorea, scoreb;

    srand(time(0));  // 设置随机数种子

    cout << "欢迎来到石头剪刀布游戏!" << endl;
    for (int i = 1; i <= 10; i++){
        cout << "1. 石头 ";
        cout << "2. 剪刀 ";
        cout << "3. 布" << endl;
        cout << "你想出:";
        cin >> choice;
        computerChoice = rand() % 3 + 1;  // 随机生成电脑的选择

        cout << "电脑出";
        switch (computerChoice) {
            case 1:
                cout << "石头" << endl;
                break;
            case 2:
                cout << "剪刀" << endl;
                break;
            case 3:
                cout << "布" << endl;
                break;
            default:
                break;
        }
        if (choice == computerChoice) {
            result = 0;  // 平局
        } else if ((choice == 1 && computerChoice == 2) || (choice == 2 && computerChoice == 3) || (choice == 3 && computerChoice == 1)) {
            result = 1;  // 玩家胜利
        } else {
            result = -1;  // 玩家失败
        }

        switch (result) {
            case 0:
                cout << "平局!" << endl;
                break;
            case 1:
                cout << "恭喜你,你赢了!" << endl;
                break;
            case -1:
                cout << "很遗憾,你输了!" << endl;
                break;
            default:
                break;
        }
    }
    return 0;
}
五、得分

赛完了,可是得分在哪里?

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    int choice;
    int computerChoice;
    int result;
    int scorea, scoreb, k;

    srand(time(0));  // 设置随机数种子

    cout << "欢迎来玩石头剪刀布游戏!" << endl;
    cout << "请问你要玩几局后结算结果?(写阿拉伯数字)" << endl;
    cout << "我要玩";
    cin >> k;
    cout << "局。";
    for (int i = 1; i <= k; i++){
        cout << "1. 石头 ";
        cout << "2. 剪刀 ";
        cout << "3. 布" << endl;
        cout << "你想出:";
        cin >> choice;
        computerChoice = rand() % 3 + 1;  // 随机生成电脑的选择

        cout << "电脑出";
        switch (computerChoice) {
            case 1:
                cout << "石头" << endl;
                break;
            case 2:
                cout << "剪刀" << endl;
                break;
            case 3:
                cout << "布" << endl;
                break;
            default:
                break;
        }
        if (choice == computerChoice) {
            result = 0;  // 平局
        } else if ((choice == 1 && computerChoice == 2) || (choice == 2 && computerChoice == 3) || (choice == 3 && computerChoice == 1)) {
            result = 1;  // 玩家胜利
        } else {
            result = -1;  // 玩家失败
        }

        switch (result) {
            case 0:
                cout << "平局!" << endl;
                scorea++;
                scoreb++;
                break;
            case 1:
                cout << "恭喜你,你赢了!" << endl;
                scorea++;
                break;
            case -1:
                cout << "很遗憾,你输了!" << endl;
                scoreb++;
                break;
            default:
                break;
        }
    }
    if (scorea > scoreb){
        cout << scorea << "比" << scoreb << ",你赢了!";
    }else if(scorea == scoreb){
        cout << scorea << "比" << scoreb << ",平局!";
    }else{
        cout << scoreb << "比" << scorea << ",你输了!";
    }
    return 0;
}

大工告成!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值