c c语言拼图游戏代码大全,纯C语言写的拼图游戏源码

#include

#include

#include

#include

int step=0;

void map()//游戏菜单函数。

{ printf("▇▇▇▇▇▇▇▇▇▇▇▇▇▇\n");

printf("█ 拼 图 游 戏 █\n");

printf("█ 1. 3×3 █\n");

printf("█ 2. 4×4 █\n");

printf("█ 3. 5×5 █\n");

printf("█ 4. 退出 █\n");

printf("▇▇▇▇▇▇▇▇▇▇▇▇▇▇\n");

}

void fun(int *a,int t,int m,int r)

{

int i;

int g;

for(i=0;i

{

if(i%r==(r-1)&&(i+t)%r==0)//让数组不能跨行转换。

continue;

if(i%r==0&&(i+t)%r==(r-1))

continue;

if(a[i+t]==0 && (i+t)>=0 && (i+t)

{

g=a[i];

a[i]=a[i+t];

a[i+t]=g;

step++;

return ;

}

}

}

void move(int *a,int m,int r)

{//上下左右移动函数。

char ch;

int i,j;

while(ch=getch())

{

system("cls");

if(ch=='w')

{

i=-r;

fun(a,i,m,r);

}

else if(ch=='s')

{

i=r;

fun(a,i,m,r);

}

else if(ch=='a')

{

i=-1;

fun(a,i,m,r);

}

else if(ch=='d')

{

i=1;

fun(a,i,m,r);

}

else if(ch=='4')

{

printf("您已退出游戏,请下次再玩!\n");

exit(0);

}

for(i=0;i

{

if(a[i]==0)

printf(" ");

else

printf("%2d ",a[i]);

if((i+1)%r==0)

putchar(10);

}

printf("共走:%d步!\n",step);

}

}

void map_x(int m,int r)

{

int i=1,j,a[25],n;

srand(time(NULL));

printf("拼图游戏开始了:\n");

while(i

{

n=1+rand()%(m-1);

for(j=1;j

{

if(n==a[j])

break;

}

if(j==i)

{

a[i]=n;

i++;

}

}

a[0]=0;

for(i=0;i

{

if(a[i]==0)

printf(" ");

else

printf("%2d ",a[i]);

if((i+1)%r==0)

putchar(10);

}

printf("共走:%d步!\n",step);

move(a,m,r);

}

int main()

{

int i,n;

char ch;

map();//显示菜单。

printf("请选择所玩游戏的级别:\n");

ch=getch();

while(ch!='1' && ch!='2' && ch!='3' &&ch!='4')

{

printf("输入有误,请重新输入:\n");

ch=getch();

}

switch(ch)

{

case '1':

map_x(9,3);

break;

case '2':

map_x(16,4);

break;

case '3':

map_x(25,5);

break;

case '4':

printf("您已退出游戏,请下次再玩!\n");

exit(0);

break;

}

return 0;

}

//对于确定的循环一般用for;而不确定的循环则用while.

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com

特别注意:本站所有转载文章言论不代表本站观点!

本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值