晾下俺的QT猜数小程序

     朋友前些日子写个猜数字的小程序,闲来无事,我也用QT写个玩玩,
     QT是跑在虚拟机上LINUX带的,当然用QTOPIA也是可以的,小小程序,不过统统都是是常量字符串,处理起数字来也有些小麻烦。功能不多就主要给数,猜数,核对答案。然后写下中间冲突判断,输入的判断。都是主要功能,只是玩玩,也拿过来晾晾吧,呵呵。

    

 

       原码不会传,好象还些BUG,没时间改了,想要代码的朋友留言,或者留下传的方法吧。这个传代码还没学会。现在就先晾下,呵呵。原码就有form1.h, form1.cpp, game.h, game.cpp, main.cpp.工程文件和Makefile用Qmake的就可以,main里面改过了。

      把game.cpp帖过来吧,有什么问题大家可以给些意见,现在真的懒得改了。

    

  1. #include "game.h"
  2. GamePanel::GamePanel(QWidget* parent, const char* name,WFlags f1):GuessNumber( parent, name,f1)
  3. {
  4. }
  5. int GamePanel::GiveNum()
  6. {
  7.     int a,b,c,d;
  8.     int temp=0;
  9.     int judge=0;
  10.     while(!judge)
  11.     {
  12.          a=1+(rand()%9);   
  13.          b=1+(rand()%9);   
  14.          c=1+(rand()%9);   
  15.          d=1+(rand()%9); 
  16.         if(a!=b&&a!=c&&a!=d&&b!=c&&b!=d&&c!=d)
  17.         {
  18.             temp=a*1000+b*100+c*10+d; 
  19.             judge=1;
  20.         }
  21.     }
  22.     return temp;
  23. }
  24. void GamePanel::OKSlot()
  25. {
  26.    char *ptet;
  27.    char *pinp;
  28.    char input[10];
  29.    char output[50];
  30.    static int chance=0;
  31.    int nt,A=0,B=0,digit=0,lch,i=0,j=0;
  32.    
  33.    
  34.    if(showAns->text()=="The Number is ready, please guess!")
  35.    {
  36.       chance=0;
  37.    }
  38. if(showAns->text()==""||chance>=7||showAns->text()=="Right,you have succeeded!/nPlay it again.")
  39.    {
  40.       QMessageBox::information( this"please clicke ReadyGo!:","Click ReadyGo!",
  41.                               QMessageBox::Ok);
  42.       return;
  43.    }
  44.    
  45.    strcpy(input,inputLine1->text());
  46.    pinp=input;
  47.    
  48.    while(*pinp)            
  49.    {
  50.       if(*pinp>='0'&&*pinp<='9')
  51.        {
  52.          *pinp++;
  53.           digit++;
  54.        }
  55.        else
  56.        {
  57.          QMessageBox::information( this"please input correct number:","Wrong format!",
  58.                               QMessageBox::Ok);
  59.          inputLine1->clear();
  60.         return;          
  61.        }  
  62.    }
  63.    nt=atoi(inputLine1->text());
  64.     if(nt>0 && nt<=9999 && digit==4)       //number
  65.     {
  66.        showAns->setText(showAns->text()+tr("/n")+inputLine1->text());
  67.        if(nt==atoi(test))       //sucess
  68.        {
  69.           showAns->setText("Right,you have sucessed!/nPlay it again.");
  70.        }
  71.        else                     //don't sucess
  72.        {
  73.           ptet=test;
  74.           pinp=input;
  75.        
  76.           while(i<=3)             
  77.                {
  78.         j=0;
  79.         while(j<=3)
  80.         {
  81.             if(*(ptet+i)==*(pinp+j))
  82.             {
  83.             if(j==i)
  84.             {
  85.                 A++;
  86.             }
  87.             else
  88.             {
  89.                 B++;
  90.             }
  91.              }
  92.             j++;
  93.         }
  94.         i++;
  95.            }
  96.           chance++;
  97.           lch=7-chance;
  98.           sprintf(output,"the result is %d%c%d%c,you still have %d opportunities",A,'A',B,'B',lch);
  99.           showAns->setText(showAns->text()+tr("/n")+output);
  100.        
  101.           if(chance==7)
  102.           {
  103.              showAns->setText(tr("Sorry,you has no chance!Try again!/nthe correct is :")+test);
  104.                  inputLine1->clear();
  105.                  return;
  106.           }
  107.        }
  108.        inputLine1->clear();
  109.     }
  110.    else     
  111.     {
  112.      QMessageBox::information( this"please input your number","Input 4-digit",
  113.                               QMessageBox::Ok); 
  114.      inputLine1->clear();
  115.     }
  116. }
  117. void GamePanel::CancleSlot()
  118. {   
  119.      inputLine1->setText(tr(""));
  120. }
  121. void GamePanel::ReadySlot()
  122. {
  123.     sprintf(test,"%d",GiveNum());     //test[5]
  124.     //sprintf(test,"%d",1234);
  125.     showAns->setText(tr("The Number is ready, please guess!"));
  126. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值