#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;i<HEIGHT;i++){
        for(int j=0;j<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;
   int 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][0]][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 &&x < HEIGHT && y >=0 &&y < 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));

    init_maze();
    generate_maze(0,0);

    maze[0][0]=' ';
    player_x =0;
    player_y =0;

    set_exit();

    while(1){
        print_maze();

        char move =getch();
        move_player(move);

        if(player_x == exit_X && player_y ==exit_y){
            print_maze();
            printf("Congratulations you`ve reached the exit\n");
            break;
        }
    }
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: