c语言推箱子

#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
void move();
int map[9][9]={
{1,1,1,1,1,0,0,0,0},
{1,5,0,0,1,0,0,0,0},
{1,0,4,4,1,0,1,1,1},
{1,0,4,0,1,0,1,3,1},
{1,1,1,0,1,1,1,3,1},
{0,1,1,0,0,0,0,3,1},
{0,1,0,0,0,1,0,0,1},
{0,1,0,0,0,1,1,1,1},
{0,1,1,1,1,1,0,0,0},
};
void mapp();
int main()
{
mapp();
move();
return 0;
}
void mapp()//0 空格 1 墙 3 目的地 4 箱子 5 人
{
int i,j;
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
switch(map[i][j])
{
case 0:
printf(" ");break;
case 1:
printf("■");break;
case 3:
printf("×");break;
case 4:
printf("卍");break;
case 5:
printf("♀");break;
}
}
printf("\n");
}
}
void move()
{
int m,n;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(map[i][j]==5||map[i][j]==8)
{
m=i;
n=j;
}
while(1)
{
getch();getch();
switch(getch())
{
case 72:
case 'W':
case 'w':
if(map[m-1][n]==0||map[m-1][n]==3)
{
map[m][n]-=5;
map[m-1][n]+=5;
}
else if(map[m-1][n]==4)
{
if (map[m-2][n]==0||map[m-2][n]==3)
{ map[m][n]-=5;
map[m-1][n]+=1;
map[m-2][n]+=4;}
}
break;
case 'D':
case 'd':
if(map[m][n+1]==0||map[m][n+1]==3)
{
map[m][n]-=5;
map[m][n+1]+=5;
}
else if(map[m][n+1]==4)
{if (map[m][n+2]==0||map[m][n+2]==3)
{
map[m][n]-=5;
map[m][n+1]+=1;
map[m][n+2]+=4;
}}
break;
case 'A':
case 'a':
if(map[m][n-1]==0||map[m][n-1]==3)
{
map[m][n]-=5;
map[m][n-1]+=5;
}
else if(map[m][n-1]==4)
{if (map[m][n+2]==0||map[m][n-2]==3)
{
map[m][n]-=5;
map[m][n-1]+=1;
map[m][n-2]+=4;
}}
break;
case 80:
case 'S':
case 's':
if(map[m+1][n]==0||map[m+1][n]==3)
{
map[m][n]-=5;
map[m+1][n]+=5;
}
else if(map[m+1][n]==4)
{if (map[m+2][n]==0||map[m+2][n]==3)
{
map[m][n]-=5;
map[m][n+1]+=1;
map[m][n+2]+=4;
}}
break;
system("cls");
}
}
}
}
}这个程序按键小人动不了怎么改啊,新人求大神指教

//空:0  墙:1  箱子:3   巢:4   箱子与巢重合:5
[MAPCOUNT]
map_count=8
[MAP1]
w=8
h=8
nest_count=4
l1=0 0 0 1 1 1 0 0
l2=0 0 0 1 3 1 0 0
l3=1 1 1 1 0 1 0 0
l4=1 3 2 0 2 1 1 1
l5=1 1 1 4 2 0 3 1
l6=0 0 1 2 1 1 1 1
l7=0 0 1 3 1 0 0 0
l8=0 0 1 1 1 0 0 0
[MAP2]
w=9
h=9
nest_count=3
l1=1 1 1 1 1 0 0 0 0
l2=1 4 0 0 1 0 0 0 0
l3=1 0 2 2 1 0 1 1 1
l4=1 0 2 0 1 0 1 3 1
l5=1 1 1 0 1 1 1 3 1
l6=0 1 1 0 0 0 0 3 1
l7=0 1 0 0 0 1 0 0 1
l8=0 1 0 0 0 1 1 1 1
l9=0 1 1 1 1 1 0 0 0
[MAP3]
w=10
h=7
nest_count=4
l1=0 1 1 1 1 1 1 1 0 0
l2=0 1 0 0 0 0 0 1 1 1
l3=1 1 2 1 1 1 0 0 0 1
l4=1 0 4 0 2 0 0 2 0 1
l5=1 0 3 3 1 0 2 0 1 1
l6=1 1 3 3 1 0 0 0 1 0
l7=0 1 1 1 1 1 1 1 1 0
[MAP4]
w=6
h=8
nest_count=5
l1=0 1 1 1 1 0
l2=1 1 0 0 1 0
l3=1 4 2 0 1 0
l4=1 1 2 0 1 1
l5=1 1 0 2 0 1
l6=1 3 2 0 0 1
l7=1 3 3 5 3 1
l8=1 1 1 1 1 1
//以上为地图数据文件,保存为boxdata.dat文件
//空:0  墙:1  箱子:3   巢:4   箱子与巢重合:5
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <windows.h>
#include <string.h>

typedef struct
{
int x;
int y;
} PT;

int** s;
PT man;
PT* nest=NULL;
PT prev;

int nest_count=0;
int map_count=0;
int gate=1;
int w,h;
char work_dir[100]={'\0'};
char data_file[100]={'\0'};

void GetDataFromFile();
void GetIntFromLineString(char* ch, int len, int i);
void Draw();
bool is_Success();

int main()
{
printf("Loading...");
CONSOLE_CURSOR_INFO cci;
cci.bVisible = FALSE;
cci.dwSize = sizeof(cci);
HANDLE handle = 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't find map data file !");
getch();
exit(0);
}

while(1)
{
GetDataFromFile();
int sel=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;
}
}
else if(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;
}
}
else if(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;
}
}
else if(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;
}
}
else if(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;
}
}
else if(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;
}
}
else if(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;
}
}
else if(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;
}
}
int i;
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("\n\nmap is end!");
fflush(stdin);
getch();
exit(0);
}
break;
}
}
else if(sel=='q' || sel=='Q')
{
exit(0);
}
else if(sel=='r' || sel=='R')
{
break;
}
}
}
return 0;
}

void GetDataFromFile()
{
int i;
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("gate finish!");
getch();
exit(0);
}
char section[20]={'\0'};
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("w or h or box_nest data error!");
getch();
exit(0);
}
s=(int**)malloc(sizeof(int*)*h);
for(i=0;i<h;i++)
{
*(s+i)=(int*)malloc(sizeof(int)*w);
}

char key[20]={'\0'};
char line[50]={'\0'};
int len;
int j;
for(i=0;i<h;i++)
{
memset(line,'\0',50);
sprintf(key,"l%d",i+1);
GetPrivateProfileString(section,key,"\0",line,50,data_file);
len=strlen(line);
if(len>0)
{
line[len++]=' ';
line[len]='\0';
}
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++;
}
else if(s[i][j]==5)
{
nest[len].y=i;
nest[len].x=j;
len++;
s[i][j]=2;
}
}
}
}

void strmyncpy(char* source, char* target, int begin, int end)
{
int i=0;
while(1)
{
if(source[begin]!=' ')
{
target[i]=source[begin];
}
i++;
begin++;
if(begin>end)
{
target[i]='\0';
break;
}
}
}

void GetIntFromLineString(char* ch, int len, int i)
{
int j=0;
char c[5]={'\0'};
int b=0,e=0;
while(e<len)
{
if(ch[e]==' ')
{
memset(c,'\0',5);
strmyncpy(ch,c,b,e);
b=e+1;
e++;
s[i][j++]=atoi(c);
}
e++;
}
}

void Draw()
{
int i,j,k;
bool flag=false;
system("cls");
printf("\n\n");
for(i=0;i<h;i++)
{
printf("\n\n");
for(j=0;j<w;j++)
{
if(s[i][j]==0)
{
printf("    ");
}
else if(s[i][j]==1)
{
printf(" ■ ");
}
else if(s[i][j]==2)
{
printf(" ★ ");
}
else if(s[i][j]==3)
{
printf(" ☆ ");
}
else if(s[i][j]==4)
{
printf(" ◎ ");
man.x=j;
man.y=i;
}
}
}
}

bool is_Success()
{
int i,j;
for(i=0;i<h;i++)
{
for(j=0;j<w;j++)
{
if(s[i][j]==3)
{
return false;
}
}
}
for(i=0;i<nest_count;i++)
{
if(man.x==nest[i].x && man.y==nest[i].y)
{
return false;
}
}
return true;
}
温馨提示:答案为网友推荐,仅供参考
相似回答