Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worked #70

Open
wants to merge 3 commits into
base: ppka2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions level1/p09_maze/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include"maze.c"
#include"userinput.c"
int test(pmaze m)
{
position enter;
enter._x = 1;
enter._y = 0;
randommaze(m);
//printmaze(m);
antoFootpath(m, enter);
//printmaze(m);
return m->_map[Maplong-2][Mapwidth-1];
}

int main()
{
maze m;
int frist=1;
//test(&m);
printf("��ͼ�������ɣ���ȴ�");
while(test(&m)!=2);
system("cls");
printf("��ͼ������\n");
printf("w�����ƶ� s�����ƶ� a�����ƶ� d�����ƶ� p�����ȷ·��\n");
printmaze(&m);
while(1)
{
if(kbhit())
{
ch=getch();
system("cls");
printf("w�����ƶ� s�����ƶ� a�����ƶ� d�����ƶ� p�����ȷ·��\n");
if(exitmaze(&m))
{
system("cls");
printf("you go out the maze");
exit(1);
}
userinput(&m, ch);
printmaze(&m);
}
}
// printf("please choose the pattern");
// scanf("%d",&n);
return 0;
}
Binary file added level1/p09_maze/main.o
Binary file not shown.
239 changes: 239 additions & 0 deletions level1/p09_maze/maze.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
#include"maze.h"
void initmaze(pmaze m, int Map[Maplong][Mapwidth])
{
assert(m!=NULL);
int i,j;
for(i=0; i<Maplong; i++)
{
for(j=0; j<Mapwidth; j++)
{
m->_map[i][j]=Map[i][j];
}
}
}
void randommaze(pmaze m)
{
int i,j,k;
srand(time(NULL));
m->_map[1][0]=m->_map[Maplong-2][Mapwidth-1]=1;
for(i=0; i<Maplong; i++)
{
m->_map[0][i]=m->_map[Maplong-1][i]=0;
}
for(j=2;j<Mapwidth; j++)
{
m->_map[j][0]=m->_map[j-2][Mapwidth-1]=0;
}
for(i=1; i<Maplong-1; i++)
{
for(j=1; j<Mapwidth-1; j++)
{
k=rand()%3;
if(k)
m->_map[i][j]=1;
else
{
if((i==1&&j==1)||(i==Maplong-2&&Mapwidth-2==j))
{
m->_map[i][j]=1;
}
else
{
m->_map[i][j]=0;
}
}
}
}
}
int exitmaze(pmaze m)
{
if(position_x==Maplong-2&&position_y==Mapwidth-1)
{
return 1;
}
return 0;
}
void antoprintmaze(pmaze m)
{
int i,j;
for(i=0; i<Maplong; i++)
{
for(j=0; j<Mapwidth; j++)
{
switch(m->_map[i][j])
{
case(0):
printf("0");
break;
case(1):
printf(" ");
break;
case(2):
printf("*");
break;
case(3):
printf(" ");
break;
default:
break;
}
}
printf("\n");
}
printf("\n");
}
void initstack(pstack m)
{
assert(m!=NULL);
m->top=0;
}
void popstack(pstack m)
{
assert(m!=NULL);
if(m->top)
m->top--;
}
void pushstack(pstack m,mazenode n)
{
assert(m!=NULL);
if(m->top==Maxstep)
return;
else
{
m->num[m->top]=n;
m->top++;
}
}
int emptystack(pstack s)
{
assert(s!=NULL);
if(0==s->top)
return 1;
return 0;
}
mazenode Stacktop(pstack s)
{
assert((s!=NULL));
if(0==s->top)
{
printf("error");
exit(1);
}
return s->num[s->top-1];
}
int entermaze(pmaze m,position e)
{
assert(m!=NULL);
if((e._x==0||e._x==Maplong-1||e._y==0||e._y==Mapwidth-1)&&(e._x>=0)&&(e._x<Maplong)&&(e._y>=0)&&(e._y<Mapwidth))
{
return 1;
}
return 0;
}
int Passmaze(pmaze m,position e)
{
assert(m!=NULL);
if(1==m->_map[e._x][e._y])
{
return 1;
}
return 0;
}
void printmaze(pmaze m)
{
int i,j;
for(i=0; i<Maplong; i++)
{
for(j=0; j<Mapwidth; j++)
{
if(i==position_x&&j==position_y)
{
if(one==1)
{
temp= m->_map[i][j];
px=i;
py=j;
one=0;
}
m->_map[i][j]=4;
}
switch(m->_map[i][j])
{
case(0):
printf("0");
break;
case(1):
printf(" ");
break;
case(2):
printf(" ");
break;
case(3):
printf(" ");
break;
case(4):
printf("@");
break;
default:
break;
}
}
printf("\n");
}
printf("\n");

}
//void footpath(pmaze m,positon e)
void antoFootpath(pmaze m,position e)
{
assert(m!=NULL);
Stack s;
initstack(&s);
pushstack(&s,e);
while(!emptystack(&s))
{
position now;
position next;
now=Stacktop(&s);
m->_map[now._x][now._y]=2;
if(now._x==Maplong-2&&now._y==Mapwidth-1)
{
return;
}
else
{
next=now;
next._x+=1;
if(Passmaze(m,next))
{
pushstack(&s,next);
continue;
}
next=now;
next._y+=1;
if(Passmaze(m,next))
{
pushstack(&s,next);
continue;
}
next=now;
next._y-=1;
if(Passmaze(m,next))
{
pushstack(&s,next);
continue;
}
next=now;
next._x-=1;
if(Passmaze(m,next))
{
pushstack(&s,next);
continue;
}
m->_map[now._x][now._y]=3;
popstack(&s);
}
}


}
52 changes: 52 additions & 0 deletions level1/p09_maze/maze.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#ifndef MAZE_H_INCLUDED
#define MAZE_H_INCLUDED
#include<stdio.h>
#include<assert.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#include<windows.h>
#define Maplong 20
#define Mapwidth 20
#define Maxstep 500
int position_x=1;
int position_y=0;
int one=1;
int temp;
int px;
int py;
char ch;
typedef struct position
{
int _x;
int _y;
}position;
typedef position mazenode ;
typedef struct Stack
{
int top;
mazenode num[Maxstep];
}Stack,*pstack;
typedef struct maze
{
int _map[Maplong][Mapwidth];
}maze,*pmaze;
void antoprintmaze(pmaze m);
void initmaze(pmaze m,int Map[Maplong][Mapwidth]);
void initstack(pstack m);
void popstack(pstack m);
void pushstack(pstack m,mazenode n );
//void Random();
int Passmaze(pmaze m,position e);
void Footpath(pmaze m,position e);
int emptystack(pstack s);
mazenode Stacktop(pstack s);
int entermaze(pmaze m,position e);
int outmaze(position m,position e);
void printmaze(pmaze m);
void randommaze();
void antoFootpath(pmaze m,position e);
int exitmaze(pmaze m);
void wall(pmaze m);

#endif // MAZE_H_INCLUDED
19 changes: 19 additions & 0 deletions level1/p09_maze/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include"maze.c"
void test()
{
maze m;
position enter;
enter._x = 1;
enter._y = 0;
randommaze(&m);
printmaze(&m);
Footpath(&m, enter);
printmaze(&m);
}

int main()
{
test();

return 0;
}
Loading