time 
设为首页】【收藏本站
当前位置: 主页 > 程序设计 > C\C++\VC > C语言 > C语言猜数字游戏

C语言猜数字游戏

时间:2009-09-20 23:31 点击:548次 字体:[ ]




#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[i] = 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[i] == numIn[i])
     {
        (result.right)++;
        flag[i].Dft = 0;
        flag[i].In = 0;
     }
   for (i = 0; i < 4; i++)
       for (j = 0; j < 4; j++)
          if (flag[i].Dft && flag[j].In && numDft[i] == numIn[j])
          {
             (result.place)++;
             flag[i].Dft = 0;
             flag[j].In = 0;
          }
   return result;
}



本文地址 : http://www.fengfly.com/plus/view-77292-1.html
标签: C语言 猜数字游戏
------分隔线----------------------------
最新评论 查看所有评论
发表评论 查看所有评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:
本栏分类