首页 > 编程资源分享区 > C/C++源代码共享 > C语言猜数字游戏重写版
2012
03-22

C语言猜数字游戏重写版

原帖及讨论:http://bbs.bccn.net/thread-167090-1-1.html

*/ ————————————————————————————–
*/ 出自: 编程中国  http://www.bccn.net
*/ 作者: andyzhshg        
*/ 时间: 2007-9-1  编程论坛首发
*/ 声明: 尊重作者劳动,转载请保留本段文字
*/ ————————————————————————————–
我对前不久在这里发表的猜数字游戏进行了重写,对语句进行了精简,对结构作了一些优化。
虽然说还是很幼稚,但比原作有了不少进步(自夸一下(:)。大家可以同时参考下原作,以做一下对比,
并希望大家继续给我提出意见,以期写出比较完美的程序。

其实这个程序算法上很简单,我之所以写得很长是想实现一个好的人机交互,这可以在creatDialog()
的众多语句中看出来。
此外我改动了以前的随机数生成函数,并对原数和输入数得比较函数的格式完全重写(其实算法大致一样,执行效率不一定能提高,不过代码长度大大减少)。
不足之处还望大家不吝赐教。

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

struct right_place {int right; int place;};

int main (void)
{
  int creatDialog (int x, int *p );

  void rndProduce (int numDft[]);
  struct right_place numCompare (int numDft[], int numIn[]);

  int dialog, quitOrContinue, allQuit;
  int numDft[4], numIn[4], result[2];
  struct right_place compareBack;
  
  creatDialog (1, NULL);
  
  allQuit = 1;
  while (allQuit == 1)
  {
     creatDialog (7, NULL);
     rndProduce (numDft);
     quitOrContinue = 1;
     while (quitOrContinue == 1)
     {
        dialog = 2;
        while (dialog == 2)
        {
           creatDialog (2, numIn);
           dialog = creatDialog (3, numIn);           
        }
        compareBack = numCompare (numDft, numIn);
        result[0] = compareBack.right ;
        result[1] = compareBack.place ;
        quitOrContinue = creatDialog (4, result);
     }
     if (quitOrContinue == 2)
        allQuit = creatDialog (5, numDft);
     if (quitOrContinue == 3)
        allQuit = 1;
     if (quitOrContinue == 4)
        allQuit = 2;     
   }

  if (allQuit == 2)
     creatDialog (6, NULL);

  getchar ();
  return 0;
}

int creatDialog (int x, int *p)
{
  int back;
  int i;
  switch (x)
  {
     case 0:
         printf (“************************************************************************\n”);
         break;
         case 1:
                 creatDialog (0, NULL);
                 printf (“欢迎玩猜数字游戏!\n版本2.0\n作者:andyzhshg\n制作日期:2007.9.1\n”);
                 creatDialog (0, NULL);
                 printf (“在本游戏中,你需要输入四个数字,\n然后电脑会给出你猜测的正确情况。”);
                 printf (“电脑会用它之前生成的\n四位数与之你的输入比较,然后告诉你”);
                 printf (“你猜对了几个数字,包\n括数值和顺序的信息.\n”);
                 break;
     case 2:
         creatDialog (0, NULL);
         printf (“请输入你猜测的四位数字(用空格、回车、或Tab隔开):\n”);
                 for (i = 0; i < 4; i++, p++)
                 scanf (“%i”, p);
         break;
         case 3:
                 creatDialog (0, NULL);
                 printf (“你输入的数字是:%i %i %i %i ,确认请输入“1”,重新输入按“2”。\n”, *p, *(p + 1), *(p + 2), *(p + 3));
                 scanf (“%i”, &back);
                 return back;
                 break;
         case 4:
                 creatDialog (0, NULL);
                 printf (“你输入的数字中:\n数值和位置都正确的有%i个,\n数值正确但位置不正确的有%i个.\n”, *p, *(p+1));
                 if ((*p) == 4)
                 {
                     printf (“恭喜你,答对了!\n”);
                     printf (“继续游戏吗?继续请输入“1”,退出游戏请输入“2”。\n”);
                     scanf (“%i”, &back);
                     if (back == 1)
                         return 3;
                     if (back == 2)
                         return 4;
                 }
                 else
                 {
                    printf (“继续吗?继续请输入“1”,显示正确答案请输入“2”。\n”);
                    scanf (“%i”, &back);
                    return back;
                 }
                 break;
         case 5:
                 creatDialog (0, NULL);
                 printf (“正确答案是:%i %i %i %i,再接再厉啊!\n”, *p, *(p + 1), *(p + 2), *(p + 3));
                 printf (“继续游戏吗?继续请输入“1”,退出游戏请输入“2”。\n”);
                 scanf (“%i”, &back);
                 return back;
                 break;
         case 6:
                 creatDialog (0, NULL);
                 printf (“感谢你玩本游戏,再见!\n按任意键退出\n”);
                 break;
         case 7:
                 creatDialog (0, NULL);
                 printf (“新的游戏开始了!GOOD LUCK!\n”);
                 break;
     default:
         break;
  }
}

void rndProduce (int numDft[])
{
   int i;
   srand( (unsigned)time( NULL ) ) ;
   for ( i = 0; i < 4; i++)
   numDft = rand () % 10;
   //printf (“%i %i %i %i\n”, numDft[0], numDft[1], numDft[2], numDft[3]);
}

struct right_place numCompare (int numDft[], int numIn[])
{
   int i, j;
   struct {int Dft; int In;} flag[4] = { 1, 1, 1 ,1 ,1, 1, 1, 1 };
   struct right_place result = { 0, 0 };
   for (i = 0; i < 4; i++)
     if (numDft == numIn)
     {
        (result.right)++;
        flag.Dft = 0;
        flag.In = 0;
     }
   for (i = 0; i < 4; i++)
       for (j = 0; j < 4; j++)
          if (flag.Dft && flag[j].In && numDft == numIn[j])
          {
             (result.place)++;
             flag.Dft = 0;
             flag[j].In = 0;
          }
   return result;
}

点击下载该文件


留下一个回复