当前位置:首页 » 编程语言 » c语言推箱子游戏设计说明
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言推箱子游戏设计说明

发布时间: 2023-05-28 06:33:42

① 用c语言写推箱子游戏,怎样把每次的步数写进文件并比较的出最小步数

我可以给你一些C操作文件的建议
写入文件使用fwrite()函数,
比较文件中各个数据的最小值,你可以逐个读出,在比较
下面是一个例子
#include<stdio.h>
int main()
{
FILE *fp;
int num[5]={10,20,50,40,60};
int i;
int number;
fp=fopen("Data","w"); //打开Data文件,文件操作为写入
if(NULL==fp) //若打开失败,返回NULL
{
printf("Can't open the file\n");
return;
}
for(i=0;i<5;i++) //将num中的数据逐个写入
{
fwrite(&num[i],sizeof(int),1,fp);
}
fclose(fp); //关闭文件
fp=fopen("Data","r"); //重新打开,文件操作为读
while(fread(&number,sizeof(int),1,fp)!=0) //逐个读取文件中的数据,并赋予number
printf("\n%d",number); //输出每个数据
fclose(fp);
}

② 教你如何使用C语言编写简单小游戏

编写程序,实现如下表所示的5-魔方阵。
17

24

1

8

15

23

5

7

14

16

4

6

13

20

22

10

12

19

21

3

11

18

25

2

9

5-魔方阵
问题分析
所谓“n-魔方阵”,指的是使用1〜n2共n2个自然数排列成一个n×n的方阵,其中n为奇数;该方阵的每行、每列及对角线元素之和都相等,并为一个只与n有关的常数,该常数为n×(n2+1)/2。
例如5-魔方阵,其第一行、第一列及主对角线上各元素之和如下:
第一行元素之和:17+24+1+8+15=65
第一列元素之和:17+23+4+10+11=65
主对角线上元素之和:17+5+13+21+9=65

n×(n2+1)/2=5×(52+1)/2=65
可以验证,5-魔方阵中其余各行、各列及副对角线上的元素之和也都为65。
假定阵列的行列下标都从0开始,则魔方阵的生成方法为:在第0行中间置1,对从2开始的其余n2-1个数依次按下列规则存放:
(1)
假定当前数的下标为(i,j),则下一个数的放置位置为当前位置的右上方,即下标为(i-1,j+1)的位置。
(2)
如果当前数在第0行,即i-1小于0,则将下一个数放在最后一行的下一列上,即下标为(n-1,j+1)的位置。
(3)
如果当前数在最后一列上,即j+1大于n-1,则将下一个数放在上一行的第一列上,即下标为(i-1,0)的位置。
(4)
如果当前数是n的倍数,则将下一个数直接放在当前位置的正下方,即下标为(i+1,j)的位置。
算法设计
在设计算法时釆用了下面一些方法:
定义array()函数,array()函数的根据输入的n值,生成并显示一个魔方阵,当发现n不是奇数时,就加1使之成为奇数。
使用动态内存分配与释放函数malloc()与free(),在程序执行过程中动态分配与释放内存,这样做的好处是使代码具有通用性,同时提高内存的使用率。
在分配内存时还要注意,由于一个整型数要占用两个内存,因此,如果魔方阵中要存放的数有max个,则分配内存时要分配2*max个单元,从而有malloc(max+max)。在malloc()函数中使用max+max而不是2*max是考虑了程序运行的性能。
显然应该使用二维数组来表示魔方阵,但虽然数组是二维形式的,而由于内存是一维线性的,因此在存取数组元素时,要将双下标转换为单个索引编号。在程序中直接定义了指针变量来指向数组空间,即使用malloc()函数分配的内存。

③ 求C语言小游戏源程序

新手要方便写代码,可以收藏下面几个自编函数:

  1. gtxy (6, 3) //光标定位于窗口的第6列,第3行处(准备输出,行与列都是从0算起)

  2. Color (4, 0) //设置为红字配黑底 如 Color (10, 0)则是淡绿字配黑底

  3. yinc (1,0) //隐藏光标(第二个参数设为0就隐藏,没有光标闪烁,yinc代表隐藏)

  4. kou(80,25) //设定窗口缓冲区大小为80列,25行

    下面几个是库函数,不需自己编写,只要用#include包含就可以使用。

  5. SetConsoleTitle("俄罗斯方块"); //设置窗口左上角标题栏处出现"俄罗斯方块"5个字

  6. srand( (unsigned) time(NULL) ); //初始化随机数发生器

  7. n= rand( ) % 20; //产生随机数0-19中的一个. 如 rand( )%5 就产生0-4中的一个数

    SetConsoleTitle( )函数在<windows.h>里,srand( )函数与rand( )函数要配合用,

    就是同时要用,在<stdlib.h>里。如果 rand( )%10+1 就产生1-10之中的一个数。

  8. Sleep(300); //延时300毫秒(就是程序暂停300毫秒后继续运行)

  9. system("cls"); //清屏(把窗口里的内容全部清除,光标定于(0,0)位置处)

    这两个函数都在<windows.h>里。开头4个自编函数 编写如下:

void gtxy (int x, int y) //控制光标位置的函数

{ COORD pos;

pos.X = x;

pos.Y = y;

SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos );

}

void Color (short ForeColor= 7, short BackGroundColor= 0) //设定颜色的函数

{ HANDLE hl = GetStdHandle ( STD_OUTPUT_HANDLE );

SetConsoleTextAttribute ( hl, ForeColor + BackGroundColor * 0x10 );

}

声明时原型可写 void Color (short x, short y);

void yinc (int x,int y) //隐藏光标的函数

{ CONSOLE_CURSOR_INFO gb={ x , y }; //gb代表光标

SetConsoleCursorInfo ( GetStdHandle(STD_OUTPUT_HANDLE), &gb );

}

void kou(int w,int h) //设置窗口大小的函数

{HANDLE hl=GetStdHandle ( STD_OUTPUT_HANDLE ) ;

COORD size={ w , h };

SetConsoleScreenBufferSize( hl , size );

SMALL_RECT rc={ 0, 0, w, h };

SetConsoleWindowInfo( hl, 1, &rc );

}

最后这个函数,参数w是宽h是高。里边5行中第一行定义了句柄型变量hl,并给它赋值。

第二行定义了坐标型结构体变量size,它的取值决定了缓冲区的大小。第三行就是使用

size的值设置好缓冲区大小。第四行定义了变量rc,它的值决定当前窗口显示的位置与

大小(不得超过缓冲区的大小)。前两个0,0是从缓冲区左上角0列0行位置处开始,后两

个参数可以小于w和h.比如rc={0,0,w-10,h-5}; 最后一行使用rc的值设置好窗口,中间

那个参数要为" 1 "或写“ true ”才有效。

④ c语言推箱子代码

Here
you
are!
编译通过。
/*
推箱子游戏
*/
#include
<dos.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<ctype.h>
#include
<conio.h>
#include
<bios.h>
#include
<alloc.h>
/*
定义二维数组ghouse来记录屏幕上各点的状态,
其中:0表示什么都没有,'b'表示箱子,'w'表示墙壁,'m'表示目的地,'i'表示箱子在目的地。
*/
char
ghouse[20][20];
/*
以下函数为直接写屏函数,很酷的函数哦!是我朋友告诉我的。
*/
char
far
*screen=(char
far*
)0xb8000000;
void
putchxy(int
y,int
x,char
ch,char
fc,char
bc)
{
screen[(x*160)+(y<<1)+0]=ch;
screen[(x*160)+(y<<1)+1]=(bc*16)+fc;
}
/*
定义判断是否胜利的数据结构
*/
typedef
struct
winer
{
int
x,y;
struct
winer
*p;
}winer;
/*
箱子位置的数据结构
*/
typedef
struct
boxs
{
int
x,y;
struct
boxs
*next;
}boxs;
/*
在特定的坐标上画墙壁并用数组记录状态的函数
*/
void
printwall(int
x,int
y)
{
putchxy(y-1,x-1,219,MAGENTA,BLACK);
ghouse[x][y]='w';
}
/*
在特定的坐标上画箱子并用数组记录状态的函数
*/
void
printbox(int
x,int
y)
{
putchxy(y-1,x-1,10,WHITE,BLACK);
ghouse[x][y]='b';
}
/*
在特定的坐标上画目的地并用数组记录状态的函数
*/
void
printwhither1(int
x,int
y,winer
**win,winer
**pw)
{
winer
*qw;
putchxy(y-1,x-1,'*',YELLOW,BLACK);
ghouse[x][y]='m';
if(*win==NULL)
{
*win=*pw=qw=(winer*
)malloc(sizeof(winer));
(*pw)->x=x;(*pw)->y=y;(*pw)->p=NULL;
}
else
{
qw=(winer*
)malloc(sizeof(winer));
qw->x=x;qw->y=y;(*pw)->p=qw;(*pw)=qw;qw->p=NULL;
}
}
/*
在特定的坐标上画目的地并用数组记录状态的函数
*/
void
printwhither(int
x,int
y)
{
putchxy(y-1,x-1,'*',YELLOW,BLACK);
ghouse[x][y]='m';
}
/*
在特定的坐标上画人的函数
*/
void
printman(int
x,int
y)
{
gotoxy(y,x);
_AL=02;_CX=01;_AH=0xa;
geninterrupt(0x10);
}
/*
在特定的坐标上画箱子在目的地上并用数组记录状态的函数
*/
void
printboxin(int
x,int
y)
{
putchxy(y-1,x-1,10,YELLOW,BLACK);
ghouse[x][y]='i';
}
/*
初始化函数,初始化数组和屏幕
*/
void
init()
{
int
i,j;
system("cls");
for(i=0;i<20;i++)
for(j=0;j<20;j++)
ghouse[i][j]=0;
_AL=3;
_AH=0;
geninterrupt(0x10);
gotoxy(40,4);
printf("Welcome
to
push
box
world!");
gotoxy(40,6);
printf("Press
up,down,left,right
to
play.");
gotoxy(40,8);
printf("Press
Esc
to
quit
it.");
gotoxy(40,10);
printf("Press
space
to
reset
the
game.");
gotoxy(40,12);
printf("April
30th
2004.");
}
/*
第一关的图象初始化
*/
winer
*inithouse1()
{
int
x,y;
winer
*win=NULL,*pw;
gotoxy(8,2);
printf("Level
No.1");
for(x=1,y=5;y<=9;y++)
printwall(x+4,y+10);
for(y=5,x=2;x<=5;x++)
printwall(x+4,y+10);
for(y=9,x=2;x<=5;x++)
printwall(x+4,y+10);
for(y=1,x=3;x<=8;x++)
printwall(x+4,y+10);
for(x=3,y=3;x<=5;x++)
printwall(x+4,y+10);
for(x=5,y=8;x<=9;x++)
printwall(x+4,y+10);
for(x=7,y=4;x<=9;x++)
printwall(x+4,y+10);
for(x=9,y=5;y<=7;y++)
printwall(x+4,y+10);
for(x=8,y=2;y<=3;y++)
printwall(x+4,y+10);
printwall(5+4,4+10);
printwall(5+4,7+10);
printwall(3+4,2+10);
printbox(3+4,6+10);
printbox(3+4,7+10);
printbox(4+4,7+10);
printwhither1(4+4,2+10,&win,&pw);
printwhither1(5+4,2+10,&win,&pw);
printwhither1(6+4,2+10,&win,&pw);
printman(2+4,8+10);
return
win;
}
/*
第三关的图象初始化
*/
winer
*inithouse3()
{int
x,y;
winer
*win=NULL,*pw;
gotoxy(8,3);
printf("Level
No.3");
for(x=1,y=2;y<=8;y++)
printwall(x+4,y+10);
for(x=2,y=2;x<=4;x++)
printwall(x+4,y+10);
for(x=4,y=1;y<=3;y++)
printwall(x+4,y+10);
for(x=5,y=1;x<=8;x++)
printwall(x+4,y+10);
for(x=8,y=2;y<=5;y++)
printwall(x+4,y+10);
for(x=5,y=5;x<=7;x++)
printwall(x+4,y+10);
for(x=7,y=6;y<=9;y++)
printwall(x+4,y+10);
for(x=3,y=9;x<=6;x++)
printwall(x+4,y+10);
for(x=3,y=6;y<=8;y++)
printwall(x+4,y+10);
printwall(2+4,8+10);
printwall(5+4,7+10);
printbox(6+4,3+10);
printbox(4+4,4+10);
printbox(5+4,6+10);
printwhither1(2+4,5+10,&win,&pw);
printwhither1(2+4,6+10,&win,&pw);
printwhither1(2+4,7+10,&win,&pw);
printman(2+4,4+10);
return
win;
}
/*
第二关的图象初始化
*/
winer
*inithouse2()
{int
x,y;
winer
*win=NULL,*pw;
gotoxy(8,2);
printf("Level
No.2");
for(x=1,y=4;y<=7;y++)
printwall(x+4,y+10);
for(x=2,y=2;y<=4;y++)
printwall(x+4,y+10);
for(x=2,y=7;x<=4;x++)
printwall(x+4,y+10);
for(x=4,y=1;x<=8;x++)
printwall(x+4,y+10);
for(x=8,y=2;y<=8;y++)
printwall(x+4,y+10);
for(x=4,y=8;x<=8;x++)
printwall(x+4,y+10);
for(x=4,y=6;x<=5;x++)
printwall(x+4,y+10);
for(x=3,y=2;x<=4;x++)
printwall(x+4,y+10);
for(x=4,y=4;x<=5;x++)
printwall(x+4,y+10);
printwall(6+4,3+10);
printbox(3+4,5+10);
printbox(6+4,6+10);
printbox(7+4,3+10);
printwhither1(5+4,7+10,&win,&pw);
printwhither1(6+4,7+10,&win,&pw);
printwhither1(7+4,7+10,&win,&pw);
printman(2+4,6+10);
return
win;
}
/*
第四关的图象初始化
*/
winer
*inithouse4()
{int
x,y;
winer
*win=NULL,*pw;
gotoxy(8,2);
printf("Level
No.4");
for(x=1,y=1;y<=6;y++)
printwall(x+4,y+10);
for(x=2,y=7;y<=8;y++)
printwall(x+4,y+10);
for(x=2,y=1;x<=7;x++)
printwall(x+4,y+10);
for(x=7,y=2;y<=4;y++)
printwall(x+4,y+10);
for(x=6,y=4;y<=9;y++)
printwall(x+4,y+10);
for(x=3,y=9;x<=5;x++)
printwall(x+4,y+10);
for(x=3,y=3;y<=4;y++)
printwall(x+4,y+10);
printwall(3+4,8+10);
printbox(3+4,5+10);
printbox(4+4,4+10);
printbox(4+4,6+10);
printbox(5+4,5+10);
printbox(5+4,3+10);
printwhither1(3+4,7+10,&win,&pw);
printwhither1(4+4,7+10,&win,&pw);
printwhither1(5+4,7+10,&win,&pw);
printwhither1(4+4,8+10,&win,&pw);
printwhither1(5+4,8+10,&win,&pw);
printman(2+4,2+10);
return
win;
}
/*
移动在空地上的箱子到空地上
*/
movebox(int
x,int
y,char
a)
{switch(a)
{
case
'u':ghouse[x-1][y]=0;printf("
");
printbox(x-2,y);printman(x-1,y);
ghouse[x-2][y]='b';break;
case
'd':ghouse[x+1][y]=0;printf("
");
printbox(x+2,y);printman(x+1,y);
ghouse[x+2][y]='b';break;
case
'l':ghouse[x][y-1]=0;printf("
");
printbox(x,y-2);printman(x,y-1);
ghouse[x][y-2]='b';break;
case
'r':ghouse[x][y+1]=0;printf("
");
printbox(x,y+2);printman(x,y+1);
ghouse[x][y+2]='b';break;
default:
break;
}
}
/*
移动在目的地上的箱子到空地上
*/
moveinbox(int
x,int
y,char
a)
{switch(a)
{
case
'u':ghouse[x-1][y]='m';printf("
");
printbox(x-2,y);printman(x-1,y);
ghouse[x-2][y]='b';break;
case
'd':ghouse[x+1][y]='m';printf("
");
printbox(x+2,y);printman(x+1,y);
ghouse[x+2][y]='b';break;
case
'l':ghouse[x][y-1]='m';printf("
");
printbox(x,y-2);printman(x,y-1);
ghouse[x][y-2]='b';break;
case
'r':ghouse[x][y+1]='m';printf("
");
printbox(x,y+2);printman(x,y+1);
ghouse[x][y+2]='b';break;
default:
break;
}
}
/*
移动在空地上的箱子到目的地上
*/
moveboxin(int
x,int
y,char
a)
{switch(a)
{
case
'u':ghouse[x-1][y]=0;printf("
");
printboxin(x-2,y);printman(x-1,y);
ghouse[x-2][y]='i';break;
case
'd':ghouse[x+1][y]=0;printf("
");
printboxin(x+2,y);printman(x+1,y);
ghouse[x+2][y]='i';break;
case
'l':ghouse[x][y-1]=0;printf("
");
printboxin(x,y-2);printman(x,y-1);
ghouse[x][y-2]='i';break;
case
'r':ghouse[x][y+1]=0;printf("
");
printboxin(x,y+2);printman(x,y+1);
ghouse[x][y+2]='i';break;
default:
break;
}
}
/*
移动在目的地上的箱子到目的地
*/
moveinboxin(int
x,int
y,char
a)
{switch(a)
{
case
'u':ghouse[x-1][y]='m';printf("
");
printboxin(x-2,y);printman(x-1,y);
ghouse[x-2][y]='i';break;
case
'd':ghouse[x+1][y]='m';printf("
");
printboxin(x+2,y);printman(x+1,y);
ghouse[x+2][y]='i';break;
case
'l':ghouse[x][y-1]='m';printf("
");
printboxin(x,y-2);printman(x,y-1);
ghouse[x][y-2]='i';break;
case
'r':ghouse[x][y+1]='m';printf("
");
printboxin(x,y+2);printman(x,y+1);
ghouse[x][y+2]='i';break;
default:
break;
}
}
/*
判断特定的坐标上的状态
*/
int
judge(int
x,int
y)
{int
i;
switch(ghouse[x][y])
{
case
0:
i=1;break;
case
'w':
i=0;break;
case
'b':
i=2;break;
case
'i':
i=4;break;
case
'm':
i=3;break;
default:
break;
}
return
i;
}
/*
处理按下键盘后,人物移动的主函数
*/
move(int
x,int
y,char
a)
{switch(a)
{
case
'u':if(!judge(x-1,y))
{gotoxy(y,x);break;}
else
if(judge(x-1,y)==1||judge(x-1,y)==3)
{if(judge(x,y)==3)
{
printwhither(x,y);printman(x-1,y);break;}
else
{printf("
");printman(x-1,y);break;}
}
else
if(judge(x-1,y)==2)
{
if(judge(x-2,y)==1)
{movebox(x,y,'u');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y,x-1);
}
else
if(judge(x-2,y)==3)
{
moveboxin(x,y,'u');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y,x-1);
}
else
gotoxy(y,x);
break;
}
else
if(judge(x-1,y)==4)
{
if(judge(x-2,y)==1)
{moveinbox(x,y,'u');
if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x-1);
}
else
if(judge(x-2,y)==3)
{
moveinboxin(x,y,'u');
if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x-1);
}
else
gotoxy(y,x);
break;
}
case
'd':if(!judge(x+1,y))
{gotoxy(y,x);break;}
else
if(judge(x+1,y)==1||judge(x+1,y)==3)
{if(judge(x,y)==3)
{
printwhither(x,y);printman(x+1,y);break;}
else
{printf("
");printman(x+1,y);break;}
}
else
if(judge(x+1,y)==2)
{
if(judge(x+2,y)==1)
{movebox(x,y,'d');
if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x+1);
}
else
if(judge(x+2,y)==3)
{moveboxin(x,y,'d');
if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x+1);
}
else
gotoxy(y,x);
break;
}
else
if(judge(x+1,y)==4)
{
if(judge(x+2,y)==1)
{moveinbox(x,y,'d');
if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x+1);
}
else
if(judge(x+2,y)==3)
{moveinboxin(x,y,'d');
if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x+1);
}
else
gotoxy(y,x);
break;
}
case
'l':if(!judge(x,y-1))
{gotoxy(y,x);break;}
else
if(judge(x,y-1)==1||judge(x,y-1)==3)
{if(judge(x,y)==3)
{
printwhither(x,y);printman(x,y-1);break;}
else
{printf("
");printman(x,y-1);break;}
}
else
if(judge(x,y-1)==2)
{
if(judge(x,y-2)==1)
{movebox(x,y,'l');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y-1,x);
}
else
if(judge(x,y-2)==3)
{moveboxin(x,y,'l');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y-1,x);
}
else
gotoxy(y,x);
break;
}
else
if(judge(x,y-1)==4)
{
if(judge(x,y-2)==1)
{moveinbox(x,y,'l');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y-1,x);
}
else
if(judge(x,y-2)==3)
{moveinboxin(x,y,'l');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y-1,x);
}
else
gotoxy(y,x);
break;
}
case
'r':if(!judge(x,y+1))
{gotoxy(y,x);break;}
else
if(judge(x,y+1)==1||judge(x,y+1)==3)
{if(judge(x,y)==3)
{printwhither(x,y);printman(x,y+1);break;}
else
{printf("
");printman(x,y+1);break;}
}
else
if(judge(x,y+1)==2)
{
if(judge(x,y+2)==1)
{movebox(x,y,'r');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y+1,x);
}
else
if(judge(x,y+2)==3)
{moveboxin(x,y,'r');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y+1,x);
}
else
gotoxy(y,x);
break;
}
else
if(judge(x,y+1)==4)
{
if(judge(x,y+2)==1)
{moveinbox(x,y,'r');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y+1,x);
}
else
if(judge(x,y+2)==3)
{moveinboxin(x,y,'r');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y+1,x);
}
else
gotoxy(y,x);
break;
}
default:
break;
}
}
/*
按下空格键后,回到本关开头的函数
*/
void
reset(int
i)
{switch(i)
{
case
0:
init();
inithouse1();break;
case
1:
init();
inithouse2();break;
case
2:
init();
inithouse3();break;
case
3:
init();
inithouse4();break;
default:break;
}
}
/*
主函数main
*/
main()
{int
key,x,y,s,i=0;
winer
*win,*pw;
_AL=3;_AH=0;
geninterrupt(0x10);
init();
win=inithouse1();
do{
_AH=3;
geninterrupt(0x10);
x=_DH+1;y=_DL+1;
while(bioskey(1)==0);
key=bioskey(0);
switch(key)
{
case
0x4800:move(x,y,'u');break;
/*
按下向上键后
*/
case
0x5000:move(x,y,'d');break;
/*
按下向下键后
*/
case
0x4b00:move(x,y,'l');break;
/*
按下向左键后
*/
case
0x4d00:move(x,y,'r');break;
/*
按下向右键后
*/
case
0x3920:reset(i);break;
/*
按下空格键后
*/
default:break;
}
s=0;
pw=win;
while(pw)
{
if(ghouse[pw->x][pw->y]=='m')
s++;
pw=pw->p;
}
if(s==0)
{
free(win);
gotoxy(25,2);
printf("Congratulate!
You
have
passed
Level
%d!",i+1);
getch();
i++;
switch(i)
{
case
1:
init();
win=inithouse2();break;
case
2:
init();
win=inithouse3();break;
case
3:
init();
win=inithouse4();break;
case
4:
gotoxy(8,14);
printf("Great!
You
have
passed
all
the
levels!
Press
any
key
to
quit!");
key=0x011b;getch();break;
default:
break;
}
}
}while(key!=0x011b);
_AL=3;
_AH=0;
geninterrupt(0x10);
}

⑤ 请问大佬如何用C语言实现推箱子的主菜单界面和游戏结束弹出框

可以自己用绘图的方式来实现游戏主菜单,主菜单也可以用Dialog的方式来做,但是不友好。
游戏结束弹出框调用系统对话框就行了
MessageBox函数或MFC的AfxMessageBox函数。
建议去下载推箱子游戏的源代码,自己慢慢研究研究,网络是这种代码应该很好找。

望采纳谢谢

⑥ C语言小程序推箱子问题

你好,这是推箱子的全部代码
/*图形模式下的推箱子*/
#define MAX 2 /*游戏中总关数*/
#define STARTX 180
#define STARTY 80
#define BKCOLOR BLACK
#define MANCOLOR RED
#define OBJECTCOLOR YELLOW
#define TIMEINT 2
#define STARNUM 300
#define STEPMAX 5
#include<string.h>
#include<bios.h>
#include<stdio.h>
#include<dos.h>
#include<graphics.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#define Key_R 0x1372
#define Key_Up 0x4800
#define Key_Enter 0x1c0d
#define Key_Down 0x5000
#define Key_P 0x1970
#define Key_Esc 0x11b
#define Key_Right 0x4d00
#define Key_Left 0x4b00

typedef struct star
{
int x;
int y;
int c;
}Star;
Star s[STARNUM];

typedef struct record
{
char name[20];
int second;
struct time t;
struct date d;
}Record;
Record r[MAX];

typedef struct c
{
int x;
int y;
}Add;

typedef struct a
{
int x;
int y;
}Player;
Player p;

char name[20]; /*进入游戏时记录玩家的姓名*/
time_t t1,t2; /*游戏结束时的时间*/
int Ide; /*开始进入游戏时,选择菜单时返的功能号(1,2,3)*/
int MissionNum; /*玩家正在玩的关数*/
int BoxNum; /*目的地的个数*/
int Key; /*玩家按键*/
int map[10][10]; /*地图.(空=0),(人=1),(箱子=2),(墙=3),(目的地=4),(人+目的地=5),(箱子+目的地=6)*/
int StepNum;
int DirectionKey; /*用来表示方向键最后一次按键*/
int BoxMove[STEPMAX];
int Step[STEPMAX];

/*函数定义*/
void InputName();
void Init();
void MainMenu();
void JudgeRecord();
void WriteRecord();
void JudgeIde();
void DrawMenu(int );
void Game();
void InitMission(int );
void NextMission();
void InitPic(int ,int ,int );
int Move(Add );
void DrawWall(int ,int );
void DrawBack(int ,int );
void DrawBox(int ,int );
void DrawObject(int ,int );
void DrawMan(int ,int );
void DrawStar();
int JudgeWin();
void InitMission1();
void InitMission2();
int TimeCome();
void ChangeStar();
void InputName();
void ViewRecords();
void DeleteRecords();
void RegisterStep();
void ReverselyMove();
void MoveBack(Add );

void main()
{
InputName();
Init(); /*驱动显卡*/
srand(time(&t1));
MainMenu(); /*画开始菜单(1.start game 2.view records 3.delete records 4.exit game)*/
}

void InputName()
{
char b;
char c;
do
{
printf("\n\nPlease input your password:");
scanf("%s",&b);
}while(b!='l');
clrscr();
do
{
printf("\n\nPlease input your name:");
scanf("%s",name);
printf("Are you sure the name right(Y/N):");
do
{
c=getch();
}while(c!='Y'&&c!='y'&&c!='N'&&c!='n');
}while(c!='Y'&&c!='y');
}
void Init()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\tc");
}

void MainMenu()
{
setbkcolor(BKCOLOR);
cleardevice();
DrawStar(); /*画开始菜单的背景画面*/
DrawStar();
Ide=0,Key=0;
DrawMenu(Ide);
do
{
if(bioskey(1)) /*有键按下则处理按键*/
{
Key=bioskey(0);
switch(Key)
{
case Key_Down: {Ide++;Ide=Ide%4;DrawMenu(Ide);break;}
case Key_Up: {Ide--;Ide=(Ide+4)%4;DrawMenu(Ide);break;}
}
}
else {if(TimeCome()) ChangeStar();} /*改变背景*/
}while(Key!=Key_Enter);
JudgeIde(); /*根据Ide运行不同的程序*/
}

void JudgeIde()
{
switch(Ide)
{
case 0:Game();break;
case 1:{ViewRecords();bioskey(0);MainMenu();break;}
case 2:{DeleteRecords();ViewRecords();bioskey(0);MainMenu();break;}
case 3:exit(0);
}
}

void Game()
{
int i,j,flag;
Add ad;
MissionNum=0;
NextMission();
do
{
flag=0;
Key=bioskey(0);
switch(Key)
{
case Key_Enter:{NextMission();time(&t1);break;}
case Key_Up:{ad.x=-1;ad.y=0;flag=1;DirectionKey=Key;break;}
case Key_Down:{ad.x=1;ad.y=0;flag=1;DirectionKey=Key;break;}
case Key_Left:{ad.x=0;ad.y=-1;flag=1;DirectionKey=Key;break;}
case Key_Right:{ad.x=0;ad.y=1;flag=1;DirectionKey=Key;break;}
case Key_R:{MissionNum--;NextMission();break;}
case Key_Esc:{MainMenu();break;}
case Key_P:{ReverselyMove();break;}
}
if(flag==1)
{if(Move(ad)) {RegisterStep(); if(JudgeWin()) {JudgeRecord();NextMission();}}}
}while(1);
}

void InitMission(int n)
{
int i,j;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
map[i][j]=0;
switch(n)
{
case 1:InitMission1();break; /*第一关*/
case 2:InitMission2();break; /*第二关*/
}
}

void InitPic(int n,int i,int j)
{
switch(n)
{
case 0:DrawBack(i,j);break;
case 1:DrawMan(i,j);break;
case 2:DrawBox(i,j);break;
case 3:DrawWall(i,j);break;
case 4:DrawObject(i,j);break;
case 5:DrawMan(i,j);break;
case 6:DrawBox(i,j);break;
}
}

void NextMission()
{
int i,j;
if(MissionNum+1>MAX) MissionNum=1;
else MissionNum++;
InitMission(MissionNum);
setbkcolor(BKCOLOR);
cleardevice();
for(i=0;i<10;i++)
for(j=0;j<10;j++)
InitPic(map[i][j],i,j);
switch(MissionNum)
{
case 1:outtextxy(200,230,"Mission 1");break;
case 2:outtextxy(200,230,"Mission 2");break;
}
time(&t1);
for(i=0;i<STEPMAX;i++)
{Step[i]=BoxMove[i]=0;}
StepNum=0;
}

int Move(Add a)
{
int flag;
int i=StepNum%STEPMAX;
switch(map[p.x+a.x][p.y+a.y]) /*看下一位置为什么*/
{
case 0:{map[p.x][p.y]-=1;InitPic(map[p.x][p.y],p.x,p.y);
p.x=p.x+a.x;p.y=p.y+a.y;
map[p.x][p.y]+=1;InitPic(map[p.x][p.y],p.x,p.y);flag=1;break;}
case 2:{if(map[p.x+2*a.x][p.y+2*a.y]==0||map[p.x+2*a.x][p.y+2*a.y]==4)
{map[p.x][p.y]-=1;map[p.x+a.x][p.y+a.y]=1;map[p.x+2*a.x][p.y+2*a.y]+=2;
InitPic(map[p.x][p.y],p.x,p.y);
InitPic(map[p.x+a.x][p.y+a.y],p.x+a.x,p.y+a.y);
InitPic(map[p.x+2*a.x][p.y+2*a.y],p.x+2*a.x,p.y+2*a.y);
p.x=p.x+a.x;p.y=p.y+a.y;flag=1;BoxMove[i]=1;}
else flag=0;
break;}
case 3:flag=0;break;
case 4:{map[p.x][p.y]-=1;InitPic(map[p.x][p.y],p.x,p.y);
p.x=p.x+a.x;p.y=p.y+a.y;
map[p.x][p.y]+=1;InitPic(map[p.x][p.y],p.x,p.y);flag=1;break;}
case 6:{if(map[p.x+2*a.x][p.y+2*a.y]==0||map[p.x+2*a.x][p.y+2*a.y]==4)
{map[p.x][p.y]-=1;map[p.x+a.x][p.y+a.y]=5;map[p.x+2*a.x][p.y+2*a.y]+=2;
InitPic(map[p.x][p.y],p.x,p.y);
InitPic(map[p.x+a.x][p.y+a.y],p.x+a.x,p.y+a.y);
InitPic(map[p.x+2*a.x][p.y+2*a.y],p.x+2*a.x,p.y+2*a.y);
p.x=p.x+a.x;p.y=p.y+a.y;flag=1;BoxMove[i]=1;}
else flag=0;
break;}
}
return flag;
}

void DrawWall(int i,int j)
{
DrawBack(i,j);
setfillstyle(9,1);
bar(STARTX+20*j-9,STARTY+20*i-9,STARTX+20*j+9,STARTY+20*i+9);
}

void DrawMan(int i,int j)
{
DrawBack(i,j);
setcolor(MANCOLOR);
circle(STARTX+20*j,STARTY+20*i,9);
arc(STARTX+20*j-3,STARTY+20*i-2,20,160,3);
arc(STARTX+20*j+4,STARTY+20*i-2,20,160,3);
arc(STARTX+20*j,STARTY+20*i-2,220,320,7);
}

void DrawBack(int i,int j)
{
setfillstyle(1,BKCOLOR);
bar(STARTX+20*j-9,STARTY+20*i-9,STARTX+20*j+9,STARTY+20*i+9);
}

void DrawObject(int i,int j)
{
DrawBack(i,j);
setcolor(OBJECTCOLOR);
line(STARTX+20*j-9,STARTY+20*i,STARTX+20*j+9,STARTY+20*i);
line(STARTX+20*j-9,STARTY+20*i-9,STARTX+20*j+9,STARTY+20*i+9);
line(STARTX+20*j-9,STARTY+20*i+9,STARTX+20*j+9,STARTY+20*i-9);
}

void DrawBox(int i,int j)
{
DrawBack(i,j);
setfillstyle(9,3);
bar(STARTX+20*j-9,STARTY+20*i-9,STARTX+20*j+9,STARTY+20*i+9);
}

void DrawMenu(int j)
{
int n;
char *s[4]={"1.Start Game","2.View Records","3.Delete Records","4.Exit Game"};
settextstyle(0,0,1);
setcolor(GREEN);
for(n=0;n<4;n++)
outtextxy(250,170+n*20,s[n]);
setcolor(RED);
outtextxy(250,170+j*20,s[j]);
}

void DrawStar()
{
int w,h,i,dotx,doty,color,maxcolor;
w=getmaxx();
h=getmaxy();
maxcolor=getmaxcolor();
for(i=0;i<STARNUM;i++)
{
s[i].x=1+random(w-1);
s[i].y=1+random(h-1);
s[i].c=random(maxcolor);
putpixel(s[i].x,s[i].y,s[i].c);
}
}
void ChangeStar()
{
int i,maxcolor;
maxcolor=getmaxcolor();
for(i=0;i<STARNUM;i++)
{
s[i].c=random(maxcolor);
putpixel(s[i].x,s[i].y,s[i].c);
}
}

int TimeCome()
{

static long tm, old;
tm=biostime(0,tm);
if(tm-old<TIMEINT) return 0;
else
{
old=tm; return 1;
}
}

int JudgeWin()
{
int n=0,i,j;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(map[i][j]==6) n++;
if(n==BoxNum) return 1;
else return 0;
}

void InitMission1() /*第九关*/
{
int i,j;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
map[i][j]=0;
for(i=0;i<=5;i++)
map[0][i]=3;
for(i=5;i<=7;i++)
{map[2][i]=map[i-1][1]=3;}
for(i=1;i<=4;i++)
{map[6][i]=map[5][i+3]=map[i][0]=3;}
map[3][7]=map[4][7]=map[1][5]=3;
for(i=2;i<=4;i++)
map[2][i]=2;
map[3][4]=map[4][5]=2;
for(i=2;i<=3;i++)
{map[3][i]=map[4][i]=4;}
map[4][4]=4;
p.x=3;p.y=5;
map[3][5]=1;
BoxNum=5;
}

void InitMission2()
{
int i,j;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
map[i][j]=0;
for(i=1;i<=5;i++)
{map[0][i]=map[6][i]=3;}
for(i=2;i<=4;i++)
{map[1][i+3]=map[i][7]=map[i+2][5]=map[i][0]=3;}
map[1][1]=map[2][1]=map[5][0]=map[5][5]=map[4][6]=map[6][0]=3;
map[2][4]=map[3][3]=map[4][2]=map[4][3]=2;
map[2][3]=map[3][2]=map[3][4]=map[4][4]=4;
p.x=1;p.y=3;
map[1][3]=1;
BoxNum=4;
}

void ViewRecords()
{
FILE *fp;
int i;
setbkcolor(BKCOLOR);
cleardevice();
if((fp=fopen("record","r"))==NULL)
{
printf("\nerror on open file!");
getch();
exit(1);
}
gotoxy(1,1);
printf("\n\t\t\tRecord Information\n");
printf("Record-holder Achievement(s)\t Time(h:m:s)\t\tDate(y/m/d)");
for(i=0;i<MAX;i++)
{fseek(fp,i*sizeof(Record),0);
fread(&r[i],sizeof(Record),1,fp);
printf("\n%-10s\t%d\t\t %02d:%02d:%02d\t\t%02d/%02d/%02d",r[i].name,r[i].second,r[i].t.ti_hour,r[i].t.ti_min,r[i].t.ti_sec,r[i].d.da_year,r[i].d.da_mon,r[i].d.da_day);}
fclose(fp);
gotoxy(10,25);
printf("Press any key to return mainmenu...");
}

void DeleteRecords()
{
int i;
FILE *fp;
fp=fopen("record","w");
for(i=0;i<MAX;i++)
{
strcpy(r[i].name,"nameless");
r[i].second=0;
gettime(&r[i].t);
getdate(&r[i].d);
}
for(i=0;i<MAX;i++)
fwrite(&r[i],sizeof(Record),1,fp);
fclose(fp);
}

void JudgeRecord()
{
int i=MissionNum-1;
time(&t2);
if(r[i].second==0||difftime(t2,t1)<r[i].second)
{
gotoxy(10,3);printf("\t\tYou have broken the record");
r[i].second=difftime(t2,t1);
strcpy(r[i].name,name);
gettime(&r[i].t);
getdate(&r[i].d);
WriteRecord();
}
else
{gotoxy(10,3);printf("\t\tYou have pass this mission");}
gotoxy(10,4);
printf("\t\tpress any key continue...");
getch();
getch();
}

void WriteRecord()
{
FILE *fp;
int i=MissionNum-1;
fp=fopen("record","rt+");
fseek(fp,i*sizeof(Record),0);
fwrite(&r[MissionNum-1],sizeof(Record),1,fp);
fclose(fp);
}

void RegisterStep()
{
int i;
StepNum++;
i=(StepNum-1)%STEPMAX;
Step[i]=DirectionKey;
}

void ReverselyMove()
{
int i;
Add ad;
i=(StepNum-1)%STEPMAX;
if(Step[i]==0) return;
else
{
switch(Step[i])
{
case Key_Up:{ad.x=1;ad.y=0;MoveBack(ad);break;}
case Key_Down:{ad.x=-1;ad.y=0;MoveBack(ad);break;}
case Key_Left:{ad.x=0;ad.y=1;MoveBack(ad);break;}
case Key_Right:{ad.x=0;ad.y=-1;MoveBack(ad);break;}
}
StepNum--;Step[i]=0;BoxMove[i]=0;
}
}

void MoveBack(Add a) /*一定可以移动*/
{
int i=(StepNum-1)%STEPMAX;
if(BoxMove[i]==0)
{
map[p.x][p.y]-=1;InitPic(map[p.x][p.y],p.x,p.y);
p.x=p.x+a.x;p.y=p.y+a.y;
map[p.x][p.y]+=1;InitPic(map[p.x][p.y],p.x,p.y);
}
else if(BoxMove[i]==1)
{
map[p.x-a.x][p.y-a.y]-=2;InitPic(map[p.x-a.x][p.y-a.y],p.x-a.x,p.y-a.y);
map[p.x][p.y]+=1;InitPic(map[p.x][p.y],p.x,p.y);
p.x=p.x+a.x;p.y=p.y+a.y;
map[p.x][p.y]+=1;InitPic(map[p.x][p.y],p.x,p.y);
}
}

⑦ 使用c语言制作游戏,如:贪吃蛇、黑白棋、推箱子等

# include<stdio.h>
# include<string.h>
# include<stdlib.h>
# define SPA 0
# define MAN 1
# define COM 2 /* 空位置设为0 ,玩家下的位置设为1 ,做渣电脑下的位置设为2 */
int qipan[15][15]; /* 15*15的棋盘 */
int a,b,c,d,x; /* a b为玩家下子坐标姿胡搏 ,c d为电脑下子坐标 x为剩余空位置*/
void start(); /* 程序的主要控制函数 */
void draw(); /* 画棋盘 */
int win(int p,int q); /* 判断胜利 p q为判断点坐标 */
void AI(int *p,int *q); /* 电脑下子 p q返回下子坐标 */
int value(int p,int q); /* 计算空点p q的价值 */
int qixing(int n,int p,int q); /* 返回空点p q在n方向上的棋型 n为1-8方向 从右顺时针开始数 */
void yiwei(int n,int *i,int *j); /* 在n方向上对坐标 i j 移位 n为1-8方向 从右顺时针开始数 */
void main()
{
char k;
do{
x=225;
start();
printf("还要再来一把吗?输入y或n:"); getchar(); scanf("%c",&k);
while(k!='y'&&k!='n'){
printf("输入错误,请重新输入\n"); scanf("%c",&k); }
system("cls"); }while(k=='y'); printf("谢谢使用!\n");
}
void start()
{
int i,j,a1,b1,c1,d1,choice; /* a1 b1储存玩家上手坐标 c1 d1储存电脑上手坐标 */
char ch;
printf("\t╔══════════════════════════════╗\n"); printf("\t║ ║\n"); printf("\t║ 欢迎使用五子棋对战程序 祝您玩的愉快挑战无极限 ║\n"); printf("\t║ ║\n"); printf("\t║ ._______________________. ║\n"); printf("\t║ | _____________________ | ║\n"); printf("\t║ | I I | ║\n"); printf("\t║ | I 五 子 棋 I | ║\n"); printf("\t║ | I I | ║\n"); printf("\t║ | I made by 晓之蓬 I | ║\n"); printf("\t║ | I___________________I | ║\n"); printf("\t║ !_______________________! ║\n"); printf("\t║ ._[__________]_. ║\n"); printf("\t║ .___|_______________|___. ║\n"); printf("\t║ |::: ____ | ║\n"); printf("\t║ | ~~~~ [CD-ROM] | ║\n"); printf("\t║ !_____________________! ║\n"); printf("\t║ ║\n"); printf("\t║ ║\n"); printf("\t║ 寒 星 溪 月 疏 星 首,花 残 二 月 并 白 莲。 ║\n"); printf("\t║ 雨 月 金 星 追 黑 玉,松 丘 新 宵 瑞 山 腥。 ║\n"); printf("\t║ 星 月 长 峡 恒 水 流,白 莲 垂 俏 云 浦 岚。 ║\n"); printf("\t║ 黑 玉 银 月 倚 明 星,斜 月 明 月 堪 称 朋。 ║\n"); printf("\t║ 二 十 六 局 先 弃 二,直 指 游 星 斜 彗 星。 ║\n"); printf("\t║ ║\n"); printf("\t║ ║\n"); printf("\t║ 1.人机对战 2.人人迹祥对战 ║\n"); printf("\t║ ║\n"); printf("\t╚═══════════════════════════ ══╝\n"); printf("\t\t\t请输入1或2:");
scanf("%d",&choice); /* 选择模式:人机或人人 */
while(choice!=1&&choice!=2) {
printf("输入错误,请重新输入:"); scanf("%d",&choice); }
if(choice==1){ /* 人机模式 */
system("cls");
printf("欢迎使用五子棋人机对战!下子请输入坐标(如13 6)。悔棋请输入15 1 5。\n\n\n");
for(j=0;j<15;j++)
for(i=0;i<15;i++)
qipan[j][i]=SPA; /* 置棋盘全为空 */
draw();
printf("先下请按1,后下请按2:"); scanf("%d",&i);
while(i!=1&&i!=2) { printf("输入错误,请重新输入:"); scanf("%d",&i); }
if(i==1) { /* 如果玩家先手下子 */
printf("请下子:"); scanf("%d%d",&a,&b);
while((a<0||a>14)||(b<0||b>14)) {
printf("坐标错误!请重新输入:"); scanf("%d%d",&a,&b); }
a1=a; b1=b; x--; qipan[b][a]=MAN; system("cls"); draw();
}
while(x!=0){
if(x==225) {
c=7; d=7; qipan[d][c]=COM; x--; system("cls"); draw(); } /* 电脑先下就下在7 7 */
else { AI(&c,&d); qipan[d][c]=COM; x--; system("cls"); draw(); } /* 电脑下子 */
c1=c; d1=d; /* 储存电脑上手棋型 */
if(win(c,d)){ /* 电脑赢 */
printf("要悔棋吗?请输入y或n:"); getchar(); scanf("%c",&ch);
while(ch!='y'&&ch!='n') { printf("输入错误,请重新输入:");
scanf("%c",&ch); }
if(ch=='n') {
printf("下不过电脑很正常,请不要灰心!!!\n"); return; }
else { x+=2; qipan[d][c]=SPA; qipan[b1][a1]=SPA;
system("cls"); draw(); } /* 悔棋 */
}
printf("电脑下在%d %d\n请输入:",c,d);
scanf("%d%d",&a,&b); /* 玩家下子 */
if(a==15&&b==15) {
x+=2; qipan[d][c]=SPA; qipan[b1][a1]=SPA; system("cls"); draw();
printf("请输入:"); scanf("%d%d",&a,&b); } /* 悔棋 */
while((a<0||a>14)||(b<0||b>14)||qipan[b][a]!=SPA) {
printf("坐标错误或该位置已有子!请重新输入:");
scanf("%d%d",&a,&b); }
a1=a; b1=b; x--; qipan[b][a]=MAN; system("cls"); draw();
if(win(a,b)){ printf("电脑神马的都是浮云!!!\n");
return; } /* 玩家赢 */
}
printf("和局\n");
}
if(choice==2){
system("cls");
printf("欢迎使用五子棋人人对战!下子请输入坐标(如13 6)。悔棋请输入15 15。 \n\n\n");
for(j=0;j<15;j++)
for(i=0;i<15;i++)
qipan[j][i]=SPA; /* 置棋盘全为空 */
draw();
while(x!=0){
printf("1P请输入:"); scanf("%d%d",&a,&b);
if(a==15&&b==15) {
x+=2; qipan[d][c]=SPA; qipan[b1][a1]=SPA; system("cls");
draw(); printf("1P请输入:"); scanf("%d%d",&a,&b); }
while((a<0||a>14)||(b<0||b>14)||qipan[b][a]!=SPA) {
printf("坐标错误或该位置已有子!请重新输入:");
scanf("%d%d",&a,&b); }
a1=a; b1=b; x--; qipan[b][a]=MAN; system("cls"); draw();
printf("1P下在%d %d。\n",a,b);
if(win(a,b)){ printf("你真棒!!!\n"); return; } /* 玩家1赢 */
printf("2P请输入:"); scanf("%d%d",&c,&d);
if(c==15&&d==15) {
x+=2; qipan[b][a]=SPA; qipan[d1][c1]=SPA; system("cls"); draw();
printf("2P请输入:"); scanf("%d%d",&c,&d); }
while((c<0||c>14)||(d<0||d>14)||qipan[d][c]!=SPA) {
printf("坐标错误或该位置已有子!请重新输入:"); scanf("%d%d",&c,&d);
}
c1=c; d1=d; x--; qipan[d][c]=COM; system("cls"); draw();
printf("2P下在%d %d。\n",c,d);
if(win(c,d)){ printf("你真棒!!!\n"); return; } /* 玩家2赢 */
}
printf("和局\n");
}
}
void draw() /* 画棋盘 */
{
int i,j;
char p[15][15][4];
for(j=0;j<15;j++)
for(i=0;i<15;i++){
if(qipan[j][i]==SPA) strcpy(p[j][i]," \0");
if(qipan[j][i]==MAN) strcpy(p[j][i],"●\0");
if(qipan[j][i]==COM) strcpy(p[j][i],"◎\0"); }
printf(" 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 \n");
printf(" ┌—┬—┬—┬—┬—┬—┬—┬—┬—┬—┬—┬—┬—┬—┬—┐\n");
for(i=0,j=0;i<14;i++,j++){
printf(" %2d│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%d\n",j,p[i][0],p[i][1],p[i][2],p[i][3],p[i][4],p[i][5],p[i][6],p[i][7],p[i][8],p[i][9],p[i][10],p[i][11],p[i][12],p[i][13],p[i][14],j);
printf(" ├—┼—┼—┼—┼—┼—┼—┼—┼—┼—┼—┼—┼—┼—┼—┤\n"); }
printf(" 14│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│%s│0\n",p[14][0],p[14][1],p[14][2],p[14][3],p[14][4],p[14][5],p[14][6],p[14][7],p[14][8],p[14][9],p[14][10],p[14][11],p[14][12],p[14][13],p[14][14]);
printf(" └—┴—┴—┴—┴—┴—┴—┴—┴—┴—┴—┴—┴—┴—┴—┘\n");
printf(" 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 \n");
}
int win(int p,int q) /* 判断胜利 p q为判断点坐标,胜利返回1,否则返回0 */
{
int k,n=1,m,P,Q; /* k储存判断点p q的状态COM或MAN。P Q储存判断点坐标。n为判断方向。m为个数。 */
P=p; Q=q; k=qipan[q][p];
while(n!=5){
m=0;
while(k==qipan[q][p]){
m++; if(m==5) return 1;
yiwei(n,&p,&q); if(p<0||p>14||q<0||q>14) break;
}
n+=4; m-=1; p=P; q=Q; /* 转向判断 */
while(k==qipan[q][p]){
m++;
if(m==5) return 1;
yiwei(n,&p,&q); if(p<0||p>14||q<0||q>14) break;
}
n-=3; p=P; q=Q; /* 不成功则判断下一组方向 */
}
return 0;
}
void AI(int *p,int *q) /* 电脑下子 *p *q返回下子坐标 */
{
int i,j,k,max=0,I,J; /* I J为下点坐标 */
for(j=0;j<15;j++)
for(i=0;i<15;i++)
if(qipan[j][i]==SPA){ /* 历遍棋盘,遇到空点则计算价值,取最大价值点下子。 */
k=value(i,j); if(k>=max) { I=i; J=j; max=k; }
}
*p=I; *q=J;
}
int value(int p,int q) /* 计算空点p q的价值 以k返回 */
{
int n=1,k=0,k1,k2,K1,K2,X1,Y1,Z1,X2,Y2,Z2,temp;
int a[2][4][4]={40,400,3000,10000,6,10,600,10000,20,120,200,0,6,10,500,0,30,300,2500,5000,2,8,300,8000,26,160,0,0,4,20,300,0}; /* 数组a中储存己方和对方共32种棋型的值 己方0对方1 活0冲1空活2空冲3 子数0-3(0表示1个子,3表示4个子) */
while(n!=5){
k1=qixing(n,p,q); n+=4; /* k1,k2为2个反方向的棋型编号 */
k2=qixing(n,p,q); n-=3;
if(k1>k2) { temp=k1; k1=k2; k2=temp; } /* 使编号小的为k1,大的为k2 */
K1=k1; K2=k2; /* K1 K2储存k1 k2的编号 */
Z1=k1%10; Z2=k2%10; k1/=10; k2/=10; Y1=k1%10; Y2=k2%10; k1/=10; k2/=10;
X1=k1%10; X2=k2%10; /* X Y Z分别表示 己方0对方1 活0冲1空活2空冲3 子数0-3(0表示1个子,3表示4个子) */
if(K1==-1) {
if(K2<0) { k+=0; continue; } else k+=a[X2][Y2][Z2]+5; continue; }; /* 空棋型and其他 */
if(K1==-2) { if(K2<0) { k+=0; continue; }
else k+=a[X2][Y2][Z2]/2; continue; }; /* 边界冲棋型and其他 */
if(K1==-3) { if(K2<0) { k+=0; continue; }
else k+=a[X2][Y2][Z2]/3; continue; }; /* 边界空冲棋型and其他 */
if(((K1>-1&&K1<4)&&((K2>-1&&K2<4)||(K2>9&&K2<14)))||((K1>99&&K1<104)&&((K2>99&&K2<104)||(K2>109&&K2<114)))){
/* 己活己活 己活己冲 对活对活 对活对冲 的棋型赋值*/
if(Z1+Z2>=2) { k+=a[X2][Y2][3]; continue; }
else { k+=a[X2][Y2][Z1+Z2+1]; continue; }
}
if(((K1>9&&K1<14)&&(K2>9&&K2<14))||((K1>109&&K1<114)&&(K2>109&&K2<114))){
/* 己冲己冲 对冲对冲 的棋型赋值*/
if(Z1+Z2>=2) { k+=10000; continue; }
else { k+=0; continue; }
}
if(((K1>-1&&K1<4)&&((K2>99&&K2<104)||(K2>109&&K2<114)))||((K1>9&&K1<14)&&((K2>99&&K2<104)||(K2>109&&K2<114)))){
/* 己活对活 己活对冲 己冲对活 己冲对冲 的棋型赋值*/
if(Z1==3||Z2==3) { k+=10000; continue; }
else { k+=a[X2][Y2][Z2]+a[X1][Y1][Z1]/4; continue; }
}
else
{ k+=a[X1][Y1][Z1]+a[X2][Y2][Z2]; continue; } /* 其他棋型的赋值 */
}
return k;
}
int qixing(int n,int p,int q) /* 返回空点p q在n方向上的棋型号 n为1-8方向 从右顺时针开始数 */
{
int k,m=0; /* 棋型号注解: 己活000-003 己冲010-013 对活100-103 对冲110-113 己空活020-023 己空冲030-033 对空活120-123 对空冲130-133 空-1 边界冲-2 边界空冲-3*/
yiwei(n,&p,&q);
if(p<0||p>14||q<0||q>14) k=-2; /* 边界冲棋型 */
switch(qipan[q][p]){
case COM:{
m++; yiwei(n,&p,&q);
if(p<0||p>14||q<0||q>14) { k=m+9; return k; }
while(qipan[q][p]==COM) {
m++; yiwei(n,&p,&q); if(p<0||p>14||q<0||q>14) { k=m+9; return k; }
}
if(qipan[q][p]==SPA) k=m-1; /* 己方活棋型 */
else k=m+9; /* 己方冲棋型 */
}break;
case MAN:{
m++; yiwei(n,&p,&q);
if(p<0||p>14||q<0||q>14) { k=m+109; return k; }
while(qipan[q][p]==MAN) {
m++; yiwei(n,&p,&q); if(p<0||p>14||q<0||q>14) { k=m+109; return k; }
}
if(qipan[q][p]==SPA) k=m+99; /* 对方活棋型 */
else k=m+109; /* 对方冲棋型 */
}break;
case SPA:{
yiwei(n,&p,&q);
if(p<0||p>14||q<0||q>14) { k=-3; return k; } /* 边界空冲棋型 */
switch(qipan[q][p]){
case COM:{
m++; yiwei(n,&p,&q);
if(p<0||p>14||q<0||q>14) { k=m+29; return k; }
while(qipan[q][p]==COM) {
m++; yiwei(n,&p,&q);
if(p<0||p>14||q<0||q>14) { k=m+29; return k; }
}
if(qipan[q][p]==SPA) k=m+19; /* 己方空活棋型 */
else k=m+29; /* 己方空冲棋型 */
}break;
case MAN:{
m++; yiwei(n,&p,&q);
if(p<0||p>14||q<0||q>14) { k=m+129; return k; }
while(qipan[q][p]==MAN) {
m++; yiwei(n,&p,&q);
if(p<0||p>14||q<0||q>14) { k=m+129; return k; }
}
if(qipan[q][p]==SPA) k=m+119; /* 对方空活棋型 */
else k=m+129; /* 对方空冲棋型 */
}break;
case SPA: k=-1; break; /* 空棋型 */
}
}break;
}
return k;
}
void yiwei(int n,int *i,int *j) /* 在n方向上对坐标 i j 移位 n为1-8方向 从右顺时针开始数 */
{
switch(n){
case 1: *i+=1; break;
case 2: *i+=1; *j+=1; break;
case 3: *j+=1; break;
case 4: *i-=1; *j+=1; break;
case 5: *i-=1; break;
case 6: *i-=1; *j-=1; break;
case 7: *j-=1; break;
case 8: *i+=1; *j-=1; break;
}
}

⑧ 如何用C语言编写一如图模型的推箱子游戏的程序!

/*TC环境下编的*/
#include <dos.h>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <bios.h>
#include <alloc.h>

typedef struct winer
{
int x,y;
struct winer *p;
}winer;

char status [20][20];
char far *printScreen=(char far* )0xB8000000;

void putoutChar(int y,int x,char ch,char fc,char bc);
void printWall(int x, int y);
void printBox(int x, int y);
void printBoxDes(int x, int y);
void printDestination(int x, int y);
void printDestination1(int x,int y,winer **win,winer **pw);
void printMan(int x, int y);
void init();
winer *initStep1();
winer *initStep2();
winer *initStep3();
winer *initStep4();
void moveBoxSpacetoSpace(int x ,int y, char a);
void moveBoxDestoSpace(int x ,int y, char a) ;
void moveBoxSpacetoDes(int x, int y, char a);
void moveBoxDestoDes(int x, int y, char a);
int judge(int x, int y);
void move(int x, int y, char a);
void reset(int i);

void putoutChar(int y,int x,char ch,char fc,char bc)
{
printScreen[(x*160)+(y<<1)+0]=ch;
printScreen[(x*160)+(y<<1)+1]=(bc*16)+fc;
}
void printWall(int x,int y)
{
putoutChar(y-1,x-1,219,GREEN,BLACK);
status[x][y]='w';
}

void printBox(int x,int y)
{
putoutChar(y-1,x-1,10,WHITE,BLACK);
status[x][y]='b';
}

void printDestination1(int x,int y,winer **win,winer **pw)
{
winer *qw;
putoutChar(y-1,x-1,003,YELLOW,BLACK);
status[x][y]='m';
if(*win==NULL)
{
*win=*pw=qw=(winer* )malloc(sizeof(winer));
(*pw)->x=x;
(*pw)->y=y;
(*pw)->p=NULL;
}
else
{
qw=(winer* )malloc(sizeof(winer));
qw->x=x;
qw->y=y;
(*pw)->p=qw;
(*pw)=qw;qw->p=NULL;
}
}

void printDestination(int x,int y)
{
putoutChar(y-1,x-1,003,YELLOW,BLACK);
status[x][y]='m';
}

void printMan(int x,int y)
{
gotoxy(y,x);
_AL=02;
_CX=01;
_AH=0xa;
geninterrupt(0x10);
}

void printBoxDes(int x,int y)
{
putoutChar(y-1,x-1,10,YELLOW,BLACK);
status[x][y]='i';
}

void init()
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<20;j++)
status[i][j]=0;
_AL=3;
_AH=0;
geninterrupt(0x10);
gotoxy(40,4);
printf("Welcome to the box world!");
gotoxy(40,6);
printf("You can use up, down, left,");
gotoxy(40,8);
printf("right key to control it, or");
gotoxy(40,10);
printf("you can press Esc to quit it.");
gotoxy(40,12);
printf("Press space to reset the game.");
gotoxy(40,14);
printf("Wish you have a good time !");
gotoxy(40,16);
printf("April , 2007");

}
winer *initStep1()
{
int x;
int y;
winer *win=NULL;
winer *pw;
for(x=1,y=5;y<=9;y++)
printWall(x+4,y+10);
for(y=5,x=2;x<=5;x++)
printWall(x+4,y+10);
for(y=9,x=2;x<=5;x++)
printWall(x+4,y+10);
for(y=1,x=3;x<=8;x++)
printWall(x+4,y+10);
for(x=3,y=3;x<=5;x++)
printWall(x+4,y+10);
for(x=5,y=8;x<=9;x++)
printWall(x+4,y+10);
for(x=7,y=4;x<=9;x++)
printWall(x+4,y+10);
for(x=9,y=5;y<=7;y++)
printWall(x+4,y+10);
for(x=8,y=2;y<=3;y++)
printWall(x+4,y+10);
printWall(5+4,4+10);
printWall(5+4,7+10);
printWall(3+4,2+10);
printBox(3+4,6+10);
printBox(3+4,7+10);
printBox(4+4,7+10);
printDestination1(4+4,2+10,&win,&pw);
printDestination1(5+4,2+10,&win,&pw);
printDestination1(6+4,2+10,&win,&pw);
printMan(2+4,8+10);
return win;
}

winer *initStep2()
{
int x;
int y;
winer *win=NULL;
winer *pw;
for(x=1,y=4;y<=7;y++)
printWall(x+4,y+10);
for(x=2,y=2;y<=4;y++)
printWall(x+4,y+10);
for(x=2,y=7;x<=4;x++)
printWall(x+4,y+10);
for(x=4,y=1;x<=8;x++)
printWall(x+4,y+10);
for(x=8,y=2;y<=8;y++)
printWall(x+4,y+10);
for(x=4,y=8;x<=8;x++)
printWall(x+4,y+10);
for(x=4,y=6;x<=5;x++)
printWall(x+4,y+10);
for(x=3,y=2;x<=4;x++)
printWall(x+4,y+10);
for(x=4,y=4;x<=5;x++)
printWall(x+4,y+10);
printWall(6+4,3+10);
printBox(3+4,5+10);
printBox(6+4,6+10);
printBox(7+4,3+10);
printDestination1(5+4,7+10,&win,&pw);
printDestination1(6+4,7+10,&win,&pw);
printDestination1(7+4,7+10,&win,&pw);
printMan(2+4,6+10);
return win;
}
winer *initStep3()
{
int x;
int y;
winer *win=NULL;
winer *pw;
for(x=1,y=2;y<=8;y++)
printWall(x+4,y+10);
for(x=2,y=2;x<=4;x++)
printWall(x+4,y+10);
for(x=4,y=1;y<=3;y++)
printWall(x+4,y+10);
for(x=5,y=1;x<=8;x++)
printWall(x+4,y+10);
for(x=8,y=2;y<=5;y++)
printWall(x+4,y+10);
for(x=5,y=5;x<=7;x++)
printWall(x+4,y+10);
for(x=7,y=6;y<=9;y++)
printWall(x+4,y+10);
for(x=3,y=9;x<=6;x++)
printWall(x+4,y+10);
for(x=3,y=6;y<=8;y++)
printWall(x+4,y+10);
printWall(2+4,8+10);
printWall(5+4,7+10);
printBox(6+4,3+10);
printBox(4+4,4+10);
printBox(5+4,6+10);
printDestination1(2+4,5+10,&win,&pw);
printDestination1(2+4,6+10,&win,&pw);
printDestination1(2+4,7+10,&win,&pw);
printMan(2+4,4+10);
return win;
}

winer *initStep4()
{
int x;
int y;
winer *win=NULL;
winer *pw;
for(x=1,y=1;y<=6;y++)
printWall(x+4,y+10);
for(x=2,y=7;y<=8;y++)
printWall(x+4,y+10);
for(x=2,y=1;x<=7;x++)
printWall(x+4,y+10);
for(x=7,y=2;y<=4;y++)
printWall(x+4,y+10);
for(x=6,y=4;y<=9;y++)
printWall(x+4,y+10);
for(x=3,y=9;x<=5;x++)
printWall(x+4,y+10);
for(x=3,y=3;y<=4;y++)
printWall(x+4,y+10);
printWall(3+4,8+10);
printBox(3+4,5+10);
printBox(4+4,4+10);
printBox(4+4,6+10);
printBox(5+4,5+10);
printBox(5+4,3+10);
printDestination1(3+4,7+10,&win,&pw);
printDestination1(4+4,7+10,&win,&pw);
printDestination1(5+4,7+10,&win,&pw);
printDestination1(4+4,8+10,&win,&pw);
printDestination1(5+4,8+10,&win,&pw);
printMan(2+4,2+10);
return win;
}

void moveBoxSpacetoSpace(int x,int y,char a)
{
switch(a)
{
case 'u':
status[x-1][y]=0;
printf(" ");
printBox(x-2,y);
printMan(x-1,y);
status[x-2][y]='b';
break;
case 'd':
status[x+1][y]=0;
printf(" ");
printBox(x+2,y);
printMan(x+1,y);
status[x+2][y]='b';
break;
case 'l':
status[x][y-1]=0;
printf(" ");
printBox(x,y-2);
printMan(x,y-1);
status[x][y-2]='b';
break;
case 'r':
status[x][y+1]=0;
printf(" ");
printBox(x,y+2);
printMan(x,y+1);
status[x][y+2]='b';
break;
default:
break;
}
}

void moveBoxDestoSpace(int x,int y,char a)
{
switch(a)
{
case 'u':
status[x-1][y]='m';
printf(" ");
printBox(x-2,y);
printMan(x-1,y);
status[x-2][y]='b';
break;
case 'd':
status[x+1][y]='m';
printf(" ");
printBox(x+2,y);
printMan(x+1,y);
status[x+2][y]='b';
break;
case 'l':
status[x][y-1]='m';
printf(" ");
printBox(x,y-2);
printMan(x,y-1);
status[x][y-2]='b';
break;
case 'r':
status[x][y+1]='m';
printf(" ");
printBox(x,y+2);
printMan(x,y+1);
status[x][y+2]='b';
break;
default:
break;
}
}

void moveBoxSpacetoDes(int x,int y,char a)
{
switch(a)
{
case 'u':
status[x-1][y]=0;
printf(" ");
printBoxDes(x-2,y);
printMan(x-1,y);
status[x-2][y]='i';
break;
case 'd':
status[x+1][y]=0;
printf(" ");
printBoxDes(x+2,y);
printMan(x+1,y);
status[x+2][y]='i';
break;
case 'l':
status[x][y-1]=0;
printf(" ");
printBoxDes(x,y-2);
printMan(x,y-1);
status[x][y-2]='i';
break;
case 'r':
status[x][y+1]=0;
printf(" ");
printBoxDes(x,y+2);
printMan(x,y+1);
status[x][y+2]='i';
break;
default:
break;
}
}

void moveBoxDestoDes(int x,int y,char a)
{
switch(a)
{
case 'u':
status[x-1][y]='m';
printf(" ");
printBoxDes(x-2,y);
printMan(x-1,y);
status[x-2][y]='i';
break;
case 'd':
status[x+1][y]='m';
printf(" ");
printBoxDes(x+2,y);
printMan(x+1,y);
status[x+2][y]='i';
break;
case 'l':
status[x][y-1]='m';
printf(" ");
printBoxDes(x,y-2);
printMan(x,y-1);
status[x][y-2]='i';
break;
case 'r':
status[x][y+1]='m';
printf(" ");
printBoxDes(x,y+2);
printMan(x,y+1);
status[x][y+2]='i';
break;
default:
break;
}
}

int judge(int x,int y)
{
int i;
switch(status[x][y])
{
case 0:
i=1;
break;
case 'w':
i=0;
break;
case 'b':
i=2;
break;
case 'i':
i=4;
break;
case 'm':
i=3;
break;
default:
break;
}
return i;
}

void move(int x,int y,char a)
{
switch(a)
{
case 'u':
if(!judge(x-1,y))
{
gotoxy(y,x);
break;
}
else if(judge(x-1,y)==1||judge(x-1,y)==3)
{
if(judge(x,y)==3)
{
printDestination(x,y);
printMan(x-1,y);
break;
}
else
{
printf(" ");
printMan(x-1,y);
break;
}
}
else if(judge(x-1,y)==2)
{
if(judge(x-2,y)==1)
{
moveBoxSpacetoSpace(x,y,'u');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x-1);
}
else if(judge(x-2,y)==3)
{
moveBoxSpacetoDes(x,y,'u');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x-1);
}
else
gotoxy(y,x);
break;
}
else if(judge(x-1,y)==4)
{
if(judge(x-2,y)==1)
{
moveBoxDestoSpace(x,y,'u');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x-1);
}
else if(judge(x-2,y)==3)
{
moveBoxDestoDes(x,y,'u');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x-1);
}
else
gotoxy(y,x);
break;
}
case 'd':
if(!judge(x+1,y))
{
gotoxy(y,x);
break;
}
else if(judge(x+1,y)==1||judge(x+1,y)==3)
{
if(judge(x,y)==3)
{
printDestination(x,y);
printMan(x+1,y);
break;
}
else
{
printf(" ");
printMan(x+1,y);
break;
}
}
else if(judge(x+1,y)==2)
{
if(judge(x+2,y)==1)
{
moveBoxSpacetoSpace(x,y,'d');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x+1);
}
else if(judge(x+2,y)==3)
{
moveBoxSpacetoDes(x,y,'d');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x+1);
}
else
gotoxy(y,x);
break;
}
else if(judge(x+1,y)==4)
{
if(judge(x+2,y)==1)
{
moveBoxDestoSpace(x,y,'d');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x+1);
}
else if(judge(x+2,y)==3)
{
moveBoxDestoDes(x,y,'d');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x+1);
}
else
gotoxy(y,x);
break;
}
case 'l':
if(!judge(x,y-1))
{
gotoxy(y,x);
break;
}
else if(judge(x,y-1)==1||judge(x,y-1)==3)
{
if(judge(x,y)==3)
{
printDestination(x,y);
printMan(x,y-1);
break;
}
else
{
printf(" ");
printMan(x,y-1);
break;
}
}
else if(judge(x,y-1)==2)
{
if(judge(x,y-2)==1)
{
moveBoxSpacetoSpace(x,y,'l');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y-1,x);
}
else if(judge(x,y-2)==3)
{
moveBoxSpacetoDes(x,y,'l');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y-1,x);
}
else
gotoxy(y,x);
break;
}
else if(judge(x,y-1)==4)
{
if(judge(x,y-2)==1)
{
moveBoxDestoSpace(x,y,'l');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y-1,x);
}
else if(judge(x,y-2)==3)
{
moveBoxDestoDes(x,y,'l');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y-1,x);
}
else
gotoxy(y,x);
break;
}
case 'r':
if(!judge(x,y+1))
{
gotoxy(y,x);
break;
}
else if(judge(x,y+1)==1||judge(x,y+1)==3)
{
if(judge(x,y)==3)
{
printDestination(x,y);
printMan(x,y+1);
break;
}
else
{
printf(" ");
printMan(x,y+1);
break;
}
}
else if(judge(x,y+1)==2)
{
if(judge(x,y+2)==1)
{
moveBoxSpacetoSpace(x,y,'r');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y+1,x);
}
else if(judge(x,y+2)==3)
{
moveBoxSpacetoDes(x,y,'r');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y+1,x);
}
else
gotoxy(y,x);
break;
}
else if(judge(x,y+1)==4)
{
if(judge(x,y+2)==1)
{
moveBoxDestoSpace(x,y,'r');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y+1,x);
}
else if(judge(x,y+2)==3)
{
moveBoxDestoDes(x,y,'r');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y+1,x);
}
else
gotoxy(y,x);
break;
}
default:
break;
}
}

void reset(int i)
{
switch(i)
{
case 0:
init();
initStep1();
break;
case 1:
init();
initStep2();
break;
case 2:
init();
initStep3();
break;
case 3:
init();
initStep4();
break;
default:
break;
}
}

void main()
{
int key;
int x;
int y;
int s;
int i=0;
winer *win;
winer *pw;
_AL=3;
_AH=0;
geninterrupt(0x10);
init();
win=initStep1();
do{
_AH=3;
geninterrupt(0x10);
x=_DH+1;
y=_DL+1;
while(bioskey(1)==0);
key=bioskey(0);
switch(key)
{
case 0x4800:
move(x,y,'u');
break;
case 0x5000:
move(x,y,'d');
break;
case 0x4b00:
move(x,y,'l');
break;
case 0x4d00:
move(x,y,'r');
break;
case 0x3920:
reset(i);
break;
default:
break;
}
s=0;
pw=win;
while(pw)
{
if(status[pw->x][pw->y]=='m')
s++;
pw=pw->p;
}
if(s==0)
{
free(win);
gotoxy(25,2);
printf("congratulate! You have done this step!");
getch();
i++;
switch(i)
{
case 1:
init();
win=initStep2();
break;
case 2:
init();
win=initStep3();
break;
case 3:
init();
win=initStep4();
break;
case 4:
gotoxy(15,21);
printf("Congratulation! \n");
gotoxy(15,23);
printf("You have done all the steps, Welcome to play again!");
key=0x011b;
getch();
break;
default:
break;
}
}

}while(key!=0x011b);
_AL=3;
_AH=0;
geninterrupt(0x10);
}

⑨ 求助啊,谁有有趣的c语言小程序,并且要有源代码!!

一个贪吃蛇C源代码,本人稍加优化,练手应当不错。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <Windows.h>

#define WIDTH 78 //地图的宽,x轴
#define HEIGHT 26 //地图的高,y轴
int dire=3; //方向变量,初值为向“左”
int Flag=0; //判断是否吃了食物的标志
int score=0; //玩家得分

struct foods{ int x;
int y;
}food; //结构体food有2个成员
struct snakes{int len;
int speed;
int x[100];
int y[100];
}snake; //结构体snake有4个成员

void gotoxy( int x,int y) //获得句柄,才能控制光标移动
{ COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void gotoxy( int x,int y); //以下声明要用到的几个自编函数
void csh( );
void keyDown( );
void Move( );
void putFood( );
int pdOver( );

int main( ) //主函数
{ csh( );
while(1)
{ keyDown( );
Move( );
putFood( );
if(pdOver( ))
{system(“cls”);
gotoxy(WIDTH/2+1,HEIGHT/2);
printf(“游戏结束!T__T”);
gotoxy(WIDTH/2+1,HEIGHT/2+1);
printf(“玩家总分:%d分”,score);
getch( );
break; }
Sleep(snake.speed);
}
return 0;
}

void csh( ) //初始化界面
{ int i;
gotoxy(0,0);
CONSOLE_CURSOR_INFO cursor_info={1,0}; //光标值设为0就隐藏了
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

for(i=0;i<=WIDTH;i=i+2) //横坐标要为偶数,因为这里要打印的字符占2个位置
{ gotoxy(i,0); //打印上边框
printf("■");
gotoxy(i,HEIGHT); //打印下边框
printf("■");
}
for(i=1;i<HEIGHT;i++)
{ gotoxy(0,i); //打印左边框
printf("■");
gotoxy(WIDTH,i); //打印右边框
printf("■");
}
while(1)
{ srand((unsigned int)time(NULL)); //设定种子为当前时间
food.x=rand()%(WIDTH-4)+2;
food.y=rand()%(HEIGHT-2)+1;
if(food.x%2==0)break;
}
gotoxy(food.x,food.y); //到食物坐标处打印初试食物
printf("●");

snake.len=3; //蛇身长
snake.speed=350; //刷新蛇的时间,即是移动速度
snake.x[0]=WIDTH/2+1; //蛇头横坐标为偶数
snake.y[0]=HEIGHT/2; //蛇头纵坐标
gotoxy(snake.x[0], snake.y[0]); //打印蛇头
printf("■");

for(i=1;i<snake.len;i++)
{ snake.x[i]=snake.x[i-1]+2;
snake.y[i]=snake.y[i-1];
gotoxy(snake.x[i],snake.y[i]); //打印蛇身
printf("■");
}
return;
}

void keyDown( ) //按键操作
{ int key;
if(kbhit( )) //如有按键输入才执行下面操作
{ key=getch( );
if(key==224) //值为224表示按下了方向键,下面要再次获取键值
{ key=getch( );
if(key==72&&dire!=2)dire=1; //72为向上
if(key==80&&dire!=1)dire=2; //80为向下
if(key==75&&dire!=4)dire=3; //75为向左
if(key==77&&dire!=3)dire=4; //77为向右
}
if(key==13)
{ while(1) if((key=getch( ))==13) break; } //13为回车键,这儿用来暂停
}
}

void Move( ) //蛇移动一节
{ if(Flag==0) //如没吃食物,才执行下面操作擦掉蛇尾
{ gotoxy(snake.x[snake.len-1],snake.y[snake.len-1]);
printf(" ");
}
int i;
for (i = snake.len - 1; i > 0; i--) //从蛇尾起每节存储前一节坐标值(蛇头除外)
{ snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
switch (dire) //以下判断蛇头该往哪个方向移动,并获取最新坐标值
{ case 1: snake.y[0]--; break;
case 2: snake.y[0]++; break;
case 3: snake.x[0]-=2; break;
case 4: snake.x[0]+=2; break;
}
gotoxy(snake.x[0], snake.y[0]); //打印蛇头
printf("■");
if (snake.x[0] == food.x && snake.y[0] == food.y) //如吃到食物执行以下操作
{ snake.len++; score += 50; snake.speed -= 5; Flag = 1;}
else Flag = 0;
if(snake.speed<160) snake.speed= snake.speed+5; //作弊码,不让速度无限加快
}

void putFood( ) //投放食物
{ if(Flag == 1) //如吃到食物才执行以下操作,生成另一个食物
{ while (1)
{ int i,n= 1;
srand((unsigned int)time(NULL)); //设定当前时间,接下产生食物坐标值
food.x = rand( ) % (WIDTH - 4) + 2;
food.y = rand( ) % (HEIGHT - 2) + 1;
for (i = 0; i < snake.len; i++) //随机生成的食物不能在蛇的身体上
{ if (food.x == snake.x[i] &&food.y == snake.y[i])
{ n= 0; break;}
}
if (n && food.x % 2 == 0) break; //n不为0且横坐标为偶数,食物坐标取值成功
}
gotoxy(food.x, food.y); //光标到取得的坐标处打印食物
printf("●");
}
return;
}

int pdOver( ) //判断游戏是否结束
{ int i;
gotoxy(2,HEIGHT+1); //以下打印一些其它信息
printf(“暂停键:Enter.”);
gotoxy(2,HEIGHT+2);
printf(“游戏得分:%d”,score);
if (snake.x[0] == 0 || snake.x[0] == WIDTH) return 1; //蛇头触碰左右边界
if (snake.y[0] == 0 || snake.y[0] == HEIGHT) return 1; //蛇头触碰上下边界
for (i = 1; i < snake.len; i++)
{ if (snake.x[0] == snake.x[i] && snake.y[0] == snake.y[i]) return 1; } //蛇头触碰自身
return 0;
}

⑩ c语言推箱子

//空:0墙:1箱子:3巢:4箱子与巢重合:5
[MAPCOUNT]
map_count=8
[MAP1]
w=8
h=8
nest_count=4
l1=00011100
l2=00013100
l3=11110100
l4=13202111
l5=11142031
l6=00121111
l7=00131000
l8=00111000
[MAP2]
w=9
h=9
nest_count=3
l1=111110000
l2=140010000
l3=102210111
l4=102010131
l5=111011131
l6=011000031
l7=010001001
l8=010001111
l9=011111000
[MAP3]
w=10
h=7
nest_count=4
l1=0111111100
l2=0100000111
l3=1121110001
l4=1040200201
l5=1033102011
l6=1133100010
l7=0111111110
[MAP4]
w=6
h=8
nest_count=5
l1=011110
l2=110010
l3=142010
l4=112011
l5=110201
l6=132001
l7=133531
l8=111111
//以上为地图数据文件,保存为boxdata.dat文件
//空:0墙:1箱子:3巢:4箱子与巢重合:5
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<windows.h>
#include<string.h>

typedefstruct
{
intx;
inty;
}PT;

int**s;
PTman;
PT*nest=NULL;
PTprev;

intnest_count=0;
intmap_count=0;
intgate=1;
intw,h;
charwork_dir[100]={''};
chardata_file[100]={''};

voidGetDataFromFile();
voidGetIntFromLineString(char*ch,intlen,inti);
voidDraw();
boolis_Success();

intmain()
{
printf("Loading...");
CONSOLE_CURSOR_INFOcci;
cci.bVisible=FALSE;
cci.dwSize=sizeof(cci);
HANDLEhandle=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle,&cci);

_getcwd(work_dir,100);
strcpy(data_file,work_dir);
strcat(data_file,"\boxdata.dat");
if(access(data_file,0))
{
printf("Don'tfindmapdatafile!");
getch();
exit(0);
}

while(1)
{
GetDataFromFile();
intsel=0;
Draw();
while(1)
{
fflush(stdin);
sel=getch();
if(sel==224)
{
sel=getch();
prev=man;
if(sel==77)//right
{
if(s[man.y][man.x+1]==2)
{
if(s[man.y][man.x+2]==0||s[man.y][man.x+2]==3)
{
s[man.y][man.x+2]=2;
s[man.y][man.x+1]=4;
s[man.y][man.x]=0;
}
else
{
continue;
}
}
elseif(s[man.y][man.x+1]==0||s[man.y][man.x+1]==3)
{
s[man.y][man.x+1]=4;
s[man.y][man.x]=0;
}
else
{
continue;
}
}
elseif(sel==80)//down
{
if(s[man.y+1][man.x]==2)
{
if(s[man.y+2][man.x]==0||s[man.y+2][man.x]==3)
{
s[man.y+2][man.x]=2;
s[man.y+1][man.x]=4;
s[man.y][man.x]=0;
}
else
{
continue;
}
}
elseif(s[man.y+1][man.x]==0||s[man.y+1][man.x]==3)
{
s[man.y+1][man.x]=4;
s[man.y][man.x]=0;
}
else
{
continue;
}
}
elseif(sel==72)//up
{
if(s[man.y-1][man.x]==2)
{
if(s[man.y-2][man.x]==0||s[man.y-2][man.x]==3)
{
s[man.y-2][man.x]=2;
s[man.y-1][man.x]=4;
s[man.y][man.x]=0;
}
else
{
continue;
}
}
elseif(s[man.y-1][man.x]==0||s[man.y-1][man.x]==3)
{
s[man.y-1][man.x]=4;
s[man.y][man.x]=0;
}
else
{
continue;
}
}
elseif(sel==75)//left
{
if(s[man.y][man.x-1]==2)
{
if(s[man.y][man.x-2]==0||s[man.y][man.x-2]==3)
{
s[man.y][man.x-2]=2;
s[man.y][man.x-1]=4;
s[man.y][man.x]=0;
}
else
{
continue;
}
}
elseif(s[man.y][man.x-1]==0||s[man.y][man.x-1]==3)
{
s[man.y][man.x-1]=4;
s[man.y][man.x]=0;
}
else
{
continue;
}
}
inti;
for(i=0;i<nest_count;i++)
{
if(nest[i].x==prev.x&&nest[i].y==prev.y)
{
s[prev.y][prev.x]=3;
break;
}
}
Draw();
if(is_Success()==true)
{
gate++;
if(gate>map_count)
{
printf(" mapisend!");
fflush(stdin);
getch();
exit(0);
}
break;
}
}
elseif(sel=='q'||sel=='Q')
{
exit(0);
}
elseif(sel=='r'||sel=='R')
{
break;
}
}
}
return0;
}

voidGetDataFromFile()
{
inti;
if(s!=NULL)
{
if(h!=0)
{
for(i=0;i<h;i++)
{
free(s+i);
}
free(s);
}
else
{
printf("fail");
getch();
exit(0);
}
}

if(nest!=NULL)
{
free(nest);
}
map_count=GetPrivateProfileInt("MAPCOUNT","map_count",0,data_file);

if(map_count<gate)
{
printf("gatefinish!");
getch();
exit(0);
}
charsection[20]={''};
sprintf(section,"MAP%d",gate);
nest_count=GetPrivateProfileInt(section,"nest_count",0,data_file);
nest=(PT*)malloc(sizeof(PT)*nest_count);

w=GetPrivateProfileInt(section,"w",0,data_file);
h=GetPrivateProfileInt(section,"h",0,data_file);
if(w<5||h<5||nest_count<1)
{
printf("worhorbox_nestdataerror!");
getch();
exit(0);
}
s=(int**)malloc(sizeof(int*)*h);
for(i=0;i<h;i++)
{
*(s+i)=(int*)malloc(sizeof(int)*w);
}

charkey[20]={''};
charline[50]={''};
intlen;
intj;
for(i=0;i<h;i++)
{
memset(line,'',50);
sprintf(key,"l%d",i+1);
GetPrivateProfileString(section,key,"",line,50,data_file);
len=strlen(line);
if(len>0)
{
line[len++]='';
line[len]='';
}
GetIntFromLineString(line,strlen(line),i);
}
len=0;
for(i=0;i<h;i++)
{
for(j=0;j<w;j++)
{
if(s[i][j]==3)
{
nest[len].y=i;
nest[len].x=j;
len++;
}
elseif(s[i][j]==5)
{
nest[len].y=i;
nest[len].x=j;
len++;
s[i][j]=2;
}
}
}
}

voidstrmyncpy(char*source,char*target,intbegin,intend)
{
inti=0;
while(1)
{
if(source[begin]!='')
{
target[i]=source[begin];
}
i++;
begin++;
if(begin>end)
{
target[i]='';
break;
}
}
}

voidGetIntFromLineString(char*ch,intlen,inti)
{
intj=0;
charc[5]={''};
intb=0,e=0;
while(e<len)
{
if(ch[e]=='')
{
memset(c,'',5);
strmyncpy(ch,c,b,e);
b=e+1;
e++;
s[i][j++]=atoi(c);
}
e++;
}
}

voidDraw()
{
inti,j,k;
boolflag=false;
system("cls");
printf(" ");
for(i=0;i<h;i++)
{
printf(" ");
for(j=0;j<w;j++)
{
if(s[i][j]==0)
{
printf("");
}
elseif(s[i][j]==1)
{
printf("■");
}
elseif(s[i][j]==2)
{
printf("★");
}
elseif(s[i][j]==3)
{
printf("☆");
}
elseif(s[i][j]==4)
{
printf("◎");
man.x=j;
man.y=i;
}
}
}
}

boolis_Success()
{
inti,j;
for(i=0;i<h;i++)
{
for(j=0;j<w;j++)
{
if(s[i][j]==3)
{
returnfalse;
}
}
}
for(i=0;i<nest_count;i++)
{
if(man.x==nest[i].x&&man.y==nest[i].y)
{
returnfalse;
}
}
returntrue;
}