推箱子源代码
推箱子游戏的代码)
#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++)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(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);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(" ");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);}elsegotoxy(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);}elsegotoxy(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);}elsegotoxy(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);}elsegotoxy(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);}elsegotoxy(y,x);break;}else if(judge(x,y-1)==4){{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);}elsegotoxy(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');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);}elsegotoxy(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);}elsegotoxy(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);}。
推箱子游戏代码
#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) {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);gotoxy(40,12);printf("Press space to reset the game."); gotoxy(40,14);printf("Wish you have a good time !");gotoxy(40,16);printf("December , 2015");}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++)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;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);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);}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);}elsegotoxy(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);}elsegotoxy(y,x);break;}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);}elsegotoxy(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);}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);}elsegotoxy(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');printDestination(x,y);gotoxy(y-1,x);}elsegotoxy(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);}elsegotoxy(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);}elsegotoxy(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);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();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);}。
java推箱子游戏源代码
}
/** * 设置内容面板 */ void initContentPane() {
zhuobu.setBackground(Color.red); zhuobu.setLayout(null);
//调用父类的属性和方法 super.setContentPane(zhuobu);
}
/** * 把某个图片以组件的方式加入窗体 * @param imgPath 图片路径
* @param x
x
* @param y * @param width
y 宽度
* @param height 高度
* @return
添加完的组件
*/
void addComponent(int tag, String imgPath, int x, int y) {
ImageIcon img = new ImageIcon(imgPath); //创建 JLabel 并把 ImageIcon 通过构造方法传参传入 //把食物放到盘子里
gameFrame.setResizable(false); gameFrame.setImgSize(48); gameFrame.addComponent(3, "workerUp.png", 400, 100); gameFrame.addComponent(1, "box.png", 160, 60); gameFrame.addComponent(2, "goal.png", 80, 520); int[][] wallLocations ={
for (int i = 0; i < walls.length; i++) { //创建没每一个围墙,他们使用的是同一个图片 walls[i] = new JLabel(wallImg);
C语言推箱子源代码推箱子代码完善
#include <stdio.h>#include <stdlib.h>#include <Windows.h>typedef struct MAP//地图{int wall[50][2];//墙int box[9][2];//箱子int des[9][2];//终点int overlap[9][2];//箱子与终点的重叠位置int all,now;//总箱子个数,到位箱子个数int x,y;//起点} MAP,*map;//全局变量//int U=1,D=2,L=3,R=4; //定义按键状态//int jump;int ofx=10,ofy=9; //定义xy偏移量int player[2];//玩家位置int nextp[2],nextb[2];//玩家下一步位置,箱子下一步位置MAP Pass[5];//关卡数组map Group,p;//关卡数组指针,当前关卡指针int level;//关卡等级int status;//玩家状态int boxc[9][2],overlapc[9][2];//箱子状态栈,重叠箱子状态栈int nowbox;//当前到位箱子个数int reset;//是否重玩//声明全部函数//void Pos(int x,int y)//设置光标位置{COORD pos;HANDLE hOutput;pos.X=x;pos.Y=y;hOutput=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hOutput,pos);system("color 3B");}void welcometogame()//开始界面{system("title 2017程序设计之推箱子");Pos(28,3);printf("欢迎来到推箱子!");Pos(50,24);printf(" 软件1603 刘刈16110572087\n");Pos(27,9);printf("用↑.↓.←.→控制人物的移动。
C语言推箱子代码
#include<stdio.h>#include<stdlib.h>int i,j,steps = 0,gk = 1;char command,t,m = 0,n = 0;int map_1[8][11],map[8][11] = {{0,0,0,0,0,0,0,0,0,0,0},{1,1,1,1,0,0,0,0,0,0,0},{1,0,0,1,1,1,1,1,0,0,0},{1,0,0,6,0,0,4,1,0,0,0},{1,3,0,2,0,0,4,1,0,0,0},{1,0,0,1,1,1,1,1,0,0,0},{1,1,1,1,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0}};int map_2[8][11] = {{0,0,0,1,1,1,1,0,0,0,0},{0,0,0,1,4,0,1,0,0,0,0},{1,1,1,1,0,0,1,1,0,0,0},{1,0,0,0,3,2,0,1,0,0,0},{1,0,1,1,0,0,0,1,0,0,0},{1,0,6,0,0,0,0,1,1,0,0},{1,1,1,1,0,0,0,4,1,0,0},{0,0,0,1,1,1,1,1,1,0,0}};int map_3[8][11] = {{0,0,0,1,1,1,1,0,0,0,0},{0,0,0,1,4,4,1,0,0,0,0},{1,1,1,1,0,0,1,1,0,0,0},{1,0,0,0,0,0,0,1,0,0,0},{1,0,0,0,0,0,0,1,1,0,0},{1,1,1,0,1,6,2,0,1,0,0},{0,0,1,0,0,3,0,0,1,0,0},{0,0,1,1,1,1,1,1,1,0,0}};typedef struct position{int x;int y;}location;location agg[3];//全部通过void clearance(){if(map[agg[1].x][agg[1].y] == 5&&map[agg[2].x + n][agg[2].y + m] == 5&&gk == 3) {printf("你赢了!\n");system("pause");exit(0);}}//判断是否通过void control_adopt(){if(map[agg[1].x][agg[1].y] == 5&&map[agg[2].x + n][agg[2].y + m] == 5) {if(gk != 3){for(i = 0;i < 8;i++)for(j = 0;j < 11;j++){map[i][j] = map_2[i][j];map_2[i][j] = map_3[i][j];map_1[i][j] = map[i][j];}gk++;}steps = 0;}}//重新开始void control_reset(char reset_1){if(reset_1 == 'f'){for(i = 0;i < 8;i++)for(j = 0;j < 11;j++)map[i][j] = map_1[i][j];steps = 0;}}//结束void control_end(char end_0){if(end_0 == 't')exit(0);}//控制方向+ 箱子堵住陷阱void control_direction(char direction_){if(direction_ == 'd'||direction_ == 'w'||direction_ == 'a'||direction_ == 's'){if(direction_ == 'd')m = 1;if(direction_ == 'a')m = -1;if(direction_ == 'w')n = -1;if(direction_ == 's')n = 1;if(agg[0].x + n == agg[1].x&&(agg[0].y + m) == agg[1].y&&map[agg[1].x + n][agg[1].y + m] != 1&&map[agg[1].x][agg[1].y] != 5){if(map[agg[1].x + n][agg[1].y + m] == 4){map[agg[1].x][agg[1].y] = 0;map[agg[1].x + n][agg[1].y + m] = 5;agg[1].y = agg[1].y + m;agg[1].x = agg[1].x + n;}else{t = map[agg[1].x + n][agg[1].y + m];map[agg[1].x + n][agg[1].y + m] = map[agg[1].x][agg[1].y];map[agg[1].x][agg[1].y] = t;agg[1].y = agg[1].y + m;agg[1].x = agg[1].x + n;}}//第二个箱子的判断条件if(agg[0].x + n == agg[2].x&&(agg[0].y + m) == agg[2].y&&map[agg[2].x + n][agg[2].y + m] != 1&&map[agg[2].x][agg[2].y] != 5){if(map[agg[2].x + n][agg[2].y + m] == 4){map[agg[2].x][agg[2].y] = 0;map[agg[2].x + n][agg[2].y + m] = 5;agg[2].y = agg[2].y + m;agg[2].x = agg[2].x + n;}else{t = map[agg[2].x + n][agg[2].y + m];map[agg[2].x + n][agg[2].y + m] = map[agg[2].x][agg[2].y];map[agg[2].x][agg[2].y] = t;agg[2].y = agg[2].y + m;agg[2].x = agg[2].x + n;}}if(map[agg[0].x + n][agg[0].y + m] == 0){t = map[agg[0].x + n][agg[0].y + m];map[agg[0].x + n][agg[0].y + m] = map[agg[0].x][agg[0].y];map[agg[0].x][agg[0].y] = t;agg[0].y = agg[0].y + m;agg[0].x = agg[0].x + n;steps++;}m = 0;n = 0;}}//输出,寻找1,2,3void control_output(){printf("关卡%d:\n",gk);printf("步数:%d\n",steps);for(i = 0;i < 8;i++){for(j = 0;j < 11;j++){if(map[i][j] == 1)printf("■");if(map[i][j] == 0)printf("");if(map[i][j] == 2){printf("□");agg[1].x = i;agg[1].y = j;}if(map[i][j] == 3){printf("⊙");agg[0].x = i;agg[0].y = j;}if(map[i][j] == 4)printf("☆");if(map[i][j] == 5)printf("★");if(map[i][j] == 6){printf("□");agg[2].x = i;agg[2].y = j;}}printf("\n");}printf("w,a,s,d控制方向,t退出游戏,f重新开始!\n");}//输入指令void input_command(){while(1){scanf("%c",&command);if(command == 'w'||command == 'a'||command == 's'||command == 'd'||command == 't'||command == 'f')break;}}main(){for(i = 0;i < 8;i++){for(j = 0;j < 12;j++){map_1[i][j] = map[i][j];}}while(1){control_output();input_command();control_direction(command);control_reset(command);control_end(command);control_adopt();system("cls");clearance();}}【下载本文档,可以自由复制内容或自由编辑修改内容,更多精彩文章,期待你的好评和关注,我将一如既往为您服务】。
C++实现推箱子游戏源代码
#define qiang 1#define ren 2#define xiangzi 3#define jia 6#define men 8#define space 9int map[13][17] = {0};int fuben[13][17];int num_guan; //现在的关数int num_xiangzi; //箱子的个数int move_num = 0; //人行走的次数int push_num = 0; //人推箱子的次数int people[2]; //M[0] 是人所在位置的横坐标M[1]是人所在位置的纵坐标int door[2]; //门所在的坐标int tem = 0;int cc;void play();void chushi2();void guan1();void guan2();void guan3();void guan4();void guan5();void guan6();void guan7();void guan8();void guan9();void guan10();void guan11();void guan12();void guan13();void guan14();void dayin();int control();int finish();/****************************************************** ****主方法******************************************************* ******************/int main(){void jiemian();void shuoming();void xuanguan();void chushi(int nn);void chushi1();int i;char c;chushi1();for(i = 0;i<=999;i++){jiemian();c = getche();if(c == '1')play();else if(c == '2')xuanguan();else if(c == '3')shuoming();else if(c == '4')break;else system("cls");}}/*************************游戏主界面******************************/void jiemian(){printf("\n\n\n\n");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY | FOREGROUND_BLUE);printf(" ╭╮■■□□◆◆◆◆◆◆◆◆\n");printf(" ││■■□□□□□□□□◆ \n");printf(" ╭─┘└╮■■■■■□■□■◆◆ \n");printf(" ╰─┐┌■■■□┏━━━┓◆◆ \n");printf(" ││■■■■□□□□┃┏━┓┃◆◆ \n");printf(" ╭─┘└╮■■□┃┏━┓┃┏━━━━◆━━┓\n");printf(" ╰─┐┌╯■■■■□□┃┏━┓┃┗━━━━◆━━┛\n");printf(" ╭─┘│■■□□□┃┗━┛┃◆ \n");printf(" ╰──╯■■■■■□□□┗━━━┛◆◆◆◆◆◆ \n\n");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf(" >>1 开始游戏\n\n");printf(" >>2 选择关卡\n\n");printf(" >>3 游戏帮助\n\n");printf(" >>4 退出游戏\n\n");}void chushi1(){int i,j;for(i = 0;i<=12;i++)for (j = 0;j<=16;j++)fuben[i][j] = 0;}void chushi2(){int i,j;move_num = 0;push_num=0;for(i = 0;i<=12;i++)for(j = 0;j<=16;j++){map[i][j] = 0;fuben[i][j] = 0;}}/*************************游戏规则及说明******************************/void shuoming(){int i;char c;for(i = 0;i<=999;i++){system("cls");LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("\t\t\t╭───────────╮\n");printf("\t\t\t│游戏规则及说明│\n");printf("\t\t\t╰───────────╯\n\n ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),BACKGROUND_INTENSITY|FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN |BACKGROUND_BLUE|FOREGROUND_RED|FOREGROUND_RED);printf("囧");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf(" ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("别看了,这就是你了,还挺cool的吧,嘿嘿\n\n\n");LE),FOREGROUND_INTENSITY | FOREGROUND_INTENSITY);printf(" █ ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("这个是墙,放心,不是豆腐渣工程,要不你可以去撞撞看,\n");printf(" 你会懂得......\n\n\n");printf(" ■ ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("这个是箱子,就是要你去推的那个,不要试图一下推两个箱\n");printf(" 子,更不要想推着箱子把墙撞开,没用的...不要沮丧,再免\n");printf(" 费给你一句忠告,要是把箱子推到了死角,呵呵,直接下一\n");printf(" 关吧\n\n\n");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY |FOREGROUND_GREEN | FOREGROUND_RED);printf(" ★ ");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HAND LE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);printf("这里就是箱子们的最终归宿了,箱子要是回不到家,哼哼,\n");printf(" 你自己掂量着办吧。
推箱子-C语言源码
#include <stdio.h>#include <stdlib.h>//用到system()#include <conio.h>//因为需要用到getch()函数//推箱子_C语言版//写了一下午,第一个C语言游戏终于写好了,想想有点小激动....//蔚蓝之链编写//分享给大家参考下(在看得懂的情况下...),代码写得很凌乱.....,勿喷#define X 20#define Y 20#define REN 2 //表示人#define XIANGZI 3 //箱子#define WEIZHI 4 //箱子要推到的位置#define KONG 0 //空位#define QIANG 1 //墙#define RW 6 //人在箱子要推到的位置上#define XW 5 //箱子在箱子要推到的位置上#define bool short#define false 0#define true 1//数组内部数值表示内容:0表示空位,1表示墙体,2表示人,3表示箱子,4表示箱子要推到的位置,5表示既有箱子又有又位置,6表示既有位置又有人//01,2,3,4符号表示://0 :" " 1:■2:♀3:回4:◎void create_map_arr(int _x,int _y,int (*map_arr)[Y]);//创建地图数组void creata_map(int _x,int _y, int (*map)[Y]);//创建地图,重绘游戏画面void amend_map(int _x,int _y,char key , int (* arr_map)[Y]);//接收输入,修改游戏画面bool is_finish(int _x, int _y,int _total ,int (*arr_map)[Y]);//判断是否完成推箱子(地图x,地图y,总箱子数,地图数组)int main(void){int map_arr[X][Y];//存储地图的二维数组create_map_arr(X,Y,map_arr);//创建数组printf("C语言_推箱子v1.0 蔚蓝之链编写\n\n");printf("控制键:W↑,A←,S↓,D→\n");printf("提示:玩本游戏前请先关闭输入法\n");system("pause");system("cls");while(1){creata_map(X,Y,map_arr);//创建地图,重绘游戏画面if(is_finish(X,Y,2,map_arr) == true){printf("恭喜哦,推箱子完成!\n");system("pause");return 0;}amend_map(X,Y,getch(),map_arr);//接收输入,修改画面system("cls");//清空画面}getchar();return 0;}void create_map_arr (int _x,int _y,int (*map_arr)[Y])//创建地图数组{int x,y;for(x=0;x<_x;x++){for (y=0;y<_y;y++){if(y==0 || x==0 || x==_x-1 || y==_y-1){*(*(map_arr+x)+y) = QIANG;//printf ("■");}else{*(*(map_arr+x)+y) = KONG;//printf (" ");}}//printf("\n");}//这里可以修改箱子创建的位置,人创建的位置及箱子要达到的位置,默认两个箱子*(*(map_arr+3)+7)=REN;//人*(*(map_arr+5)+6)=XIANGZI;//箱子*(*(map_arr+5)+7)=XIANGZI;//箱子*(*(map_arr+1)+8)=WEIZHI;//箱子要推到的位置*(*(map_arr+2)+8)=WEIZHI;//箱子要推到的位置}void creata_map(int _x,int _y, int (*map)[Y])//创建地图(地图数组[][]){int x,y;for(x = 0; x < _x; x++){for (y = 0; y < _y; y++){if (*(*(map + x) + y) == QIANG){printf ("■");}else if(*(*(map + x) + y) == KONG){printf (" ");}else if(*(*(map + x) + y)==REN || *(*(map + x) + y)==RW){printf ("♀");}else if (* (*(map + x) + y)==XIANGZI){printf("□");}else if (*(*(map + x) + y)==WEIZHI){printf("◎");}else if(*(*(map + x) + y) == XW){printf("回");}}printf ("\n");}}//这个函数写的太乱了,自己看着也头大,不过还好,总算完成了^_^void amend_map(int _x,int _y,char key , int (* arr_map)[Y])//移动(地图x,地图y,按键值,地图数组){int x,y;for(x = 0; x < _x; x++){for (y = 0; y < _y; y++){if(*( * (arr_map + x ) + y) == REN || *( * (arr_map + x ) + y) == RW){if(key == 'a' || key == 'A')//←{if (y > 0){if(*(*(arr_map + x) +(y)) == RW)//人在最终位置{if(*( * (arr_map + x ) + (y-1)) == WEIZHI)//人左边还是最终位置{*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + y) = WEIZHI;}else if(*( * (arr_map + x ) + (y-1)) == XIANGZI || *( * (arr_map + x ) + (y-1)) == XW){if(*( * (arr_map + x ) + (y-1)) == XW){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if(*(*(arr_map + x) + (y - 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}}else if(*(*(arr_map + x) + (y - 1)) == KONG){*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if (*(*(arr_map + x) +(y)) == REN)//人在空位{if(*( * (arr_map + x ) + (y-1)) == WEIZHI)//人左边是最终位置{*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + y) = KONG;}else if(*( * (arr_map + x ) + (y-1)) == XIANGZI || *( * (arr_map + x ) + (y-1)) == XW){if(*( * (arr_map + x ) + (y-1)) == XW){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + x) + (y - 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}else if(*(*(arr_map + x) + (y - 1)) == KONG){*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}//人在空位ENDelse if (*( * (arr_map + x ) + (y-1)) == XW || *( * (arr_map + x ) + (y-1)) == XIANGZI)//人左边有箱子或箱子在最终位置{if(*( * (arr_map + x ) + (y-1)) == XIANGZI || *( * (arr_map + x ) + (y-1)) == XW){if(*( * (arr_map + x ) + (y-1)) == XW){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + x) + (y - 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y-2)) == KONG){*( * (arr_map + x ) + (y-2)) = XIANGZI;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y-2)) = XW;*( * (arr_map + x ) + (y-1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}}else if(*(*(arr_map + x) + (y-1)) == WEIZHI){*(*(arr_map + x) + (y-1)) = RW;*(*(arr_map + x) + (y)) = KONG;}else if(*(*(arr_map + x) + (y-1)) == KONG){*(*(arr_map + x) + (y-1)) = REN;*(*(arr_map + x) + (y)) = KONG;}}//IF(Y>0)END}//←else if(key == 'd' || key == 'D')//→{if (y < _y){if(*(*(arr_map + x) +(y)) == RW)//人在最终位置{if(*( * (arr_map + x ) + (y+1)) == WEIZHI)//人右边还是最终位置{*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + y) = WEIZHI;}else if(*( * (arr_map + x ) + (y+1)) == XIANGZI || *( * (arr_map + x ) + (y+1)) == XW){if(*( * (arr_map + x ) + (y+1)) == XW){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + x ) + (y+2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if(*(*(arr_map + x) + (y + 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + x ) + (y+2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}}else if(*(*(arr_map + x) + (y + 1)) == KONG){*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if (*(*(arr_map + x) +(y)) == REN)//人在空位{if(*( * (arr_map + x ) + (y+1)) == WEIZHI)//人右边是最终位置{*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + y) = KONG;}else if(*( * (arr_map + x ) + (y+1)) == XIANGZI || *( * (arr_map + x ) + (y+1)) == XW){if(*( * (arr_map + x ) + (y+1)) == XW){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y-2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + x) + (y + 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y+2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}else if(*(*(arr_map + x) + (y + 1)) == KONG){*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}//人在空位ENDelse if (*( * (arr_map + x ) + (y+1)) == XW || *( * (arr_map + x ) + (y+1)) == XIANGZI)//人右边有箱子或箱子在最终位置{if(*( * (arr_map + x ) + (y+1)) == XIANGZI || *( * (arr_map + x ) + (y+1)) == XW){if(*( * (arr_map + x ) + (y+1)) == XW){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y+2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + x) + (y + 1)) == XIANGZI ){if (*( * (arr_map + x ) + (y+2)) == KONG){*( * (arr_map + x ) + (y+2)) = XIANGZI;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + x ) + (y+2)) == WEIZHI){*( * (arr_map + x ) + (y+2)) = XW;*( * (arr_map + x ) + (y+1)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}}else if(*(*(arr_map + x) + (y+1)) == WEIZHI){*(*(arr_map + x) + (y+1)) = RW;*(*(arr_map + x) + (y)) = KONG;}else if(*(*(arr_map + x) + (y+1)) == KONG){*(*(arr_map + x) + (y+1)) = REN;*(*(arr_map + x) + (y)) = KONG;}}//IF(Y>0)END}//→else if(key == 'w' || key == 'W')//↑{if (x-1 > 0){if(*(*(arr_map + (x)) +(y)) == RW)//人在最终位置{if(*( * (arr_map + (x-1) ) + (y)) == WEIZHI)//人上边还是最终位置{*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + (x) ) + y) = WEIZHI;}else if(*( * (arr_map + (x-1) ) + (y)) == XIANGZI || *( * (arr_map + (x-1) ) + (y)) == XW){if(*( * (arr_map + (x-1) ) + (y)) == XW){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + (x) ) + (y)) = WEIZHI;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if(*(*(arr_map + (x-1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}}else if(*(*(arr_map + (x-1)) + (y)) == KONG){*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if (*(*(arr_map + x) +(y)) == REN)//人在空位{if(*( * (arr_map + (x-1) ) + (y)) == WEIZHI)//人上边是最终位置{*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + y) = KONG;}else if(*( * (arr_map + (x-1) ) + (y)) == XIANGZI || *( * (arr_map + (x-1) ) + (y)) == XW)//人上边是箱子或在最终位置的箱子{if(*( * (arr_map + (x-1) ) + (y)) == XW){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + (x-1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}else if(*(*(arr_map + (x-1)) + (y)) == KONG){*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}//人在空位ENDelse if (*( * (arr_map + (x-1) ) + (y)) == XW || *( * (arr_map + (x-1) ) + (y)) == XIANGZI)//人上边有箱子或箱子在最终位置{if(*( * (arr_map + (x-1) ) + (y)) == XIANGZI || *( * (arr_map + (x-1) ) + (y)) == XW){if(*( * (arr_map + (x-1) ) + (y)) == XW){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + (x-1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x-2) ) + (y)) == KONG){*( * (arr_map + (x-2) ) + (y)) = XIANGZI;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x-2) ) + (y)) == WEIZHI){*( * (arr_map + (x-2) ) + (y)) = XW;*( * (arr_map + (x-1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}}else if(*(*(arr_map + (x-1)) + (y)) == WEIZHI){*(*(arr_map + (x-1)) + (y)) = RW;*(*(arr_map + x) + (y)) = KONG;}else if(*(*(arr_map + (x-1)) + (y)) == KONG){*(*(arr_map + (x-1)) + (y)) = REN;*(*(arr_map + x) + (y)) = KONG;}}//IF(Y>0)END}//↑else if(key == 's' || key == 'S')//↓{if (x < _x+1 > 0){if(*(*(arr_map + (x)) +(y)) == RW)//人在最终位置{if(*( * (arr_map + (x+1) ) + (y)) == WEIZHI)//人下边还是最终位置{*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + (x) ) + y) = WEIZHI;}else if(*( * (arr_map + (x+1) ) + (y)) == XIANGZI || *( * (arr_map + (x+1) ) + (y)) == XW){if(*( * (arr_map + (x+1) ) + (y)) == XW){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + (x) ) + (y)) = WEIZHI;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if(*(*(arr_map + (x+1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}}else if(*(*(arr_map + (x+1)) + (y)) == KONG){*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = WEIZHI;}}else if (*(*(arr_map + x) +(y)) == REN)//人在空位{if(*( * (arr_map + (x+1) ) + (y)) == WEIZHI)//人下边是最终位置{*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + y) = KONG;}else if(*( * (arr_map + (x+1) ) + (y)) == XIANGZI || *( * (arr_map + (x+1) ) + (y)) == XW){if(*( * (arr_map + (x+1) ) + (y)) == XW){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + (x+1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}else if(*(*(arr_map + (x+1)) + (y)) == KONG){*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}//人在空位ENDelse if (*( * (arr_map + (x+1) ) + (y)) == XW || *( * (arr_map + (x+1) ) + (y)) == XIANGZI)//人下边有箱子或箱子在最终位置{if(*( * (arr_map + (x+1) ) + (y)) == XIANGZI || *( * (arr_map + (x+1) ) + (y)) == XW){if(*( * (arr_map + (x+1) ) + (y)) == XW){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = RW;*( * (arr_map + x ) + (y)) = KONG;}}else if(*(*(arr_map + (x+1)) + (y)) == XIANGZI ){if (*( * (arr_map + (x+2) ) + (y)) == KONG){*( * (arr_map + (x+2) ) + (y)) = XIANGZI;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}else if(*( * (arr_map + (x+2) ) + (y)) == WEIZHI){*( * (arr_map + (x+2) ) + (y)) = XW;*( * (arr_map + (x+1) ) + (y)) = REN;*( * (arr_map + x ) + (y)) = KONG;}}}}else if(*(*(arr_map + (x+1)) + (y)) == WEIZHI){*(*(arr_map + (x+1)) + (y)) = RW;*(*(arr_map + x) + (y)) = KONG;}else if(*(*(arr_map + (x+1)) + (y)) == KONG){*(*(arr_map + (x+1)) + (y)) = REN;*(*(arr_map + x) + (y)) = KONG;}}//IF(Y>0)END}//↓return;}}}}bool is_finish(int _x, int _y,int _total ,int (*arr_map)[Y])//判断是否完成推箱子(地图x,地图y,总箱子数,地图数组){int x,y,temp = 0;for (x = 0; x < _x; x++){for (y = 0; y < _y; y++){if (*(*(arr_map+x)+y) == XW){temp++;}}}if (_total == temp){return true;}return false;}。
推箱子游戏代码,非界面版
import java.util.Random;import java.util.Scanner;public class 推箱子 {public static void main(String[] args) {//推箱子int map[][]=new int[10][10]; //固定箱子,人,目的地的位置// int rx=0;// int ry=0;// map[rx][ry]=1;// int xx=5;// int xy=1;// map[xx][xy]=2;// int mx=6;// int my=4;// map[mx][my]=3;//随机生成人,箱子,目的地的位置Random r=new Random();int rx=r.nextInt(10);int ry=r.nextInt(10);map[rx][ry]=1;int xx=r.nextInt(10);int xy=r.nextInt(10);map[xx][xy]=2;int mx=r.nextInt(10);int my=r.nextInt(10);map[mx][my]=3;while(true){for(int i=0;i<map.length;i++){for(int j=0;j<map[i].length;j++){if(map[i][j]==1){System.out.print(" 人 ");}if(map[i][j]==2){//△代表箱子System.out.print(" △ ");}if(map[i][j]==3){//★代表箱子System.out.print(" ★ ");}if(map[i][j]==0){//◇代表箱子System.out.print(" ◇ ");}}System.out.println();}if(xx==mx&&xy==my){break;}if(xx==0&&mx!=0||xy==0&&my!=0||xx==9&&mx!=9||xy==9&&my!=9){// 判断箱子到边上而目的地不在边上的情况System.out.println("永远也到不了了~~~~(>_<)~~~~ ");}Scanner s = new Scanner(System.in);System.out.println("请输入要操作的动作:");System.out.println("w↑,s↓,a←,d→");String t=s.next();if(t.equals("d")){if(ry==9){System.out.println("此路不通!!~~~~(>_<)~~~~ ");}else if(t.equals("d")){ry++;if(map[rx][ry]==0){map[rx][ry]=1;map[rx][ry-1]=0;}else if(map[rx][ry]==2){xy++;map[xx][xy]=2;map[rx][ry]=1;map[rx][ry-1]=0;}}}if(t.equals("a")){if(ry==0){System.out.println("此路不通!!~~~~(>_<)~~~~ ");}else {ry--;if(map[rx][ry]==0){map[rx][ry]=1;map[rx][ry+1]=0;}else if(map[rx][ry]==2){xy--;map[xx][xy]=2;map[rx][ry+1]=0;map[rx][ry]=1;}}}if(t.equals("s")){if(rx==9){System.out.println("此路不通!!~~~~(>_<)~~~~ ");}else {rx++;if(map[rx][ry]==0){map[rx][ry]=1;map[rx-1][ry]=0;}else if(map[rx][ry]==2){xx++;map[xx][xy]=2;map[rx-1][ry]=0;map[rx][ry]=1;}}}if(t.equals("w")){if(rx==0){System.out.println("此路不通!!~~~~(>_<)~~~~ ");}else{rx--;if(map[rx][ry]==0){map[rx][ry]=1;map[rx+1][ry]=0;}else if(map[rx][ry]==2){xx--;map[xx][xy]=2;map[rx+1][ry]=0;map[rx][ry]=1;}}}}System.out.println("游戏结束!");}}。
推箱子 源代码
#include"stdio.h"#include"conio.h"#include"stdlib.h"#include"time.h"void show(char b[9][9]){printf("\t\t\t\t***************************\n");printf("\t\t\t\t 推箱子游戏");printf("\n\t\t\t\t***************************\n");printf("\t\t\t\t 人:★墙:■\n\t\t\t\t 箱子:◎目的地:□\n\t\t\t\t 按d 键退出游戏\n\t\t\t\t 限时30秒!");printf("\n\t\t\t\t***************************\n");for(int i=0;i<9;i++) //使用循环将地图转换输出{printf("\t\t\t\t ");for(int k=0;k<9;k++){if(b[i][k]=='#'){printf("■");}else{if(b[i][k]==3){printf("□");}else{if(b[i][k]==5){printf("◎");}else{if(b[i][k]==1){printf("★");}else{if(b[i][k]=='!'){printf("⊙");}else{printf("%c ",b[i][k]);}}}}}}printf("\n");}printf("\t\t\t\t***************************\n");}下面的是cpp文件#include"show.h"/*============================================移动函数=============================================================*/void move(char c[9][9],int *x,int *y,int X,int Y,int A,int B,int N,int M,char *t){*t=getch();/*============================================结束=============================================================*/if(*t=='d')//遇到d返回{}/*============================================向左=============================================================*/if(*t==75){if(c[*x][*y-1]=='#'||c[*x][*y-1]==5&&c[*x][*y-2]==5||c[*x][*y-1]==5&&c[*x][*y-2]=='!')//遇到墙或推两个箱子{}else{if(c[*x][*y-1]=='!')//把箱子推出目的地{if(c[*x][*y-2]!=0){}else{c[*x][*y]=0;*y-=1;c[*x][*y]=1;c[*x][*y-1]=5;}}else{if(c[*x][*y-1]==5&&c[*x][*y-2]!='#')//推箱子{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M])//推着箱子离开目的地{c[*x][*y]=3;*y-=1;c[*x][*y]=1;c[*x][*y-1]=5;}else{c[*x][*y]=0;*y-=1;c[*x][*y]=1;c[*x][*y-1]=5;}}else{if(c[*x][*y-1]==5&&c[*x][*y-2]=='#')//推不动箱子{}else{if(c[*x][*y-1]==c[X][Y]||c[*x][*y-1]==c[A][B]||c[*x][*y-1]==c[N][M])//进到目的地{c[*x][*y]=0;*y-=1;c[*x][*y]=1;}else{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M])//离开目的地{c[*x][*y]=3;*y-=1;c[*x][*y]=1;}else //走到空地{c[*x][*y]=0;*y-=1;c[*x][*y]=1;}}}}}}if(c[X][Y]==5||c[A][B]==5||c[N][M]==5)//箱子推到目的地{c[*x][*y-1]='!';}}/*============================================向右=============================================================*/if(*t==77){if(c[*x][*y+1]=='#'||c[*x][*y+1]==5&&c[*x][*y+2]==5||c[*x][*y+1]==5&&c[*x][*y+2]=='!')//遇到墙或推两个箱子{}else{if(c[*x][*y+1]=='!')//把箱子推出目的地{if(c[*x][*y+2]!=0){}else{c[*x][*y]=0;*y+=1;c[*x][*y]=1;c[*x][*y+1]=5;}}else{if(c[*x][*y+1]==5&&c[*x][*y+2]!='#')//推箱子{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]){c[*x][*y]=3;*y+=1;c[*x][*y]=1;c[*x][*y+1]=5;}else{c[*x][*y]=0;*y+=1;c[*x][*y]=1;c[*x][*y+1]=5;}}else{if(c[*x][*y+1]==5&&c[*x][*y+2]=='#')//推不动箱子{}else{if(c[*x][*y+1]==c[X][Y]||c[*x][*y+1]==c[A][B]||c[*x][*y+1]==c[N][M])//进到目的地{c[*x][*y]=0;*y+=1;c[*x][*y]=1;}else{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M])//离开目的地{c[*x][*y]=3;*y+=1;c[*x][*y]=1;}else //走到空地{c[*x][*y]=0;*y+=1;c[*x][*y]=1;}}}}}}if(c[X][Y]==5||c[A][B]==5||c[N][M]==5)//箱子进入目的地{c[*x][*y+1]='!';}}/*============================================向上=============================================================*/if(*t==72){if(c[*x-1][*y]=='#'||c[*x-1][*y]==5&&c[*x-2][*y]==5||c[*x-1][*y]==5&&c[*x-2][*y]=='!')//遇到墙或推两个箱子{}else{if(c[*x-1][*y]=='!')//把箱子推出目的地{if(c[*x-2][*y]!=0){}else{c[*x][*y]=0;*x-=1;c[*x][*y]=1;c[*x-1][*y]=5;}}else{if(c[*x-1][*y]==5&&c[*x-2][*y]!='#')//推箱子{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]){c[*x][*y]=3;*x-=1;c[*x][*y]=1;c[*x-1][*y]=5;}else{c[*x][*y]=0;*x-=1;c[*x][*y]=1;c[*x-1][*y]=5;}}else{if(c[*x-1][*y]==5&&c[*x-2][*y]=='#')//推不动箱子{}else{if(c[*x-1][*y]==c[X][Y]||c[*x-1][*y]==c[A][B]||c[*x-1][*y]==c[N][M])//进到目的地{c[*x][*y]=0;*x-=1;c[*x][*y]=1;}else{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M])//走出目的地{c[*x][*y]=3;*x-=1;c[*x][*y]=1;}else //走到空地{c[*x][*y]=0;*x-=1;c[*x][*y]=1;}}}}}}if(c[X][Y]==5||c[A][B]==5||c[N][M]==5)//箱子进入目的地{c[*x-1][*y]='!';}}/*============================================向下=============================================================*/if(*t==80){if(c[*x+1][*y]=='#'||c[*x+1][*y]==5&&c[*x+2][*y]==5||c[*x+1][*y]==5&&c[*x+2][*y]=='!')//遇到墙或推两个箱子{}else{if(c[*x+1][*y]=='!')//把箱子推出目的地{if(c[*x+2][*y]!=0){}else{c[*x][*y]=0;*x+=1;c[*x][*y]=1;c[*x+1][*y]=5;}}else{if(c[*x+1][*y]==5&&c[*x+2][*y]!='#')//推箱子{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M]){c[*x][*y]=3;*x+=1;c[*x][*y]=1;c[*x+1][*y]=5;}else{c[*x][*y]=0;*x+=1;c[*x][*y]=1;c[*x+1][*y]=5;}}else{if(c[*x+1][*y]==5&&c[*x+2][*y]=='#')//推不动箱子{}else{if(c[*x+1][*y]==c[X][Y]||c[*x+1][*y]==c[A][B]||c[*x+1][*y]==c[N][M])//进到目的地{c[*x][*y]=0;*x+=1;c[*x][*y]=1;}else{if(c[*x][*y]==c[X][Y]||c[*x][*y]==c[A][B]||c[*x][*y]==c[N][M])//走出目的地{c[*x][*y]=3;*x+=1;c[*x][*y]=1;}else //走到空地{c[*x][*y]=0;*x+=1;c[*x][*y]=1;}}}}}}if(c[X][Y]==5||c[A][B]==5||c[N][M]==5)//箱子进入目的地{c[*x+1][*y]='!';}}}/*============================================第一关=============================================================*/void map1(){time_t p,q,o;time(&p);int i=4,j=4,m=6,n=2,t=1,k=3;system("cls");//刷屏system("color 1E");int x=1,y=7;char z,a[9][9]={{'#','#','#','#','#','#','#','#','#'},{'#','#','#', 3 , 0 , 0 , 0 , 1 ,'#'},{'#','#', 0 , 0 , 0 ,'#', 0 ,'#','#'},{'#', 0 , 0 ,'#', 0 , 0 , 0 , 0 ,'#'},{'#', 0 , 5 , 0 , 3 , 5 , 5 , 0 ,'#'},{'#','#','#','#', 0 , 0 ,'#','#','#'},{'#','#', 3 , 0 , 0 , 0 ,'#','#','#'},{'#','#','#','#','#','#','#','#','#'},{'#','#','#','#','#','#','#','#','#'}};show(a);loop:move(a,&x,&y,i,j,m,n,t,k,&z);if(a[i][j]=='!'&&a[m][n]=='!'&&a[t][k]=='!'){system("cls");//刷屏show(a);printf("\t\t\t\t YOU ARE WIN!\n");time(&q);printf("\t\t\t\t\t 用时%.3d秒\n",q-p);printf("\t\t\t\t*******回车重新选择*******\n\t\t\t\t");getch();}else{time(&o);if(z=='d'){return;}if(o-p>30){loop1: system("cls");//刷屏printf("\n\n\n\n\t\t\t\t时间已过请重新回车选关!");int g=getch();if(g==13)//判断是否为回车符{return;}else{goto loop1;}}system("cls");//刷屏show(a);//调用输出函数goto loop;}}/*============================================第二关=============================================================*/void map2(){time_t p,q,o;time(&p);int i=1,j=3,m=6,n=2,t=2,k=1;system("cls");//刷屏system("color 2E");int x=4,y=1;char z,b[9][9]={{'#','#','#','#','#','#','#','#','#'},{'#','#','#', 3 , 0 ,'#','#','#','#'},{'#', 3 ,'#', 0 , 0 , 0 , 0 ,'#','#'},{'#', 0 , 0 , 0 ,'#','#', 0 , 0 ,'#'},{'#', 1 , 0 , 5 , 0 , 0 , 5 , 0 ,'#'},{'#', 0 , 0 ,'#', 0 , 0 , 0 ,'#','#'},{'#','#', 3 , 0 , 0 , 5 ,'#','#','#'},{'#','#','#','#', 0 , 0 ,'#','#','#'},{'#','#','#','#','#','#','#','#','#'}};show(b);loop:move(b,&x,&y,i,j,m,n,t,k,&z);if(b[i][j]=='!'&&b[m][n]=='!'&&b[t][k]=='!'){system("cls");//刷屏show(b);printf("\t\t\t\t YOU ARE WIN!\n");time(&q);printf("\t\t\t\t\t 用时%.3d秒\n",q-p);printf("\t\t\t\t*******回车重新选择*******\n\t\t\t\t");getch();}else{time(&o);if(z=='d'){return ;}if(o-p>30){loop1: system("cls");//刷屏printf("\n\n\n\n\t\t\t\t时间已过请重新回车选关!");int g=getch();if(g==13){return;}else{goto loop1;}}system("cls");//刷屏show(b);goto loop;}}/*============================================第三关=============================================================*/void map3(){time_t p,q,o;time(&p);int i=7,j=6,m=6,n=2,t=2,k=1;system("cls");//刷屏system("color 2F");int x=5,y=3;char z, b[9][9]={{'#','#','#','#','#','#','#','#','#'},{'#','#','#','#','#', 0 , 0 ,'#','#'},{'#', 3 ,'#', 0 , 5 , 0 , 0 , 0 ,'#'},{'#', 0 , 5 , 0 , 0 ,'#', 0 ,'#','#'},{'#', 0 , 0 , 0 , 0 , 0 , 5 , 0 ,'#'},{'#','#', 0 , 1 , 0 , 0 , 0 , 0 ,'#'},{'#','#', 3 , 0 ,'#', 0 ,'#', 0 ,'#'},{'#','#','#', 0 , 0 , 0 , 3 ,'#','#'},{'#','#','#','#','#','#','#','#','#'}};show(b);loop:move(b,&x,&y,i,j,m,n,t,k,&z);if(b[i][j]=='!'&&b[t][k]=='!'&&b[m][n]=='!'){system("cls");//刷屏show(b);printf("\t\t\t\t YOU ARE WIN!\n");time(&q);printf("\t\t\t\t\t 用时%.3d秒\n",q-p);printf("\t\t\t\t*******回车重新选择*******\n\t\t\t\t");getch();}else{time(&o);if(z=='d'){return;}if(o-p>30){loop1: system("cls");//刷屏printf("\n\n\n\n\t\t\t\t时间已过请重新回车选关!");int g=getch();if(g==13){return;}else{goto loop1;}}system("cls");//刷屏show(b);goto loop;}}/*============================================主函数=============================================================*/void main(int argc,char*argv[]){time_t a,b;time(&a);system("color 4E");loop1: {system("cls");//刷屏printf("\n\n\n\n\n\n\t\t\t\t欢迎进入推箱子游戏\n\n\t\t\t\t 请按a b c选择游戏\n\n\t\t\t\t 按 d 键结束");int t=getch();if(t=='d'){printf("\n\t\t\t ");}else{if(t=='a'){map1();goto loop1;}if(t=='b'){map2();goto loop1;}if(t=='c'){map3();goto loop1;}else{printf("\n\n\t\t\t\t 请重新输入:");goto loop1;}}}time(&b);printf("\n\n\n\n\t\t\t\t 游戏总共用时%.3d秒\n\t\t\t ",b-a);getch();//等待读取回车符以结束程序}。
推箱子(C语言小游戏代码)
#include<stdio.h> #include<stdlib.h>#include<windows.h>#include<conio.h>//打印地图void DrawMap();//设置颜色void SetColor(int nColor);//获取玩家坐标POINT GetGamerPosition();//上void Up();//下void Down();//左void Left();//右void Right();//获取空箱子个数int GetSpareBox();//地图//0:空地;3箱子的目的地4箱子// 6人7箱子与目的地的集合//1:墙壁;9人在箱子的目的地int g_map[2][10][12]={{{0,0,0,0,1,1,1,0,0,0,0,0 },{0,0,0,0,1,3,1,0,0,0,0,0 },{0,0,0,0,1,0,1,1,1,1,1,1 },{1,1,1,1,1,4,0,4,0,0,3,1 },{1,3,0,0,0,4,6,1,1,1,1,1 },{1,1,1,1,1,1,4,1,0,0,0,0 },{0,0,0,0,0,1,0,1,0,0,0,0 },{0,0,0,0,0,1,0,1,0,0,0,0 },{0,0,0,0,0,1,3,1,0,0,0,0 },{0,0,0,0,0,1,1,1,0,0,0,0 },},{{1,1,1,1,1,0,0,0,0,0,0,0 },{1,0,0,0,1,0,1,1,1,0,0,0 },{1,0,4,0,1,0,1,1,1,1,1,1 },{1,0,4,6,1,0,1,0,0,0,3,1 },{1,1,1,4,1,1,1,0,0,0,3,1 },{1,1,1,0,0,0,0,0,0,0,3,1 },{0,1,0,0,0,1,0,0,0,0,0,1 },{0,1,0,0,0,1,0,0,0,0,0,1 },{0,1,0,0,0,1,1,1,1,1,1,1 },{1,1,1,1,1,1,0,0,0,0,0,0 },}};int g_nCurrentLevel = 0;//当前管卡int main(){//设置窗口大小system("mode con cols=26 lines=11");//设置标题//SetConsoLeTitle("推箱子");char nInput = 0;//输入字符while(1){//如果箱子推完了过关if (0==GetSpareBox())g_nCurrentLevel++;//清屏system("cls");//如果没有关卡,那就恭喜你成功通关。
用java实现推箱子(sokoban)游戏
推箱子游戏一、功能描述:➢可以通过面板上的按钮或是键盘上的pageup,pagedown键选择上下关➢可以通过面板上按钮或是键盘上的Backspace键后退,一直后退自己想要的位置,知道游戏开始时的位置。
➢可以通过面板上的按钮或是键盘上的字母r重新开始当前关卡游戏。
➢可以在复选框选择想要玩的关卡。
二、界面及运行截图三、源代码(三部分)1、地图类package box;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;//读取字符文件类FileReader import java.io.IOException;public class map {int[][] map=new int[20][20];int manX,manY;public map(int level){String filepath="mapc/"+level+".txt";File file = new File(filepath);FileReader fr = null;//利用FileReader流来读取一个文件中的数据BufferedReader br = null;//字符读到缓存里try {fr = new FileReader(file);br = new BufferedReader(fr);for (int i = 0; i < 15; i++){String line = br.readLine();//以行为单位,一次读一行利用BufferedReader 的readLine,读取分行文本byte[] point = line.getBytes();//将字符串转换为字节数组for (int j = 0; j < 15; j++) {map[i][j] = point[j] - 48;// 根据ASCall码表要减掉30H(十进制的48)if (map[i][j] == 5 || map[i][j] == 6 || map[i][j] == 7|| map[i][j] == 8){manX = i;manY = j;}}}}catch (FileNotFoundException e){e.printStackTrace();//深层次的输出异常调用的流程}catch (IOException e){e.printStackTrace();//深层次的输出异常调用的流程}catch(NullPointerException e){e.printStackTrace();//深层次的输出异常调用的流程}finally {if (br == null){try{br.close();}catch (IOException e){e.printStackTrace();}br = null;}if (fr == null){try {fr.close();} catch (IOException e){e.printStackTrace();}fr = null;}}}public int[][] getMap() {return map;}public int getManX() {return manX;}public int getManY() {return manY;}}2、游戏运行类package box;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.*;import javax.swing.*;public class pushbox extends JFrame implements KeyListener,ActionListener{ map map_level=new map(1); //首次获取地图int[][] map=new int[15][15]; //地图上每个位置所对应的图片int[][] map_restart=new int[15][15]; //备用图片static int manX,manY; //人所处的位置static int level=1; //所处关卡static int step=0;Image[] picture; //地图上的所有图片ImageIcon[] image=new ImageIcon[10];Stack<Integer> back_stack = new Stack<Integer>(); //存储所前行的路径JButton next,previous,back,restart,exit; //按钮:下一关,上一关,重新开始,退出String current_level="第"+level+"关"; //当前所处关卡(右上角显示文本)String[] level_button=new String[13]; //复选框的容JComboBox selectBox; //选关复选框final int Up = 0;final int Down = 1;final int Left = 2;final int Right = 3;int[] dir_x = {-1, 1, 0, 0};int[] dir_y = {0, 0, -1, 1};public pushbox(){super("推箱子");getmap(map);previous=new JButton("上一关");next=new JButton("下一关");restart=new JButton("重新开始");back=new JButton("后退");exit=new JButton("退出游戏");for(int i=0;i<13;i++)level_button[i]="第"+Integer.toString(i+1)+"关";selectBox=new JComboBox(level_button);FlowLayout layout=new FlowLayout(); //布局模式Container c=getContentPane();c.setLayout(layout);layout.setAlignment(FlowLayout.CENTER); //控件在布局中的位置c.setBackground(Color.CYAN); //背景色c.add(previous);c.add(next);c.add(back);c.add(restart);c.add(selectBox);c.add(exit);previous.addActionListener((ActionListener) this);previous.addKeyListener(this);next.addActionListener((ActionListener) this);next.addKeyListener(this);back.addActionListener(this);back.addKeyListener(this);restart.addActionListener((ActionListener) this);restart.addKeyListener(this);exit.addActionListener((ActionListener) this);exit.addKeyListener(this);selectBox.addItemListener( //监听复选框点击事件new ItemListener(){public void itemStateChanged(ItemEvent e){if(e.getStateChange()==e.SELECTED){step=0;back_stack.clear(); //每次重新选关后都要清空路径栈level=selectBox.getSelectedIndex()+1; //获取所选的关卡map_level=new map(level); //重新获取对应关卡的地图getmap(map);current_level="第"+level+"关"; //右上角提示所处关卡文本的更新repaint(); //重画地图requestFocus(); //重新获取焦点(因为按钮事件后键盘事件不起作用)}}});for(int k=0;k<10;k++){image[k]=new ImageIcon("picture/"+k+".png"); //获取图片}picture=new Image[]{image[0].getImage(),image[1].getImage(),image[2].getImage(),image[3].getImage(),image[4].getImage(),image[5].getImage(),image[6].getImage(),image[7].getImage(),image[8].getImage(),image[9].getImage()};this.setFocusable(true);this.addKeyListener((KeyListener)this);setSize(640,640);setVisible(true);}//判断是否通关(方法是,检查地图中是否还有箱子编号4)public Boolean pass(){int p=0;for(int i=0;i<15;i++)for(int j=0;j<15;j++){if(map[i][j]==4) p++;}if(p==0) return true;else return false;}//获取地图上每个位置所对应的图片编号public void getmap(int[][] map){for(int i=0;i<15;i++)for(int j=0;j<15;j++)map[i][j]=map_level.map[i][j];manX=map_level.manX;manY=map_level.manY;}public void paint(Graphics g){super.paint(g);//提示信息back_stack.push(Down + 5); //这里是一个细节,先将“DOWN”存入栈g.drawString(current_level,500,90);g.drawString("第"+step+"步",400,90);g.drawString("用键盘上的R重新开始,Q退出游戏",200,565);g.drawString("用键盘pageup,pagedown切换关卡",200,580);g.drawString("用键盘上的上下左右键控制人的移动",200,600);g.drawString("用键盘上的Backspace键后退",200,620);//在面板上画地图for (int i = 0; i < 15; i++)for (int j = 0; j < 15; j++)g.drawImage(picture[map[i][j]],100+30*j,100+30*i, this);}public void Step(Graphics g){g.setColor(Color.cyan);g.drawString("第"+step+"步",400,90);step++;g.setColor(Color.BLACK);g.drawString("第"+step+"步",400,90);}public void move(int dir, Graphics g){int man = dir + 5;int dx = dir_x[dir];int dy = dir_y[dir];int x = manX;int y = manY;manX += dx;manY += dy;int origin_picture = map[manX][manY];int next_picture = map[manX + dx][manY + dy];if((map[manX][manY]==4||map[manX][manY]==3)&&(map[manX+dx][manY+dy]==2||map[ manX+dx][manY+dy]==4||map[manX+dx][manY+dy]==3)) //上面是箱子,箱子上面是墙或箱子{manX -= dx;manY -= dy;return ;}if(map[manX][manY]==2) //上面是墙{manX -= dx;manY -= dy;return ;}if(map[manX][manY]==1||map[manX][manY]==9){ //人上面是草地或是目的地g.drawImage(picture[man],100+30*manY,manX*30+100,this); //人的朝向变化if(map[x][y]==9) //原来位置是目的地g.drawImage(picture[9],100+30*y,100+30*x,this);else{ //原来位置是草地g.drawImage(picture[1],100+30*y,x*30+100,this);map[x][y]=1;}Step(g);}if((map[manX][manY]==4||map[manX][manY]==3)&&map[manX+dx][manY+dy]==1){//上面是箱子,箱子上面是草地g.drawImage(picture[man],100+30*manY,manX*30+100,this); //人的朝向变化if(map[manX][manY]==3) //若是移动一大目的地的箱子,原箱子位置值为9map[manX][manY]=9;if(map[x][y]==9) //原来位置是目的地g.drawImage(picture[9],100+30*y,100+30*x,this);else{ //原来位置是草地g.drawImage(picture[1],100+30*y,x*30+100,this);map[x][y]=1;}g.drawImage(picture[4],100+30*(manY+dy),(manX+dx)*30+100,this); //草地位置变箱子map[manX+dx][manY+dy]=4;Step(g);}if((map[manX][manY]==4||map[manX][manY]==3)&&map[manX+dx][manY+dy]==9){ //上面是箱子,箱子上面是目的地g.drawImage(picture[man],100+30*manY,manX*30+100,this); //人的朝向变化if(map[manX][manY]==3) //若是移动一大目的地的箱子,原箱子位置值为9map[manX][manY]=9;else map[manX][manY]=0;if(map[x][y]==9) //原来位置是目的地g.drawImage(picture[9],100+30*y,100+30*x,this);else { //原来位置是草地g.drawImage(picture[1],100+30*y,x*30+100,this);map[x][y]=1;}g.drawImage(picture[3],100+30*(manY+dy),(manX+dx)*30+100,this); //目的地位置变箱子map[manX+dx][manY+dy]=3;Step(g);}back_stack.push(next_picture);back_stack.push(origin_picture);back_stack.push(manY);back_stack.push(manX);back_stack.push(man);if(pass())JOptionPane.showMessageDialog(null, "恭喜你过关了!"); }//键盘事件public void keyPressed(KeyEvent e) {Graphics g=getGraphics();int dir ;switch(e.getKeyCode()){case KeyEvent.VK_UP:dir = Up;move(dir, g);break;case KeyEvent.VK_DOWN :dir = Down;move(dir, g);break;case KeyEvent.VK_LEFT:dir = Left;move(dir, g);break;case KeyEvent.VK_RIGHT:dir = Right;move(dir, g);break;case KeyEvent.VK_BACK_SPACE:back.doClick();break;case KeyEvent.VK_PAGE_DOWN :next.doClick();break;case KeyEvent.VK_PAGE_UP:previous.doClick();break;case KeyEvent.VK_R:restart.doClick();break;case KeyEvent.VK_Q :exit.doClick();break;}}public void keyReleased(KeyEvent e) {}public void keyTyped(KeyEvent e) {}//悔步后退public void step_back(Graphics g){g.setColor(Color.cyan);g.drawString("第"+step+"步",400,90);step--;g.setColor(Color.BLACK);g.drawString("第"+step+"步",400,90);}public void Back(){if(back_stack.size()==2)return;int dir=back_stack.pop() - 5; //获取上一步的方向int s = back_stack.pop();int t = back_stack.pop();int origin_picture = back_stack.pop();int netx_picture = back_stack.pop();int pre_dir = back_stElement();Graphics g=getGraphics();int dx = dir_x[dir];int dy = dir_y[dir];g.drawImage(picture[origin_picture],100+30*t,100+30*s,this);g.drawImage(picture[netx_picture],100+30*(t+dy),100+30*(s+dx),this);g.drawImage(picture[pre_dir],100+30*(t-dy),100+30*(s-dx),this);manX = s - dx;manY = t - dy;map[s][t] = origin_picture;map[s+dx][t+dy] = netx_picture;map[s-dx][t-dy] = pre_dir;step_back(g);}//按钮事件监听public void actionPerformed(ActionEvent e) {if(e.getSource()==restart){ //重新开始back_stack.clear(); //清空路径栈step=0;getmap(map);repaint();}else if(e.getSource()==exit) //退出游戏this.setVisible(false);else if(e.getSource()==next){ //下一关back_stack.clear();step=0;level++;if(level!=14){map_level=new map(level);getmap(map);current_level="第"+level+"关";repaint();}}else if(e.getSource()==previous){ //上一关back_stack.clear();step=0;level--;if(level!=0){map_level=new map(level);getmap(map);current_level="第"+level+"关";repaint();}}else if(e.getSource()==back)Back();requestFocus();}}3、开始界面package box;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Game extends JFrame implements ActionListener{ Image image[];ImageIcon icon;JButton start,tip,exit;Container c;public Game(){super("推箱子");c=getContentPane();FlowLayout layout=new FlowLayout();c.setLayout(layout);layout.setAlignment(FlowLayout.CENTER);icon=new ImageIcon("picture/game.png");image=new Image[]{icon.getImage()};start=new JButton("开始游戏");exit=new JButton("退出游戏");tip=new JButton("游戏说明");c.add(start);c.add(tip);c.add(exit);start.addActionListener(this);tip.addActionListener(this);exit.addActionListener(this);setSize(640,480);setVisible(true);}public void paint(Graphics g){super.paint(g);g.drawImage(image[0],10,65,this);}public static void main(String args[]){Game g=new Game();g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}Overridepublic void actionPerformed(ActionEvent e) {if(e.getSource()==exit)this.setVisible(false);if(e.getSource()==start){this.setVisible(false);pushbox c=new pushbox();}if(e.getSource()==tip){String t="本游戏分为13关,难度逐关递增,可以自己选关";JOptionPane.showMessageDialog(null, t);}}}。
推箱子c语言程序代码
推箱子c语言程序代码推箱子游戏是一款益智类游戏,在游戏中需要利用角色推动箱子到指定位置,并通过通关来获得胜利。
在这里,我将为您介绍一个简单的推箱子的C语言程序代码。
首先,我们需要定义一些变量和常量来描述游戏中的元素和状态:```c#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <windows.h>#define ROW 10 //地图行数#define COL 15 //地图列数//定义角色、箱子和目标的符号#define PLAYER 'P'#define BOX '$'#define TARGET '*'#define WALL '#'#define SPACE ' '接下来,我们需要定义一个结构体来表示地图上的每一个位置,包含如下元素:- `type`:表示该位置的类型,可以是空格、墙、箱子、目标或者角色。
- `hasBox`: 表示该位置上是否有箱子,是一个布尔值。
```cstruct Position {char type;bool hasBox;};```- `map`:表示地图,是一个二维数组,每个元素都是一个 `Position` 结构体。
- `playerRow` 和 `playerCol`:表示角色当前的位置。
- `boxRow` 和 `boxCol`:表示当前推动的箱子的位置。
```cint playerRow, playerCol, boxRow, boxCol;Position map[ROW][COL];```接下来,我们需要编写地图的初始化函数。
在这个函数中,我们先将地图上所有的位置都标记为空格,并将所有的箱子标记为 `false`。
然后,我们可以将墙、箱子、目标和角色放置到正确的位置上。
推箱子源小游戏代码
<!DOCTYPE html ><html xmlns="#" lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>推箱子</title><style type="text/css">*{margin:0px; padding:0px;}.bg{margin:0px;width:32px;height:32px;background-image: url(gameImg/01.jpg);float:left;} /*背景*/.box{margin:0px;width:32px;height:32px;background-image: url(gameImg/05.jpg);float:left;} /*箱子*/.box2{margin:0px;width:32px;height:32px;background-image: url(gameImg/06.jpg);float:left;} /*箱子2(放目标上)*/.wall{margin:0px;width:32px;height:32px;background-image: url(gameImg/03.jpg);float:left;} /*墙*/.goal{margin:0px;width:32px;height:32px;background-image: url(gameImg/02.jpg);float:left;} /*目标*/.role{margin:0px;width:32px;height:32px;background-image: url(gameImg/04.jpg);float:left;} /*角色*/</style><script type="text/javascript">var maxX = 10;var maxY = 10;var mapAry =[ //地图状态数组0:墙 1:路 2:目标[1,1,0,0,0,0,0,0,1,1],[0,0,0,1,1,2,1,0,0,0],[0,1,1,2,1,1,1,1,1,0],[0,1,0,1,0,2,1,0,1,0],[0,1,1,2,1,2,1,0,1,0],[0,0,0,1,1,1,1,1,1,0],[1,1,0,1,1,2,0,0,0,0],[1,1,0,1,1,1,0,1,1,1],[1,1,0,1,1,1,0,1,1,1],[1,1,0,0,0,0,0,1,1,1]];var imgAry = new Array(); //地图图片数组var role = new Point(4,2); //创建角色坐标var boxs = [new Point(2,2),new Point(2,4),new Point(2,5),new Point(2,6),new Point(6,4),new Point(5,5)]; //创建箱子坐标数组//定义坐标类function Point(x,y){this.x = x;this.y = y;}//创建单个方块function CreateDiv(obj,css){var d = document.createElement(obj); //创建一个DIV 元素d.className = css; //设置样式document.getElementById("main").appendChild(d);return d;}//初始化界面function Show(){//显示地图for (var i=0; i<maxX; i++) {imgAry[i] = new Array();for (var j=0; j<maxY; j++) {if(mapAry[i][j]==0){imgAry[i][j]= CreateDiv("div","wall");}else if(mapAry[i][j]==1){imgAry[i][j]= CreateDiv("div","bg");}else if(mapAry[i][j]==2){imgAry[i][j]= CreateDiv("div","goal");}}}//显示角色imgAry[role.x][role.y].className = "role";//显示箱子for (var i=0; i<boxs.length; i++) {imgAry[boxs[i].x][boxs[i].y].className = "box";}}//控制角色移动function RoleMove(k){var p = new Point(role.x,role.y); //保存角色移动前的坐标//改变角色坐标if(k==37){ //向左role.y--;}else if(k==38){ //向上role.x--;}else if(k==39){ //向右role.y++;}else if(k==40){ //向下role.x++;}//还原角色所在地图背景if(mapAry[role.x][role.y]==0){ //如果是墙,则不能移动role = p;}else{ //否则还原地图背景if(mapAry[p.x][p.y]==1){imgAry[p.x][p.y].className = "bg";}else if(mapAry[p.x][p.y]==2){imgAry[p.x][p.y].className = "goal";}//判断是否推到箱子for (var i=0; i<boxs.length; i++) {if(role.x==boxs[i].x &&role.y==boxs[i].y){//如果角色和箱子重叠,调用箱子移动方法,根据返回值来判断角色坐标是否还原if(BoxMove(role,k)==false){role = p;}break;}}}//显示角色imgAry[role.x][role.y].className = "role";}//箱子移动方法,p表示和角色重叠的箱子坐标,k表示键盘按钮function BoxMove(p,k){var bl = true; //箱子是否可以移动:true可以移动,false不能移动var tmpP; //保存箱子另一侧的坐标//获取箱子另一侧坐标if(k==37){ //向左tmpP = new Point(p.x,p.y-1);}else if(k==38){ //向上tmpP = new Point(p.x-1,p.y);}else if(k==39){ //向右tmpP = new Point(p.x,p.y+1);}else if(k==40){ //向下tmpP = new Point(p.x+1,p.y);}//判断该坐标是否是墙if(mapAry[tmpP.x][tmpP.y] == 0){bl = false;}//判断该坐标是否有箱子for (var i=0; i<boxs.length; i++) {if(tmpP.x==boxs[i].x && tmpP.y==boxs[i].y){bl = false;break;}}//如果箱子可以移动,则改变箱子坐标并在新坐标显示箱子if(bl == true){for (var i=0; i<boxs.length; i++) {if(boxs[i].x==p.x && boxs[i].y==p.y){boxs[i] = tmpP;break;}}imgAry[tmpP.x][tmpP.y].className = "box";//如果新坐标是目标,则显示成箱子2的样式if(mapAry[tmpP.x][tmpP.y] == 2){imgAry[tmpP.x][tmpP.y].className = "box2";}}return bl;}//按键控制移动function Move(){var k = event.keyCode;var count = 0; //与目标点重叠的箱子个数RoleMove(k);//判断游戏是否结束判断所有箱子的坐标点的状态,如果状态都是2则游戏结束for (var i=0; i<boxs.length; i++) {if(mapAry[boxs[i].x][boxs[i].y]==2){count++;}}if(count==6){alert("恭喜过关,游戏结束!");}}document.onkeydown = Move;</script></head><body onload="Show();"><div id="main" style="margin:50px auto; border:solid1px blue; width:320px; height:320px;"></div></body> </html>。
推箱子c++源代码
//typedef enum object////nclude<stdio.h>//#include<conio.h>//#include <windows.h>////接下来是几个宏定义,////以后用到那些被定义的数字或字符的候,直接用你想好的名称就可以了////这样只要改动宏定义,后面的数据都会变//对程序修改拓展等都很方便!//#define H 15//#define W 20//#define UP 'w'//#define DOWN 's'//#define LEFT 'a'//#define RIGHT 'd'////kbhit_remember是一个全局变量////用来记录用户的按键////因为许多函数都要知道用户按键是什么////所以我们把它设置为全局变量!//char kbhit_remember;////二维数组是用来储存数字的,不同的数字//代表的对象也不同,我们后边的绘图函数//就是根据这些数字来判断要输出什么样////的图形的//char map[H][W];////下面是枚举,像START等其实就代表着////一个数字,记忆这些数字代表了什么////显然不是一个程序员应该做的事////这些事就交给我们的枚举朋友了!////下面是游戏状态的枚举//typedef enum game//{// START,// RUNNING,// OVER//} gs;////下面是物体(对象)的枚举{// BOX_NULL_E,// BOX_SUCCESS_E,// PERSON_E,// OBSTACLE_E,// TARGET_E,// WALL_E,// NULL_E//} obj;////这下小伙伴们应该明白了吧!//typedef enum direction//{// UP_E,// DOWN_E,// LEFT_E,// RIGHT_E//} direction;//typedef struct//{// int y;// int x;// int sign;//} person_t;//typedef struct//{// int y;// int x;// int sign;//} box_t;//typedef struct//{// int y;// int x;// int sign;//} obstacle_t;//typedef struct//{// int y;// int x;// int sign;//} target_t;//person_t person;////这个数组里边都是box_t类型的元素!//box_t box[4];//obstacle_t obstacle[4];//target_t target[4];//void set_person(person_t * person, int x, int y) //{// person->x = x;// person->y = y;// //看看看!我们不需要知道人是哪个// //数字代表,只需要这样赋值就可以了// //sign,传说中的第三个成员!!// person->sign = PERSON_E;//}////箱子//void set_box(box_t box[], int n, int x, int y)//{// box[n].x = x;// box[n].y = y;// box[n].sign = BOX_NULL_E;//}////这个单词是。
推箱子游戏源代码
#include<stdio.h>#include<windows.h>#include<conio.h>int i=0;//记录所走步数//人的结构体struct person{int x;int y;}per={3,3};//定义光标移动函数void gotoxy(int x,int y){COORD coord={x,y};SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord); }//初始化图形界面int arr_set(char arr[9][9]){int i=0;int j=0;for(i=0;i<9;i++)for(j=0;j<9;j++)arr[i][j]='*';arr[1][1]='';arr[1][2]='';arr[1][3]='';arr[2][1]='';arr[2][2]='';arr[2][3]='';arr[3][1]='';arr[3][2]='';arr[3][3]='';arr[3][7]='';arr[4][3]='';arr[4][7]='';arr[5][3]='';arr[5][4]='';arr[5][5]='';arr[5][6]='';arr[5][7]='';arr[6][2]='';arr[6][3]='';arr[6][4]='';arr[6][6]='';arr[6][7]='';arr[7][2]='';arr[7][3]='';arr[7][4]='';arr[2][2]=2;arr[3][2]=2;arr[4][3]=2;arr[3][3]=12;arr[3][7]=4;arr[4][7]=4;arr[5][7]=4;return0;}//打印图形界面int prin(char arr[9][9]){int i=0;int j=0;for(i=0;i<9;i++){for(j=0;j<9;j++){if('*'==arr[i][j])printf("▓");if(2==arr[i][j])printf("□");if(12==arr[i][j])printf("♀");if(4==arr[i][j])printf("○");if(''==arr[i][j])printf("");}printf("\n");}//操作提示gotoxy(30,2);printf("WSAD分别操作上移、下移、左移、右移");gotoxy(30,4);printf("□代表箱子、♀代表人、○代表目的地");gotoxy(30,6);printf("把所有箱子推到目的地即完成任务");gotoxy(2*per.x,per.y);return0;}//向上移动int move_up(char arr[9][9]){//如果上面没有东西if(''==arr[per.y-1][per.x]||4==arr[per.y-1][per.x]){gotoxy(2*per.x,per.y);printf("");arr[per.y][per.x]='';gotoxy(2*per.x,per.y-1);printf("♀");gotoxy(2*per.x,per.y-1);arr[per.y-1][per.x]=12;per.y--;i++;}//如果上面是箱子并且箱子上面是空格则箱子和人同时上移光标回到人处if(2==arr[per.y-1][per.x]&&(''==arr[per.y-2][per.x]||4==arr[per.y-2][per.x])) {gotoxy(2*per.x,per.y);printf("");arr[per.y][per.x]='';gotoxy(2*per.x,per.y-1);printf("♀");arr[per.y-1][per.x]=12;gotoxy(2*per.x,per.y-2);printf("□");arr[per.y-2][per.x]=2;per.y--;gotoxy(2*per.x,per.y);i++;}if(arr[3][7]==''){gotoxy(14,3);printf("○");arr[3][7]=4;}if(arr[4][7]==''){gotoxy(14,4);printf("○");arr[4][7]=4;}if(arr[5][7]==''){gotoxy(14,5);printf("○");arr[5][7]=4;}return0;}//向下移动int move_down(char arr[9][9]){//如果下面没有东西if(''==arr[per.y+1][per.x]||4==arr[per.y+1][per.x]){gotoxy(2*per.x,per.y);printf("");arr[per.y][per.x]='';gotoxy(2*per.x,per.y+1);printf("♀");gotoxy(2*per.x,per.y+1);arr[per.y+1][per.x]=12;per.y++;i++;}//如果下面是箱子并且箱子下面是空格则箱子和人同时下移光标回到人处不用boxz if(2==arr[per.y+1][per.x]&&(''==arr[per.y+2][per.x]||''==arr[per.y+2][per.x])) {gotoxy(2*per.x,per.y);printf("");arr[per.y][per.x]='';gotoxy(2*per.x,per.y+1);printf("♀");arr[per.y+1][per.x]=12;gotoxy(2*per.x,per.y+2);printf("□");arr[per.y+2][per.x]=2;per.y++;gotoxy(2*per.x,per.y);i++;}if(arr[3][7]==''){gotoxy(14,3);printf("○");arr[3][7]=4;}if(arr[4][7]==''){gotoxy(14,4);printf("○");arr[4][7]=4;}if(arr[5][7]==''){gotoxy(14,5);printf("○");arr[5][7]=4;}return0;}//向左移动int move_left(char arr[9][9]){//如果左边没有东西if(''==arr[per.y][per.x-1]||4==arr[per.y][per.x-1]){gotoxy(2*per.x,per.y);printf("");arr[per.y][per.x]='';gotoxy(2*per.x-2,per.y);printf("♀");gotoxy(2*per.x-2,per.y);arr[per.y][per.x-1]=12;per.x--;i++;}//如果左面是箱子并且箱子左面是空格则箱子和人同时左移光标回到人处if(2==arr[per.y][per.x-1]&&(''==arr[per.y][per.x-2]||4==arr[per.y][per.x-2])) {gotoxy(2*per.x,per.y);printf("");arr[per.y][per.x]='';gotoxy(2*per.x-2,per.y);printf("♀");arr[per.y][per.x-1]=12;gotoxy(2*per.x-4,per.y);printf("□");arr[per.y][per.x-2]=2;per.x--;gotoxy(2*per.x,per.y);i++;}if(arr[3][7]==''){gotoxy(14,3);printf("○");arr[3][7]=4;}if(arr[4][7]==''){gotoxy(14,4);printf("○");arr[4][7]=4;}if(arr[5][7]==''){gotoxy(14,5);printf("○");arr[5][7]=4;}return0;}//向右移动int move_right(char arr[9][9]){//如果右边没有东西if(''==arr[per.y][per.x+1]||4==arr[per.y][per.x+1]) {gotoxy(2*per.x,per.y);printf("");arr[per.y][per.x]='';gotoxy(2*per.x+2,per.y);printf("♀");gotoxy(2*per.x+2,per.y);arr[per.y][per.x+1]=12;per.x++;i++;}//如果右面是箱子并且箱子右面是空格则箱子和人同时右移光标回到人处if(2==arr[per.y][per.x+1]&&(''==arr[per.y][per.x+2]||4==arr[per.y][per.x+2])) {gotoxy(2*per.x,per.y);printf("");arr[per.y][per.x]='';gotoxy(2*per.x+2,per.y);printf("♀");arr[per.y][per.x+1]=12;gotoxy(2*per.x+4,per.y);printf("□");arr[per.y][per.x+2]=2;per.x++;gotoxy(2*per.x,per.y);i++;}if(arr[3][7]==''){gotoxy(14,3);printf("○");arr[3][7]=4;}if(arr[4][7]==''){gotoxy(14,4);printf("○");arr[4][7]=4;}if(arr[5][7]==''){gotoxy(14,5);printf("○");arr[5][7]=4;}return0;}//主函数int main(void){char ch=0;char arr[9][9]={0};//构建界面arr_set(arr);prin(arr);//游戏运行while(1){ch=_getch();switch(ch){case'w':move_up(arr);break;case's':move_down(arr);break;case'a':move_left(arr);break;case'd':move_right(arr);break;}if(arr[3][7]==2&&arr[4][7]==2&&arr[5][7]==2){gotoxy(15,15);printf("恭喜过关!共%d步",i);goto_out;}}_out:return0;}。
c语言推箱子源代码及实验报告
《C语言推箱子源代码及实验报告》一、概述随着计算机科学技术的迅速发展,编程语言成为了计算机科学领域的重要组成部分。
C语言作为一种通用的编程语言,被广泛应用于系统软件,应用软件,驱动程序等领域。
本次实验将用C语言实现一个推箱子的小游戏,并编写实验报告。
二、实验目的1. 掌握C语言的基本语法和编程技巧;2. 学习如何利用C语言实现简单的游戏功能;3. 提高对C语言的理解和运用能力。
三、实验内容1. 设计并编写一个推箱子的小游戏;2. 实现游戏中的推箱子逻辑,包括箱子的移动、目标检测等功能;3. 编写实验报告,记录游戏设计思路、源代码分析和遇到的问题及解决方法。
四、实验步骤1. 设计游戏地图- 定义地图大小和布局- 设计箱子、目标点和玩家的初始位置2. 确定游戏规则- 确定箱子和玩家的移动规则- 确定胜利条件和失败条件3. 编写C语言代码- 定义地图的数据结构- 实现游戏初始化和终止的函数- 实现玩家和箱子的移动函数- 实现胜利和失败条件的检测函数4. 调试和测试- 对代码进行调试,确保游戏逻辑正确 - 测试不同情况下的游戏表现五、源代码展示以下展示部分游戏源代码:```#include <stdio.h>#include <stdlib.h>#include <conio.h>int m本人n() {// 游戏初始化initMap();drawMap();while(1) {// 监听玩家输入char input = getch();// 根据玩家输入移动玩家或者箱子 // 绘制移动后的地图// 判断胜利或失败条件// 提示玩家胜利或失败}return 0;}```六、实验结果分析通过本次实验,我们成功实现了一个简单的推箱子游戏。
在编写代码的过程中,我们熟练掌握了C语言的各种语法和逻辑结构,同时也加深了对C语言的理解和应用。
七、实验总结本次实验不仅让我们掌握了C语言的编程技巧,还提高了我们的逻辑思维能力和问题解决能力。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include <cstring>#include <cstdlib>#include <ctime>#include <iostream>#include <conio.h>using std::cout;using std::endl;#ifndef SOKOBAN_H_ //防止文件重复包含#define SOKOBAN_H_#include <queue>using std::queue;//每一步的数据类型struct node{int bx, by; //箱子的坐标int px, py; //人的坐标};//推箱子类class Sokoban{private:enum {L = 15, H = 7};char GameMap[H][L]; //地图int Pex, Pey; //人的位置int Boxx, Boxy; //箱子的位置int Succeed, Prove; //是否成功到目的地, 是否可玩性int dx[4], dy[4]; //方向数组protected:char Empty;char People;char Box;char Block;char Target;int dir; //记录按键方向node s, e;public:Sokoban(); //构建函数~Sokoban() {} //析构函数,即为inline//地图初始化函数void Initial();//箱子路劲验证函数,参数为箱子坐标(bx,by),人坐标(px,py) void Box_Bfs(int bx, int by, int px, int py);//人路劲验证函数,人所到的目的地(ex,ey)bool People_Bfs(int ex, int ey);//地图刷新函数void Show();//按键判断函数void Button();//箱子人移动函数void Move();//验证越界函数bool Check(int x, int y);};#endif</pre><pre class="cpp" name="code"> Sokoban::Sokoban() //构建函数即对变量初始化{dir = -1;Succeed = Prove = 0;memset(GameMap, '.', sizeof(GameMap));Empty = '.';People = 'P';Box = '#';Block = '*';Target = 'T';//方向依次为上右下左dx[0] = -1; dx[1] = 0; dx[2] = 1; dx[3] = 0;dy[0] = 0; dy[1] = 1; dy[2] = 0; dy[3] = -1;//随机种子,使程序每次运行时所产生的随机数不同srand(time(0));}//地图初始化函数void Sokoban::Initial(){int count = 0, x, y;//对地图中随机产生25个阻碍物while(count != 25){x = rand()%H;y = rand()%L;if(GameMap[x][y] == Empty){GameMap[x][y] = Block;count++;}}while(true) //随机产生人开始的位置{x = rand()%H;y = rand()%L;if(GameMap[x][y] == Empty){GameMap[x][y] = People;Pex = x;Pey = y;break;}}while(true) //随机产生箱子开始的位置{x = rand()%H;y = rand()%L;//不让箱子在地图的边界处if(GameMap[x][y] == Empty && x != 0 && y != 0&& x != H-1 && y != L-1){GameMap[x][y] = Box;Boxx = x;Boxy = y;break;}}while(true) //随机产生目标的位置{x = rand()%H;y = rand()%L;if(GameMap[x][y] == Empty){GameMap[x][y] = Target;break;}}//对游戏地图检查是否可将箱子推到目的地,即判断游戏可玩性Sokoban::Box_Bfs(Boxx, Boxy, Pex, Pey);//如游戏不可玩,即再随机产生地图if(!Prove){memset(GameMap, '.', sizeof(GameMap)); Sokoban::Initial();}elseSokoban::Show();}//箱子路劲验证函数//用BFS算法对箱子验证是否可到目的地void Sokoban::Box_Bfs(int bx, int by, int px, int py){queue<node>_Box; //创建箱子队列//visit对上一步走到下一步的记录,防止箱子走重复路劲//visit[i][j][z][k]表示箱子从点(i,j)到点(z,k)//visit[][][][]为0时表示为走过,1时表示已走过int visit[H][L][H][L];memset(visit, 0, sizeof(visit)); //visit数组初始化s.bx = bx; s.by = by; //将起始的箱子、人位置放入队列s.px = px; s.py = py;_Box.push(s);int pe_x, pe_y;while(!_Box.empty()) //队列为空时跳出{s = _Box.front();_Box.pop();if(GameMap[s.bx][s.by] == Target) //到达目的地{Prove = 1;break;}for(int i = 0; i < 4; i++){e.bx = s.bx + dx[i]; e.by = s.by + dy[i];switch(i) //人推箱子的位置{case 0: pe_x = s.bx + dx[2]; pe_y = s.by + dy[2]; break;case 1: pe_x = s.bx + dx[3]; pe_y = s.by + dy[3]; break;case 2: pe_x = s.bx + dx[0]; pe_y = s.by + dy[0]; break;case 3: pe_x = s.bx + dx[1]; pe_y = s.by + dy[1]; break;}//验证箱子和人的位置的合法性if(!Check(e.bx, e.by) || !Check(pe_x, pe_y)|| GameMap[e.bx][e.by] == Block || GameMap[pe_x][pe_y] == Block|| visit[s.bx][s.by][e.bx][e.by] )continue;//如人可推箱子即进入队列if(Sokoban::People_Bfs(pe_x, pe_y)){//保存人推箱子后的位置e.px = pe_x; e.py = pe_y;_Box.push(e);visit[s.bx][s.by][e.bx][e.by] = 1; //箱子路劲的标记}}}}//人路劲验证函数//用BFS算法对人验证是否可推箱子bool Sokoban::People_Bfs(int ex, int ey){queue<node>_People;node t, end;//visit数组对人的路劲进行标记,0为未走过,1为走过int visit[H][L];//visit数组初始化为0memset(visit, 0, sizeof(visit));t.px = s.px; t.py = s.py; //人初始位置进入队列_People.push(t);visit[t.px][t.py] = 1;while(!_People.empty()) //对立为空时跳出{t = _People.front();_People.pop();if(t.px == ex && t.py == ey) //人可到达(ex,ey)该点return 1;for(int i = 0; i < 4; i++){end.px = t.px + dx[i]; end.py = t.py + dy[i];//检查人的位置合法性if(!Check(end.px, end.py) || GameMap[end.px][end.py] == Block || GameMap[end.px][end.py] == Box || visit[end.px][end.py])continue;//进入队列_People.push(end);visit[end.px][end.py] = 1; //记录}}return 0;}//地图刷新函数void Sokoban::Show(){int i, j;while(true){//每半秒刷新一次地图clock_t s = clock();while(clock() - s < CLOCKS_PER_SEC/2);//先判断按键在移动Sokoban::Button();Sokoban::Move();system("cls");for(i = 0; i < H; i++){for(j = 0; j < L; j++)cout << GameMap[i][j];cout << endl;}cout << endl;cout << "\n**********************************" << endl; cout << "* 小小C++语言推箱子游戏*" << endl; cout << "* 游戏规则: *" << endl;cout << "* P: 人#: 箱子*" << endl;cout << "* *: 障碍物T: 目的地*" << endl; cout << "**********************************" << endl;cout << "* 每次游戏地图不一样*" << endl;cout << "* 人将箱子推到目的地即过关*" << endl;cout << "*所给地图,一定可过关,请慎重移箱子*" << endl;cout << "* 箱子无路可走时,机器不会提示*" << endl;cout << "**********************************" << endl;//箱子成功到达目的地if(Succeed){cout << "\n ^_^ >_<" << endl;cout << "恭喜过关成功! 再来一盘吧" << endl;getchar();break;}}}//按键判断函数void Sokoban::Button(){int key;if(kbhit() != 0) //检查当前是否有键盘输入,若有则返回一个非0值,否则返回0 {while(kbhit() != 0) //可能存在多个按键,要全部取完,以最后一个为主key = getch(); //将按键从控制台中取出并保存到key中switch(key){//上case 72: dir = 0;break;//右case 77: dir = 1;break;//下case 80: dir = 2;break;//左case 75: dir = 3;break;}}}//人推箱子移动函数void Sokoban::Move(){int x, y;//有按键时if(dir != -1){//人所推向的位置坐标x = Pex + dx[dir]; y = Pey + dy[dir];//人所推位置为空,即走向该位置if(Check(x, y) && GameMap[x][y] == '.'){GameMap[Pex][Pey] = '.'; //人的位置改变GameMap[x][y] = 'P';Pex = x; Pey = y;dir = -1; //按键记录为无即-1}else //人所推位置为箱子,即将箱子推向该方向的前面这点if(Check(x, y) && GameMap[x][y] == '#'&& Check(x+dx[dir], y+dy[dir])&& GameMap[ x+dx[dir] ][ y+dy[dir] ] == '.'){GameMap[Boxx][Boxy] = '.'; //箱子的位置改变GameMap[x+dx[dir] ][ y+dy[dir] ] = '#';Boxx = x + dx[dir]; Boxy = y + dy[dir];GameMap[Pex][Pey] = '.'; //人的位置改变GameMap[x][y] = 'P';Pex = x; Pey = y;dir = -1;}else //将箱子推向该方向的前面这点为目的地if(Check(x, y) && GameMap[x][y] == '#'&& Check(x+dx[dir], y+dy[dir])&& GameMap[ x+dx[dir] ][ y+dy[dir] ] == 'T') {GameMap[Boxx][Boxy] = '.'; //箱子的位置改变GameMap[x+dx[dir] ][ y+dy[dir] ] = '#';Boxx = x + dx[dir]; Boxy = y + dy[dir];GameMap[Pex][Pey] = '.'; //人的位置改变GameMap[x][y] = 'P';Pex = x; Pey = y;dir = -1;Succeed = 1; //记录成功到达目的地}}}//判断越界情况bool Sokoban::Check(int x, int y){if(x < 0 || x >= H || y < 0 || y >= L)return 0;elsereturn 1;}#include <iostream>//#include "Sokoban.h"using namespace std;int main(){Sokoban s;s.Initial();return 0;}。