I finnaly got working the program with <ncurses.h> in the virtual box that i put Ubuntu.
A test program for handling the screen is given to us,
this:

#include <ncurses.h>

int main(int argc, char **argv)
{
    int X = 10;
    int Y = 10;
    int ch;
    initscr();
    noecho();
    keypad(stdscr,TRUE);
    move(Y, X);
    printw("M");
    refresh();
    do
    {
        ch = getch();
        move(Y, X);
        printw(" ");
        switch(ch)
        {
	    case KEY_UP: Y--; break;
            case KEY_DOWN: Y++; break;
            case KEY_LEFT: X--; break;
            case KEY_RIGHT: X++; break;
        }
        move(Y, X);
        printw("M");
        refresh();
    }     
    while (ch!=27);
    endwin();
    return 0;
}

//To compile: gcc screen.c -lncurses

but that only moves the letter "M" into the screen with the cursor buttons.
I want the maze in the screen along with the moving "M" but i don't know how that happens...
i don't want or expect to solve the maze for me, i will try it myself
but all i want is to represent the screen along with the moving "M".

Something like this: E-->(exit)

************************************************************
*                                                          E
* ********* **** ************ ****************** ********* *
* ********* **** ************ *        ********* ********* *
* ********* **** ************ * ****** ********* ********* *
* ********* ****            * *        ********* ********* *
* ********* ***************** ******** ********* ********* *
*           *                                  *           *
* ******************* * *** * **************************** *
* ******************* * *** * **************************** *
* ******************* * *** * **                        ** *
* ******************* * *** * ************ *************** *
*           ********* *     * ************ *****           *
* **** **** ********* ******* ************ ***** **** **** *
* **** **** *********         ************ ***** **** **** *
*           ****************************** ***** **** **** *
* **** **** *                                  * ****   ** *
* **** **** ***************** ****************** ********* *
M                                                          *
************************************************************

Plz if anyone know to do that in C just tell me how, i'm lost.
Thanks.

Recommended Answers

All 5 Replies

do something like:

typedef struct
{
        int x;
        int y;
}coord;

/*store the coordinate of the maze as follows in an array*/
coord maze[]={{1,1},{1,2},{1,3},{1,5},{2,3}, {1,4}};
int mazesize = sizeof(maze)/sizeof(coord);/*hence u won't need to count how many coords are there manually*/

void drawMaze()/*draw the maze*/
{
        int i;
        for(i=0;i<mazesize;i++)
        {
                move(maze[i].x, maze[i].y);
                printw("*");
        }
}

call the above function before displaying the M ion the loop.

int main(int argc, char **argv)
{
    int X = 10;
    int Y = 10;
    int ch;
    initscr();
    noecho();
    keypad(stdscr,TRUE);
    drawMaze();/*******here***********/
    move(Y, X);
    printw("M");
    refresh();
    do
    {
        ch = getch();
        move(Y, X);
        printw(" ");
        switch(ch)
        {
	    case KEY_UP: Y--; break;
            case KEY_DOWN: Y++; break;
            case KEY_LEFT: X--; break;
            case KEY_RIGHT: X++; break;
        }
        drawMaze();/************here**************/

        /****FIRST VALIDATE THE MOVE*****/
        move(Y, X);
        printw("M");
        refresh();
    }     
    while (ch!=27);
    endwin();
    return 0;
}

but now u have to check for a valid move also because that position may be a blockage(i.e. occupied by '*').
So write a function to validate your move and then ony execute the requested move.

one correction to my previous post:

do something like:

typedef struct
{
        int x;
        int y;
}coord;

/*store the coordinate of the maze as follows in an array*/
coord maze[]={{1,1},{1,2},{1,3},{1,5},{2,3}, {1,4}};
int mazesize = sizeof(maze)/sizeof(coord);/*hence u won't need to count how many coords are there manually*/

void drawMaze()/*draw the maze*/
{
        int i;
        for(i=0;i<mazesize;i++)
        {
                move(maze[i].x, maze[i].y);
                printw("*");
        }
}

call the above function before displaying the M ion the loop.
correst: call the above function only once in the beginning

int main(int argc, char **argv)
{
    int X = 10;
    int Y = 10;
    int ch;
    initscr();
    noecho();
    keypad(stdscr,TRUE);
    drawMaze();/*******here***********/
    move(Y, X);
    printw("M");
    refresh();
    do
    {
        ch = getch();
        move(Y, X);
        printw(" ");
        switch(ch)
        {
	    case KEY_UP: Y--; break;
            case KEY_DOWN: Y++; break;
            case KEY_LEFT: X--; break;
            case KEY_RIGHT: X++; break;
        }
       // drawMaze();/************here*******NOT REQUIRED*******/

        /****FIRST VALIDATE THE MOVE*****/
        move(Y, X);
        printw("M");
        refresh();
    }     
    while (ch!=27);
    endwin();
    return 0;
}

but now u have to check for a valid move also because that position may be a blockage(i.e. occupied by '*').
So write a function to validate your move and then ony execute the requested move.

call the drawMaze() method only once in the start. No need to call it on every move because it will be there at the console already.

commented: ++ +0

here is another good approach

#include<ncurses.h>
#include<curses.h>

typedef struct
{
        int x;
        int y;
}coord;

int bitmaprow=22, bitmapcol=30;
int bitmap[22][30]={
                        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1},                        {1,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1},
{1,0,1,1,1,1,1,1,1,0,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,1,1,1},
{1,0,1,0,0,0,0,0,0,0,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1},
{1,0,1,0,1,1,1,1,1,0,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1},
{1,0,1,0,1,1,1,1,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
{1,0,1,0,0,0,0,0,0,0,1,1,0,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1},
{1,0,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1},
{1,0,1,0,1,1,1,1,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,1,1,0,1,1,1,1},
{1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1},
{1,0,1,1,1,1,1,1,1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1},
{1,0,1,1,1,1,1,0,0,0,0,0,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1},
{1,0,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1},
{1,0,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1},
{1,0,1,1,1,1,1,1,1,1,0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1},
{1,0,1,1,1,1,1,1,0,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1},
{1,0,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,1,1,1,1},
{1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1},
{1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};


int mazesize;
coord *maze;

void loadMaze();
void drawMaze();
int isOccupied(int x, int y);
int main()
{
        mazesize = bitmaprow*bitmapcol;
        maze =(coord *)malloc(mazesize*sizeof(coord));
        int x = 21;
        int y = 1;
        int maxx=bitmaprow;
        int maxy=bitmapcol;
        int ch;
        initscr();
        noecho();

        keypad(stdscr, true);
        loadMaze();
        drawMaze();
        move(x, y);
        printw("M");
        refresh();
        int px, py;
        do
        {
                ch = getch();
                px = x;
                py = y;
                switch(ch)
                {
                        case KEY_UP: if(x>0)x--; break;
                        case KEY_DOWN: if(x<maxx-1)x++; break;
                        case KEY_LEFT: if(y>0)y--; break;
                        case KEY_RIGHT: if(y<maxy-1)y++; break;
                }
                if(isOccupied(x, y)==0)
                {
                        move(px, py);
                        printw(" ");
                        move(x, y);
                        printw("M");
                        refresh();
                }
                else
                {
                        x = px; y = py;
                }
        }while(ch!='s');
        return 0;
}
void drawMaze()
{
        int i;
        for(i=0;i<mazesize;i++)
        {
                move(maze[i].x, maze[i].y);
                printw("*");
        }
}
int isOccupied(int x, int y)
{
        int i;
        for(i=0;i<mazesize;i++)
        {
                if(x == maze[i].x && y == maze[i].y)
                        return 1;
        }
        return 0;
}
void loadMaze()
{
        static int isLoaded = 0;
        if(isLoaded == 1)
                return;
        int count = 0;
        int i,j;
        for(i=0;i<bitmaprow;i++)
                for(j=0;j<bitmapcol;j++)
                        if(bitmap[i][j]==1)
                        {
                                maze[count].x = i;
                                maze[count].y = j;
                                count++;
                        }
        isLoaded = 1;
}

man many many many thankssssssss, you rock
the only thing i have to do now is to get it find
the shortest path and when find in the end to say
you escaped.
you helped me so mush, now i'm gonna
developing it every single day until perfection.
THANKSSSSSSSSSS!!!!!!!!!!!!!!!!!

man many many many thankssssssss, you rock
the only thing i have to do now is to get it find
the shortest path and when find in the end to say
you escaped.
you helped me so mush, now i'm gonna
developing it every single day until perfection.
THANKSSSSSSSSSS!!!!!!!!!!!!!!!!!

u welcome.

And mark it as solved so that others dont' waste their time on a solved thread. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.