c语言推箱子文件函数 推箱子代码c语言的数据结构
用C语言写推箱子游戏,怎样把每次的步数写进文件并比较的出最小步数
我可以给你一些C操作文件的建议
成都创新互联公司是一家专业提供西陵企业网站建设,专注与成都网站设计、成都网站制作、H5开发、小程序制作等业务。10年已为西陵众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。
写入文件使用fwrite()函数,
比较文件中各个数据的最小值,你可以逐个读出,在比较
下面是一个例子
#includestdio.h
int main()
{
FILE *fp;
int num[5]={10,20,50,40,60};
int i;
int number;
fp=fopen("Data","w"); //打开Data文件,文件操作为写入
if(NULL==fp) //若打开失败,返回NULL
{
printf("Can't open the file\n");
return;
}
for(i=0;i5;i++) //将num中的数据逐个写入
{
fwrite(num[i],sizeof(int),1,fp);
}
fclose(fp); //关闭文件
fp=fopen("Data","r"); //重新打开,文件操作为读
while(fread(number,sizeof(int),1,fp)!=0) //逐个读取文件中的数据,并赋予number
printf("\n%d",number); //输出每个数据
fclose(fp);
}
c语言推箱子
//空: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;inest_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(gatemap_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;ih;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_countgate)
{
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(w5 || h5 || nest_count1)
{
printf("w or h or box_nest data error!");
getch();
exit(0);
}
s=(int**)malloc(sizeof(int*)*h);
for(i=0;ih;i++)
{
*(s+i)=(int*)malloc(sizeof(int)*w);
}
char key[20]={'\0'};
char line[50]={'\0'};
int len;
int j;
for(i=0;ih;i++)
{
memset(line,'\0',50);
sprintf(key,"l%d",i+1);
GetPrivateProfileString(section,key,"\0",line,50,data_file);
len=strlen(line);
if(len0)
{
line[len++]=' ';
line[len]='\0';
}
GetIntFromLineString(line,strlen(line),i);
}
len=0;
for(i=0;ih;i++)
{
for(j=0;jw;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(beginend)
{
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(elen)
{
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;ih;i++)
{
printf("\n\n");
for(j=0;jw;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;ih;i++)
{
for(j=0;jw;j++)
{
if(s[i][j]==3)
{
return false;
}
}
}
for(i=0;inest_count;i++)
{
if(man.x==nest[i].x man.y==nest[i].y)
{
return false;
}
}
return true;
}
用C语言如何编写推箱子,给个思路呗
#includestdio.h
#include conio.h
#include windows.h
#define R 2//小人
#define B 4//箱子
#define O 3//目的地
#define W 43//墙
#define RO 5//箱子与人重合
#define BO 6//箱子与目的地重合
#define Size 13//数组大小
int main ()
{
printf ("推箱子(1~10关):\nwasd或↑↓←→控制方向。n下一关。r重新开始。\n请注意输入法是否为小写英文输入\n");
system ("pause");
MessageBox (NULL,"第一关"," ",MB_OK);//第一关
int move (char a[Size][Size]);
char a[Size][Size]={0};
int i,j;
void all0 (char a[Size][Size]);
NO1:
all0 (a);
for (i=3;i6;i++)
a[0][i]=W;
a[1][3]=W;
a[1][5]=W;
for (i=0;i4;i++)
a[2][i]=W;
a[2][5]=W;
a[3][0]=W;
for (i=0;i3;i++)
a[4][i]=W;
a[4][7]=W;
for (i=5;i8;i++)
{
a[3][i]=W;
a[i][2]=W;
a[i][4]=W;
a[5][i]=W;
}
a[7][3]=W;
a[1][4]=O;
a[3][1]=O;
a[4][6]=O;
a[6][3]=O;
a[3][2]=B;
a[3][4]=B;
a[4][4]=B;
a[5][3]=B;
a[4][3]=R;
if (move (a))
{
printf ("WIN\n");
system ("pause");
}
else
goto NO1;
MessageBox (NULL,"第二关"," ",MB_OK);//第二关
NO2:
all0 (a);
for (i=0;i5;i++)
{
a[i][0]=W;
a[0][i]=W;
a[i][4]=W;
}
a[5][2]=W;
a[4][2]=W;
for (i=4;i9;i++)
a[i][1]=W;
for (i=2;i6;i++)
a[8][i]=W;
a[6][5]=W;
for (i=5;i9;i++)
a[7][i]=W;
for (i=2;i8;i++)
a[i][8]=W;
a[2][7]=W;
for (i=2;i5;i++)
a[i][6]=W;
a[4][5]=W;
a[1][1]=R;
a[2][2]=B;
a[2][3]=B;
a[3][2]=B;
a[3][7]=O;
a[4][7]=O;
a[5][7]=O;
if (move (a))
{
printf ("WIN\n");
system ("pause");
}
else
goto NO2;
MessageBox (NULL,"第三关"," ",MB_OK);//第三关
NO3:
all0 (a);
for (i=1;i8;i++)
a[0][i]=W;
a[1][1]=W;
for (i=7;i10;i++)
a[1][i]=W;
for (i=0;i2;i++)
a[2][i]=W;
for (i=3;i6;i++)
a[2][i]=W;
a[2][9]=W;
a[3][0]=W;
a[3][9]=W;
a[4][0]=W;
a[4][4]=W;
a[4][9]=W;
a[4][8]=W;
a[5][0]=W;
a[5][1]=W;
a[5][4]=W;
a[5][8]=W;
for (i=1;i9;i++)
a[6][i]=W;
a[2][2]=B;
a[3][4]=B;
a[3][2]=R;
a[3][7]=B;
a[4][6]=B;
a[4][2]=O;
a[4][3]=O;
a[5][2]=O;
a[5][3]=O;
if (move (a))
{
printf ("WIN\n");
system ("pause");
}
else
goto NO3;
MessageBox (NULL,"第四关"," ",MB_OK);//第四关
NO4:
all0 (a);
for (i=1;i5;i++)
{a[0][i]=W;a[7][i]=W;}
a[1][1]=W;
a[1][4]=W;
a[2][1]=R;
a[2][2]=B;
a[2][4]=W;
a[3][1]=W;
a[3][2]=B;
a[3][4]=W;
a[3][5]=W;
a[4][1]=W;
for (i=1;i8;i++)
a[i][0]=W;
a[4][3]=B;
a[5][2]=B;
a[5][1]=O;
a[6][1]=O;
a[6][2]=O;
a[6][3]=BO;
a[6][4]=O;
for (i=1;i4;i++)
a[i][4]=W;
for (i=3;i8;i++)
a[i][5]=W;
if (move (a))
{
printf ("WIN\n");
system ("pause");
}
else
goto NO4;
MessageBox (NULL,"第五关"," ",MB_OK);//第五关
NO5:
all0 (a);
for (i=1;i6;i++)
a[0][i]=W;
for (i=1;i4;i++)
a[i][1]=W;
for (i=3;i8;i++)
a[i][0]=W;
for (i=1;i8;i++)
a[7][i]=W;
for (i=4;i7;i++)
{a[1][i]=W;a[i][1]=O;}
for (i=3;i7;i++)
a[i][7]=W;
a[2][6]=W;
a[3][6]=W;
a[1][2]=R;
a[2][3]=B;
a[3][2]=W;
a[4][2]=W;
a[3][4]=W;
a[4][4]=W;
a[5][5]=W;
a[5][2]=B;
a[6][5]=B;
if (move (a))
{
printf ("WIN\n");
system ("pause");
}
else
goto NO5;
MessageBox (NULL,"第六关"," ",MB_OK);//第六关
NO6:
all0 (a);
for (i=1;i8;i++)
a[i][0]=W;
for (i=7;i11;i++)
a[i][1]=W;
for (i=1;i4;i++)
{a[1][i]=W;a[i][9]=W;}
for (i=3;i10;i++)
a[0][i]=W;
for (i=3;i8;i++)
a[i][10]=W;
for (i=7;i11;i++)
a[i][12]=W;
a[7][11]=W;
for (i=2;i13;i++)
a[10][i]=W;
for (i=3;i6;i++)
{a[i][2]=W;a[8][i]=W;}
for (i=5;i8;i++)
{a[2][i]=W;a[i][8]=W;}
a[3][4]=W;
a[6][3]=W;
a[4][7]=W;
a[7][6]=W;
a[9][8]=W;
a[9][7]=W;
a[2][4]=O;
a[6][2]=O;
a[4][8]=O;
a[8][6]=O;
a[4][4]=B;
a[4][6]=B;
a[5][5]=BO;
a[6][4]=B;
a[6][6]=B;
a[8][11]=R;
if (move (a))
{
printf ("WIN\n");
system ("pause");
}
else
goto NO6;
MessageBox (NULL,"第七关"," ",MB_OK);//第七关
NO7:
all0 (a);
for (i=3;i10;i++)
a[0][i]=W;
for (i=1;i6;i++)
a[i][2]=W;
for (i=5;i8;i++)
a[i][0]=W;
a[5][1]=W;
a[1][3]=W;
a[6][0]=W;
for (i=0;i9;i++)
a[7][i]=W;
a[5][8]=W;
a[6][8]=W;
for (i=1;i6;i++)
a[i][9]=W;
a[1][6]=W;
a[2][6]=W;
a[4][5]=W;
a[4][6]=W;
a[5][6]=W;
a[3][3]=B;
a[3][5]=B;
a[3][7]=B;
a[4][4]=B;
a[5][4]=B;
a[1][8]=R;
for (i=1;i6;i++)
a[6][i]=O;
if (move (a))
{
printf ("WIN\n");
system ("pause");
}
else
goto NO7;
MessageBox (NULL,"第八关"," ",MB_OK);//第八关
NO8:
all0 (a);
for (i=3;i9;i++)
a[0][i]=W;
for (i=1;i4;i++)
a[1][i]=W;
a[2][1]=W;
a[1][8]=W;
a[2][8]=W;
for (i=2;i6;i++)
a[i][0]=W;
for (i=1;i6;i++)
a[5][i]=W;
for (i=2;i5;i++)
a[i][9]=W;
for (i=4;i7;i++)
a[i][8]=W;
for (i=5;i9;i++)
a[6][i]=W;
a[2][5]=W;
a[2][6]=W;
a[2][4]=B;
a[3][3]=B;
a[3][5]=B;
a[4][4]=B;
a[4][6]=B;
a[3][8]=R;
a[3][1]=O;
a[4][1]=O;
for (i=2;i5;i++)
a[i][2]=O;
if (move (a))
{
printf ("WIN\n");
system ("pause");
}
else
goto NO8;
MessageBox (NULL,"第九关"," ",MB_OK);//第九关
NO9:
all0 (a);
for (i=1;i10;i++)
{a[0][i]=W;a[8][i]=W;}
for (i=1;i6;i++)
{a[i][1]=W;a[i][9]=W;}
for (i=5;i9;i++)
{a[i][0]=W;a[i][10]=W;}
for (i=4;i7;i++)
{a[3][i]=W;a[4][i]=O;a[5][i]=O;}
a[1][4]=W;
a[1][5]=W;
a[4][3]=W;
a[5][3]=W;
a[4][7]=W;
a[5][7]=W;
a[7][6]=W;
a[2][5]=B;
a[3][2]=B;
a[3][8]=B;
a[6][2]=B;
a[6][5]=B;
a[6][8]=B;
a[7][8]=R;
if (move (a))
{
printf ("WIN\n");
system ("pause");
}
else
goto NO9;
MessageBox (NULL,"第十关"," ",MB_OK);//第十关
NO10:
all0 (a);
for (i=2;i8;i++)
a[0][i]=W;
a[1][2]=W;
for (i=0;i2;i++)
a[2][i]=W;
for (i=3;i6;i++)
a[i][0]=W;
for (i=1;i4;i++)
a[5][i]=W;
for (i=3;i7;i++)
a[6][i]=W;
for (i=1;i5;i++)
a[i][7]=W;
a[4][6]=W;
a[5][6]=W;
a[3][1]=R;
for (i=3;i6;i++)
{a[2][i]=B;a[4][i]=O;}
a[3][3]=B;
a[3][4]=O;
a[3][5]=O;
a[4][2]=B;
if (move (a))
{
printf ("WIN\n");
system ("pause");
}
else
goto NO10;
MessageBox(NULL,"恭喜您通关了。"," ",MB_OK);
return 0;
}
int move (char a[Size][Size])//控制小人的移动
{
int condition (char a [Size][Size],int x,int y,int b,int c,int d,int e);
void print ( char a [Size][Size]);
int x,y;
char c;
int i,j;
for (i=0;iSize;i++)//给小人定位
for (j=0;jSize;j++)
if (a[i][j]==R)
{ x=i;y=j; }
print (a);
loop:
c=getch();
if (c==0)//如果是键盘上下左右键用扫描码
c=getch();
switch(c)
{
case 'n':
case 'N': return 1;//下一关
case 'r':
case 'R': return 0;//返回0,重新开始
case 'w':
case 'W':
case 72: if (condition(a,x,y,x-1,y,x-2,y))x=x-1;break;//上
case 'a':
case 'A':
case 75: if (condition(a,x,y,x,y-1,x,y-2))y=y-1;break;//下
case 's':
case 'S':
case 80: if (condition(a,x,y,x+1,y,x+2,y))x=x+1;break;//左
case 'd':
case 'D':
case 77: if (condition(a,x,y,x,y+1,x,y+2))y=y+1;break;//右
default : goto loop;
}
print (a);
for (i=0;iSize;i++)//用是否存在箱子作胜利条件
for (j=0;jSize;j++)
if (a[i][j]==B)
goto loop;
return 1;
}
void print ( char a [Size][Size])//输出数组函数
{
int i,j;
system ("cls");//清屏函数
for (i=0;iSize;i++)
{
for (j=0;jSize;j++)
if (a[i][j]!=0)
printf ("%2c",a[i][j]);
else
printf (" ");
printf ("\n");
}
printf ("\n");
}
int condition (char a [Size][Size],int x,int y,int b,int c,int d,int e)//小人移动后的情况处理
{
if (a[b][c]!=W)
{
if (a[x][y]==Ra[b][c]==0)
{a[x][y]=0;a[b][c]=R;return 1;}
if (a[x][y]==ROa[b][c]==0)
{a[x][y]=O;a[b][c]=R;return 1;}
if ((a[x][y]==R||a[x][y]==RO)(a[b][c]==B||a[b][c]==BO)(a[d][e]==B||a[d][e]==BO||a[d][e]==W))
return 0;
if (a[x][y]==Ra[b][c]==O)
{a[x][y]=0;a[b][c]=RO;return 1;}
if (a[x][y]==ROa[b][c]==O)
{a[x][y]=O;a[b][c]=RO;return 1;}
if (a[x][y]==Ra[b][c]==Ba[d][e]==O)
{a[x][y]=0;a[b][c]=R;a[d][e]=BO;return 1;}
if (a[x][y]==Ra[b][c]==Ba[d][e]==0)
{a[x][y]=0;a[b][c]=R;a[d][e]=B;return 1;}
if (a[x][y]==Ra[b][c]==BOa[d][e]==0)
{a[x][y]=0;a[b][c]=RO;a[d][e]=B;return 1;}
if (a[x][y]==Ra[b][c]==BOa[d][e]==O)
{a[x][y]=0;a[b][c]=RO;a[d][e]=BO;return 1;}
if (a[x][y]==ROa[b][c]==Ba[d][e]==O)
{a[x][y]=O;a[b][c]=R;a[d][e]=BO;return 1;}
if (a[x][y]==ROa[b][c]==Ba[d][e]==0)
{a[x][y]=O;a[b][c]=R;a[d][e]=B;return 1;}
if (a[x][y]==ROa[b][c]==BOa[d][e]==O)
{a[x][y]=O;a[b][c]=RO;a[d][e]=BO;return 1;}
if (a[x][y]==ROa[b][c]==BOa[d][e]==0)
{a[x][y]=O;a[b][c]=RO;a[d][e]=B;return 1;}
}
return 0;
}
void all0 (char a[Size][Size])//数组成员归为0
{
int i,j;
for (i=0;iSize;i++)
for (j=0;jSize;j++)
a[i][j]=0;
}
网页题目:c语言推箱子文件函数 推箱子代码c语言的数据结构
文章转载:http://ybzwz.com/article/docssdg.html