找回密码
 申请新用户
搜索
热搜: 活动 交友 discuz
查看: 8343|回复: 8

小玩娱情

[复制链接]
发表于 2003-1-19 10:45:25 | 显示全部楼层 |阅读模式
限定十次猜出给定的数字:

一个C语言小游戏。它也需要一些推理哟~

[upload=zip]uploadImages/200311910451873074.zip[/upload]


[此贴子已经被作者于2003-1-19 10:45:56编辑过]

发表于 2003-1-19 14:14:09 | 显示全部楼层
嘿嘿~~我玩这个最拿手了~~~在文曲星上总能看到“聪明伶俐一百分”的字样~哈~
回复

使用道具 举报

发表于 2003-1-20 10:59:56 | 显示全部楼层
下载玩玩看!
回复

使用道具 举报

 楼主| 发表于 2003-2-18 20:33:52 | 显示全部楼层
源程序:

#include < stdio.h >/* for flushall() */
#include < stdlib.h >/* for random() */
#include < conio.h >/* for clrscr() */
#include < time.h >/* for time () */
#define TIMES 10
#define NUM 4
void show_help();
void checkarr(char check[]);
char check[NUM];

void show_help()
{ printf ("  十次把一个互不相同的四位数字(0-9)猜准确,是不是开玩笑?想想,你也能做到。\n\n");
  printf ("  A表示数字准确并位置准确,B表示数字准确可能位置不准确。如:给定的数组是0935,你猜的是");
  printf ("0379,则显示的提示信息是1A3B。\n\n  明白了吗? 一起来吧!\n\n\n  Press Anykey To Continue.......");
}

void checkarr (char check[Num])
{
   int i=0,temp;
   srand (time(0));
   do
    {WRONG:check=''''0''''+random(9);
      temp=i;
      if (i>0)
          while ((temp--)>0)
              if (check==check[temp] goto WRONG;
     i++;
    }
while (i<=3);

}


main()
{void show_help();
void checkarr (char check[NUM]);
char input [NUM];

int i=0;
int n;
int acount,bcount;
int temp;
char YN;
WHILE:while(1) /* The game is looping */
{ clrscr();
   printf ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
   acount=bcount=0;
   /* Initialize */

  printf ("  第一次来吗?看看帮助? (Yes or No):");
  if ((YN=getchar ())==''''Y''''||YN==''''y'''')
   { printf ("\n\b");
     flushall ();
     show_help();
     scanf ("%*c);
     flushall();
  }

printf ("\n");


checkarr (check); /* The check array. */


  clrscr();
  printf ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  printf ("四位数字已经产生!!!!开始猜数.........\n\n");


  for (n=1;n<=TIMES;n++)
  {
    SORRY:printf ("四位不同数字,NO.%d(q 退出,n 重来):",n);
   

    for (i=0;i<=NUM-1;i++)
    {input =getchar();
     if (input [0]==''''q''''||input [0]==''''Q'''')
       {clrscr();
         printf ("\n\n\n\n\n\n\n\n\n    ******程序己退出,请关闭窗口*****\n\n\n\n\n");
        exit(0);
      }
     if (iinput[0]==''''n''''||input[0]==''''N'''')
       {printf ("重来一盘....\n\n");
         goto WHILE;
       }
}


i=0;

do /*check a char */
{ if (input<''''0''''||input>''''9'''')
  {printf ("%c  >>>>>>>>>  非法字符!\n\n",input);
    flushall(); /* clear input stream! */
    goto SORRY;
}

  temp=i;
      while ((temp++)<2)
       if (input ==input[temp])
         {printf ("%c  >>>>>>>>>  相同数字!\n\n",input);
           flushall();
           goto SORRY;
          }


for (temp=0;temp<=3;temp++)
  {if (input==check[temp]&&i==temp)
       acount++;
   if (input==check[temp])
      bcount++;
}

   printf ("%c",input[i++]);
   
  }while (i<=3);

if (acount==4)
{printf ("\n\n\n");
   for (temp=0;temp<=3;temp++)
        printf ("%c ",check[temp]);

   if (n<=5)
    { printf ("做的好,你真优秀!  \n\n再来一盘?(Yes or No):");
       if(((YN=getchar()==''''N'''')||((YN==getchar())==''''n'''');
         {clrscr();
         printf ("\n\n\n\n\n\n\n\n\n    ******程序己退出,请关闭窗口*****\n\n\n\n\n");
        exit(0);}
      break; /* end of for loop */
      }
  
     else
        if (n>5&&n<=10)
            {printf ("你赢了!  \n\n再来一盘?(Yes or No):");
       if(((YN=getchar()==''''N'''')||((YN==getchar())==''''n'''');
         {clrscr();
         printf ("\n\n\n\n\n\n\n\n\n    ******程序己退出,请关闭窗口*****\n\n\n\n\n");
        exit(0);}
      break; /* end of for loop */
      }

    flushall();
  }
    else printf ( "       %dA%dB",acount,bcount);

  /*repeat the game. */

  printf ("\n\n");
  flushall();
  acount=bcount=0;
  
  } /* end of for loop */

if (n>10)
  {printf ("给出的数字为:");
   for (temp=0;temp<=3;temp++) printf ("%c",check[temp]);
   printf ("  别灰心。   \n\n再来一盘?(Yes or No):");
    if(((YN=getchar()==''''N'''')||((YN==getchar())==''''n'''');
         {clrscr();
         printf ("\n\n\n\n\n\n\n\n\n    ******程序己退出,请关闭窗口*****\n\n\n\n\n");
        exit(0);}
    flushall();
} /* end of while loop. */
} /* end of main(). */
  







[此贴子已经被作者于2003-3-1 20:08:05编辑过]

回复

使用道具 举报

 楼主| 发表于 2003-2-18 20:58:47 | 显示全部楼层
TURBOC2.0,DOS7.0下编译通过。

flushall():清除输入流。

clrscr():清屏。

回复

使用道具 举报

发表于 2003-2-19 10:59:24 | 显示全部楼层
建议谁改成window版?
回复

使用道具 举报

发表于 2003-2-24 09:54:56 | 显示全部楼层

  Turbo C,听着真是亲切的名字,还有已经不存在的Borland……
  不知道这儿有没有人用过Turbo Pascal,我是说的3.0的老版本。小小一个30多K的文件,包括了编辑、编译、调试、运行的全部功能。
  以前,我曾花1天时间把那东西的提示汉化了,可惜后来丢掉,和我我以前汉化的Turbo Pascal 5.5、Turbo C 2.0、PC Tools 5.0一起都没有了。真遗憾。
回复

使用道具 举报

发表于 2003-2-26 09:15:36 | 显示全部楼层
Turbo C.,是呀,很亲切的名字,很多人,学c都是重它开始的,不过,我已经很久没用过的,毕竟无法适应软件开发的发展了!回想过去,但更要向前看!
回复

使用道具 举报

 楼主| 发表于 2003-3-1 20:04:17 | 显示全部楼层
嗯,WATCHER说的对。得改成WINDOWS下的,呵呵。

有两个错误:

1、我没有使用语句 BREAK 中断FOR 循环;

2、以下程序段

temp=i;
if (i>0)
while ((temp--)>0)
if (input ==input[temp])
{printf ("%c  >>>>>>>>>  相同数字!\n\n",input);
flushall();
goto SORRY;
}

产生了一个在A、B的统计上的错误。原因在于它没有检查input[0]。

应该改为:

temp=i;
while ((temp++)<3)
if (input ==input[temp])
{printf ("%c  >>>>>>>>>  相同数字!\n\n",input);
flushall();
goto SORRY;
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 申请新用户

本版积分规则

守望轩 ( 湘ICP备17013730号-2 )|网站地图

GMT+8, 2024-3-29 10:01 , Processed in 0.026751 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表