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

📄 puzzle.c

📁 A C++ logic game number game
💻 C
字号:
# include <stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>

union REGS i,o;
int count=0,maxx,maxy,button,x,y,midx,midy,b1=4,b2=7,b3=5,b4=2,b5=8,b6=1,b7=6,b8=3,b9=0;
void main()
{
 int gd=DETECT,gm;
 char ch;
 initgraph(&gd,&gm,"c:\\tc\\bgi"); maxx=getmaxx();
 maxy=getmaxy();

 initmouse();
 restrictmouse(maxx/2-50,maxy/2-50,maxx/2+79,maxy/2+79);
 showmouse();

 setcolor(BLUE);
 setbkcolor(LIGHTCYAN);

 outtextxy(maxx/2-90,maxy/2-100,"Press esc to exit");

 rect();
 fillbox();

 while(1)
 {
  if(b1==1 && b2==2 && b3==3 && b4==4 && b5==5 && b6==6 && b7==7 && b8==8 && b9==0)
  {
	gotoxy(36,7);
	setcolor(BLACK);
	printf("Congrats! you have won in %d clicks",count);
	getch();
	closegraph();
	restorecrtmode();
	break;
  }
  if(kbhit())
   {
     ch=(int)getch();
     if(ch==27)
     {
      closegraph();
      restorecrtmode();
      break;
     }
   }

  getmousepos(&button,&x,&y);
  if((button & 1) == 1)
   {
    getfocus();
    fillbox();
    findbox();
    changebox();
    fillbox();
   }
  if(button==2)
  {
    closegraph();
    restorecrtmode();
    break;
  }
 }
}
initmouse()
{
 i.x.ax=0;
 int86(0x33,&i,&o);
}
showmouse()
{
 i.x.ax=1;
 int86(0x33,&i,&o);
}
restrictmouse(int x1,int y1,int x2,int y2)
{
 i.x.ax=7;
 i.x.cx=x1;
 i.x.dx=x2;
 int86(0x33,&i,&o); i.x.ax=8;
 i.x.cx=y1;
 i.x.dx=y2;
 int86(0x33,&i,&o);
}
fillbox()
{
 gotoxy(36,13);
 printf("%d",b1);
 gotoxy(42,13);
 printf("%d",b2);
 gotoxy(48,13);
 printf("%d",b3);
 gotoxy(36,16);
 printf("%d",b4);
 gotoxy(42,16);
 printf("%d",b5);
 gotoxy(48,16);
 printf("%d",b6);
 gotoxy(36,19);
 printf("%d",b7);
 gotoxy(42,19);
 printf("%d",b8);
 gotoxy(48,19);
 printf("%d",b9);
}
rect()
{
 rectangle(maxx/2-70,maxy/2-70,maxx/2+100,maxy/2+100);
 setfillstyle(SOLID_FILL,RED);
 rectangle(maxx/2-60,maxy/2-60,maxx/2-10,maxy/2-10);
 floodfill(maxx/2-59,maxy/2-59,BLUE);
 rectangle(maxx/2-10,maxy/2-60,maxx/2+40,maxy/2-10);
 rectangle(maxx/2+40,maxy/2-60,maxx/2+90,maxy/2-10);
 rectangle(maxx/2-60,maxy/2-10,maxx/2-10,maxy/2+40);
 rectangle(maxx/2-10,maxy/2-10,maxx/2+40,maxy/2+40);
 rectangle(maxx/2+40,maxy/2-10,maxx/2+90,maxy/2+40);
 rectangle(maxx/2-60,maxy/2+40,maxx/2-10,maxy/2+90);
 rectangle(maxx/2-10,maxy/2+40,maxx/2+40,maxy/2+90);
 rectangle(maxx/2+40,maxy/2+40,maxx/2+90,maxy/2+90);
}
getmousepos(int *button,int *x,int *y)
{
 i.x.ax=3;
 int86(0x33,&i,&o);
 *button=o.x.bx;
 *x=o.x.cx;
 *y=o.x.dx;
}
getfocus()
{
 int m,n,color;
 for(m=maxx/2-59;m<=maxx/2+89;m+=5)
 {
  for(n=maxy/2-59;n<=maxy/2+89;n+=5)
  {
   {
 setfillstyle(SOLID_FILL,LIGHTCYAN);
 floodfill(m,n,BLUE);
   }
  }
 }
 setfillstyle(SOLID_FILL,RED);
 floodfill(x,y,BLUE);
 count++;
}
findbox()
{
 if(x<=maxx/2-10)
   midx=36;
 else if(x<=maxx/2+40)
   midx=42;
      else
      midx=48;
  if(y<=maxy/2-10)
   midy=13;
 else if(y<=maxy/2+40)
   midy=16;
      else
      midy=19;
}
changebox()
{
  int *a,*b,*temp;
  if(midx==36&&midy==13)
	 a=&b1;
  if(midx==42&&midy==13)
	 a=&b2;
  if(midx==48&&midy==13)
	 a=&b3;
  if(midx==36&&midy==16)
	 a=&b4;
  if(midx==42&&midy==16)
	 a=&b5;
  if(midx==48&&midy==16)
	 a=&b6;
  if(midx==36&&midy==19)
	 a=&b7;
  if(midx==42&&midy==19)
	 a=&b8;
  if(midx==48&&midy==19)
	 a=&b9;

  if(midx<=42)
  {
   if(a==&b1)
	 b=&b2;
   if(a==&b2)
	 b=&b3;
   if(a==&b4)
	 b=&b5;
   if(a==&b5)
	 b=&b6;
   if(a==&b7)
	 b=&b8;
   if(a==&b8)
	 b=&b9;
   if(*b==0)
   {
    *temp=*b;
    *b=*a;
    *a=*temp;
   }
  }
  if(midx>=42)
  {
   if(a==&b2)
	 b=&b1;
   if(a==&b3)
	 b=&b2;
   if(a==&b5)
	 b=&b4;
   if(a==&b6)
	 b=&b5;
   if(a==&b8)
	 b=&b7;
   if(a==&b9)
	 b=&b8;
   if(*b==0)
   {
    *temp=*b;
    *b=*a;
    *a=*temp;
   }
  }
  if(midy<=16)
  {
   if(a==&b1)
	 b=&b4;
   if(a==&b2)
	 b=&b5;
   if(a==&b3)
	 b=&b6;
   if(a==&b4)
	 b=&b7;
   if(a==&b5)
	 b=&b8;
   if(a==&b6)
	 b=&b9;
   if(*b==0)
   {
    *temp=*b;
    *b=*a;
    *a=*temp;
   }
  }
  if(midy>=16)
  {
   if(a==&b4)
      b=&b1;
   if(a==&b5)
      b=&b2;
   if(a==&b6)
       b=&b3;
   if(a==&b7)
	b=&b4;
   if(a==&b8)
	 b=&b5;
   if(a==&b9)
	 b=&b6;
   if(*b==0)
    {
     *temp=*b;
     *b=*a;
     *a=*temp;
    }
  }
}

⌨️ 快捷键说明

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