注册 登录
编程论坛 C++教室

模拟“石头、剪子、布”游戏编程问题

lixin_2009 发布于 2009-09-19 22:34, 1218 次点击
编程用计算机模拟“石头、剪子、布”游戏。在这个游戏中,两个人同时说“石头、剪子、布”,压过另一方的为胜者。规则为:布压过石头,石头压过剪子,剪子压过布。其中的选择和结果使用枚举类型。
(小弟刚学c++,对这个题毫无头绪,请大家出出思路,另外,小弟刚来,还没有分,请大家原谅)

[ 本帖最后由 lixin_2009 于 2009-9-20 11:24 编辑 ]
6 回复
#2
mfkblue2009-09-24 00:03
也搞不懂这题什么意思,原来编了个,不知行不.
#include <iostream.h>
#include <stdlib.h>
#include <ctime>
int roll(void)
{int i;
i=rand()%3+1;
return i;
}
void main()
{    int i,j,game;
srand((unsigned)time(0));
    cout<<"输入 '1' 代表 石头 "<<endl;
    cout<<"输入 '2' 代表 剪刀"<<endl;
    cout<<"输入 '3' 代表 布"<<endl;
lol:
    cin>>i;
    if (i<1||i>3) cout<<"输入错误";
    else
    {j=roll();
    if (i==j) {game=0; cout<<"一样"<<endl;}
    else
        {switch(i)
            {case 1: if (j==2) {game=1;cout<<"石头 vs 剪刀"<<endl;}  
                    else {game=2; cout<<"石头 vs 布"<<endl ;}
            break;
            case 2: if(j==1) {game=2;cout<<"剪刀 vs 石头"<<endl;}  
                        else {game=1; cout<<"剪刀 vs 布"<<endl;}
            break;
     
            case 3: if(j==1) {game=1;cout<<"布 vs 石头"<<endl;}  
                else {game=2;cout<<"布 vs 剪刀"<<endl;}
            }
        }
 
    if (game==0) cout<<"平局"<<endl<<endl;
    if (game==1) cout<<"你赢了"<<endl<<endl;
    if (game==2) cout<<"你输了"<<endl<<endl;
    }
    cout<<endl;
    goto lol;
}
#3
forclwy2009-09-24 09:22
拿去运行下去
#4
酷新2009-09-24 20:00
看起来好复杂啊
#5
realfree2009-09-25 16:32
两个人同时?怎么同时,用socket编程?
#6
lixin_20092009-10-01 17:03
好厉害!
谢谢mfkblue
#7
鬼3095535972009-10-09 12:22
我写的是自己出1 2 3,系统用随机数出 1 2 3代表石头剪刀布来比较
我也是新手,写的不是很好,指导下



#include <iostream>  
#include <cstdlib>  
#include <ctime>
using namespace std;  
int xitong()//系统出的1 2 3代表石头 剪刀 布
{  
    srand(time(0));
    int x=rand()%3+1;  
    return x;  
}  
int main()  
{  
    int ping = 0;
    int shu = 0;
    int ying = 0;
    cout<<"请输入1 2 3代表石头 剪刀 布"<<endl;
    int m;//自己出的1 2 3代表石头.剪刀.布
    cin>>m;
    do
    {
        int n=xitong();
        switch(m)
        {
        case 1:
            if( n==1 )
            {
                cout<<"系统出石头,你出石头,平局"<<endl;
                ping++;
                break;
            }
            if( n==2 )
            {
                cout<<"系统出剪刀,你出石头,你赢了"<<endl;
                ying++;
                break;
            }
            if( n==3 )
            {
                cout<<"系统出布,你出石头,你输了"<<endl;
                shu++;
                break;
            }
        case 2:
            if( n==1 )
            {
                cout<<"系统出石头,你出剪刀,你输了"<<endl;
                shu++;
                break;                 
            }
            if( n==2 )
            {
                cout<<"系统出剪刀,你出剪刀,平局"<<endl;
                ping++;
                break;
            }
            if( n==3 )
            {
                cout<<"系统出布,你出剪刀,你赢了"<<endl;
                ying++;
                break;
            }
        case 3:
            if( n==1 )
            {
                cout<<"系统出石头,你出布,你赢了"<<endl;
                ying++;
                break;
            }
            if( n==2 )
            {
                cout<<"系统出剪刀,你出布,你输了"<<endl;
                shu++;
                break;
            }
            if( n==3 )
            {
                cout<<"系统出布,你出布,平局"<<endl;
                ping++;
                break;
            }
        default:
            cout<<"输入错误"<<endl;
            break;
        }
        cout<<"请输入1 2 3代表石头 剪刀 布"<<endl;
        cin>>m;
    }while( m!=-1 );//当输入-1时结束
    cout<<"赢"<<ying<<"局输"<<shu<<"局平"<<ping<<"局"<<endl;
    return 0;
}  
 
1