人机对战石头剪刀布代码

#include<bits/stdc++.h>
using namespace std;
int main() {
    int com,user,ans;
    double a=0,b=0;
    char c;
    srand((unsigned)time(NULL));//rand函数
    long long n;
    char p[200];
    cout<<"请输入您的大名:"<<endl;
    scanf("%s",&p);
    cout<<"您要玩的次数:";
    cin>>n;
    printf("1.剪刀 2.石头 3.布:");
    for(int i=1; i<=n; i++) {
        scanf("%d",&user);
        cout<<endl;
        com=rand()%3+1;
        printf("电脑出%s\n",com==1?"剪刀":com==2?"石头":"");
        ans=com-user ;//用两次相减结果来判断胜负
        if(ans==0) {
            printf("打平了,在试一次就赢了 ! ! \n");
            a+=0.5,b+=0.5;
        } else if(ans==1||ans==-2) {
            printf("很遗憾,您输了,加油 ! ! !\n");
            a++;
        } else if(ans==-1||ans==2) {
            printf("您赢了,乘胜追击 ! !\n");
            b++;
        }
    }
    cout<<endl;
    cout<<endl;
    cout<<endl;
    cout<<endl;
    cout<<"最终比分:" <<endl;
    cout<<"电脑"<<" "<<a<<" "<<":"<<" "<<b<<" "<<p<<endl;
    if(a>b)
        cout<<"很遗憾,你输了,双击f11在来一次哦 ! !" ;
    else if(a==b)
        cout<<"打平了,在试一次一定能赢,双击f11在来一次哦 ! !" ;
    else
        cout<<"你赢了,按两次f11乘胜追击哦 ! ! !";
    return 0;
}

 

posted @ 2019-08-13 17:13  小向同学  阅读(403)  评论(1编辑  收藏  举报