C语言网

 找回密码
 加入社区!

QQ登录

只需一步,快速开始

C语言网 网站首页 C语言 C语言程序 查看内容

猜数字游戏——C语言程序

2009-8-28 20:46| 发布者: admin| 查看: 5094| 评论: 4

摘要: 本人学TC不久,前几天玩文曲星上的猜数字,就想到用TC2.0来实现这个游戏。代码如下:#include#include#include#define MAXTIMES 8#define winer 1#define loser 0int main(){ int aid,num,i,j,times,key,key2,A,B; 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();
}

鲜花

握手

雷人

路过

鸡蛋
发表评论

最新评论

引用 龙文 2011-12-20 22:54
请问最开始的3个库函数具体是什么啊?
引用 firnsan 2011-10-19 23:49
B=B-A是干嘛的啊
引用 firnsan 2011-10-19 23:48
B=B-A是干嘛的啊
引用 txfyteen 2011-3-24 19:16
用 winTC无法实现

C语言 ( 粤ICP备11042647号-2 )

GMT+8, 2012-2-6 14:42

©2009-2011 cyuyan.com.cn

回顶部