c语言推箱子

Python014

c语言推箱子,第1张

//空: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=0i<nest_counti++)

{

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=0i<hi++)

{

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=0i<hi++)

{

*(s+i)=(int*)malloc(sizeof(int)*w)

}

char key[20]={'\0'}

char line[50]={'\0'}

int len

int j

for(i=0i<hi++)

{

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=0i<hi++)

{

for(j=0j<wj++)

{

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=0i<hi++)

{

printf("\n\n")

for(j=0j<wj++)

{

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=0i<hi++)

{

for(j=0j<wj++)

{

if(s[i][j]==3)

{

return false

}

}

}

for(i=0i<nest_counti++)

{

if(man.x==nest[i].x && man.y==nest[i].y)

{

return false

}

}

return true

}

#include "stdio.h"

#include <conio.h>

#include "Windows.h"

void gotoxy(int x, int y) {

system("cls")

COORD cr

cr.X = x

cr.Y = y

HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE)

SetConsoleCursorPosition(handle, cr)

printf("*")

}

int main() {

static int x = 10, y = 10

gotoxy(x, y)

char c

while(1) {

c = getch()

switch(c) {

case 'w':

case 'W':

y--

gotoxy(x, y)

break

case 'a':

case 'A':

x--

gotoxy(x, y)

break

case 's':

case 'S':

y++

gotoxy(x, y)

break

case 'd':

case 'D':

x++

gotoxy(x, y)

break

default:

break

}

}

}

我只是给你举个简单的例子,你应该能明白这个程序了

我可以给你一些C操作文件的建议

写入文件使用fwrite()函数,

比较文件中各个数据的最小值,你可以逐个读出,在比较

下面是一个例子

#include<stdio.h>

int main()

{

FILE *fp

int num[5]={10,20,50,40,60}

int i

int number

fp=fopen("Data","w") //打开Data文件,文件操作为写入

if(NULL==fp) //若打开失败,返回NULL

{

printf("Can't open the file\n")

return

}

for(i=0i<5i++) //将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)

}