⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cai.c

📁 一款猜数字的小游戏
💻 C
字号:
/*输入四个不重复的数字,按回车键后结果显示在右边方框,其形式为"?A?B".四位数字中, 
位置和数字都正确为A,数字相同而位置不同为B,当4A时为猜对.&quot;<&quot;,&quot;>&quot;控制光标.*/ 
#include          <stdio.h> 
#include          <stdlib.h> 
#include          <float.h> 
#include          <graphics.h> 
#include          <math.h> 
#include          <conio.h> 
#include          <dos.h> 
#define x 150        /* 本程序的所有坐标都是相对x,y来画的  */
#define y 150 
char ransum[4];      /* 装入随机产生的数  */
static char insum[4][2]={{’’,’\0’},{’’,’\0’},{’’,’\0’},{’’,’\0’}};/* 装入输入的数  */
void main(void) 
{ 
void shuijisum(void);/* 产生不相同的四个随机数  */
void huatu(void);    /* 初始化基本的屏幕  */
void input(void);    /* 输入猜的四个数字,并在方格内显示  */
int  bijiao(int);    /* 比较输入的数和随机产生的数,得出?A?B的结果  */
void ok(void);      /* 猜对数在方格下显示&quot;YOU ARE GOOD&quot;  */
void no(void);      /* 猜错数在方格下显示&quot;YOU ARE PIG&quot;  */
int  a,c,i,g_driver=VGA, g_mode=VGAHI; 
char  yn; 
detectgraph(&amp;g_driver, &amp;g_mode); 
initgraph(&amp;g_driver, &amp;g_mode, &quot;..\\bgi&quot;); 
kaishi: 
do 
{ 
  huatu(); 
  shuijisum(); 
  for(c=0;c<8;c++)  /* 最多猜8次  */
  { 
  input();          /* 猜对数显示&quot;YOU ARE GOOD&quot;,没猜对显示&quot;YOU ARE PIG&quot;,如果8  */
  a=bijiao(c);      /* 次都没猜对则询问&quot;you are coutinue(y/n)&quot;  */
  if(a==4) 
  { 
    ok(); 
    delay(1500); 
    goto kaishi; 
  } 
  else 
    no(); 
  } 
  if(a!=4) 
  { 
  settextstyle(1,0,1); 
  bar(x-40,y+75,x+190,y+100); 
  outtextxy(x-40,y+70,&quot;you are coutinue(y/n)&quot;); 
  } 
}while((yn=getch())==’y’); 
closegraph(); 
} 
void huatu(void) 
{ 
int i; 
char biaohao[1]; 
setfillstyle(1,BLACK); 
bar(0,0,640,480); 
setcolor(YELLOW); 
settextstyle(1,0,6); 
outtextxy(x-10,y-100,&quot;CAI SHU ZI v1.0&quot;); 
setcolor(RED); 
settextstyle(0,0,1); 
outtextxy(x+262,y-93,&quot;jk&quot;); 
setcolor(BLUE); 
rectangle(x,y,y+120,y+40); 
for(i=0;i<=1;i++) 
  rectangle(x+30+i*30,y,x+30+(i+1)*30,y+40); 
rectangle(x+200,y,x+340,y+160); 
setcolor(YELLOW); 
settextstyle(1,0,1); 
for(i=0;i<8;i++)              
  {sprintf(biaohao,&quot;%d:&quot;,i+1); 
  outtextxy(x+205,y+i*20,biaohao); 
  } 
} 
void input(void) 
{ 
int i=0,j; 
char key; 
settextstyle(1,0,4); 
do 
{ 
  setcolor(LIGHTGREEN); 
  rectangle(x+i*30+5,y+35,x+(i+1)*30-5,y+33); 
  key=getch(); 
  setcolor(BLACK); 
  if(key==’1’||key==’2’||key==’3’||key==’4’||key==’5’||key==’6’||key==’7’||key==’8’||key==’9’||key==’0’) 
  { 
  for(j=0;j<4;j++)    /* 判断输入的数字有没有重复的,如果有则消除原先的,在当前  */
  {if(j==i)            /* 的位置装入刚输入的数字  */
    j+=1; 
    if(key==insum[j][0]) 
    {bar(x+5+30*j,y+5,x+25+30*j,y+35); 
      insum[j][0]=’’; 
    } 
  } 
  insum[i][0]=key; 
  setcolor(YELLOW); 
  bar(x+5+30*i,y+5,x+25+30*i,y+35); 
  outtextxy(x+8+i*30,y,insum[i]); 
  i+=1; 
  } 
  if(key==’,’)        /* 判断按键如果是&quot;,&quot;则光标左移一格  */
  { 
  rectangle(x+i*30+5,y+35,x+(i+1)*30-5,y+33); 
  i-=1; 
  } 
  if(key==’.’)        /* 判断按键如果是&quot;.&quot;则光标右移一格  */
  { 
  rectangle(x+i*30+5,y+35,x+(i+1)*30-5,y+33); 
  i+=1; 
  } 
if(i<0) i=0;          /* 使光标始终在方格内  */
if(i>3) i=3; 
}while(key!=’\r’);    /* 判断按键如果是回车键则结束输入数字,去执行判断函数  */
rectangle(x+i*30+5,y+35,x+(i+1)*30-5,y+33); 
} 
void shuijisum(void) 
{int i,j,t; 
randomize(); 
for(i=0;i<4;i++) 
  {jk: 
  t=random(10)+48; 
  for(j=0;j<i;j++) 
    if(t==ransum[j]) 
    goto jk; 
  ransum[i]=t; 
  } 
} 
int bijiao(int c) 
{ 
int i,j,a=0,b=0; 
char ab[6];                    /* 一位一位的比较,如果有位置和数字都相同的则  */
for(i=0;i<4;i++)                /* a=a+1;如果是位置不同但数字相同则b=b+1  */
  for(j=0;j<4;j++) 
  if(insum[i][0]==ransum[j]) 
    if(i==j) 
    { 
      a+=1; 
      break; 
    } 
    else 
    { 
      b+=1; 
      break; 
    } 
setcolor(YELLOW); 
settextstyle(1,0,1); 
sprintf(ab,&quot;--%dA%dB&quot;,a,b); 
for(i=0;i<4;i++) 
  outtextxy(x+225+i*11,y+20*c,insum[i]); 
outtextxy(x+270,y+20*c,ab); 
return(a);        /* 返回的a是用来判断猜的数是  */
} 
void ok(void) 
{ 
bar(x-40,y+75,x+190,y+100); 
settextstyle(1,0,4); 
outtextxy(x-40,y+70,&quot;YOU ARE GOOD&quot;); 
} 
void no(void) 
{ 
bar(x-40,y+75,x+190,y+100); 
settextstyle(1,0,4); 
outtextxy(x-40,y+70,&quot;YOU ARE PIG&quot;); 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -