用C++做一个石头剪刀布的游戏

今天,我想用电脑玩一个叫做石头剪刀布的游戏,因为平时也没人和我玩。所以我就打开的电脑,开始整活!

游戏规则及运作

在下面的程序里,输入1代表石头,输入2代表剪刀,输入3表示布,就这样,然后计算机根据时间生成一个随机数种子,然后根据这个随机数通过计算得到计算机出的手势,然后根据以下规则进行判断输赢。

界面

我们先

cout << "石头剪刀布游戏" << endl;
cout << "1. 石头" << endl;
cout << "2. 剪刀" << endl;
cout << "3. 布" << endl;

电脑的随机数

根据上面的描述,可知先用srand(time(0))来进行随机数种子的生成,然后,我们使用rand() % 3 + 1生成1到3的随机数。所以,计算机的随机数代码如下:

srand(time(0));//随机数种子
//在此放置其他代码
int computerChoice = rand() % 3 + 1;//本语句表示用computerChoice变量进行存储随机数数据,以便后面的计算和使用。
//最后面的比较判定代码

在这里,计算机随机出数字的代码就完成了。

玩家的选择输入

我们先在界面上显示游戏说明,以便玩家输入。

cout << "石头剪刀布游戏" << endl;
cout << "1. 石头" << endl;
cout << "2. 剪刀" << endl;
cout << "3. 布" << endl;

然后,我们需要一个变量g来存储玩家输入的数据,然后用switch-case进行比较,从而知道输入的是什么和输赢判定

代码如下:

// 玩家进行选择
cout << "请选择(输入数字1-3):";
cin >> playerChoice;
// 输出玩家和电脑的选择
cout << "玩家选择了:";
switch (playerChoice) {
case 1:
    cout << "剪刀" << endl;
    break;
case 2:
    cout << "石头" << endl;
    break;
case 3:
    cout << "布" << endl;
    break;
default:
    cout << "无效选择" << endl;
    return 0;
}
cout << "电脑选择了:";
switch (computerChoice) {
case 1:
    cout << "剪刀" << endl;
    break;
case 2:
    cout << "石头" << endl;
    break;
case 3:
    cout << "布" << endl;
    break;
}
if (playerChoice == computerChoice) {
    cout << "平局" << endl;
}
else if (
(playerChoice == 1 && computerChoice == 3) ||
(PlayerChoice == 2 && computerChoice == 1) ||
(playerChoice == 3 && computerChoice == 2)
) {
    cout << "你赢了" << endl;
}
else {
    cout << "你输了" << endl;
}

然后上完整代码:

#include <iostream>
#include <ctime>
using namespace std;

int main() {
    srand(time(0)); // 初始化随机数生成器

    int playerChoice;
    int computerChoice = rand() % 3 + 1; // 生成1到3之间的随机数

    // 输出游戏规则
    cout << "剪刀石头布游戏" << endl;
    cout << "1. 剪刀" << endl;
    cout << "2. 石头" << endl;
    cout << "3. 布" << endl;

    // 玩家进行选择
    cout << "请选择(输入数字1-3):";
    cin >> playerChoice;

    // 输出玩家和电脑的选择
    cout << "玩家选择了:";
    switch (playerChoice) {
    case 1:
        cout << "剪刀" << endl;
        break;
    case 2:
        cout << "石头" << endl;
        break;
    case 3:
        cout << "布" << endl;
        break;
    default:
        cout << "无效选择" << endl;
        return 0;
    }
    cout << "电脑选择了:";
    switch (computerChoice) {
    case 1:
        cout << "剪刀" << endl;
        break;
    case 2:
        cout << "石头" << endl;
        break;
    case 3:
        cout << "布" << endl;
        break;
    }

    // 判断胜负
    if (playerChoice == computerChoice) {
        cout << "平局" << endl;
    }
    else if (
        (playerChoice == 1 && computerChoice == 3) ||
        (playerChoice == 2 && computerChoice == 1) ||
        (playerChoice == 3 && computerChoice == 2)
        ) {
        cout << "你赢了" << endl;
    }
    else {
        cout << "你输了" << endl;
    }

    return 0;
}

好了,今天的文章到此为止,记得点赞加关注加粉丝。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的石头剪刀布游戏的 c 语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int player, computer, result; srand(time(NULL)); printf("欢迎来玩石头剪刀布游戏!\n"); printf("请出拳:(1. 石头 2. 剪刀 3. 布)\n"); scanf("%d", &player); computer = rand() % 3 + 1; switch (player) { case 1: if (computer == 1) { printf("电脑出了石头,平局!\n"); result = 0; } else if (computer == 2) { printf("电脑出了剪刀,你赢了!\n"); result = 1; } else { printf("电脑出了布,你输了!\n"); result = -1; } break; case 2: if (computer == 1) { printf("电脑出了石头,你输了!\n"); result = -1; } else if (computer == 2) { printf("电脑出了剪刀,平局!\n"); result = 0; } else { printf("电脑出了布,你赢了!\n"); result = 1; } break; case 3: if (computer == 1) { printf("电脑出了石头,你赢了!\n"); result = 1; } else if (computer == 2) { printf("电脑出了剪刀,你输了!\n"); result = -1; } else { printf("电脑出了布,平局!\n"); result = 0; } break; default: printf("输入错误,请重新输入!\n"); return 0; } if (result == 1) { printf("恭喜你,你赢了!\n"); } else if (result == -1) { printf("很遗憾,你输了!\n"); } else { printf("好险,差点输了!\n"); } return 0; } ``` 运行结果如下: ``` 欢迎来玩石头剪刀布游戏! 请出拳:(1. 石头 2. 剪刀 3. 布) 1 电脑出了剪刀,你赢了! 恭喜你,你赢了! ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会叫的喵

这次是我第一次创作,多多鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值