不错的拼图游戏、
#include "stdio.h"
#include "conio.h"
#include <stdlib.h>
int v,i,j,row,col,xx,yy,temp;
char t;
char x[3][3]={ {'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
void display_pailie()
{
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
{ printf("%c ",x[i][j]);
if(j==2)printf("\n");
}
}
int check_over()
{
int ok;
if(x[0][0]==' ' &&
x[0][1]=='2' &&
x[0][2]=='3' &&
x[1][0]=='4' &&
x[1][1]=='5' &&
x[1][2]=='6' &&
x[2][0]=='7' &&
x[2][1]=='8' &&
x[2][2]=='9'
) ok=1; else ok=0;
return(ok);
}
void exchange(int r1, int c1, int r2, int c2)
{
int h;
if(r2>=0 && r2<=2 && c2>=0 && c2<=2)
{
h=x[r1][c1];
x[r1][c1]=x[r2][c2];
x[r2][c2]=h;
}
}
void find_char_xy(char c)
{
int i,j;
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
if(x[i][j]==c)
{
if(c==' '){ xx=i; yy=j; }
if(c!=' '){row=i; col=j;}
}
}
void rand_pailie()
{
int v,bx;
randomize();
for(bx=0;bx<70;bx++)
{
v=random(4);
find_char_xy(' ');
switch(v)
{
case 0: exchange(xx,yy,xx-1,yy); break;
case 1: exchange(xx,yy,xx,yy-1); break;
case 2: exchange(xx,yy,xx+1,yy); break;
case 3: exchange(xx,yy,xx,yy+1); break;
}
}
}
main()
{
again:
clrscr();
display_pailie();
printf("plase enter any key...");
getch();
clrscr();
printf("1\n");
x[0][0]=' ';
rand_pailie();
display_pailie();
do{
t=getch();
find_char_xy( t ); /* row col*/
find_char_xy(' '); /* xx yy*/
if( (xx-1==row && yy==col)||(xx+1==row && yy==col) || (xx==row && yy-1==col) || (xx==row && yy+1==col) )
{
x[xx][yy]=x[row][col];
x[row][col]=' ';
}
clrscr();
printf("1\n");
display_pailie();
}while(check_over()==0);
x[0][0]='1';
clrscr();
display_pailie();
printf("GAME OVER, DO YOU PLAY AGAIN?(Y/N)");
t=getche();
if(t=='Y' || t=='y') goto again;
}