c语言推箱子

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

}

/*TC环境下编的*/

#include <dos.h>

#include <stdio.h>

#include <ctype.h>

#include <conio.h>

#include <bios.h>

#include <alloc.h>

typedef struct winer

{

int x,y

struct winer *p

}winer

char status [20][20]

char far *printScreen=(char far* )0xB8000000

void putoutChar(int y,int x,char ch,char fc,char bc)

void printWall(int x, int y)

void printBox(int x, int y)

void printBoxDes(int x, int y)

void printDestination(int x, int y)

void printDestination1(int x,int y,winer **win,winer **pw)

void printMan(int x, int y)

void init()

winer *initStep1()

winer *initStep2()

winer *initStep3()

winer *initStep4()

void moveBoxSpacetoSpace(int x ,int y, char a)

void moveBoxDestoSpace(int x ,int y, char a)

void moveBoxSpacetoDes(int x, int y, char a)

void moveBoxDestoDes(int x, int y, char a)

int judge(int x, int y)

void move(int x, int y, char a)

void reset(int i)

void putoutChar(int y,int x,char ch,char fc,char bc)

{

printScreen[(x*160)+(y<<1)+0]=ch

printScreen[(x*160)+(y<<1)+1]=(bc*16)+fc

}

void printWall(int x,int y)

{

渗雀搜putoutChar(y-1,x-1,219,GREEN,BLACK)

status[x][y]='w'

}

void printBox(int x,int y)

{

putoutChar(y-1,x-1,10,WHITE,BLACK)

status[x][y]='b'

}

void printDestination1(int x,int y,winer **win,winer **pw)

{

winer *qw

putoutChar(y-1,x-1,003,YELLOW,BLACK)

岁搜status[x][y]='丛历m'

if(*win==NULL)

{

*win=*pw=qw=(winer* )malloc(sizeof(winer))

(*pw)->x=x

(*pw)->y=y

(*pw)->p=NULL

}

else

{

qw=(winer* )malloc(sizeof(winer))

qw->x=x

qw->y=y

(*pw)->p=qw

(*pw)=qwqw->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=0i<20i++)

for(j=0j<20j++)

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=5y<=9y++)

printWall(x+4,y+10)

for(y=5,x=2x<=5x++)

printWall(x+4,y+10)

for(y=9,x=2x<=5x++)

printWall(x+4,y+10)

for(y=1,x=3x<=8x++)

printWall(x+4,y+10)

for(x=3,y=3x<=5x++)

printWall(x+4,y+10)

for(x=5,y=8x<=9x++)

printWall(x+4,y+10)

for(x=7,y=4x<=9x++)

printWall(x+4,y+10)

for(x=9,y=5y<=7y++)

printWall(x+4,y+10)

for(x=8,y=2y<=3y++)

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=4y<=7y++)

printWall(x+4,y+10)

for(x=2,y=2y<=4y++)

printWall(x+4,y+10)

for(x=2,y=7x<=4x++)

printWall(x+4,y+10)

for(x=4,y=1x<=8x++)

printWall(x+4,y+10)

for(x=8,y=2y<=8y++)

printWall(x+4,y+10)

for(x=4,y=8x<=8x++)

printWall(x+4,y+10)

for(x=4,y=6x<=5x++)

printWall(x+4,y+10)

for(x=3,y=2x<=4x++)

printWall(x+4,y+10)

for(x=4,y=4x<=5x++)

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=2y<=8y++)

printWall(x+4,y+10)

for(x=2,y=2x<=4x++)

printWall(x+4,y+10)

for(x=4,y=1y<=3y++)

printWall(x+4,y+10)

for(x=5,y=1x<=8x++)

printWall(x+4,y+10)

for(x=8,y=2y<=5y++)

printWall(x+4,y+10)

for(x=5,y=5x<=7x++)

printWall(x+4,y+10)

for(x=7,y=6y<=9y++)

printWall(x+4,y+10)

for(x=3,y=9x<=6x++)

printWall(x+4,y+10)

for(x=3,y=6y<=8y++)

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=1y<=6y++)

printWall(x+4,y+10)

for(x=2,y=7y<=8y++)

printWall(x+4,y+10)

for(x=2,y=1x<=7x++)

printWall(x+4,y+10)

for(x=7,y=2y<=4y++)

printWall(x+4,y+10)

for(x=6,y=4y<=9y++)

printWall(x+4,y+10)

for(x=3,y=9x<=5x++)

printWall(x+4,y+10)

for(x=3,y=3y<=4y++)

printWall(x+4,y+10)

printWall(3+4,8+10)

printBox(3+4,5+10)

printBox(4+4,4+10)

printBox(4+4,6+10)

printBox(5+4,5+10)

printBox(5+4,3+10)

printDestination1(3+4,7+10,&win,&pw)

printDestination1(4+4,7+10,&win,&pw)

printDestination1(5+4,7+10,&win,&pw)

printDestination1(4+4,8+10,&win,&pw)

printDestination1(5+4,8+10,&win,&pw)

printMan(2+4,2+10)

return win

}

void moveBoxSpacetoSpace(int x,int y,char a)

{

switch(a)

{

case 'u':

status[x-1][y]=0

printf(" ")

printBox(x-2,y)

printMan(x-1,y)

status[x-2][y]='b'

break

case 'd':

status[x+1][y]=0

printf(" ")

printBox(x+2,y)

printMan(x+1,y)

status[x+2][y]='b'

break

case 'l':

status[x][y-1]=0

printf(" ")

printBox(x,y-2)

printMan(x,y-1)

status[x][y-2]='b'

break

case 'r':

status[x][y+1]=0

printf(" ")

printBox(x,y+2)

printMan(x,y+1)

status[x][y+2]='b'

break

default:

break

}

}

void moveBoxDestoSpace(int x,int y,char a)

{

switch(a)

{

case 'u':

status[x-1][y]='m'

printf(" ")

printBox(x-2,y)

printMan(x-1,y)

status[x-2][y]='b'

break

case 'd':

status[x+1][y]='m'

printf(" ")

printBox(x+2,y)

printMan(x+1,y)

status[x+2][y]='b'

break

case 'l':

status[x][y-1]='m'

printf(" ")

printBox(x,y-2)

printMan(x,y-1)

status[x][y-2]='b'

break

case 'r':

status[x][y+1]='m'

printf(" ")

printBox(x,y+2)

printMan(x,y+1)

status[x][y+2]='b'

break

default:

break

}

}

void moveBoxSpacetoDes(int x,int y,char a)

{

switch(a)

{

case 'u':

status[x-1][y]=0

printf(" ")

printBoxDes(x-2,y)

printMan(x-1,y)

status[x-2][y]='i'

break

case 'd':

status[x+1][y]=0

printf(" ")

printBoxDes(x+2,y)

printMan(x+1,y)

status[x+2][y]='i'

break

case 'l':

status[x][y-1]=0

printf(" ")

printBoxDes(x,y-2)

printMan(x,y-1)

status[x][y-2]='i'

break

case 'r':

status[x][y+1]=0

printf(" ")

printBoxDes(x,y+2)

printMan(x,y+1)

status[x][y+2]='i'

break

default:

break

}

}

void moveBoxDestoDes(int x,int y,char a)

{

switch(a)

{

case 'u':

status[x-1][y]='m'

printf(" ")

printBoxDes(x-2,y)

printMan(x-1,y)

status[x-2][y]='i'

break

case 'd':

status[x+1][y]='m'

printf(" ")

printBoxDes(x+2,y)

printMan(x+1,y)

status[x+2][y]='i'

break

case 'l':

status[x][y-1]='m'

printf(" ")

printBoxDes(x,y-2)

printMan(x,y-1)

status[x][y-2]='i'

break

case 'r':

status[x][y+1]='m'

printf(" ")

printBoxDes(x,y+2)

printMan(x,y+1)

status[x][y+2]='i'

break

default:

break

}

}

int judge(int x,int y)

{

int i

switch(status[x][y])

{

case 0:

i=1

break

case 'w':

i=0

break

case 'b':

i=2

break

case 'i':

i=4

break

case 'm':

i=3

break

default:

break

}

return i

}

void move(int x,int y,char a)

{

switch(a)

{

case 'u':

if(!judge(x-1,y))

{

gotoxy(y,x)

break

}

else if(judge(x-1,y)==1||judge(x-1,y)==3)

{

if(judge(x,y)==3)

{

printDestination(x,y)

printMan(x-1,y)

break

}

else

{

printf(" ")

printMan(x-1,y)

break

}

}

else if(judge(x-1,y)==2)

{

if(judge(x-2,y)==1)

{

moveBoxSpacetoSpace(x,y,'u')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y,x-1)

}

else if(judge(x-2,y)==3)

{

moveBoxSpacetoDes(x,y,'u')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y,x-1)

}

else

gotoxy(y,x)

break

}

else if(judge(x-1,y)==4)

{

if(judge(x-2,y)==1)

{

moveBoxDestoSpace(x,y,'u')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y,x-1)

}

else if(judge(x-2,y)==3)

{

moveBoxDestoDes(x,y,'u')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y,x-1)

}

else

gotoxy(y,x)

break

}

case 'd':

if(!judge(x+1,y))

{

gotoxy(y,x)

break

}

else if(judge(x+1,y)==1||judge(x+1,y)==3)

{

if(judge(x,y)==3)

{

printDestination(x,y)

printMan(x+1,y)

break

}

else

{

printf(" ")

printMan(x+1,y)

break

}

}

else if(judge(x+1,y)==2)

{

if(judge(x+2,y)==1)

{

moveBoxSpacetoSpace(x,y,'d')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y,x+1)

}

else if(judge(x+2,y)==3)

{

moveBoxSpacetoDes(x,y,'d')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y,x+1)

}

else

gotoxy(y,x)

break

}

else if(judge(x+1,y)==4)

{

if(judge(x+2,y)==1)

{

moveBoxDestoSpace(x,y,'d')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y,x+1)

}

else if(judge(x+2,y)==3)

{

moveBoxDestoDes(x,y,'d')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y,x+1)

}

else

gotoxy(y,x)

break

}

case 'l':

if(!judge(x,y-1))

{

gotoxy(y,x)

break

}

else if(judge(x,y-1)==1||judge(x,y-1)==3)

{

if(judge(x,y)==3)

{

printDestination(x,y)

printMan(x,y-1)

break

}

else

{

printf(" ")

printMan(x,y-1)

break

}

}

else if(judge(x,y-1)==2)

{

if(judge(x,y-2)==1)

{

moveBoxSpacetoSpace(x,y,'l')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y-1,x)

}

else if(judge(x,y-2)==3)

{

moveBoxSpacetoDes(x,y,'l')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y-1,x)

}

else

gotoxy(y,x)

break

}

else if(judge(x,y-1)==4)

{

if(judge(x,y-2)==1)

{

moveBoxDestoSpace(x,y,'l')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y-1,x)

}

else if(judge(x,y-2)==3)

{

moveBoxDestoDes(x,y,'l')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y-1,x)

}

else

gotoxy(y,x)

break

}

case 'r':

if(!judge(x,y+1))

{

gotoxy(y,x)

break

}

else if(judge(x,y+1)==1||judge(x,y+1)==3)

{

if(judge(x,y)==3)

{

printDestination(x,y)

printMan(x,y+1)

break

}

else

{

printf(" ")

printMan(x,y+1)

break

}

}

else if(judge(x,y+1)==2)

{

if(judge(x,y+2)==1)

{

moveBoxSpacetoSpace(x,y,'r')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y+1,x)

}

else if(judge(x,y+2)==3)

{

moveBoxSpacetoDes(x,y,'r')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y+1,x)

}

else

gotoxy(y,x)

break

}

else if(judge(x,y+1)==4)

{

if(judge(x,y+2)==1)

{

moveBoxDestoSpace(x,y,'r')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y+1,x)

}

else if(judge(x,y+2)==3)

{

moveBoxDestoDes(x,y,'r')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y+1,x)

}

else

gotoxy(y,x)

break

}

default:

break

}

}

void reset(int i)

{

switch(i)

{

case 0:

init()

initStep1()

break

case 1:

init()

initStep2()

break

case 2:

init()

initStep3()

break

case 3:

init()

initStep4()

break

default:

break

}

}

void main()

{

int key

int x

int y

int s

int i=0

winer *win

winer *pw

_AL=3

_AH=0

geninterrupt(0x10)

init()

win=initStep1()

do{

_AH=3

geninterrupt(0x10)

x=_DH+1

y=_DL+1

while(bioskey(1)==0)

key=bioskey(0)

switch(key)

{

case 0x4800:

move(x,y,'u')

break

case 0x5000:

move(x,y,'d')

break

case 0x4b00:

move(x,y,'l')

break

case 0x4d00:

move(x,y,'r')

break

case 0x3920:

reset(i)

break

default:

break

}

s=0

pw=win

while(pw)

{

if(status[pw->x][pw->y]=='m')

s++

pw=pw->p

}

if(s==0)

{

free(win)

gotoxy(25,2)

printf("congratulate! You have done this step!")

getch()

i++

switch(i)

{

case 1:

init()

win=initStep2()

break

case 2:

init()

win=initStep3()

break

case 3:

init()

win=initStep4()

break

case 4:

gotoxy(15,21)

printf("Congratulation! \n")

gotoxy(15,23)

printf("You have done all the steps, Welcome to play again!")

key=0x011b

getch()

break

default:

break

}

}

}while(key!=0x011b)

_AL=3

_AH=0

geninterrupt(0x10)

}

给你一个TC版本的吧郑做兆宴,VC版本的CSDN上可以下载:

#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)=qwqw->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=0i<20i++)

for(j=0j<20j++)

status[i][j]=0

_AL=3

_AH=0

geninterrupt(0x10)

gotoxy(41,4)

printf(" --------- up")

gotoxy(41,6)

printf(" --------- down")

gotoxy(41,8)

printf(" --------- left")

gotoxy(41,10)

printf(" --------- right")

gotoxy(40,12)

printf("Space ----- reset")

gotoxy(40,14)

printf("Esc ------- quit")

gotoxy(18,24)

printf("CopyRight: 2008 LuoFuxing")

gotoxy(40,4)

_CX=01

_AH=0xa

_AL=24

geninterrupt(0x10)

gotoxy(40,6)

_CX=01

_AH=0xa

_AL=25

geninterrupt(0x10)

gotoxy(40,8)

_CX=01

_AH=0xa

_AL=27

geninterrupt(0x10)

gotoxy(40,10)

_CX=01

_AH=0xa

_AL=26

geninterrupt(0x10)

}

winer *initStep1()

{

int x

int y

winer *win=NULL

winer *pw

gotoxy(25,2)

printf("Level 1")

for(x=1,y=5y<=9y++)

printWall(x+4,y+10)

for(y=5,x=2x<=5x++)

printWall(x+4,y+10)

for(y=9,x=2x<=5x++)

printWall(x+4,y+10)

for(y=1,x=3x<=8x++)

printWall(x+4,y+10)

for(x=3,y=3x<=5x++)

printWall(x+4,y+10)

for(x=5,y=8x<=9x++)

printWall(x+4,y+10)

for(x=7,y=4x<=9x++)

printWall(x+4,y+10)

for(x=9,y=5y<=7y++)

printWall(x+4,y+10)

for(x=8,y=2y<=3y++)

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

gotoxy(25,2)

printf("Level 2")

for(x=1,y=4y<=7y++)

printWall(x+4,y+10)

for(x=2,y=2y<=4y++)

printWall(x+4,y+10)

for(x=2,y=7x<=4x++)

printWall(x+4,y+10)

for(x=4,y=1x<=8x++)

printWall(x+4,y+10)

for(x=8,y=2y<=8y++)

printWall(x+4,y+10)

for(x=4,y=8x<=8x++)

printWall(x+4,y+10)

for(x=4,y=6x<=5x++)

printWall(x+4,y+10)

for(x=3,y=2x<=4x++)

printWall(x+4,y+10)

for(x=4,y=4x<=5x++)

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

gotoxy(25,2)

printf("Level 3")

for(x=1,y=2y<=8y++)

printWall(x+4,y+10)

for(x=2,y=2x<=4x++)

printWall(x+4,y+10)

for(x=4,y=1y<=3y++)

printWall(x+4,y+10)

for(x=5,y=1x<=8x++)

printWall(x+4,y+10)

for(x=8,y=2y<=5y++)

printWall(x+4,y+10)

for(x=5,y=5x<=7x++)

printWall(x+4,y+10)

for(x=7,y=6y<=9y++)

printWall(x+4,y+10)

for(x=3,y=9x<=6x++)

printWall(x+4,y+10)

for(x=3,y=6y<=8y++)

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

gotoxy(23,2)

printf("The Last Level")

for(x=1,y=1y<=6y++)

printWall(x+4,y+10)

for(x=2,y=7y<=8y++)

printWall(x+4,y+10)

for(x=2,y=1x<=7x++)

printWall(x+4,y+10)

for(x=7,y=2y<=4y++)

printWall(x+4,y+10)

for(x=6,y=4y<=9y++)

printWall(x+4,y+10)

for(x=3,y=9x<=5x++)

printWall(x+4,y+10)

for(x=3,y=3y<=4y++)

printWall(x+4,y+10)

printWall(3+4,8+10)

printBox(3+4,5+10)

printBox(4+4,4+10)

printBox(4+4,6+10)

printBox(5+4,5+10)

printBox(5+4,3+10)

printDestination1(3+4,7+10,&win,&pw)

printDestination1(4+4,7+10,&win,&pw)

printDestination1(5+4,7+10,&win,&pw)

printDestination1(4+4,8+10,&win,&pw)

printDestination1(5+4,8+10,&win,&pw)

printMan(2+4,2+10)

return win

}

void moveBoxSpacetoSpace(int x,int y,char a)

{

switch(a)

{

case 'u':

status[x-1][y]=0

printf(" ")

printBox(x-2,y)

printMan(x-1,y)

status[x-2][y]='b'

break

case 'd':

status[x+1][y]=0

printf(" ")

printBox(x+2,y)

printMan(x+1,y)

status[x+2][y]='b'

break

case 'l':

status[x][y-1]=0

printf(" ")

printBox(x,y-2)

printMan(x,y-1)

status[x][y-2]='b'

break

case 'r':

status[x][y+1]=0

printf(" ")

printBox(x,y+2)

printMan(x,y+1)

status[x][y+2]='b'

break

default:

break

}

}

void moveBoxDestoSpace(int x,int y,char a)

{

switch(a)

{

case 'u':

status[x-1][y]='m'

printf(" ")

printBox(x-2,y)

printMan(x-1,y)

status[x-2][y]='b'

break

case 'd':

status[x+1][y]='m'

printf(" ")

printBox(x+2,y)

printMan(x+1,y)

status[x+2][y]='b'

break

case 'l':

status[x][y-1]='m'

printf(" ")

printBox(x,y-2)

printMan(x,y-1)

status[x][y-2]='b'

break

case 'r':

status[x][y+1]='m'

printf(" ")

printBox(x,y+2)

printMan(x,y+1)

status[x][y+2]='b'

break

default:

break

}

}

void moveBoxSpacetoDes(int x,int y,char a)

{

switch(a)

{

case 'u':

status[x-1][y]=0

printf(" ")

printBoxDes(x-2,y)

printMan(x-1,y)

status[x-2][y]='i'

break

case 'd':

status[x+1][y]=0

printf(" ")

printBoxDes(x+2,y)

printMan(x+1,y)

status[x+2][y]='i'

break

case 'l':

status[x][y-1]=0

printf(" ")

printBoxDes(x,y-2)

printMan(x,y-1)

status[x][y-2]='i'

break

case 'r':

status[x][y+1]=0

printf(" ")

printBoxDes(x,y+2)

printMan(x,y+1)

status[x][y+2]='i'

break

default:

break

}

}

void moveBoxDestoDes(int x,int y,char a)

{

switch(a)

{

case 'u':

status[x-1][y]='m'

printf(" ")

printBoxDes(x-2,y)

printMan(x-1,y)

status[x-2][y]='i'

break

case 'd':

status[x+1][y]='m'

printf(" ")

printBoxDes(x+2,y)

printMan(x+1,y)

status[x+2][y]='i'

break

case 'l':

status[x][y-1]='m'

printf(" ")

printBoxDes(x,y-2)

printMan(x,y-1)

status[x][y-2]='i'

break

case 'r':

status[x][y+1]='m'

printf(" ")

printBoxDes(x,y+2)

printMan(x,y+1)

status[x][y+2]='i'

break

default:

break

}

}

int judge(int x,int y)

{

int i

switch(status[x][y])

{

case 0:

i=1

break

case 'w':

i=0

break

case 'b':

i=2

break

case 'i':

i=4

break

case 'm':

i=3

break

default:

break

}

return i

}

void move(int x,int y,char a)

{

switch(a)

{

case 'u':

if(!judge(x-1,y))

{

gotoxy(y,x)

break

}

else if(judge(x-1,y)==1||judge(x-1,y)==3)

{

if(judge(x,y)==3)

{

printDestination(x,y)

printMan(x-1,y)

break

}

else

{

printf(" ")

printMan(x-1,y)

break

}

}

else if(judge(x-1,y)==2)

{

if(judge(x-2,y)==1)

{

moveBoxSpacetoSpace(x,y,'u')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y,x-1)

}

else if(judge(x-2,y)==3)

{

moveBoxSpacetoDes(x,y,'u')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y,x-1)

}

else

gotoxy(y,x)

break

}

else if(judge(x-1,y)==4)

{

if(judge(x-2,y)==1)

{

moveBoxDestoSpace(x,y,'u')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y,x-1)

}

else if(judge(x-2,y)==3)

{

moveBoxDestoDes(x,y,'u')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y,x-1)

}

else

gotoxy(y,x)

break

}

case 'd':

if(!judge(x+1,y))

{

gotoxy(y,x)

break

}

else if(judge(x+1,y)==1||judge(x+1,y)==3)

{

if(judge(x,y)==3)

{

printDestination(x,y)

printMan(x+1,y)

break

}

else

{

printf(" ")

printMan(x+1,y)

break

}

}

else if(judge(x+1,y)==2)

{

if(judge(x+2,y)==1)

{

moveBoxSpacetoSpace(x,y,'d')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y,x+1)

}

else if(judge(x+2,y)==3)

{

moveBoxSpacetoDes(x,y,'d')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y,x+1)

}

else

gotoxy(y,x)

break

}

else if(judge(x+1,y)==4)

{

if(judge(x+2,y)==1)

{

moveBoxDestoSpace(x,y,'d')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y,x+1)

}

else if(judge(x+2,y)==3)

{

moveBoxDestoDes(x,y,'d')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y,x+1)

}

else

gotoxy(y,x)

break

}

case 'l':

if(!judge(x,y-1))

{

gotoxy(y,x)

break

}

else if(judge(x,y-1)==1||judge(x,y-1)==3)

{

if(judge(x,y)==3)

{

printDestination(x,y)

printMan(x,y-1)

break

}

else

{

printf(" ")

printMan(x,y-1)

break

}

}

else if(judge(x,y-1)==2)

{

if(judge(x,y-2)==1)

{

moveBoxSpacetoSpace(x,y,'l')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y-1,x)

}

else if(judge(x,y-2)==3)

{

moveBoxSpacetoDes(x,y,'l')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y-1,x)

}

else

gotoxy(y,x)

break

}

else if(judge(x,y-1)==4)

{

if(judge(x,y-2)==1)

{

moveBoxDestoSpace(x,y,'l')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y-1,x)

}

else if(judge(x,y-2)==3)

{

moveBoxDestoDes(x,y,'l')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y-1,x)

}

else

gotoxy(y,x)

break

}

case 'r':

if(!judge(x,y+1))

{

gotoxy(y,x)

break

}

else if(judge(x,y+1)==1||judge(x,y+1)==3)

{

if(judge(x,y)==3)

{

printDestination(x,y)

printMan(x,y+1)

break

}

else

{

printf(" ")

printMan(x,y+1)

break

}

}

else if(judge(x,y+1)==2)

{

if(judge(x,y+2)==1)

{

moveBoxSpacetoSpace(x,y,'r')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y+1,x)

}

else if(judge(x,y+2)==3)

{

moveBoxSpacetoDes(x,y,'r')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y+1,x)

}

else

gotoxy(y,x)

break

}

else if(judge(x,y+1)==4)

{

if(judge(x,y+2)==1)

{

moveBoxDestoSpace(x,y,'r')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y+1,x)

}

else if(judge(x,y+2)==3)

{

moveBoxDestoDes(x,y,'r')

if(judge(x,y)==3)

printDestination(x,y)

gotoxy(y+1,x)

}

else

gotoxy(y,x)

break

}

default:

break

}

}

void reset(int i)

{

switch(i)

{

case 0:

init()

initStep1()

break

case 1:

init()

initStep2()

break

case 2:

init()

initStep3()

break

case 3:

init()

initStep4()

break

default:

break

}

}

void main()

{

int key

int x

int y

int s

int i=0

winer *win

winer *pw

_AL=3

_AH=0

geninterrupt(0x10)

init()

win=initStep1()

do{

_AH=3

geninterrupt(0x10)

x=_DH+1

y=_DL+1

while(bioskey(1)==0)

key=bioskey(0)

switch(key)

{

case 0x4800:

move(x,y,'u')

break

case 0x5000:

move(x,y,'d')

break

case 0x4b00:

move(x,y,'l')

break

case 0x4d00:

move(x,y,'r')

break

case 0x3920:

reset(i)

break

default:

break

}

s=0

pw=win

while(pw)

{

if(status[pw->x][pw->y]=='m')

s++

pw=pw->p

}

if(s==0)

{

free(win)

gotoxy(15,20)

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,22)

printf("You have done all the steps, You are very clever!")

key=0x011b

getch()

break

default:

break

}

}

}while(key!=0x011b)

_AL=3

_AH=0

geninterrupt(0x10)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12465598.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存