#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#define FrameX 8 //游戏窗口左上角的X轴坐标
#define FrameY 4 //游戏窗口左上角的Y轴坐标
#define Frame_height 6 //游戏窗口方块的高度
#define Frame_width 11 //游戏窗口方块的宽度
#define Frame_height2 5 //数字图案的高度
#define Frame_width2 6 //数字图案方块的宽度make_digit(FrameX+(Frame_width+1)/2+Frame_width,FrameY+Frame_height);
int a[9]={0},b[9],c[9],n=0;//各个区的代号 a[i]只为0 无图案 !0 ——有图案
//b[i],c[i]分别表示a[i]区的中心横竖坐标 n 表示所走的步骤数
void Set_TextColor_Green (void)
{
HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
}
void gotoxy(HANDLE hOut, int x, int y)//移动光标
{
COORD pos;
pos.X = x; //横坐标
pos.Y = y; //纵坐标
SetConsoleCursorPosition(hOut, pos);
}
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
void printn( )//打印步骤数
{
gotoxy(hOut,b[8]+Frame_width2*2+8,c[8]+2);
printf("%d",n);
}
void clear_digit(int x,int y)//清除数字图案
{
int x1,y1,i;
x1=x-Frame_width2/2;
y1=y-(Frame_height2)/2;
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y1);
printf(" ");
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y);
printf(" ");
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,(y+(Frame_height2)/2));
printf(" ");
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,x1,i);
printf(" ");
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,(x1+Frame_width2-1),i);
printf(" ");
}
}
void make_digit1(int x,int y)//给定中心坐标 制作数字1图案
{
int x1,y1,i;
x1=x-Frame_width2/2;
y1=y-(Frame_height2)/2;
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y1);
printf("%c",4);
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y);
printf("%c",4);
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,(y+(Frame_height2)/2));
printf("%c",4);
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,x1,i);
printf("%c",4);
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,(x1+Frame_width2-1),i);
printf("%c",4);
}
for(i=y1+1;i<y1+Frame_height2;i++)//左方
{
gotoxy(hOut,x1,i);
printf(" ");
}
for(i=x1+1;i<x1+Frame_width2-1;i++)//下方
{
gotoxy(hOut,i,y1+Frame_height2-1);
printf(" ");
}
for(i=x1+1;i<x1+Frame_width2-1;i++)//中间
{
gotoxy(hOut,i,y1+Frame_height2/2);
printf(" ");
}
for(i=x1+1;i<x1+Frame_width2-1;i++)//上方
{
gotoxy(hOut,i,y1);
printf(" ");
}
gotoxy(hOut,x1,y1);//左上点
printf(" ");
}
void make_digit2(int x,int y)//给定中心坐标 制作数字2图案
{
int x1,y1,i;
x1=x-Frame_width2/2;
y1=y-(Frame_height2)/2;
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y1);
printf("%c",4);
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y);
printf("%c",4);
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,(y+(Frame_height2)/2));
printf("%c",4);
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,x1,i);
printf("%c",4);
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,(x1+Frame_width2-1),i);
printf("%c",4);
}
for(i=y1+1;i<y;i++)//左上
{
gotoxy(hOut,x1,i);
printf(" ");
}
for(i=y+1;i<y1+Frame_height2-1;i++)//右下
{
gotoxy(hOut,x1+Frame_width2-1,i);
printf(" ");
}
}
void make_digit3(int x,int y)//给定中心坐标 制作数字3图案
{
int x1,y1,i;
x1=x-Frame_width2/2;
y1=y-(Frame_height2)/2;
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y1);
printf("%c",4);
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y);
printf("%c",4);
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,(y+(Frame_height2)/2));
printf("%c",4);
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,x1,i);
printf("%c",4);
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,(x1+Frame_width2-1),i);
printf("%c",4);
}
for(i=y1+1;i<y;i++)//左上
{
gotoxy(hOut,x1,i);
printf(" ");
}
for(i=y+1;i<y1+Frame_height2-1;i++)//左下
{
gotoxy(hOut,x1,i);
printf(" ");
}
}
void make_digit4(int x,int y)//给定中心坐标 制作数字4图案
{
int x1,y1,i;
x1=x-Frame_width2/2;
y1=y-(Frame_height2)/2;
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y1);
printf("%c",4);
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y);
printf("%c",4);
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,(y+(Frame_height2)/2));
printf("%c",4);
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,x1,i);
printf("%c",4);
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,(x1+Frame_width2-1),i);
printf("%c",4);
}
for(i=x1+1;i<x1+Frame_width2-1;i++)//下方
{
gotoxy(hOut,i,y1+Frame_height2-1);
printf(" ");
}
for(i=y+1;i<y1+Frame_height2-1;i++)//左下
{
gotoxy(hOut,x1,i);
printf(" ");
}
for(i=x1+1;i<x1+Frame_width2-1;i++)//上方
{
gotoxy(hOut,i,y1);
printf(" ");
}
gotoxy(hOut,x1,y1+Frame_height2-1);//左下点
printf(" ");
}
void make_digit5(int x,int y)//给定中心坐标 制作数字5图案
{
int x1,y1,i;
x1=x-Frame_width2/2;
y1=y-(Frame_height2)/2;
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y1);
printf("%c",4);
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y);
printf("%c",4);
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,(y+(Frame_height2)/2));
printf("%c",4);
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,x1,i);
printf("%c",4);
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,(x1+Frame_width2-1),i);
printf("%c",4);
}
for(i=y1+1;i<y;i++)//右上
{
gotoxy(hOut,x1+Frame_width2-1,i);
printf(" ");
}
for(i=y+1;i<y1+Frame_height2-1;i++)//左下
{
gotoxy(hOut,x1,i);
printf(" ");
}
}
void make_digit6(int x,int y)//给定中心坐标 制作数字6图案
{
int x1,y1,i;
x1=x-Frame_width2/2;
y1=y-(Frame_height2)/2;
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y1);
printf("%c",4);
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y);
printf("%c",4);
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,(y+(Frame_height2)/2));
printf("%c",4);
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,x1,i);
printf("%c",4);
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,(x1+Frame_width2-1),i);
printf("%c",4);
}
for(i=y1+1;i<y;i++)//右上
{
gotoxy(hOut,x1+Frame_width2-1,i);
printf(" ");
}
}
void make_digit7(int x,int y)//给定中心坐标 制作数字7图案
{
int x1,y1,i;
x1=x-Frame_width2/2;
y1=y-(Frame_height2)/2;
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y1);
printf("%c",4);
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y);
printf("%c",4);
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,(y+(Frame_height2)/2));
printf("%c",4);
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,x1,i);
printf("%c",4);
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,(x1+Frame_width2-1),i);
printf("%c",4);
}
for(i=y1+1;i<y1+Frame_height2;i++)//左方
{
gotoxy(hOut,x1,i);
printf(" ");
}
for(i=x1+1;i<x1+Frame_width2-1;i++)//下方
{
gotoxy(hOut,i,y1+Frame_height2-1);
printf(" ");
}
for(i=x1+1;i<x1+Frame_width2-1;i++)//中间
{
gotoxy(hOut,i,y1+Frame_height2/2);
printf(" ");
}
}
void make_digit8(int x,int y)//给定中心坐标 制作数字8图案
{
int x1,y1,i;
x1=x-Frame_width2/2;
y1=y-(Frame_height2)/2;
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y1);
printf("%c",4);
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,y);
printf("%c",4);
}
for(i=x1;i<x1+Frame_width2;i++)
{
gotoxy(hOut,i,(y+(Frame_height2)/2));
printf("%c",4);
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,x1,i);
printf("%c",4);
}
for(i=y1;i<y1+Frame_height2;i++)
{
gotoxy(hOut,(x1+Frame_width2-1),i);
printf("%c",4);
}
}
void printdigi(int x,int i)//根据 i 打印相应a[i]的数字图案 i表示 区域的角标
{
switch(x)
{
case 1:make_digit1(b[i],c[i]);break;
case 2:make_digit2(b[i],c[i]);break;
case 3:make_digit3(b[i],c[i]);break;
case 4:make_digit4(b[i],c[i]);break;
case 5:make_digit5(b[i],c[i]);break;
case 6:make_digit6(b[i],c[i]);break;
case 7:make_digit7(b[i],c[i]);break;
case 8:make_digit8(b[i],c[i]);break;
}
}
void make_dia(int x,int y)//给出左上角坐标 制出方块
{
int x1,y1;
x1=x;
y1=y;
Set_TextColor_Green ( );
for(x=x1;x<=(x1+Frame_width);x++)
for(y=y1;y<=(y1+Frame_height);y+=Frame_height)
{
gotoxy(hOut,x,y);
printf("%c",4);
}
for(y=y1;y<=(y1+Frame_height);y++)
for(x=x1;x<=(x1+Frame_width);x+=Frame_width)
{
gotoxy(hOut,x,y);
printf("%c",4);
}
}
void over_game1( )
{
system("cls");
gotoxy(hOut,32,13);
printf("游戏结束\n");
gotoxy(hOut,30,15);
printf("你一共走了");
printf("%d步\n",n);
Sleep(2000);
exit(0);
}
void over_game2( )
{
system("cls");
gotoxy(hOut,32,13);
printf("你好厉害\n");
gotoxy(hOut,30,15);
printf("你一共走了");
printf("%d步\n",n);
Sleep(2000);
exit(0);
}
void make_frame( )// 制作游戏窗口
{
int x,y;
for(x=FrameX;x<=FrameX+Frame_width*2;x+=Frame_width)
for(y=FrameY;y<=FrameY+Frame_height*2;y+=Frame_height)
make_dia(x,y);
gotoxy(hOut,b[1]-4,c[1]-Frame_height2);
printf("拼图游戏");
gotoxy(hOut,b[8]+Frame_width2*2,c[8]-13);
printf("范图:");
gotoxy(hOut,b[8]+Frame_width2*2+8,c[8]-13);
printf("1 2 ");
gotoxy(hOut,b[8]+Frame_width2*2+6,c[8]-11);
printf("3 4 5");
gotoxy(hOut,b[8]+Frame_width2*2+6,c[8]-9);
printf("6 7 8");
gotoxy(hOut,b[8]+Frame_width2*2,c[8]-2);
printf("ese:退出");
gotoxy(hOut,b[8]+Frame_width2*2,c[8]-5);
printf("方向键:移动");
gotoxy(hOut,b[8]+Frame_width2*2,c[8]+2);
printf("步骤数:");
}
void start_game( )
{
int i,j;//控制循环
unsigned char ch=77;//定义用于接收键盘输入的字符变量
for(i=0;i<=6;i+=3)
b[i]=FrameX+(Frame_width+1)/2+Frame_width*0;
for(i=1;i<=7;i+=3)
b[i]=FrameX+(Frame_width+1)/2+Frame_width*1;
for(i=2;i<=8;i+=3)
b[i]=FrameX+(Frame_width+1)/2+Frame_width*2;
for(i=0;i<3;i++)
c[i]=FrameY+Frame_height/2+Frame_height*0;
for(i=3;i<6;i++)
c[i]=FrameY+Frame_height/2+Frame_height*1;
for(i=6;i<9;i++)
c[i]=FrameY+Frame_height/2+Frame_height*2;
srand(time(NULL));
for(i=0;i<8;i++)
{
a[i]=rand()%8+1;
for(j=0;j<i;j++)
{
if(a[i]==a[j])
{
i--;
break;
}
}
}
for(i=0;i<8;i++)
switch(a[i])//根据 a[i]的 打印相应的数字图案
{
case 1:make_digit1(b[i],c[i]);break;
case 2:make_digit2(b[i],c[i]);break;
case 3:make_digit3(b[i],c[i]);break;
case 4:make_digit4(b[i],c[i]);break;
case 5:make_digit5(b[i],c[i]);break;
case 6:make_digit6(b[i],c[i]);break;
case 7:make_digit7(b[i],c[i]);break;
case 8:make_digit8(b[i],c[i]);break;
}
// gotoxy(hOut,b[8]+Frame_width2*2,c[8]-4);
while(1)
{
if(a[0]==0&&a[1]==1&&a[2]==2&&a[3]==3&&a[4]==4&&a[5]==5&&a[6]==6&&a[7]==7&&a[8]==8)
over_game2( );
if(kbhit())
{/* 判断是否按下键盘,如果按下,ch接收键盘输入 */
ch=getch();
if(ch==27)
over_game1( );
if(ch==75)//向左移动
for(i=0;i<9;i++)
if((i==0||i==1||i==4||i==3||i==7||i==6)&&a[i]==0)
{
printdigi(a[i+1],i);
clear_digit(b[i+1],c[i+1]);
a[i]=a[i+1];
a[i+1]=0;
n++;
printn( );
break;
}
if(ch==72)//向上移动
for(i=0;i<9;i++)
if((i==1||i==2||i==4||i==5||i==3||i==0)&&a[i]==0)
{
printdigi(a[i+3],i);
clear_digit(b[i+3],c[i+3]);
a[i]=a[i+3];
a[i+3]=0;
n++;
printn( );
break;
}
if(ch==80)//向下移动
for(i=0;i<9;i++)
if((i==7||i==8||i==4||i==5||i==3||i==6)&&a[i]==0)
{
printdigi(a[i-3],i);
clear_digit(b[i-3],c[i-3]);
a[i]=a[i-3];
a[i-3]=0;
n++;
printn( );
break;
}
if(ch==77)//向右移动
for(i=0;i<9;i++)
if((i==1||i==2||i==4||i==7||i==8||i==5)&&a[i]==0)
{
printdigi(a[i-1],i);
clear_digit(b[i-1],c[i-1]);
a[i]=a[i-1];
a[i-1]=0;
n++;
printn( );
break;
}
}
}
}
int main( )
{
system("color 0D");
system("mode con: cols=70 lines=25");
int i,j;
for(i=0;i<=6;i+=3)
b[i]=FrameX+(Frame_width+1)/2+Frame_width*0;
for(i=1;i<=7;i+=3)
b[i]=FrameX+(Frame_width+1)/2+Frame_width*1;
for(i=2;i<=8;i+=3)
b[i]=FrameX+(Frame_width+1)/2+Frame_width*2;
for(i=0;i<3;i++)
c[i]=FrameY+Frame_height/2+Frame_height*0;
for(i=3;i<6;i++)
c[i]=FrameY+Frame_height/2+Frame_height*1;
for(i=6;i<9;i++)
c[i]=FrameY+Frame_height/2+Frame_height*2;
make_frame( );
start_game( );
return 0;
}