當前位置:首頁 » 編程語言 » c語言推箱子
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言推箱子

發布時間: 2022-02-09 14:16:12

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語言推箱子

//空: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;
}

❸ C語言怎麼做推箱子的移動

#include"stdio.h"
#include<conio.h>
#include"Windows.h"

voidgotoxy(intx,inty){
system("cls");
COORDcr;
cr.X=x;
cr.Y=y;
HANDLEhandle=GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleCursorPosition(handle,cr);
printf("*");
}

intmain(){
staticintx=10,y=10;
gotoxy(x,y);
charc;
while(1){
c=getch();
switch(c){
case'w':
case'W':
y--;
gotoxy(x,y);
break;
case'a':
case'A':
x--;
gotoxy(x,y);
break;
case's':
case'S':
y++;
gotoxy(x,y);
break;
case'd':
case'D':
x++;
gotoxy(x,y);
break;
default:
break;
}
}
}

我只是給你舉個簡單的例子,你應該能明白這個程序了

❹ c語言推箱子論文

/* 一個C語言編寫的推箱子游戲源代碼 */
/* 本游戲是字元模式的,請不要在中文dos下運行。本游戲在TURBO C下調試通過 */
#include <dos.h>
#include <stdio.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,GREEN,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;
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 come 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("Procer : wangdehao.");
gotoxy(40,14);
printf("Mar. 30th 2003.");
}

/* 第一關的圖象初始化 */
winer *inithouse1()
{
int x,y;
winer *win=NULL,*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);
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;
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;
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;
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 */
void 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 did a good job!");
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(15,21);
printf("My dear Friend, How smart you are! Welcome to play again!");
key=0x011b;getch();break;
default: break;
}
}

}while(key!=0x011b);

_AL=3;
_AH=0;
geninterrupt(0x10);
}

❺ 如何用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語言,想要在一個推箱子的游戲中加上一個「選關」的功能

/*
可以設置F2為選關鍵
F2的按鍵掃描碼為:0x3c00

在判斷按鍵的程序中加入這個F2鍵的判斷就可以了
*/

#define F2 0x3c00

case F2:
關數加1;
if(關數>最大關數)
關數=1;
初始化當前關數的所有數據;
goto Begin;
/*break;*/

❼ 求C語言解推箱子的演算法

不知道是如何推箱子的,但是如果跟迷宮有關的話,就應該用到遞歸演算法了.就是在函數中運用該函數.

❽ C語言程序設計推箱子演算法

#include"stdio.h"
#include"bios.h"
#define LEFT 75
#define RIGHT 77
#define UPPER 72
#define DOWN 80
#define ESC 27
struct Boxss /*定義箱子結構體,其中包含坐標屬性*/
{
int x,y;
};
union keyboard /*定義讀取鍵盤碼的共用體類型*/
{
unsigned int iKeyInfo;
char chKeyBit[2];
};
int fnGetKey(void) /*定義讀取鍵盤碼的函數*/
{
union keyboard uniKey1; /*定義讀取鍵盤碼的共用體變數*/
while(bioskey(1)==0); /*檢測用戶是否按鍵*/
uniKey1.iKeyInfo=bioskey(0); /*讀取按鍵信息*/
return(uniKey1.chKeyBit[0]==0?uniKey1.chKeyBit[1]:uniKey1.chKeyBit[0]); /*返回ASCII碼或擴充碼*/
}
void main()
{
int iKey,x=11,y=6,tx=11,ty=6; /*x,y為人物移動後坐標,tx,ty為人物移動前坐標*/
struct Boxss Box[4]; /*定義箱子數量*/
int chMap[10][10]={ /*用二維數組定義地圖*/
{0,0,0,0,0,0,0,0,0,0}, /*0表示牆1表示路2表示目標*/
{0,1,0,0,0,0,1,1,1,0},
{0,1,0,2,0,0,1,0,1,0},
{0,1,0,1,0,0,1,0,1,0},
{0,1,1,1,0,0,1,0,1,0},
{0,1,0,0,0,0,1,0,1,0},
{0,1,1,1,1,1,1,0,1,0},
{0,1,0,1,0,0,0,0,2,0},
{0,2,0,1,1,1,1,2,0,0},
{0,0,0,0,0,0,0,0,0,0},
};
int i,j;
Box[0].x=13; /*定義箱子的坐標屬性*/
Box[1].x=11;
Box[2].x=14;
Box[3].x=18;
Box[0].y=8;
Box[1].y=7;
Box[2].y=13;
Box[3].y=7;
while(1) /*反復進行求移動的坐標運算*/
{
for(i=0;i<10;i++) /*輸出新地圖(刷新地圖)*/
{
gotoxy(10,5+i);
for(j=0;j<10;j++)
{
if(chMap[i][j]==0)
printf("#");
if(chMap[i][j]==1)
printf(" ");
if(chMap[i][j]==2)
printf("X");
}
}
j=0; /*判斷是否所有箱子都在目標坐標上*/
for(i=0;i<4;i++)
if(chMap[Box[i].y-5][Box[i].x-10]==2)
j++;
if(j==4) /*如果所有箱子都就位輸出"YOU WIN!"退出*/
{
clrscr();
printf("You Win!");
break;
}
for(i=0;i<4;i++) /*在起始(或移動後)的坐標輸出箱子*/
{
gotoxy(Box[i].x,Box[i].y);
printf("0");
}
gotoxy(x,y); /*在起始(或移動後)的坐標輸出人*/
printf("*\b");
tx=x; /*記錄本次移動前的坐標*/
ty=y;
iKey=fnGetKey();
if(iKey==LEFT&&chMap[y-5][x-1-10]!=0) /*按讀取的按鍵信息改變坐標如果改變的坐標和牆(0)重合則不改變*/
x--;
if(iKey==RIGHT&&chMap[y-5][x+1-10]!=0)
x++;
if(iKey==UPPER&&chMap[y-1-5][x-10]!=0)
y--;
if(iKey==DOWN&&chMap[y+1-5][x-10]!=0)
y++; /*輸入ESC退出並輸出"YOU LOST"*/
if(iKey==ESC)
{
clrscr();
printf("You Lost");
break;
}
for(i=0;i<4;i++) /*如果移動後的人的坐標與箱子坐標重合,則改變箱子坐標向前一格*/
if(Box[i].x==x&&Box[i].y==y)
{
Box[i].x+=(x-tx);
Box[i].y+=(y-ty);
if(chMap[Box[i].y-5][Box[i].x-10]==0) /*如果移動後的箱子坐標會出現在牆上,則使箱子坐標和人坐標都返回移動前的值*/
{
Box[i].x-=(x-tx);
Box[i].y-=(y-ty);
x=tx;
y=ty;
}
break;
}
clrscr();
}
getch();
}

❾ 怎麼用C語言編寫推箱子的簡單小游戲

http://blog.csdn.net/pastthewind/article/details/52456178,這裡面有代碼

❿ c語言推箱子 把下面的一個關卡的推箱子游戲改成多關游戲環境(wintc)

有時間,一定會看,我在做俄羅斯方塊,用的是BCC編譯器。。