文档库 最新最全的文档下载
当前位置:文档库 › 拼图游戏代码

拼图游戏代码

#include
#include
#include
#include
#include
#include

void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void GotoXY(int y,int x) //光标定位
{
HANDLE hout;
COORD coord;
coord.X=x;
coord.Y=y;
hout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hout,coord);
}
int s[20][20],map[20][20];
int x,y;

void swap(int *a,int *b)
{
int c=*a;
*a=*b;
*b=c;
}
int F(int i,int j,int n) //逆序数
{
int k=s[i][j];
int ans = 0;
for(;j<=n;j++) if(k>s[i][j]) ans++;
i++;
for(;i<=n;i++)
for(j=1;j<=n;j++)
if(k>s[i][j]) ans++;
return ans;
}
void ok(int n,int ans) //判断是否可完成
{
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
ans = (ans+F(i,j,n))%2;
}
if(ans%2) //不可完成对调一块
if(s[1][1]==0) swap(&s[1][2],&s[1][3]);
else if(s[1][2] == 0) swap(&s[1][1],&s[1][3]);
else swap(&s[1][1],&s[1][2]);
}
void mk(int n) //打乱拼图
{
srand(time(0));
int i,j,a,b,t;
for (i=n;i>0;i--)
for (j=n;j>0;j--)
{
a=rand()%i+1; b=rand()%j+1;
swap(&s[a][b],&s[i][j]);
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(s[i][j]==0) {x=i;y=j;}
}
void input(int n) //初始化
{
int i,j,k,a,b,t;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
{
s[i][j]=(i-1)*n+j-1;
map[i][j]=s[i][j]+1;
}
map[n][n]=0;
}
int ins(int n) //判断是否完成拼图
{
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(s[i][j]!=map[i][j]) return 0;
return 1;
}
void move(int ex,int ey) //移动操作
{
GotoXY(ex-1,ey*3-3);
printf(" ");
GotoXY(x-1,y*3-3);
printf("%-3d",s[ex][ey]);
s[x][y]=s[ex][ey];
x=ex;y=ey;
}
void output(int n) //输出
{
int i,j,k;
system("cls");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf(s[i][j]?"%-3d":" ",s[i][j]);
printf(" ");
for(j=1;j<=n;j++) printf(map[i][j]?"%-3d":" ",map[i][j]);
printf("\n");
}
printf("\n\n\n\n\n操作:w/↑:上 a/←:左 s/↓:下 d/右:右 Esc:重新开始\n");
}
void des() //说明
{
printf("\t本游戏为数字拼图1.2,在w,a,s,d基础\n\
上增加方向键控制数字板移动\n\n\
界面很简陋,仅有控制台,本人没接触到图形界面\n\n\
其中数字代表可移动板,空格代表可移动位置\n\n\
游戏不可完成性BUG基本消除\n\n\
按任意键返回...");
if((char)getch()<0) getch();
system("cls");
}
int main()
{
SetConsoleTitle("拼图游戏");
system("mode con cols=55 lines=24");
HideCursor();
int i,j,t,k,key;
char ch;
begin:
printf("\t\t##拼图游戏欢迎您的使用##\n\n");

printf("请输入指令 1:游戏介绍 2:开始游戏 Esc:退出游戏\n");
ch=getch();
system("cls");
if(ch=='') return 0;
else if(ch=='1') {des();goto begin;}
else if(ch=='2')
while(1)
{

t=1;
printf("请输入拼图大小(3~7) Esc:上一步\n");
ch=getch();
if(ch=='') {system("cls");goto begin;}
k=ch-'0';
if(2{
input(k);
mk(k);
ok(k,x+y+k+1);
}
else
{
system("cls");
printf("\n输入有误,请重新输入!\n\n");
continue;
}
output(k);
while(1)
{
ch=getch(); if(ch == 0xe0) ch=getch();
switch(ch) //移动数字
{
case 'w':case 72:case 'W':if(xcase 'a':case 75:case 'A':if(ycase 's':case 80:case 'S':if(x>1) move(x-1,y);else t--;break;
case 'd':case 77:case 'D':if(y>1) move(x,y-1);else t--;break;
case '':system("cls"); goto begin;
default :t--;break;
}
s[x][y]=0;
if(ins(k))
{
GotoXY(k+1,k/2);
printf("****G O O D !****\n 共用%d步\n\n 按任意键继续...\n\n",t);
if((char)getch()<0) getch();
system("cls");
break;
}
else {GotoXY(k+2,1);printf("已用%d步\n",t++);}
}
}
else {goto begin;}
return 0;
}

相关文档