#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#define WIDTH 20
#define HEIGHT 10
char maze[HEIGHT][WIDTH]
int player_x,player_y;
int exit_X,exit_y;
int directions[4][2]={
{0,1},
{1,0},
{0,-1},
{-1,0},
};
void init_maze(){
for(int i=0;<HEIGHT;i++){
for(int j=0lj<WIDTH;j++){
maze[i][j]="#"'
}
}
}
void generate_maze(int x,int y){
maze[x][y];
}
for(int i=0;i<4;i++){
int r =rand() % 4;
temp[2];
temp [0] = directions[i][0];
temp[1] =directions[i][1];
directions[i][0]=directions[r][0];
directions[i][1]=directions[r][1];
directions[r][0]=temp[0];
directions[r][1]=temp[1];
}
for(int i=0;i<4;i++){
int nx =x + directions[i][0]*2;
int ny = y+directions[i][1]*2;
if(nx>=0 &&ny >= 0 &&ny <WIDTH && maze[nx][ny]=='#'){
maze[x+directions[i][o]][y+directions[i][1]]= ' ';
generate_maze[nx][ny]
}
}
}
void print_maze(){
system("cls");
for(int i =0;i<HEIGHT;i++){
for(int j=0;j<WIDTH;j++){
if(i==player_x && j == player_y)
printf("P");
else if(i==0 && j==0)
printf("S");
else if(i==exit_X && j ==exit_y)
printf("E");
else
printf("%c",maze[i][j]);
}
printf("\n")
}
}
int is_valid_movie(int x,int y){
return x>=0 && < HEIGHT && y >=0 && < WIDTH && maze[x][y]=='';
}
void move_player(char direction){
int new_x =player_x;
int new_y =player_y
if(direction == 'w') new_x--;
else if(direction=='s')new_x++;
else if(direction == 'a')new_y--
else if(direction == 'd') new_y++;
if(is_valid_movie(new_x,new_y))
{
player_x =new_x;
player_y =new_y;
}
}
void set_exit(){
int side = rand() % 4;
switch (side){
case 0:
exit_X=0;
exit_y=0;
break;
case 1:
exit_X =HEIGHT-1;
exit_y =rand()%(WIDTH-2)+1;
break;
case 2:
exit_X =rand()%(HEIGHT-2)+1;
exit_y=0;
break;
case 3:
exit_X =rand()%(HEIGHT-2)+1;
exit_y=WIDTH-1;
break;
}
maze[exit_X][exit_y]='';
}
int main(){
srand(time(0));
int_maze();
generate_maze(0,0);
maze[0][0]='';
player_x =0;
player_y =0;
set_exit();
while(1){
print_maze();
cha move =geth();
move_player(move);
if(player_x == exit_X && player_y ==exit_y){
print_maze();
prinf("Congratulations you`ve reached the exit\n");
break;
}
}
retrun 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: