您的位置:首页 > 其它

做的小游戏 猜数字

2011-06-07 14:52 330 查看
#include<iostream>
#include <time.h>
using namespace std;
class guess{
      int level[3];//游戏等级 
      char guessnumber[4]; //猜数字的答案
      char answer[10][4]; //十组猜过的答案
      int A,B;//位置、数字正确A,数字正确B 
      int use[10];//记录0-9数字,使用情况 
      public:
      guess();
      void showrule();//显示游戏规则 
      void creat();//初始化,产生需要猜的数字
      void getanswer(int i,int level);//提示获取第i次答案 
      bool checkanswer(int i);//检查当前第i组答案的正确 
      void showanswer();//未猜中,显示答案 
      
};
guess::guess()
{
   for(int i=0;i<3;i++)//初始化等级对应答题次数 
   level[i]=10-2*i;
   
}
void guess::showrule()
{
     system("cls");
     cout<<"    本游戏猜测的是一个没有重复数字的4位数,每个位上取值都在0-9之间,玩家给出猜测的数字后,"
     <<"由程序给出该组数与答案的匹配程度,A代表有数字猜对了,且位于正确的位置上;B代表猜对了数字,"
     <<"但是数字位于错误的位置,根据游戏等级不同,猜测次数不同"
     <<"例如,正确数字是1234,若输入1243,得到2A2B,表示有两个满足A条件,两个满足B条件,但不会指出满足条件的数字,"<<endl
     <<"按回车键继续……" ;
     cin.get();
}
void guess::creat()
{
     srand( (unsigned)time( NULL ) );
     
     for(int j=0;j<10;j++)
     use[j]=0;//初始化数字使用数字数组 
     for(int i=0;i<4;i++)
     {
             guessnumber[i]=rand()%10+48;//生成10以内随机数,转换成0-9字符保存 
             if(use[guessnumber[i]%48]==0)//未使用的数字 
             use[guessnumber[i]%48]=i+1;//记录位置 1-4 
             else
             i--;// 否则,重新生成 
     }   
}
void guess::getanswer(int i,int leve)
{
     if(i>9)//防止答案组数超出 
     return;
     
     else
     {
     
     cout<<"请输入所猜4位数字及排列(每位为0-9之间的数,无重复,您剩下"<<level[leve]-i<<"次机会)"<<endl;
     int flag=0; //是否需要继续输入标志 
     while(!flag)
     {
         flag=1;
         int temp[10]; 
         for(int j=0;j<10;j++)
         temp[j]=0;//初始化确定是否有重复的临时数组 
         for(int j=0;j<4;j++)
         cin>>answer[i][j];//获取四个答案 
         for(int j=0;j<4;j++)
         { 
         
                 if(answer[i][j]<48||answer[i][j]>57)
                 {
                   cout<<"输入的数据有错,请重新输入本组数据"<<endl;
                   flag=0; //恢复到初始化 
                   break;
                 }
                 if(temp[answer[i][j]%48]==1)
                 {
                 cout<<"输入的数据重复,请重新输入本组数据:"<<endl;
                 flag=0;
                 break;
                 }
                 else
                 temp[answer[i][j]%48]=1;//标记数字使用情况 
         } 
     }
     }
     
}
bool guess::checkanswer(int i)
{
     A=0;
     B=0;
     for(int j=0;j<4;j++)
     {
      if(use[answer[i][j]%48]!=0)   //检查所猜数字是否存在答案中 
      {
        if(use[answer[i][j]%48]==(j+1))//比对位置 
        A++;
        else
        B++;                            
      }    
     }
     if(A==4)//猜对
     return true;
     cout<<A<<"A"<<B<<"B"<<endl;//猜错,返回信息
     return false;
}
void guess::showanswer()
{
     for(int i=0;i<4;i++)
     cout<<guessnumber[i]<<" ";
     cout<<endl;
     
}
main()
{
      guess test;
      test.creat();
      bool answer;//是否答对标志 
      char choice='1';
      while(!(choice-'1'))
      {
      system("cls");
      cout<<"猜数字游戏  "<<endl<<"本品由马小李漫画工作窝荣誉出品"<<endl;
      cout<<"请选择选项:"<<endl;
      cout<<"1.阅读游戏规则;"<<endl;
      cout<<"2.开始游戏;"<<endl;
      cout<<"3.退出。"<<endl;
      
      cin>>choice;
      cin.get();
      while(choice<'1'|choice>'3')//选项合法性判断
      {
      cout<<"没有这个选项,请重新输入:"<<endl;
      cin>>choice;
      }
      switch(choice)//选项对应操作
      {
      case '1'://显示规则
      test.showrule();
      break;
      case '2': //开始游戏
      {
              system("cls");
              char level; 
              cout<<"请选择游戏等级:"<<endl;
              cout<<"1.初级;"<<endl;
              cout<<"2.中级;"<<endl;
              cout<<"3.高级;"<<endl;
              cout<<"4.返回上一级菜单."<<endl;
              cin>>level;
              while(level<'1'|level>'4')//选项合法性判断
              {
              cout<<"没有这个选项,请重新输入:"<<endl;
              cin>>level;
              }
              switch(level)
              {
              case '1':
              case '2':
              case '3':
                   {
                          system("cls");
                          int leve=level-'0';
                          for(int i=0;i<12-2*leve;i++)
                          {
                            test.getanswer(i,leve-1);
                            answer=test.checkanswer(i);
                                if(answer==true)
                                {
                                cout<<"恭喜您答对了!"<<endl;
                                break;
                                }  
                          }
                              if(!answer)
                              { 
                              cout<<"很抱歉您没有猜对,答案是:"; 
                              test.showanswer();
                              } 
                   }
              break;
              case '4':
              choice='1';
              
              }
              
              
      }
      break;
      }
      }
      system("pause");
}




花了点时间写的,菜鸟水平,愿与大家交流下…
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: