| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 375 人关注过本帖
标题:猜数字游戏
只看楼主 加入收藏
谷雨寒
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-6-12
结帖率:100%
收藏
 问题点数:0 回复次数:1 
猜数字游戏
C语言编程  猜数字游戏
要求程序完成以下功能:
1)    随机抽取一个4位整数k,要求这个四位数没有重复数字,最高位不是0
2)    提示玩家,猜一个数字
3)    根据玩家猜的数,与k比较,判断玩家猜的数字中位置正确的数字个数m和数字正确而位置不对的数的个数n,并以mAnB的形式输出
4)    如果玩家没有猜对,那么提示mAnB,让玩家继续猜,否则输出“你太棒了,这个数字就是****,你一共猜了*次哦!”
5)    如果玩家猜了8次还不对,那么提示玩家“你已经猜了8次了,还要继续吗?(1:yes/0:false),玩家选择1的话,就继续猜,如果选择0的话,就结束游戏,如果输入既不是1也不是0,提示用户“输入非法,重新输入”。
6)    如果玩家猜了15次还不对,那么提示玩家“你已经猜了15次啦,是不是方法不对啊?休息一会再来玩吧!”,然后退出游戏。
搜索更多相关主题的帖子: 游戏 数字 
2010-06-14 07:53
wibx化吉
Rank: 2
等 级:论坛游民
帖 子:10
专家分:10
注 册:2010-6-3
收藏
得分:0 
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
main()
{
 int a[4],b[4],i,j;
 int num,time=0;
 int m=0,n=0,o=0;
 int ch=1,chch;
 char change[100];
 clrscr();      /*clean screen*/
 a[0]=rand()%10;    /*get a four number*/
 a[1]=rand()%10;
 a[2]=rand()%10;
 a[3]=rand()%10;
 while(1)  /*the number of every position not be same*/
 {
 if((a[0]==0||a[0]==a[1]||a[0]==a[2]||a[0]==a[3]||a[1]==a[2]||a[1]==a[3]||a[2]==a[3])==0)
  {
  printf("Notice:The first number is %d:",a[0]);
  break;
  }
 else
  {
  a[0]=rand()%10;
  a[1]=rand()%10;
  a[2]=rand()%10;
  a[3]=rand()%10;
  }
 }
 while(ch==1) /*circulate for guess or not*/
 {
  n=0;
  m=0;
  o=0;
  printf("Please input the num:");
  scanf("%d",&num);
  time++;
  while(1) /*input the right number of four*/
  {
   if(num<1000||num>9999)
   {
   printf("Number input wrong,input again:");
   scanf("%d",&num);
   }
   else
   break;
  }
  b[0]=num/1000;  /* deposit num to array b*/
  b[1]=(num-b[0]*1000)/100;
  b[2]=(num-b[0]*1000-b[1]*100)/10;
  b[3]=num-b[0]*1000-b[1]*100-b[2]*10;
  for(i=0;i<4;i++)
   if(a[i]==b[i])
    m++;
  for(i=0;i<4;i++)
   for(j=0;j<4;j++)
    if(a[i]==b[j])
     o++;
  if(m==4)
   {
   printf(" Aright Grameover!\n");
   printf("You have guess %d times\n",time);
   exit(0);
   }
  else if(time==8)
   {
    n=o-m;
    printf("Sorry,guess wrong:%dA%dB\n",m,n);
    printf("You have guess 8 times,do you want again(1-Yes,0-No):");
    scanf("%s",&change[100]);
   }
  else if(time==15)
   {
    printf("You have guess 15 times.Game Over!\n");
    exit(0);
   }
  else
   {
   n=o-m;
   printf("Sorry,guess wrong: %dA%dB\n",m,n);
   printf(" Guess %d times,again(1-Yes,0-No)?",time);
   }
  do
   {
    scanf("%s",&change[100]);
    switch(change[100])
    {
     case '0': exit(0);break;
     case '1': ch=1;break;
     default:{chch=1;ch=0;printf("Input Wrong input again(1-Yes,0-No):");}
    }
   }while(chch==1&&ch!=1);
 }
}

楼主看看啊,可是能运行的哦,我也是新手,有什么问题望楼主和各大高手指教

[ 本帖最后由 wibx化吉 于 2010-6-15 15:56 编辑 ]
2010-06-15 15:54
快速回复:猜数字游戏
数据加载中...
 
   



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

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