- #include<iostream>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- int main()
- {
- int n,sum=0,win=0,die=0,step=0,m1=10,m2=0,i,j,k,x,y,h=-1;
- double average=0;
- char s1[10],s2[4];
- srand(time(NULL));
- cout<<"这是一个简 (jian) 单(lou)且标准的猜数字游戏, 按 0 可直接开始游戏"<<endl;
- cout<<"当然, 如果你没玩过, 按 2 可查看游戏介绍"<<endl<<endl;
- for(;;)
- {
- for(cin>>h;h!=0;cin>>h)
- {// 游戏功能部分(准备阶段)
- if(h==3) break;
- if(h==1)
- {// 查看战绩
- cout<<"总局数:"<<sum<<endl;
- cout<<"胜:"<<win<<endl;
- cout<<"负:"<<die<<endl;
- cout<<"总步数:"<<step<<endl;
- cout<<"最少步数:"<<m1<<endl;
- cout<<"最多步数:"<<m2<<endl;
- cout<<"平均步数(失败按 10 步算):"<<average<<endl;
- if(average>6) cout<<"别看了, 你还菜得很, 按 0 继续吧:"<<endl<<endl;
- else cout<<"我看好你哦, 按 0 继续吧:"<<endl<<endl;
- }
- if(h==2)
- {// 游戏介绍
- cout<<"游戏规则:"<<endl;
cout<<"电脑已生成一个" 各位皆不相等的 4 位数 ""<<endl;
- cout<<"你有 10 次猜数字的机会, 每次输入一个 4 位数"<<endl;
- cout<<"你每猜一个数字, 程序就要根据这个数字给出几 A 几 B"<<endl;
cout<<"A 前面的数字表示" 位置正确的数的个数 ", 而 B 前的数字表示" 数字正确而位置不对的数的个数 ""<<endl;
- cout<<"如正确答案为 5234, 而猜的人猜 5346, 则是 1A2B"<<endl;
- cout<<"其中 5 的位置对了, 记为 1A; 而 3 和 4 这两个数字对了, 但位置没对, 因此记为 2B"<<endl;
- cout<<"接着根据这些线索继续猜, 直到猜中 (即 4A0B) 为止"<<endl;
- cout<<"Ps: 比如答案为 1234, 输入 1111 结果为 1A, 输入 4441 结果为 2B"<<endl;
- cout<<"介绍到此结束, 按 0 开始游戏吧:"<<endl<<endl;
- }
- }
- if(h==3) break;
- {// 随机数生成部分
- s2[0]=rand()%9+49;// 限定首位不为 0
- for(s2[1]=rand()%10+48;s2[1]==s2[0];s2[1]=rand()%10+48){}
- for(s2[2]=rand()%10+48;s2[2]==s2[0]||s2[2]==s2[1];s2[2]=rand()%10+48){}
- for(s2[3]=rand()%10+48;s2[3]==s2[0]||s2[3]==s2[1]||s2[3]==s2[2];s2[3]=rand()%10+48){}
- }
- for(i=0;i<10;i++)
- {// 游戏部分
- x=y=0;
- cout<<"第"<<i+1<<"次尝试:";
- cin>>s1;
- for(j=0;j<4;j++)
- {// 判断 A,B 的数量
- if(s2[j]==s1[j]) x++;
- else for(k=0;k<4;k++)
- {
- if(s2[j]==s1[k]) y++;
- if(s2[j]==s1[k]) break;
- }
- }
- {// 输出判断结果
- cout<<""<<x<<'A'<<y<<'B'<<endl;
- if(x==4) cout<<"Congratulation"<<endl;
- if(x==4) break;
- }
- {// 彩蛋
- if(i==4) cout<<"加油哦 ^_^"<<endl<<endl;
- if(i==6) cout<<"似乎有点菜啊"<<endl<<endl;
- if(i==7) cout<<"真菜"<<endl<<endl;
- if(i==8) cout<<"这么菜还玩游戏?"<<endl<<endl;
- }
- }
- {// 更新玩家数据
- if(i<10) win++;
- else die++;
- if(i<10) step+=i+1;
- else step+=10;
- m1=i+1<m1?i+1:m1;
- m2=i+1>m2?i+1:m2;
- sum++;
- average=step;
- average/=sum;
- }
- {// 结局彩蛋
- if(i<2) cout<<"不, 我不相信, 你肯定是开挂了!!!"<<endl<<endl;
- else if(i<4) cout<<"呵呵, 运气好而已, 有本事再来一局>_<"<<endl<<endl;
- else if(i<6) cout<<"也就普通水准, 离我差得远呢"<<endl<<endl;
- else if(i<10) cout<<"菜的不行"<<endl<<endl;
- else cout<<"你妈喊你做作业了, 小学生 \ n 正确答案?\n"<<s2<<"嘛, 反正告诉你你也不会做"<<endl<<endl;
- }
- cout<<"那么, 再来一局?(按 0 继续, 按 1 查看战绩, 按 2 查看游戏介绍, 按 3 退出游戏)"<<endl;
- }
- }
猜数字游戏
来源: http://www.bubuko.com/infodetail-3399732.html