C++剪刀石头布!!!共124行

81 篇文章 0 订阅
15 篇文章 1 订阅

一个简单的剪刀石头布的小游戏!!!

代码~~~希望能被采纳哦~~~

#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int main()
{
    cout<<"欢迎来到剪刀石头布小游戏!!!"<<endl<<endl;
    Sleep(1000);
    string str;
    cout<<"请输入你的名字:";
    cin>>str;
    int n;
    cout<<"请输入本局比赛的局数:"; 
    cin>>n;
    cout<<""<<endl;
    cout<<"本游戏采用数字1,2,3分别代表剪刀,石头,布。"<<endl; 
    cout<<"采用"<<n<<"局"<<n/2+1<<"胜制!"<<endl<<endl;
    Sleep(1000);
    cout<<"******************"<<endl;
    cout<<"* Are you ready? *"<<endl;
    Sleep(1000);
    cout<<"*     Three      *"<<endl;
    Sleep(1000);
    cout<<"*      Two       *"<<endl;
    Sleep(1000);
    cout<<"*      One       *"<<endl;
    Sleep(1000);
    cout<<"*      Go!       *"<<endl;
    cout<<"******************";
    Sleep(1000);
    system("cls");
    Sleep(1000);
    int i,s=0,s1=0;
    for(i=1;i<=n;i++)
    {
        //1-每局开头文字 
        cout<<"*******************"<<endl;
        cout<<"*   第"<<i<<"局开始!   *"<<endl;
        cout<<"*******************"<<endl<<endl;
        //2-选手环节 
        cout<<"请"<<str<<"输入你的选择:";
        int a;
        cin>>a;
        if(a==1)
        {
            cout<<str<<"出了剪刀"""<<endl;     
        }
        else
        if(a==2)
        {
            cout<<str<<"出了石头"<<endl; 
        }
        else
        if(a==3)
        {
            cout<<str<<"出了布"<<endl;    
        }
        //3-计算机选手环节 
        Sleep(1000);
        cout<<"请计算机选手在三秒后随机选一个1-3之间的数字:";
        Sleep(3000); 
        srand(time(0));
        int b=rand()%3+1;
        cout<<b<<endl;
        if(b==1)
        {
            cout<<"计算机选手出了剪刀"<<endl; 
        }
        else
        if(b==2)
        {
            cout<<"计算机选手出了石头"<<endl;
        }
        else
        if(b==3)
        {
            cout<<"计算机选手出了布"<<endl;
        }
        //4-输出这一局的赢家
        Sleep(1000);
        if(a==b) 
        {
            cout<<"平局哦~-_-";
        }
        else
        if(a==1&&b==3||a==2&&b==1||a==3&&b==2)
        {
            cout<<str<<"选手赢了哦~^v^"<<endl;
            s++;
        }
        else
        {
            cout<<"计算机选手赢了哦~YoY"<<endl;
            s1++; 
        }
        Sleep(3000);
        system("cls");
    }
    //5-输出这一局的评分
    if(s>s1)
    {
        cout<<"本局比赛"<<str<<"选手赢了哦~^o^";
    } 
    else
    if(s1>s)
    {
        cout<<"本局比赛计算机选手赢了哦~YoY";
    }
    else
    {
        cout<<"本次比赛难分上下哦~-_-";
    }
    //7-询问是否继续
    cout<<"如果你想继续PK,请输入1;否则,输入0;";
    int c;
    cin>>c;
    if(c==1)
    {
        system("cls");
        main();    
    }
    else
    {
        cout<<"欢迎下次使用,再见ヾ( ̄▽ ̄)Bye~Bye~"; 
    }    
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用自定义函数的C++剪刀石头布游戏示例,供您参考: ```cpp #include <iostream> #include <cstdlib> #include <ctime> using namespace std; // 函数声明 void printMenu(); int getPlayerChoice(); int getComputerChoice(); void printResult(int playerChoice, int computerChoice); bool isPlayerWin(int playerChoice, int computerChoice); int main() { // 设置随机数种子 srand(time(0)); // 输出菜单 printMenu(); // 获取玩家选择 int playerChoice = getPlayerChoice(); // 获取计算机选择 int computerChoice = getComputerChoice(); // 输出结果 printResult(playerChoice, computerChoice); return 0; } // 函数定义 // 输出菜单 void printMenu() { cout << "欢迎来到剪刀石头布游戏!" << endl; cout << "1. 石头" << endl; cout << "2. 剪刀" << endl; cout << "3. 布" << endl; } // 获取玩家选择 int getPlayerChoice() { int choice; cout << "请输入您的选择:"; cin >> choice; while (choice < 1 || choice > 3) { cout << "无效的选择,请重新输入:"; cin >> choice; } return choice; } // 获取计算机选择 int getComputerChoice() { return rand() % 3 + 1; } // 输出结果 void printResult(int playerChoice, int computerChoice) { cout << "您的选择是:"; switch (playerChoice) { case 1: cout << "石头" << endl; break; case 2: cout << "剪刀" << endl; break; case 3: cout << "布" << endl; break; } 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 (isPlayerWin(playerChoice, computerChoice)) { cout << "恭喜您获胜!" << endl; } else { cout << "很遗憾,您输了!" << endl; } } // 判断玩家是否获胜 bool isPlayerWin(int playerChoice, int computerChoice) { if ((playerChoice == 1 && computerChoice == 2) || (playerChoice == 2 && computerChoice == 3) || (playerChoice == 3 && computerChoice == 1)) { return true; } else { return false; } } ``` 这个示例中,我们定义了一个 `isPlayerWin()` 函数来判断玩家是否获胜。在 `printResult()` 函数中,我们通过调用 `isPlayerWin()` 函数来判断胜负,而不是使用简单的逻辑判断。这样可以使代码更加清晰、易于维护。 希望这个示例可以帮助您更好地理解如何在C++中使用自定义函数实现剪刀石头布游戏

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值