朋友前些日子写个猜数字的小程序,闲来无事,我也用QT写个玩玩,
QT是跑在虚拟机上LINUX带的,当然用QTOPIA也是可以的,小小程序,不过统统都是是常量字符串,处理起数字来也有些小麻烦。功能不多就主要给数,猜数,核对答案。然后写下中间冲突判断,输入的判断。都是主要功能,只是玩玩,也拿过来晾晾吧,呵呵。
原码不会传,好象还些BUG,没时间改了,想要代码的朋友留言,或者留下传的方法吧。这个传代码还没学会。现在就先晾下,呵呵。原码就有form1.h, form1.cpp, game.h, game.cpp, main.cpp.工程文件和Makefile用Qmake的就可以,main里面改过了。
把game.cpp帖过来吧,有什么问题大家可以给些意见,现在真的懒得改了。
- #include "game.h"
- GamePanel::GamePanel(QWidget* parent, const char* name,WFlags f1):GuessNumber( parent, name,f1)
- {
- }
- int GamePanel::GiveNum()
- {
- int a,b,c,d;
- int temp=0;
- int judge=0;
- while(!judge)
- {
- a=1+(rand()%9);
- b=1+(rand()%9);
- c=1+(rand()%9);
- d=1+(rand()%9);
- if(a!=b&&a!=c&&a!=d&&b!=c&&b!=d&&c!=d)
- {
- temp=a*1000+b*100+c*10+d;
- judge=1;
- }
- }
- return temp;
- }
- void GamePanel::OKSlot()
- {
- char *ptet;
- char *pinp;
- char input[10];
- char output[50];
- static int chance=0;
- int nt,A=0,B=0,digit=0,lch,i=0,j=0;
- if(showAns->text()=="The Number is ready, please guess!")
- {
- chance=0;
- }
- if(showAns->text()==""||chance>=7||showAns->text()=="Right,you have succeeded!/nPlay it again.")
- {
- QMessageBox::information( this, "please clicke ReadyGo!:","Click ReadyGo!",
- QMessageBox::Ok);
- return;
- }
- strcpy(input,inputLine1->text());
- pinp=input;
- while(*pinp)
- {
- if(*pinp>='0'&&*pinp<='9')
- {
- *pinp++;
- digit++;
- }
- else
- {
- QMessageBox::information( this, "please input correct number:","Wrong format!",
- QMessageBox::Ok);
- inputLine1->clear();
- return;
- }
- }
- nt=atoi(inputLine1->text());
- if(nt>0 && nt<=9999 && digit==4) //number
- {
- showAns->setText(showAns->text()+tr("/n")+inputLine1->text());
- if(nt==atoi(test)) //sucess
- {
- showAns->setText("Right,you have sucessed!/nPlay it again.");
- }
- else //don't sucess
- {
- ptet=test;
- pinp=input;
- while(i<=3)
- {
- j=0;
- while(j<=3)
- {
- if(*(ptet+i)==*(pinp+j))
- {
- if(j==i)
- {
- A++;
- }
- else
- {
- B++;
- }
- }
- j++;
- }
- i++;
- }
- chance++;
- lch=7-chance;
- sprintf(output,"the result is %d%c%d%c,you still have %d opportunities",A,'A',B,'B',lch);
- showAns->setText(showAns->text()+tr("/n")+output);
- if(chance==7)
- {
- showAns->setText(tr("Sorry,you has no chance!Try again!/nthe correct is :")+test);
- inputLine1->clear();
- return;
- }
- }
- inputLine1->clear();
- }
- else
- {
- QMessageBox::information( this, "please input your number","Input 4-digit",
- QMessageBox::Ok);
- inputLine1->clear();
- }
- }
- void GamePanel::CancleSlot()
- {
- inputLine1->setText(tr(""));
- }
- void GamePanel::ReadySlot()
- {
- sprintf(test,"%d",GiveNum()); //test[5]
- //sprintf(test,"%d",1234);
- showAns->setText(tr("The Number is ready, please guess!"));
- }