C语言实例_猜数字游戏_C语言教程
本人学TC不久,前几天玩文曲星上的猜数字,就想到用TC2.0来实现这个游戏。代码如下:
#include
#include
#include
#define MAXTIMES 8
#define winer 1
#define loser 0
int main()
{
int aid[4],num[4],i,j,times,key,key2,A,B;
char ch;
time_t now;
Initialize:now=time(0);
srand(now);
aid[0]=(rand()+90)%10;/*生成目标数组*/
delay(20);
aid[1]=(rand()+90)%10;
delay(20);
aid[2]=(rand()+90)%10;
delay(20);
aid[3]=(rand()+90)%10;
for(i=0;i<4;i++) /*判断是否产生相同的数字*/
{for(j=0;j<4;j++)
{if((i!=j)&&(aid[i]==aid[j]))
goto Initialize;
}
}
printf(nnn);
times=1;
while(times<=MAXTIMES) /*程序主体部分*/
{
key=0;
key2=0;
printf(NO.%d:ntPlease input 4 different int numbers to play!nt,times);
scanf(%d,%d,%d,%d,&num[0],&num[1],&num[2],&num[3]);
for(i=0;i<4;i++) /*判断所输数字是否合法则*/
{
if(!((num[i]<=9)&&(num[i]>=0)))
{
printf(tError!The numbers you input must be int numbers which from 0 to 9n);
key=1;
break;
}
}
if(key==1)
continue;
for(i=0;i<4;i++) /*判断是否输入了相同的数字*/
{
for(j=i+1;j<4;j++)
{
if(num[i]==num[j])
{printf(tError!The numbers you input must be different!n);
key2=1;
}
break;
}
}
if(key2==1)
continue;
A=0;
B=0;
for(i=0;i<4;i++) /*比较两组数字*/
{
if(num[i]==aid[i])
A++;
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(num[i]==aid[j])
B++;
}
}
B=B-A;
printf(tA%dB%dn,A,B);
if(A==4) /*游戏成功,提示并返回*/
{
printf(Congratulations!n);
printf(Press any key to exit.!na);
getch();
return(winer);
}
else /*这次不完全对,允许次数内重新猜*/
{
printf(ttPress ENTER to continue,or press A to see about answer and exit.n); /*选择是否放弃,看答案*/
ch=getch();
if(ch=='n')
{
continue;
}
if((ch=='a')||(ch=='A'))
{
printf(The four numbers:%d,%d,%d,%dn,aid[0],aid[1],aid[2],aid[3]);
getch();
return(loser);
}
}
times++;
}
printf(Sorry!You have lost all the chances!nPress any key to exit!n);
getch();
}
我想看C语言视频教程
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |