I'm trying to create a maze with a mouse inside moving up down left right in 200 moves. I've got everything working besides the fact that it duplicates the heart on the other side(If it exits on the left, it'll be duplicated at the right end) when the heart shape(mouse) when it escapes the maze via the left/right exit.

Can anyone point out what i'm doing wrong?

#include <stdio.h>
#include <time.h>

int main(void){

    int y[12][12] = {{0},{0}};
    int x;
    int i;
    int j;
    int a;
    int b;
    int c;
    int d;
    int roll;
    int move;

    srand(time(NULL));

    a=5;
    b=5;
    y[5][5]=003;


{


    for(i=1; i<=10;i++){
        y[1][i] = 2;
            if(i==5 || i==6)
                y[1][i] = 0;}

    for(i=1; i<=10;i++){
        y[10][i] = 2;
            if(i==5 || i==6)
                y[10][i] = 0;}

    for(i=4; i<=7;i++){
        y[7][i] = 2;}

    for(j=1; j<=10;j++){
        y[j][1] = 2;
            if(j==5 || j==6)
                y[j][1] = 0;}

    for(j=1; j<=10;j++){
        y[j][10] = 2;
            if(j==5 || j==6)
                y[j][10] = 0;}

    for(j=1; j<=4;j++){
        y[j][4] = 2;}

    for(j=4; j<=5;j++){
        y[j][8] = 2;}}

for(roll=1;roll<=200;roll++){
    move = 1+rand()%4;

        if (move==1){
            if(y[a-1][b] !=2){
                y[a][b] = 0;
                y[a-1][b] = 003;
                a= a-1;}}

        if (move==2){
            if(y[a+1][b] !=2){
                y[a][b] = 0;
                y[a+1][b] = 003;
                a=a+1;}}

        if (move==3){
            if(y[a][b-1] !=2){
                y[a][b] = 0;
                y[a][b-1] = 003;
                b=b-1;}}

        if (move==4){
            if(y[a][b+1] !=2){
                y[a][b] = 0;
                y[a][b+1] = 003;
                b=b+1;}}


if(a>11 || b>11 || a<0 || b<0){
    printf("The Mouse Escaped");
    break;}
else{
    printf("%d\t%d\n",roll,move);}

    for(i=0; i<=12; i++){
        for(j=0; j<=12; j++){
            if (y[i][j]==003)
                printf("%c",003);
            else if (y[i][j]==2)
                printf("%c",254);
            else
                printf(" ");}
        printf("\n");
    }
}
    printf("\n");


return 0;
}

Any help is greatly appreciated, thanks first. :)

Hi. first a comment, then the answer:

the reason why it took 14 hours for someone to reply, is that your code is nearly unreadable. yes, you used code blocks and yes you used indentations, and that's a great start. thank you.

but your lack of a consistent style of bracketing, lack of a consistent style of indentation, and a seeming aversion to using whitespace or giving your variables meaningful names, well, all of that makes it very hard to understand what you're doing.

and if you think this is a pedantic criticism, it's not. unreadable code is worthless in the industry, even if it "works". companies lose millions of dollars and people lose jobs because code is not intelligible to others.

so, i felt sorry for you not getting an answer, but i had to fix your code to make it readable before i could spot the the (very simple) error.

here's your newly formatted code. merry christmas:

#include <stdio.h>
#include <time.h>

int main(void)
{

    int x, y[12][12] = {{0},{0}};
    int roll, move;

    int i, j;               // okay, these are loop indexes
    int a, b, c, d;         // but these are meaningless names here

    srand(time(NULL));

    a=5;
    b=5;
    y[5][5]=003;         // "magic numbers" are not good programming


    {  // <--- WHY IS THIS BRACKET HERE?

        for(i=1; i<=10;i++)
        {
            y[1][i] = 2;          // more magic numbers
            if(i==5 || i==6)      // okay, it just gets worse from here.
                y[1][i] = 0;      // so no more comments about it after this
        }

        for(i=1; i<=10;i++)
        {
            y[10][i] = 2;
            if(i==5 || i==6)
                y[10][i] = 0;
        }

        for(i=4; i<=7;i++)
        {
            y[7][i] = 2;
        }

        for(j=1; j<=10;j++)
        {
            y[j][1] = 2;
            if(j==5 || j==6)
                y[j][1] = 0;
        }

        for(j=1; j<=10;j++)
        {
            y[j][10] = 2;
            if(j==5 || j==6)
                y[j][10] = 0;
        }

        for(j=1; j<=4;j++)
        {
            y[j][4] = 2;
        }

        for(j=4; j<=5;j++)
        {
            y[j][8] = 2;
        }

    } // <--- END OF MEANINGLESS BRACKET PAIR


    for(roll=1;roll<=200;roll++)
    {
        getchar();
        move = 1+rand()%4;

        if (move==1)
        {
            if(y[a-1][b] !=2)
            {
                y[a][b] = 0;
                y[a-1][b] = 003;
                a= a-1;
            }
        }

        if (move==2)
        {
            if(y[a+1][b] !=2)
            {
                y[a][b] = 0;
                y[a+1][b] = 003;
                a=a+1;
            }
        }

        if (move==3)
        {
            if(y[a][b-1] !=2)
            {
                y[a][b] = 0;
                y[a][b-1] = 003;
                b=b-1;
            }
        }

        if (move==4)
        {
            if(y[a][b+1] !=2)
            {
                y[a][b] = 0;
                y[a][b+1] = 003;
                b=b+1;
            }
        }


        if(a>11 || b>11 || a<0 || b<0)
        {
            printf("The Mouse Escaped");
            break;
        }
        else
        {
            printf("%d\t%d\n",roll,move);
        }

        for(i=0; i<=12; i++)        // OK... here's the first error
        {
            for(j=0; j<=12; j++)    // and here's the second.
            {
                if (y[i][j]==003)
                    printf("%c",003);

                else if (y[i][j]==2)
                    printf("%c",254);

                else
                    printf(" ");
            }
            printf("\n");
        }
    }

    printf("\n");


    return 0;
}

and the error is the equal signs. they need to be removed:

for(i=0; i<=12; i++)
for(j=0; j<=12; j++)

the reason is the variable is declared as int y[12][12] . the index runs from 0-11, not 0-12.

see? simple. you could have had the answer in less than 15 minutes if your code was readable.

commented: Nice :) +8

Sorry about the lack of the proper programming style.

I've just started learning programming and I'm not been really taught the proper way to do it.

Thanks for the answer and I'll read up on proper programming styles.

Would this be a lot better?

#include <stdio.h>
#include <time.h>

int main(void){

    int y[11][11] = {{0},{0}};
    int i,j;             //loop indexes
    int a,b;             //position of the mouse(x,y)
    int roll,move;

    srand(time(NULL));

    a=5;                // y = 5
    b=5;                // x = 5
    y[5][5]=002;        //starting position of the mouse, 002 = ASCII code of character

    for(i=1; i<=10;i++) //loop to set the first row of the maze, 5 & 6 as the exit
    {
        y[1][i] = 254;    //254 is the ASCII code for the blocks
        if(i==5 || i==6)
            y[1][i] = 0;
    }

    for(i=1; i<=10;i++) //loop to set the last row of the maze, 5 & 6 as the exit
    {
        y[10][i] = 254; 
        if(i==5 || i==6)
            y[10][i] = 0;
    }

    for(i=4; i<=7;i++)  //loop to set the 7th row of the maze
    {
        y[7][i] = 254;
    }

    for(j=1; j<=10;j++) //loop to set the first column of the maze, 5 & 6 as the exit
    {
        y[j][1] = 254;
        if(j==5 || j==6)
            y[j][1] = 0;
    }

    for(j=1; j<=10;j++) //loop to set the last column of the maze, 5 & 6 as the exit
    {
        y[j][10] = 254;
        if(j==5 || j==6)
            y[j][10] = 0;
    }

    for(j=1; j<=4;j++) //loop to set the 4th column of the maze
    {
        y[j][4] = 254;
    }

    for(j=4; j<=5;j++)  //loop to print the 8th column of the maze
    {
        y[j][8] = 254;
    }

    for(roll=1;roll<=200;roll++)    //200 random moves
    { 
    move = 1+rand()%4;

        if (move==1)            //1 = up
        {  
            if(y[a-1][b] !=254)
            {
                y[a][b] = 0;
                y[a-1][b] = 002;
                a= a-1;
            }
        }

        if (move==2)            //2 = down
        {
            if(y[a+1][b] !=254)
            {
                y[a][b] = 0;
                y[a+1][b] = 002;
                a=a+1;
            }
        }

        if (move==3)          //3 = left
        {
            if(y[a][b-1] !=254)
            {
                y[a][b] = 0;
                y[a][b-1] = 002;
                b=b-1;
            }
        }

        if (move==4)        //4 = right
        {
            if(y[a][b+1] !=254)
            {
                y[a][b] = 0;
                y[a][b+1] = 002;
                b=b+1;
            }
        }


    if(a>=11 || b>=11 || a<=0 || b<=0)      //When the mouse gets out of the maze
    {
        printf("The Mouse Escaped\n\n");
        break;
    }
    else
    {
        printf("%d\t%d\n",roll,move);       //roll = number of moves the mouse took, move = direction of current movement

    for(i=0; i<=11; i++)
    {
        for(j=0; j<=11; j++)
        {
            if (y[i][j]==002)
                printf("%c",002);
                
            else if (y[i][j]==254)
                printf("%c",254);
                
            else
                printf(" ");
        }
        printf("\n");
    }
    printf("\n");
    }
}
return 0;
}

Sorry about the lack of the proper programming style.

I've just started learning programming and I'm not been really taught the proper way to do it.

yeah, and i was a little overly-bitchy. overall, you're on the right track: everyone has to learn somewhere, and "proper coding style" comes mostly with experience.

so, don't stress it too much. you're doing as well as a beginner should be expected. just keep it in mind that "readable code" is just as important as "working code". here's a few general principles:

-- make your variable names meaningful. don't be afraid to use long variable names. use capital letters and/or underscores to make multi-word variable names.

-- use #defines to name your "magic numbers", rather than just sticking them in the code with the hope that you'll remember what they mean 6 months later.

-- there's no single "best" bracketing style, but you do need to pick one bracketing style and always stick with it. don't mix it up.

-- make judicious use of whitespace to separate code functionality.

-- learn to make you code modular by creating subroutines that do particular tasks, rather than lumping everything under main()

Would this be a lot better?

not especially. fundamentally this is the same thing. any differences are matters of degree.

it still uses "magic numbers" that are cryptic and error-prone, variable names that are similarly meaningless to the reader, and is still a huge string of code that has no modularity or separation of functionality.

here's one quick suggestion:

#define MAX_NUM_COLUMNS         12
#define MAX_NUM_ROWS            12

#define MOUSE_CHAR              002
#define SPACE_CHAR              032
#define WALL_CHAR               254

int maze_board[MAX_NUM_COLUMNS][MAX_NUM_ROWS];  // contains maze elements
int x_pos, y_pos;                               // indexes position in maze


// ....
// ....

for (x_pos = 0; x_pos < MAX_NUM_COLUMNS; x_pos++)
{
    // ....
    for (y_pos = 0; y_pos < MAX_NUM_ROWS, y_pos++)
    {
        // ....
        prinf("%c", maze_board[x_pos][y_pos];
// ....
// etc.

now this is just a start. there would be a lot more to do along these lines, especially with regard to describing the maze and printing the walls and moving the mouse. that is where a lot of the real confusion is.

what you really need to do is modularize the various maze setup and printing operations as their own subroutines, and have them called from main() as functions.

.

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.