| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 436 人关注过本帖
标题:关于猜数字的问题!!
只看楼主 加入收藏
wswanghao
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-4-30
收藏
 问题点数:0 回复次数:0 
关于猜数字的问题!!

着是个猜数字的游戏 ,,我想编个程序让电脑自己猜出来,但编译通过,但不能让计算机自己去猜,下边是我的原代码,请高手看下。重点是字体加粗的部分。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define SIZE 4
int play(int chance);
int createnum(int a1[]);
int guess();
void d(int a1[],int a2[],int *countera,int *counterb);
int main()
{
int choice,chance1;


srand(time(NULL));
printf("Let's guess the number:\n");
printf("*******************\n 1:start the game\n 2:set the game\n 3:let the computer guess the number\n 4:quit the game\n*******************\n");
scanf("%d",&choice);
if(choice==1)
{
play(6);

}
else if(choice==2)
{
printf("Please choice the level:\n1:You have 9 chances;\n2:You have 6 chances;\n3:You have 3 chances;\n");
scanf("%d",&chance1);
if(chance1==1)
{

play(9);
}
if(chance1==2)
{

play(6);
}
if(chance1==3)
{

play(3);
}
else
{
}
}
else if(choice==3)
{
guess();
}

else
{
}

return 0;

}
int play(int chance)
{
int enter=1,guessnum,i,j,counter,countera,counterb,counter0;
int a[SIZE],b[SIZE];
while(enter==1)
{
createnum(a);


counter=1;

while(counter<=chance)
{
printf("Please guess the number:\n");
scanf("%d",&guessnum);
for(i=0;i<=SIZE-1;i++)
{
b[i]=guessnum/(int)pow(10,SIZE-1-i)%10;
}countera=0;

for(i=0;i<=SIZE-1;i++)
{
if(a[i]==b[i])
countera++;
}

counter0=0;

for(i=0;i<=SIZE-1;i++)
{
for(j=0;j<=SIZE-1;j++)
{
if(a[i]==b[j]) counter0++;
}
}
counterb=(counter0-countera);



if(countera==4)
{
printf("Wonderful!You guessed the number.Then,please input your choice:\n1: represents going on;\n2: represents breaking.\n");
counter=7;
scanf("%d",&enter);
}
if(countera<4&&counter==chance)
{
printf("Sorry!You are lost!The right number are:%d%d%d%d,Then,please input your choice:\n1: represents going on;\n2: represents breaking.\n",a[0],a[1],a[2],a[3]);
counter=7;
scanf("%d",&enter);
}
else
{
printf("%dA%dB\n",countera,counterb);
counter++;
}
}
}
while(enter==2)
{
exit(0);

}
return 0;
}
int createnum(int a1[])
{
int extra,stop,i,j;
stop=2;
while(stop==2)
{
extra=1;
for(i=0;i<=SIZE-1;i++)
a1[i]=rand()%10;
for(i=0;i<=SIZE-1;i++)
{
for(j=i+1;j<=SIZE-1;j++)
{
if(a1[i]==a1[j])
extra=2;
}
}
if(extra==1)
{
stop=1;

}
}
return 0;
}
int guess()
{
int a=0,b=0,a1=0,b1=0,q=0,p,s2;
int guess[5040][SIZE],temp[SIZE],guess2[SIZE];
int num[SIZE];
int s1=5039,t=1;
createnum(num);
for(int i=0;i<10;i++)//将所有四位不重复的数放入数组
{
for(int j=0;j<10;j++)
{
for(int m=0;m<10;m++)
{
for(int n=0;n<10;n++)
{
guess[q][0]=i;
guess[q][1]=j;
guess[q][2]=m;
guess[q][3]=n;
if((guess[q][0]==guess[q][1])||(guess[q][0]==guess[q][2])||(guess[q][0]==guess[q][3])||
(guess[q][1]==guess[q][2])||(guess[q][1]==guess[q][3])||(guess[q][2]==guess[q][3]))
goto end;
q++;
end:
;
}
}
}
}//jieshu
do{
p=rand()%s1;//从符合条件的数组中随机选择一个数
printf("This is the %d time.",t);
for(int o=0;o<SIZE;o++)
{
temp[o]=guess[p][o];
printf("%d",temp[o]);
}
printf("\n");
t++;
d(num,temp,&a,&b);
printf("%dA%dB",a,b);
if(a==SIZE)
break;
else//保留所有符合条件的数
{
s2=s1;
s1=0;
for(int i1=0;i1<(s2+1);i1++)
{
for(int i3=0;i3<SIZE;i3++)
guess2[i3]=guess[i1][i3];
d(temp,guess2,&a1,&b1);
if((a1==a)&&(b1==b))
{
for(int i4=0;i4<SIZE;i4++)
{
guess[s1][i4]=guess2[i4];
}
s1++;
}
}
}
}while(a!=SIZE);
printf("The number is ");
for(int m1=0;m1<SIZE;m1++)
printf("%d",num[m1]);
printf("\n");
return 0;
}
void d(int a1[],int a2[],int *countera,int *counterb)
{
int counter0,i,j;
*countera=0;

for(i=0;i<=SIZE-1;i++)
{
if(a1[i]==a2[i])
*countera++;
}

counter0=0;

for(i=0;i<=SIZE-1;i++)
{
for(j=0;j<=SIZE-1;j++)
{
if(a1[i]==a2[j]) counter0++;
}
}
*counterb=(counter0-*countera);
}

搜索更多相关主题的帖子: include 计算机 number choice guess 
2007-05-11 11:46
快速回复:关于猜数字的问题!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019388 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved