拼图游戏C语言代码

2022-05-19 22:27:15   第一文档网     [ 字体: ] [ 阅读: ] [ 文档下载 ]
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。下载word有问题请添加QQ:admin处理,感谢您的支持与谅解。点击这里给我发消息

#第一文档网# 导语】以下是®第一文档网的小编为您整理的《拼图游戏C语言代码》,欢迎阅读!
拼图,语言,代码,游戏

#include #include #include #include #include

int s[20][20],map[20][20]; void input(int n) {

srand(time(0)); 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; for (i=n;i>0;i--) for (j=n;j>0;j--) {

a=rand()%i+1; b=rand()%j+1; t=s[a][b]; s[a][b]=s[i][j]; s[i][j]=t; } }

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 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"); } }

void des() {

printf("\t\t游戏为数字拼图1.1版本,w,a,s,d基础\n\ \t上增加方向键控制数字板移动\n\n\

\t界面仍然很简陋,仅有控制台,但已很接近图形界面\n\n\ \t其中数字为可移动板,空格为可移动位置\n\n\ \t按任意键返回...");

if((char)getch()<0) getch(); system("cls"); }

int main() {

int x,y,i,j,t,k,key; char ch; begin:

printf("\t\t##拼图游戏1.1版本欢迎您的使用##\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~9) Esc:上一步\n"); ch=getch();

if(ch==' ') {system("cls");goto begin;} k=ch-'0';

if(2 input(k); else {

system("cls");

printf("\n输入有误,请重新输入!\n\n"); continue; }

for(i=1;i<=k;i++) for(j=1;j<=k;j++)


if(s[i][j]==0) {x=i;y=j;} output(k); while(1) {

printf("请输入操作 w/↑: a/←: s/↓: d/: Esc:重新开始\n"); ch=getch();

if(ch<0) ch=getch(); key=1; switch(ch) {

case 'w':case 72:case 'W':if(x{s[x][y]=s[x+1][y];x++;key=0;}else t--;break;

case 'a':case 75:case 'A':if(y case 's':case 80:case 'S':if(x>1) {s[x][y]=s[x-1][y];x--;key=0;}else t--;break; case 'd':case 77:case 'D':if(y>1) {s[x][y]=s[x][y-1];y--;key=0;}else t--;break; case ' ':system("cls"); goto begin; default :key=1;t--;break; }

s[x][y]=0; output(k);

if(key) printf("输入有误\n"); if(ins(k)) {

printf("****G O O D !****\n共用%d\n\n按任意键继续...\n\n",t-1); if((char)getch()<0) getch(); system("cls"); break; }

else printf(" 已用%d\n",t++); } }

else {system("cls"); printf("输入有误请重新输入\n");goto begin;} return 0; }


本文来源:https://www.dywdw.cn/733cda72a417866fb84a8e1c.html

相关推荐
推荐阅读